blob: 4302f8bb53be4082b6e1988c761b2bc1d2e40d02 [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;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001484 Module *Owner = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001485 if (SubModID) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001486 if ((Owner = getSubmodule(SubModID))) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001487 if (Owner->NameVisibility == Module::Hidden) {
1488 // The owning module is not visible, and this macro definition
1489 // should not be, either.
1490 Hidden = true;
1491
1492 // Note that this macro definition was hidden because its owning
1493 // module is not yet visible.
1494 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1495 }
1496 }
1497 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001498
1499 if (!Hidden)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001500 installImportedMacro(II, MD, Owner);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001501}
1502
1503void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1504 ModuleFile &M, uint64_t Offset) {
1505 assert(M.Kind != MK_Module);
1506
1507 BitstreamCursor &Cursor = M.MacroCursor;
1508 SavedStreamPosition SavedPosition(Cursor);
1509 Cursor.JumpToBit(Offset);
1510
1511 llvm::BitstreamEntry Entry =
1512 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1513 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1514 Error("malformed block record in AST file");
1515 return;
1516 }
1517
1518 RecordData Record;
1519 PreprocessorRecordTypes RecType =
1520 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1521 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1522 Error("malformed block record in AST file");
1523 return;
1524 }
1525
1526 // Deserialize the macro directives history in reverse source-order.
1527 MacroDirective *Latest = 0, *Earliest = 0;
1528 unsigned Idx = 0, N = Record.size();
1529 while (Idx < N) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001530 MacroDirective *MD = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001531 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001532 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1533 switch (K) {
1534 case MacroDirective::MD_Define: {
1535 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1536 MacroInfo *MI = getMacro(GMacID);
1537 bool isImported = Record[Idx++];
1538 bool isAmbiguous = Record[Idx++];
1539 DefMacroDirective *DefMD =
1540 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1541 DefMD->setAmbiguous(isAmbiguous);
1542 MD = DefMD;
1543 break;
1544 }
1545 case MacroDirective::MD_Undefine:
1546 MD = PP.AllocateUndefMacroDirective(Loc);
1547 break;
1548 case MacroDirective::MD_Visibility: {
1549 bool isPublic = Record[Idx++];
1550 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1551 break;
1552 }
1553 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001554
1555 if (!Latest)
1556 Latest = MD;
1557 if (Earliest)
1558 Earliest->setPrevious(MD);
1559 Earliest = MD;
1560 }
1561
1562 PP.setLoadedMacroDirective(II, Latest);
1563}
1564
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001565/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor7332ae42013-04-12 21:00:54 +00001566/// modules.
1567static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1568 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001569 assert(PrevMI && NewMI);
1570 if (!NewOwner)
1571 return false;
1572 Module *PrevOwner = 0;
1573 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1574 PrevOwner = Reader.getSubmodule(PrevModID);
1575 if (!PrevOwner)
1576 return false;
1577 if (PrevOwner == NewOwner)
1578 return false;
Douglas Gregor7332ae42013-04-12 21:00:54 +00001579 return PrevOwner->IsSystem && NewOwner->IsSystem;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001580}
1581
1582void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1583 Module *Owner) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001584 assert(II && MD);
1585
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001586 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001587 MacroDirective *Prev = PP.getMacroDirective(II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001588 if (Prev) {
1589 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001590 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1591 MacroInfo *NewMI = DefMD->getInfo();
Argyrios Kyrtzidisbd25ff82013-04-03 17:39:30 +00001592 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1593 /*Syntactically=*/true)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001594 // Before marking the macros as ambiguous, check if this is a case where
Douglas Gregor7332ae42013-04-12 21:00:54 +00001595 // both macros are in system headers. If so, we trust that the system
1596 // did not get it wrong. This also handles cases where Clang's own
1597 // headers have a different spelling of certain system macros:
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001598 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1599 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
Douglas Gregor7332ae42013-04-12 21:00:54 +00001600 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001601 PrevDef.getDirective()->setAmbiguous(true);
1602 DefMD->setAmbiguous(true);
1603 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001604 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001605 }
1606
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001607 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001608}
1609
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001610InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001611 // If this ID is bogus, just return an empty input file.
1612 if (ID == 0 || ID > F.InputFilesLoaded.size())
1613 return InputFile();
1614
1615 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001616 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001617 return F.InputFilesLoaded[ID-1];
1618
1619 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001620 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001621 SavedStreamPosition SavedPosition(Cursor);
1622 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1623
1624 unsigned Code = Cursor.ReadCode();
1625 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001626 StringRef Blob;
1627 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001628 case INPUT_FILE: {
1629 unsigned StoredID = Record[0];
1630 assert(ID == StoredID && "Bogus stored ID or offset");
1631 (void)StoredID;
1632 off_t StoredSize = (off_t)Record[1];
1633 time_t StoredTime = (time_t)Record[2];
1634 bool Overridden = (bool)Record[3];
1635
1636 // Get the file entry for this input file.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001637 StringRef OrigFilename = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001638 std::string Filename = OrigFilename;
1639 MaybeAddSystemRootToFilename(F, Filename);
1640 const FileEntry *File
1641 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1642 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1643
1644 // If we didn't find the file, resolve it relative to the
1645 // original directory from which this AST file was created.
1646 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1647 F.OriginalDir != CurrentDir) {
1648 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1649 F.OriginalDir,
1650 CurrentDir);
1651 if (!Resolved.empty())
1652 File = FileMgr.getFile(Resolved);
1653 }
1654
1655 // For an overridden file, create a virtual file with the stored
1656 // size/timestamp.
1657 if (Overridden && File == 0) {
1658 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1659 }
1660
1661 if (File == 0) {
1662 if (Complain) {
1663 std::string ErrorStr = "could not find file '";
1664 ErrorStr += Filename;
1665 ErrorStr += "' referenced by AST file";
1666 Error(ErrorStr.c_str());
1667 }
1668 return InputFile();
1669 }
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001670
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001671 // Check if there was a request to override the contents of the file
1672 // that was part of the precompiled header. Overridding such a file
1673 // can lead to problems when lexing using the source locations from the
1674 // PCH.
1675 SourceManager &SM = getSourceManager();
1676 if (!Overridden && SM.isFileOverridden(File)) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001677 if (Complain)
1678 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001679 // After emitting the diagnostic, recover by disabling the override so
1680 // that the original file will be used.
1681 SM.disableFileContentsOverride(File);
1682 // The FileEntry is a virtual file entry with the size of the contents
1683 // that would override the original contents. Set it to the original's
1684 // size/time.
1685 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1686 StoredSize, StoredTime);
1687 }
1688
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001689 bool IsOutOfDate = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001690
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001691 // For an overridden file, there is nothing to validate.
1692 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001693#if !defined(LLVM_ON_WIN32)
1694 // In our regression testing, the Windows file system seems to
1695 // have inconsistent modification times that sometimes
1696 // erroneously trigger this error-handling path.
1697 || StoredTime != File->getModificationTime()
1698#endif
1699 )) {
Douglas Gregor677e15f2013-03-19 00:28:20 +00001700 if (Complain) {
Argyrios Kyrtzidisf8f373f2013-03-08 20:42:38 +00001701 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001702 }
1703
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001704 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001705 }
1706
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001707 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1708
1709 // Note that we've loaded this input file.
1710 F.InputFilesLoaded[ID-1] = IF;
1711 return IF;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001712 }
1713 }
1714
1715 return InputFile();
1716}
1717
1718const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1719 ModuleFile &M = ModuleMgr.getPrimaryModule();
1720 std::string Filename = filenameStrRef;
1721 MaybeAddSystemRootToFilename(M, Filename);
1722 const FileEntry *File = FileMgr.getFile(Filename);
1723 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1724 M.OriginalDir != CurrentDir) {
1725 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1726 M.OriginalDir,
1727 CurrentDir);
1728 if (!resolved.empty())
1729 File = FileMgr.getFile(resolved);
1730 }
1731
1732 return File;
1733}
1734
1735/// \brief If we are loading a relocatable PCH file, and the filename is
1736/// not an absolute path, add the system root to the beginning of the file
1737/// name.
1738void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1739 std::string &Filename) {
1740 // If this is not a relocatable PCH file, there's nothing to do.
1741 if (!M.RelocatablePCH)
1742 return;
1743
1744 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1745 return;
1746
1747 if (isysroot.empty()) {
1748 // If no system root was given, default to '/'
1749 Filename.insert(Filename.begin(), '/');
1750 return;
1751 }
1752
1753 unsigned Length = isysroot.size();
1754 if (isysroot[Length - 1] != '/')
1755 Filename.insert(Filename.begin(), '/');
1756
1757 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1758}
1759
1760ASTReader::ASTReadResult
1761ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001762 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001763 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001764 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001765
1766 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1767 Error("malformed block record in AST file");
1768 return Failure;
1769 }
1770
1771 // Read all of the records and blocks in the control block.
1772 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001773 while (1) {
1774 llvm::BitstreamEntry Entry = Stream.advance();
1775
1776 switch (Entry.Kind) {
1777 case llvm::BitstreamEntry::Error:
1778 Error("malformed block record in AST file");
1779 return Failure;
1780 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001781 // Validate all of the non-system input files.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001782 if (!DisableValidation) {
1783 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001784 // All user input files reside at the index range [0, Record[1]).
1785 // Record is the one from INPUT_FILE_OFFSETS.
1786 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001787 InputFile IF = getInputFile(F, I+1, Complain);
1788 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001789 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001790 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001791 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001792 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001793
1794 case llvm::BitstreamEntry::SubBlock:
1795 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001796 case INPUT_FILES_BLOCK_ID:
1797 F.InputFilesCursor = Stream;
1798 if (Stream.SkipBlock() || // Skip with the main cursor
1799 // Read the abbreviations
1800 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1801 Error("malformed block record in AST file");
1802 return Failure;
1803 }
1804 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001805
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001806 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001807 if (Stream.SkipBlock()) {
1808 Error("malformed block record in AST file");
1809 return Failure;
1810 }
1811 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001812 }
Chris Lattner88bde502013-01-19 21:39:22 +00001813
1814 case llvm::BitstreamEntry::Record:
1815 // The interesting case.
1816 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001817 }
1818
1819 // Read and process a record.
1820 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001821 StringRef Blob;
1822 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001823 case METADATA: {
1824 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1825 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1826 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1827 : diag::warn_pch_version_too_new);
1828 return VersionMismatch;
1829 }
1830
1831 bool hasErrors = Record[5];
1832 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1833 Diag(diag::err_pch_with_compiler_errors);
1834 return HadErrors;
1835 }
1836
1837 F.RelocatablePCH = Record[4];
1838
1839 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001840 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001841 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1842 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1843 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1844 return VersionMismatch;
1845 }
1846 break;
1847 }
1848
1849 case IMPORTS: {
1850 // Load each of the imported PCH files.
1851 unsigned Idx = 0, N = Record.size();
1852 while (Idx < N) {
1853 // Read information about the AST file.
1854 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1855 // The import location will be the local one for now; we will adjust
1856 // all import locations of module imports after the global source
1857 // location info are setup.
1858 SourceLocation ImportLoc =
1859 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001860 off_t StoredSize = (off_t)Record[Idx++];
1861 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001862 unsigned Length = Record[Idx++];
1863 SmallString<128> ImportedFile(Record.begin() + Idx,
1864 Record.begin() + Idx + Length);
1865 Idx += Length;
1866
1867 // Load the AST file.
1868 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00001869 StoredSize, StoredModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001870 ClientLoadCapabilities)) {
1871 case Failure: return Failure;
1872 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorac39f132013-03-19 00:38:50 +00001873 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001874 case OutOfDate: return OutOfDate;
1875 case VersionMismatch: return VersionMismatch;
1876 case ConfigurationMismatch: return ConfigurationMismatch;
1877 case HadErrors: return HadErrors;
1878 case Success: break;
1879 }
1880 }
1881 break;
1882 }
1883
1884 case LANGUAGE_OPTIONS: {
1885 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1886 if (Listener && &F == *ModuleMgr.begin() &&
1887 ParseLanguageOptions(Record, Complain, *Listener) &&
1888 !DisableValidation)
1889 return ConfigurationMismatch;
1890 break;
1891 }
1892
1893 case TARGET_OPTIONS: {
1894 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1895 if (Listener && &F == *ModuleMgr.begin() &&
1896 ParseTargetOptions(Record, Complain, *Listener) &&
1897 !DisableValidation)
1898 return ConfigurationMismatch;
1899 break;
1900 }
1901
1902 case DIAGNOSTIC_OPTIONS: {
1903 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1904 if (Listener && &F == *ModuleMgr.begin() &&
1905 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1906 !DisableValidation)
1907 return ConfigurationMismatch;
1908 break;
1909 }
1910
1911 case FILE_SYSTEM_OPTIONS: {
1912 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1913 if (Listener && &F == *ModuleMgr.begin() &&
1914 ParseFileSystemOptions(Record, Complain, *Listener) &&
1915 !DisableValidation)
1916 return ConfigurationMismatch;
1917 break;
1918 }
1919
1920 case HEADER_SEARCH_OPTIONS: {
1921 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1922 if (Listener && &F == *ModuleMgr.begin() &&
1923 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1924 !DisableValidation)
1925 return ConfigurationMismatch;
1926 break;
1927 }
1928
1929 case PREPROCESSOR_OPTIONS: {
1930 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1931 if (Listener && &F == *ModuleMgr.begin() &&
1932 ParsePreprocessorOptions(Record, Complain, *Listener,
1933 SuggestedPredefines) &&
1934 !DisableValidation)
1935 return ConfigurationMismatch;
1936 break;
1937 }
1938
1939 case ORIGINAL_FILE:
1940 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001941 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001942 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1943 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1944 break;
1945
1946 case ORIGINAL_FILE_ID:
1947 F.OriginalSourceFileID = FileID::get(Record[0]);
1948 break;
1949
1950 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001951 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001952 break;
1953
1954 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001955 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001956 F.InputFilesLoaded.resize(Record[0]);
1957 break;
1958 }
1959 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001960}
1961
1962bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001963 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001964
1965 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1966 Error("malformed block record in AST file");
1967 return true;
1968 }
1969
1970 // Read all of the records and blocks for the AST file.
1971 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001972 while (1) {
1973 llvm::BitstreamEntry Entry = Stream.advance();
1974
1975 switch (Entry.Kind) {
1976 case llvm::BitstreamEntry::Error:
1977 Error("error at end of module block in AST file");
1978 return true;
1979 case llvm::BitstreamEntry::EndBlock: {
Richard Smith43828672013-04-03 22:49:41 +00001980 // Outside of C++, we do not store a lookup map for the translation unit.
1981 // Instead, mark it as needing a lookup map to be built if this module
1982 // contains any declarations lexically within it (which it always does!).
1983 // This usually has no cost, since we very rarely need the lookup map for
1984 // the translation unit outside C++.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001985 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smith43828672013-04-03 22:49:41 +00001986 if (DC->hasExternalLexicalStorage() &&
1987 !getContext().getLangOpts().CPlusPlus)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001988 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00001989
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001990 return false;
1991 }
Chris Lattner88bde502013-01-19 21:39:22 +00001992 case llvm::BitstreamEntry::SubBlock:
1993 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001994 case DECLTYPES_BLOCK_ID:
1995 // We lazily load the decls block, but we want to set up the
1996 // DeclsCursor cursor to point into it. Clone our current bitcode
1997 // cursor to it, enter the block and read the abbrevs in that block.
1998 // With the main cursor, we just skip over it.
1999 F.DeclsCursor = Stream;
2000 if (Stream.SkipBlock() || // Skip with the main cursor.
2001 // Read the abbrevs.
2002 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2003 Error("malformed block record in AST file");
2004 return true;
2005 }
2006 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002007
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002008 case DECL_UPDATES_BLOCK_ID:
2009 if (Stream.SkipBlock()) {
2010 Error("malformed block record in AST file");
2011 return true;
2012 }
2013 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002014
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002015 case PREPROCESSOR_BLOCK_ID:
2016 F.MacroCursor = Stream;
2017 if (!PP.getExternalSource())
2018 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00002019
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002020 if (Stream.SkipBlock() ||
2021 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2022 Error("malformed block record in AST file");
2023 return true;
2024 }
2025 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2026 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002027
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002028 case PREPROCESSOR_DETAIL_BLOCK_ID:
2029 F.PreprocessorDetailCursor = Stream;
2030 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00002031 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002032 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00002033 Error("malformed preprocessor detail record in AST file");
2034 return true;
2035 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002036 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002037 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2038
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002039 if (!PP.getPreprocessingRecord())
2040 PP.createPreprocessingRecord();
2041 if (!PP.getPreprocessingRecord()->getExternalSource())
2042 PP.getPreprocessingRecord()->SetExternalSource(*this);
2043 break;
2044
2045 case SOURCE_MANAGER_BLOCK_ID:
2046 if (ReadSourceManagerBlock(F))
2047 return true;
2048 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002049
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002050 case SUBMODULE_BLOCK_ID:
2051 if (ReadSubmoduleBlock(F))
2052 return true;
2053 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002054
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002055 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002056 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002057 if (Stream.SkipBlock() ||
2058 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2059 Error("malformed comments block in AST file");
2060 return true;
2061 }
2062 CommentsCursors.push_back(std::make_pair(C, &F));
2063 break;
2064 }
Chris Lattner88bde502013-01-19 21:39:22 +00002065
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002066 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002067 if (Stream.SkipBlock()) {
2068 Error("malformed block record in AST file");
2069 return true;
2070 }
2071 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002072 }
2073 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002074
2075 case llvm::BitstreamEntry::Record:
2076 // The interesting case.
2077 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002078 }
2079
2080 // Read and process a record.
2081 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002082 StringRef Blob;
2083 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002084 default: // Default behavior: ignore.
2085 break;
2086
2087 case TYPE_OFFSET: {
2088 if (F.LocalNumTypes != 0) {
2089 Error("duplicate TYPE_OFFSET record in AST file");
2090 return true;
2091 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002092 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002093 F.LocalNumTypes = Record[0];
2094 unsigned LocalBaseTypeIndex = Record[1];
2095 F.BaseTypeIndex = getTotalNumTypes();
2096
2097 if (F.LocalNumTypes > 0) {
2098 // Introduce the global -> local mapping for types within this module.
2099 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2100
2101 // Introduce the local -> global mapping for types within this module.
2102 F.TypeRemap.insertOrReplace(
2103 std::make_pair(LocalBaseTypeIndex,
2104 F.BaseTypeIndex - LocalBaseTypeIndex));
2105
2106 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2107 }
2108 break;
2109 }
2110
2111 case DECL_OFFSET: {
2112 if (F.LocalNumDecls != 0) {
2113 Error("duplicate DECL_OFFSET record in AST file");
2114 return true;
2115 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002116 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002117 F.LocalNumDecls = Record[0];
2118 unsigned LocalBaseDeclID = Record[1];
2119 F.BaseDeclID = getTotalNumDecls();
2120
2121 if (F.LocalNumDecls > 0) {
2122 // Introduce the global -> local mapping for declarations within this
2123 // module.
2124 GlobalDeclMap.insert(
2125 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2126
2127 // Introduce the local -> global mapping for declarations within this
2128 // module.
2129 F.DeclRemap.insertOrReplace(
2130 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2131
2132 // Introduce the global -> local mapping for declarations within this
2133 // module.
2134 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2135
2136 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2137 }
2138 break;
2139 }
2140
2141 case TU_UPDATE_LEXICAL: {
2142 DeclContext *TU = Context.getTranslationUnitDecl();
2143 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002144 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002145 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002146 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002147 TU->setHasExternalLexicalStorage(true);
2148 break;
2149 }
2150
2151 case UPDATE_VISIBLE: {
2152 unsigned Idx = 0;
2153 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2154 ASTDeclContextNameLookupTable *Table =
2155 ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002156 (const unsigned char *)Blob.data() + Record[Idx++],
2157 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002158 ASTDeclContextNameLookupTrait(*this, F));
2159 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2160 DeclContext *TU = Context.getTranslationUnitDecl();
2161 F.DeclContextInfos[TU].NameLookupTableData = Table;
2162 TU->setHasExternalVisibleStorage(true);
2163 } else
2164 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2165 break;
2166 }
2167
2168 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002169 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002170 if (Record[0]) {
2171 F.IdentifierLookupTable
2172 = ASTIdentifierLookupTable::Create(
2173 (const unsigned char *)F.IdentifierTableData + Record[0],
2174 (const unsigned char *)F.IdentifierTableData,
2175 ASTIdentifierLookupTrait(*this, F));
2176
2177 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2178 }
2179 break;
2180
2181 case IDENTIFIER_OFFSET: {
2182 if (F.LocalNumIdentifiers != 0) {
2183 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2184 return true;
2185 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002186 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002187 F.LocalNumIdentifiers = Record[0];
2188 unsigned LocalBaseIdentifierID = Record[1];
2189 F.BaseIdentifierID = getTotalNumIdentifiers();
2190
2191 if (F.LocalNumIdentifiers > 0) {
2192 // Introduce the global -> local mapping for identifiers within this
2193 // module.
2194 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2195 &F));
2196
2197 // Introduce the local -> global mapping for identifiers within this
2198 // module.
2199 F.IdentifierRemap.insertOrReplace(
2200 std::make_pair(LocalBaseIdentifierID,
2201 F.BaseIdentifierID - LocalBaseIdentifierID));
2202
2203 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2204 + F.LocalNumIdentifiers);
2205 }
2206 break;
2207 }
2208
2209 case EXTERNAL_DEFINITIONS:
2210 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2211 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2212 break;
2213
2214 case SPECIAL_TYPES:
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002215 if (SpecialTypes.empty()) {
2216 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2217 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2218 break;
2219 }
2220
2221 if (SpecialTypes.size() != Record.size()) {
2222 Error("invalid special-types record");
2223 return true;
2224 }
2225
2226 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2227 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2228 if (!SpecialTypes[I])
2229 SpecialTypes[I] = ID;
2230 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2231 // merge step?
2232 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002233 break;
2234
2235 case STATISTICS:
2236 TotalNumStatements += Record[0];
2237 TotalNumMacros += Record[1];
2238 TotalLexicalDeclContexts += Record[2];
2239 TotalVisibleDeclContexts += Record[3];
2240 break;
2241
2242 case UNUSED_FILESCOPED_DECLS:
2243 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2244 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2245 break;
2246
2247 case DELEGATING_CTORS:
2248 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2249 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2250 break;
2251
2252 case WEAK_UNDECLARED_IDENTIFIERS:
2253 if (Record.size() % 4 != 0) {
2254 Error("invalid weak identifiers record");
2255 return true;
2256 }
2257
2258 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2259 // files. This isn't the way to do it :)
2260 WeakUndeclaredIdentifiers.clear();
2261
2262 // Translate the weak, undeclared identifiers into global IDs.
2263 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2264 WeakUndeclaredIdentifiers.push_back(
2265 getGlobalIdentifierID(F, Record[I++]));
2266 WeakUndeclaredIdentifiers.push_back(
2267 getGlobalIdentifierID(F, Record[I++]));
2268 WeakUndeclaredIdentifiers.push_back(
2269 ReadSourceLocation(F, Record, I).getRawEncoding());
2270 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2271 }
2272 break;
2273
Richard Smith5ea6ef42013-01-10 23:43:47 +00002274 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002275 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002276 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002277 break;
2278
2279 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002280 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002281 F.LocalNumSelectors = Record[0];
2282 unsigned LocalBaseSelectorID = Record[1];
2283 F.BaseSelectorID = getTotalNumSelectors();
2284
2285 if (F.LocalNumSelectors > 0) {
2286 // Introduce the global -> local mapping for selectors within this
2287 // module.
2288 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2289
2290 // Introduce the local -> global mapping for selectors within this
2291 // module.
2292 F.SelectorRemap.insertOrReplace(
2293 std::make_pair(LocalBaseSelectorID,
2294 F.BaseSelectorID - LocalBaseSelectorID));
2295
2296 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2297 }
2298 break;
2299 }
2300
2301 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002302 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002303 if (Record[0])
2304 F.SelectorLookupTable
2305 = ASTSelectorLookupTable::Create(
2306 F.SelectorLookupTableData + Record[0],
2307 F.SelectorLookupTableData,
2308 ASTSelectorLookupTrait(*this, F));
2309 TotalNumMethodPoolEntries += Record[1];
2310 break;
2311
2312 case REFERENCED_SELECTOR_POOL:
2313 if (!Record.empty()) {
2314 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2315 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2316 Record[Idx++]));
2317 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2318 getRawEncoding());
2319 }
2320 }
2321 break;
2322
2323 case PP_COUNTER_VALUE:
2324 if (!Record.empty() && Listener)
2325 Listener->ReadCounter(F, Record[0]);
2326 break;
2327
2328 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002329 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002330 F.NumFileSortedDecls = Record[0];
2331 break;
2332
2333 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002334 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002335 F.LocalNumSLocEntries = Record[0];
2336 unsigned SLocSpaceSize = Record[1];
2337 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2338 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2339 SLocSpaceSize);
2340 // Make our entry in the range map. BaseID is negative and growing, so
2341 // we invert it. Because we invert it, though, we need the other end of
2342 // the range.
2343 unsigned RangeStart =
2344 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2345 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2346 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2347
2348 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2349 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2350 GlobalSLocOffsetMap.insert(
2351 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2352 - SLocSpaceSize,&F));
2353
2354 // Initialize the remapping table.
2355 // Invalid stays invalid.
2356 F.SLocRemap.insert(std::make_pair(0U, 0));
2357 // This module. Base was 2 when being compiled.
2358 F.SLocRemap.insert(std::make_pair(2U,
2359 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2360
2361 TotalNumSLocEntries += F.LocalNumSLocEntries;
2362 break;
2363 }
2364
2365 case MODULE_OFFSET_MAP: {
2366 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002367 const unsigned char *Data = (const unsigned char*)Blob.data();
2368 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002369
2370 // Continuous range maps we may be updating in our module.
2371 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2372 ContinuousRangeMap<uint32_t, int, 2>::Builder
2373 IdentifierRemap(F.IdentifierRemap);
2374 ContinuousRangeMap<uint32_t, int, 2>::Builder
2375 MacroRemap(F.MacroRemap);
2376 ContinuousRangeMap<uint32_t, int, 2>::Builder
2377 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2378 ContinuousRangeMap<uint32_t, int, 2>::Builder
2379 SubmoduleRemap(F.SubmoduleRemap);
2380 ContinuousRangeMap<uint32_t, int, 2>::Builder
2381 SelectorRemap(F.SelectorRemap);
2382 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2383 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2384
2385 while(Data < DataEnd) {
2386 uint16_t Len = io::ReadUnalignedLE16(Data);
2387 StringRef Name = StringRef((const char*)Data, Len);
2388 Data += Len;
2389 ModuleFile *OM = ModuleMgr.lookup(Name);
2390 if (!OM) {
2391 Error("SourceLocation remap refers to unknown module");
2392 return true;
2393 }
2394
2395 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2396 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2397 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2398 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2399 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2400 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2401 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2402 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2403
2404 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2405 SLocRemap.insert(std::make_pair(SLocOffset,
2406 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2407 IdentifierRemap.insert(
2408 std::make_pair(IdentifierIDOffset,
2409 OM->BaseIdentifierID - IdentifierIDOffset));
2410 MacroRemap.insert(std::make_pair(MacroIDOffset,
2411 OM->BaseMacroID - MacroIDOffset));
2412 PreprocessedEntityRemap.insert(
2413 std::make_pair(PreprocessedEntityIDOffset,
2414 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2415 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2416 OM->BaseSubmoduleID - SubmoduleIDOffset));
2417 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2418 OM->BaseSelectorID - SelectorIDOffset));
2419 DeclRemap.insert(std::make_pair(DeclIDOffset,
2420 OM->BaseDeclID - DeclIDOffset));
2421
2422 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2423 OM->BaseTypeIndex - TypeIndexOffset));
2424
2425 // Global -> local mappings.
2426 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2427 }
2428 break;
2429 }
2430
2431 case SOURCE_MANAGER_LINE_TABLE:
2432 if (ParseLineTable(F, Record))
2433 return true;
2434 break;
2435
2436 case SOURCE_LOCATION_PRELOADS: {
2437 // Need to transform from the local view (1-based IDs) to the global view,
2438 // which is based off F.SLocEntryBaseID.
2439 if (!F.PreloadSLocEntries.empty()) {
2440 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2441 return true;
2442 }
2443
2444 F.PreloadSLocEntries.swap(Record);
2445 break;
2446 }
2447
2448 case EXT_VECTOR_DECLS:
2449 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2450 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2451 break;
2452
2453 case VTABLE_USES:
2454 if (Record.size() % 3 != 0) {
2455 Error("Invalid VTABLE_USES record");
2456 return true;
2457 }
2458
2459 // Later tables overwrite earlier ones.
2460 // FIXME: Modules will have some trouble with this. This is clearly not
2461 // the right way to do this.
2462 VTableUses.clear();
2463
2464 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2465 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2466 VTableUses.push_back(
2467 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2468 VTableUses.push_back(Record[Idx++]);
2469 }
2470 break;
2471
2472 case DYNAMIC_CLASSES:
2473 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2474 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2475 break;
2476
2477 case PENDING_IMPLICIT_INSTANTIATIONS:
2478 if (PendingInstantiations.size() % 2 != 0) {
2479 Error("Invalid existing PendingInstantiations");
2480 return true;
2481 }
2482
2483 if (Record.size() % 2 != 0) {
2484 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2485 return true;
2486 }
2487
2488 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2489 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2490 PendingInstantiations.push_back(
2491 ReadSourceLocation(F, Record, I).getRawEncoding());
2492 }
2493 break;
2494
2495 case SEMA_DECL_REFS:
2496 // Later tables overwrite earlier ones.
2497 // FIXME: Modules will have some trouble with this.
2498 SemaDeclRefs.clear();
2499 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2500 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2501 break;
2502
2503 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002504 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2505 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2506 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002507
2508 unsigned LocalBasePreprocessedEntityID = Record[0];
2509
2510 unsigned StartingID;
2511 if (!PP.getPreprocessingRecord())
2512 PP.createPreprocessingRecord();
2513 if (!PP.getPreprocessingRecord()->getExternalSource())
2514 PP.getPreprocessingRecord()->SetExternalSource(*this);
2515 StartingID
2516 = PP.getPreprocessingRecord()
2517 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2518 F.BasePreprocessedEntityID = StartingID;
2519
2520 if (F.NumPreprocessedEntities > 0) {
2521 // Introduce the global -> local mapping for preprocessed entities in
2522 // this module.
2523 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2524
2525 // Introduce the local -> global mapping for preprocessed entities in
2526 // this module.
2527 F.PreprocessedEntityRemap.insertOrReplace(
2528 std::make_pair(LocalBasePreprocessedEntityID,
2529 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2530 }
2531
2532 break;
2533 }
2534
2535 case DECL_UPDATE_OFFSETS: {
2536 if (Record.size() % 2 != 0) {
2537 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2538 return true;
2539 }
2540 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2541 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2542 .push_back(std::make_pair(&F, Record[I+1]));
2543 break;
2544 }
2545
2546 case DECL_REPLACEMENTS: {
2547 if (Record.size() % 3 != 0) {
2548 Error("invalid DECL_REPLACEMENTS block in AST file");
2549 return true;
2550 }
2551 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2552 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2553 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2554 break;
2555 }
2556
2557 case OBJC_CATEGORIES_MAP: {
2558 if (F.LocalNumObjCCategoriesInMap != 0) {
2559 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2560 return true;
2561 }
2562
2563 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002564 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002565 break;
2566 }
2567
2568 case OBJC_CATEGORIES:
2569 F.ObjCCategories.swap(Record);
2570 break;
2571
2572 case CXX_BASE_SPECIFIER_OFFSETS: {
2573 if (F.LocalNumCXXBaseSpecifiers != 0) {
2574 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2575 return true;
2576 }
2577
2578 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002579 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002580 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2581 break;
2582 }
2583
2584 case DIAG_PRAGMA_MAPPINGS:
2585 if (F.PragmaDiagMappings.empty())
2586 F.PragmaDiagMappings.swap(Record);
2587 else
2588 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2589 Record.begin(), Record.end());
2590 break;
2591
2592 case CUDA_SPECIAL_DECL_REFS:
2593 // Later tables overwrite earlier ones.
2594 // FIXME: Modules will have trouble with this.
2595 CUDASpecialDeclRefs.clear();
2596 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2597 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2598 break;
2599
2600 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002601 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002602 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002603 if (Record[0]) {
2604 F.HeaderFileInfoTable
2605 = HeaderFileInfoLookupTable::Create(
2606 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2607 (const unsigned char *)F.HeaderFileInfoTableData,
2608 HeaderFileInfoTrait(*this, F,
2609 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002610 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002611
2612 PP.getHeaderSearchInfo().SetExternalSource(this);
2613 if (!PP.getHeaderSearchInfo().getExternalLookup())
2614 PP.getHeaderSearchInfo().SetExternalLookup(this);
2615 }
2616 break;
2617 }
2618
2619 case FP_PRAGMA_OPTIONS:
2620 // Later tables overwrite earlier ones.
2621 FPPragmaOptions.swap(Record);
2622 break;
2623
2624 case OPENCL_EXTENSIONS:
2625 // Later tables overwrite earlier ones.
2626 OpenCLExtensions.swap(Record);
2627 break;
2628
2629 case TENTATIVE_DEFINITIONS:
2630 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2631 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2632 break;
2633
2634 case KNOWN_NAMESPACES:
2635 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2636 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2637 break;
Nick Lewycky01a41142013-01-26 00:35:08 +00002638
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002639 case UNDEFINED_BUT_USED:
2640 if (UndefinedButUsed.size() % 2 != 0) {
2641 Error("Invalid existing UndefinedButUsed");
Nick Lewycky01a41142013-01-26 00:35:08 +00002642 return true;
2643 }
2644
2645 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002646 Error("invalid undefined-but-used record");
Nick Lewycky01a41142013-01-26 00:35:08 +00002647 return true;
2648 }
2649 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002650 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2651 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00002652 ReadSourceLocation(F, Record, I).getRawEncoding());
2653 }
2654 break;
2655
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002656 case IMPORTED_MODULES: {
2657 if (F.Kind != MK_Module) {
2658 // If we aren't loading a module (which has its own exports), make
2659 // all of the imported modules visible.
2660 // FIXME: Deal with macros-only imports.
2661 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2662 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2663 ImportedModules.push_back(GlobalID);
2664 }
2665 }
2666 break;
2667 }
2668
2669 case LOCAL_REDECLARATIONS: {
2670 F.RedeclarationChains.swap(Record);
2671 break;
2672 }
2673
2674 case LOCAL_REDECLARATIONS_MAP: {
2675 if (F.LocalNumRedeclarationsInMap != 0) {
2676 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2677 return true;
2678 }
2679
2680 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002681 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002682 break;
2683 }
2684
2685 case MERGED_DECLARATIONS: {
2686 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2687 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2688 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2689 for (unsigned N = Record[Idx++]; N > 0; --N)
2690 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2691 }
2692 break;
2693 }
2694
2695 case MACRO_OFFSET: {
2696 if (F.LocalNumMacros != 0) {
2697 Error("duplicate MACRO_OFFSET record in AST file");
2698 return true;
2699 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002700 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002701 F.LocalNumMacros = Record[0];
2702 unsigned LocalBaseMacroID = Record[1];
2703 F.BaseMacroID = getTotalNumMacros();
2704
2705 if (F.LocalNumMacros > 0) {
2706 // Introduce the global -> local mapping for macros within this module.
2707 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2708
2709 // Introduce the local -> global mapping for macros within this module.
2710 F.MacroRemap.insertOrReplace(
2711 std::make_pair(LocalBaseMacroID,
2712 F.BaseMacroID - LocalBaseMacroID));
2713
2714 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2715 }
2716 break;
2717 }
2718
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002719 case MACRO_TABLE: {
2720 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002721 break;
2722 }
2723 }
2724 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002725}
2726
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002727/// \brief Move the given method to the back of the global list of methods.
2728static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2729 // Find the entry for this selector in the method pool.
2730 Sema::GlobalMethodPool::iterator Known
2731 = S.MethodPool.find(Method->getSelector());
2732 if (Known == S.MethodPool.end())
2733 return;
2734
2735 // Retrieve the appropriate method list.
2736 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2737 : Known->second.second;
2738 bool Found = false;
2739 for (ObjCMethodList *List = &Start; List; List = List->Next) {
2740 if (!Found) {
2741 if (List->Method == Method) {
2742 Found = true;
2743 } else {
2744 // Keep searching.
2745 continue;
2746 }
2747 }
2748
2749 if (List->Next)
2750 List->Method = List->Next->Method;
2751 else
2752 List->Method = Method;
2753 }
2754}
2755
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002756void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002757 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2758 switch (Names[I].getKind()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002759 case HiddenName::Declaration: {
2760 Decl *D = Names[I].getDecl();
2761 bool wasHidden = D->Hidden;
2762 D->Hidden = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002763
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002764 if (wasHidden && SemaObj) {
2765 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2766 moveMethodToBackOfGlobalList(*SemaObj, Method);
2767 }
2768 }
2769 break;
2770 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002771 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002772 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002773 installImportedMacro(Macro.first, Macro.second, Owner);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002774 break;
2775 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002776 }
2777 }
2778}
2779
2780void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00002781 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00002782 SourceLocation ImportLoc,
2783 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002784 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002785 SmallVector<Module *, 4> Stack;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002786 Stack.push_back(Mod);
2787 while (!Stack.empty()) {
2788 Mod = Stack.back();
2789 Stack.pop_back();
2790
2791 if (NameVisibility <= Mod->NameVisibility) {
2792 // This module already has this level of visibility (or greater), so
2793 // there is nothing more to do.
2794 continue;
2795 }
2796
2797 if (!Mod->isAvailable()) {
2798 // Modules that aren't available cannot be made visible.
2799 continue;
2800 }
2801
2802 // Update the module's name visibility.
2803 Mod->NameVisibility = NameVisibility;
2804
2805 // If we've already deserialized any names from this module,
2806 // mark them as visible.
2807 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2808 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002809 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002810 HiddenNamesMap.erase(Hidden);
2811 }
2812
2813 // Push any non-explicit submodules onto the stack to be marked as
2814 // visible.
2815 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2816 SubEnd = Mod->submodule_end();
2817 Sub != SubEnd; ++Sub) {
2818 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2819 Stack.push_back(*Sub);
2820 }
2821
2822 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00002823 SmallVector<Module *, 16> Exports;
2824 Mod->getExportedModules(Exports);
2825 for (SmallVectorImpl<Module *>::iterator
2826 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2827 Module *Exported = *I;
2828 if (Visited.insert(Exported))
2829 Stack.push_back(Exported);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002830 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002831
2832 // Detect any conflicts.
2833 if (Complain) {
2834 assert(ImportLoc.isValid() && "Missing import location");
2835 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2836 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2837 Diag(ImportLoc, diag::warn_module_conflict)
2838 << Mod->getFullModuleName()
2839 << Mod->Conflicts[I].Other->getFullModuleName()
2840 << Mod->Conflicts[I].Message;
2841 // FIXME: Need note where the other module was imported.
2842 }
2843 }
2844 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002845 }
2846}
2847
Douglas Gregor1a49d972013-01-25 01:03:03 +00002848bool ASTReader::loadGlobalIndex() {
2849 if (GlobalIndex)
2850 return false;
2851
2852 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2853 !Context.getLangOpts().Modules)
2854 return true;
2855
2856 // Try to load the global index.
2857 TriedLoadingGlobalIndex = true;
2858 StringRef ModuleCachePath
2859 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2860 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor677e15f2013-03-19 00:28:20 +00002861 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002862 if (!Result.first)
2863 return true;
2864
2865 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002866 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-01-25 01:03:03 +00002867 return false;
2868}
2869
2870bool ASTReader::isGlobalIndexUnavailable() const {
2871 return Context.getLangOpts().Modules && UseGlobalIndex &&
2872 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2873}
2874
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002875ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2876 ModuleKind Type,
2877 SourceLocation ImportLoc,
2878 unsigned ClientLoadCapabilities) {
2879 // Bump the generation number.
2880 unsigned PreviousGeneration = CurrentGeneration++;
2881
2882 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002883 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002884 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2885 /*ImportedBy=*/0, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002886 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002887 ClientLoadCapabilities)) {
2888 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002889 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002890 case OutOfDate:
2891 case VersionMismatch:
2892 case ConfigurationMismatch:
2893 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002894 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2895 Context.getLangOpts().Modules
2896 ? &PP.getHeaderSearchInfo().getModuleMap()
2897 : 0);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002898
2899 // If we find that any modules are unusable, the global index is going
2900 // to be out-of-date. Just remove it.
2901 GlobalIndex.reset();
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002902 ModuleMgr.setGlobalIndex(0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002903 return ReadResult;
2904
2905 case Success:
2906 break;
2907 }
2908
2909 // Here comes stuff that we only do once the entire chain is loaded.
2910
2911 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002912 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2913 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002914 M != MEnd; ++M) {
2915 ModuleFile &F = *M->Mod;
2916
2917 // Read the AST block.
2918 if (ReadASTBlock(F))
2919 return Failure;
2920
2921 // Once read, set the ModuleFile bit base offset and update the size in
2922 // bits of all files we've seen.
2923 F.GlobalBitOffset = TotalModulesSizeInBits;
2924 TotalModulesSizeInBits += F.SizeInBits;
2925 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2926
2927 // Preload SLocEntries.
2928 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2929 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2930 // Load it through the SourceManager and don't call ReadSLocEntry()
2931 // directly because the entry may have already been loaded in which case
2932 // calling ReadSLocEntry() directly would trigger an assertion in
2933 // SourceManager.
2934 SourceMgr.getLoadedSLocEntryByID(Index);
2935 }
2936 }
2937
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002938 // Setup the import locations and notify the module manager that we've
2939 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002940 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2941 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002942 M != MEnd; ++M) {
2943 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002944
2945 ModuleMgr.moduleFileAccepted(&F);
2946
2947 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00002948 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002949 if (!M->ImportedBy)
2950 F.ImportLoc = M->ImportLoc;
2951 else
2952 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2953 M->ImportLoc.getRawEncoding());
2954 }
2955
2956 // Mark all of the identifiers in the identifier table as being out of date,
2957 // so that various accessors know to check the loaded modules when the
2958 // identifier is used.
2959 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2960 IdEnd = PP.getIdentifierTable().end();
2961 Id != IdEnd; ++Id)
2962 Id->second->setOutOfDate(true);
2963
2964 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00002965 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2966 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002967 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2968 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00002969
2970 switch (Unresolved.Kind) {
2971 case UnresolvedModuleRef::Conflict:
2972 if (ResolvedMod) {
2973 Module::Conflict Conflict;
2974 Conflict.Other = ResolvedMod;
2975 Conflict.Message = Unresolved.String.str();
2976 Unresolved.Mod->Conflicts.push_back(Conflict);
2977 }
2978 continue;
2979
2980 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002981 if (ResolvedMod)
2982 Unresolved.Mod->Imports.push_back(ResolvedMod);
2983 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002984
Douglas Gregor906d66a2013-03-20 21:10:35 +00002985 case UnresolvedModuleRef::Export:
2986 if (ResolvedMod || Unresolved.IsWildcard)
2987 Unresolved.Mod->Exports.push_back(
2988 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
2989 continue;
2990 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002991 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002992 UnresolvedModuleRefs.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002993
2994 InitializeContext();
2995
2996 if (DeserializationListener)
2997 DeserializationListener->ReaderInitialized(this);
2998
2999 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3000 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3001 PrimaryModule.OriginalSourceFileID
3002 = FileID::get(PrimaryModule.SLocEntryBaseID
3003 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3004
3005 // If this AST file is a precompiled preamble, then set the
3006 // preamble file ID of the source manager to the file source file
3007 // from which the preamble was built.
3008 if (Type == MK_Preamble) {
3009 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3010 } else if (Type == MK_MainFile) {
3011 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3012 }
3013 }
3014
3015 // For any Objective-C class definitions we have already loaded, make sure
3016 // that we load any additional categories.
3017 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3018 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3019 ObjCClassesLoaded[I],
3020 PreviousGeneration);
3021 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00003022
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003023 return Success;
3024}
3025
3026ASTReader::ASTReadResult
3027ASTReader::ReadASTCore(StringRef FileName,
3028 ModuleKind Type,
3029 SourceLocation ImportLoc,
3030 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003031 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003032 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003033 unsigned ClientLoadCapabilities) {
3034 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003035 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003036 ModuleManager::AddModuleResult AddResult
3037 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3038 CurrentGeneration, ExpectedSize, ExpectedModTime,
3039 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003040
Douglas Gregor677e15f2013-03-19 00:28:20 +00003041 switch (AddResult) {
3042 case ModuleManager::AlreadyLoaded:
3043 return Success;
3044
3045 case ModuleManager::NewlyLoaded:
3046 // Load module file below.
3047 break;
3048
3049 case ModuleManager::Missing:
3050 // The module file was missing; if the client handle handle, that, return
3051 // it.
3052 if (ClientLoadCapabilities & ARR_Missing)
3053 return Missing;
3054
3055 // Otherwise, return an error.
3056 {
3057 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3058 + ErrorStr;
3059 Error(Msg);
3060 }
3061 return Failure;
3062
3063 case ModuleManager::OutOfDate:
3064 // We couldn't load the module file because it is out-of-date. If the
3065 // client can handle out-of-date, return it.
3066 if (ClientLoadCapabilities & ARR_OutOfDate)
3067 return OutOfDate;
3068
3069 // Otherwise, return an error.
3070 {
3071 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3072 + ErrorStr;
3073 Error(Msg);
3074 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003075 return Failure;
3076 }
3077
Douglas Gregor677e15f2013-03-19 00:28:20 +00003078 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003079
3080 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3081 // module?
3082 if (FileName != "-") {
3083 CurrentDir = llvm::sys::path::parent_path(FileName);
3084 if (CurrentDir.empty()) CurrentDir = ".";
3085 }
3086
3087 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003088 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003089 Stream.init(F.StreamFile);
3090 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3091
3092 // Sniff for the signature.
3093 if (Stream.Read(8) != 'C' ||
3094 Stream.Read(8) != 'P' ||
3095 Stream.Read(8) != 'C' ||
3096 Stream.Read(8) != 'H') {
3097 Diag(diag::err_not_a_pch_file) << FileName;
3098 return Failure;
3099 }
3100
3101 // This is used for compatibility with older PCH formats.
3102 bool HaveReadControlBlock = false;
3103
Chris Lattner99a5af02013-01-20 00:00:22 +00003104 while (1) {
3105 llvm::BitstreamEntry Entry = Stream.advance();
3106
3107 switch (Entry.Kind) {
3108 case llvm::BitstreamEntry::Error:
3109 case llvm::BitstreamEntry::EndBlock:
3110 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003111 Error("invalid record at top-level of AST file");
3112 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003113
3114 case llvm::BitstreamEntry::SubBlock:
3115 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003116 }
3117
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003118 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003119 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003120 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3121 if (Stream.ReadBlockInfoBlock()) {
3122 Error("malformed BlockInfoBlock in AST file");
3123 return Failure;
3124 }
3125 break;
3126 case CONTROL_BLOCK_ID:
3127 HaveReadControlBlock = true;
3128 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3129 case Success:
3130 break;
3131
3132 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003133 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003134 case OutOfDate: return OutOfDate;
3135 case VersionMismatch: return VersionMismatch;
3136 case ConfigurationMismatch: return ConfigurationMismatch;
3137 case HadErrors: return HadErrors;
3138 }
3139 break;
3140 case AST_BLOCK_ID:
3141 if (!HaveReadControlBlock) {
3142 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3143 Diag(diag::warn_pch_version_too_old);
3144 return VersionMismatch;
3145 }
3146
3147 // Record that we've loaded this module.
3148 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3149 return Success;
3150
3151 default:
3152 if (Stream.SkipBlock()) {
3153 Error("malformed block record in AST file");
3154 return Failure;
3155 }
3156 break;
3157 }
3158 }
3159
3160 return Success;
3161}
3162
3163void ASTReader::InitializeContext() {
3164 // If there's a listener, notify them that we "read" the translation unit.
3165 if (DeserializationListener)
3166 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3167 Context.getTranslationUnitDecl());
3168
3169 // Make sure we load the declaration update records for the translation unit,
3170 // if there are any.
3171 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3172 Context.getTranslationUnitDecl());
3173
3174 // FIXME: Find a better way to deal with collisions between these
3175 // built-in types. Right now, we just ignore the problem.
3176
3177 // Load the special types.
3178 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3179 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3180 if (!Context.CFConstantStringTypeDecl)
3181 Context.setCFConstantStringType(GetType(String));
3182 }
3183
3184 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3185 QualType FileType = GetType(File);
3186 if (FileType.isNull()) {
3187 Error("FILE type is NULL");
3188 return;
3189 }
3190
3191 if (!Context.FILEDecl) {
3192 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3193 Context.setFILEDecl(Typedef->getDecl());
3194 else {
3195 const TagType *Tag = FileType->getAs<TagType>();
3196 if (!Tag) {
3197 Error("Invalid FILE type in AST file");
3198 return;
3199 }
3200 Context.setFILEDecl(Tag->getDecl());
3201 }
3202 }
3203 }
3204
3205 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3206 QualType Jmp_bufType = GetType(Jmp_buf);
3207 if (Jmp_bufType.isNull()) {
3208 Error("jmp_buf type is NULL");
3209 return;
3210 }
3211
3212 if (!Context.jmp_bufDecl) {
3213 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3214 Context.setjmp_bufDecl(Typedef->getDecl());
3215 else {
3216 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3217 if (!Tag) {
3218 Error("Invalid jmp_buf type in AST file");
3219 return;
3220 }
3221 Context.setjmp_bufDecl(Tag->getDecl());
3222 }
3223 }
3224 }
3225
3226 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3227 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3228 if (Sigjmp_bufType.isNull()) {
3229 Error("sigjmp_buf type is NULL");
3230 return;
3231 }
3232
3233 if (!Context.sigjmp_bufDecl) {
3234 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3235 Context.setsigjmp_bufDecl(Typedef->getDecl());
3236 else {
3237 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3238 assert(Tag && "Invalid sigjmp_buf type in AST file");
3239 Context.setsigjmp_bufDecl(Tag->getDecl());
3240 }
3241 }
3242 }
3243
3244 if (unsigned ObjCIdRedef
3245 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3246 if (Context.ObjCIdRedefinitionType.isNull())
3247 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3248 }
3249
3250 if (unsigned ObjCClassRedef
3251 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3252 if (Context.ObjCClassRedefinitionType.isNull())
3253 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3254 }
3255
3256 if (unsigned ObjCSelRedef
3257 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3258 if (Context.ObjCSelRedefinitionType.isNull())
3259 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3260 }
3261
3262 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3263 QualType Ucontext_tType = GetType(Ucontext_t);
3264 if (Ucontext_tType.isNull()) {
3265 Error("ucontext_t type is NULL");
3266 return;
3267 }
3268
3269 if (!Context.ucontext_tDecl) {
3270 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3271 Context.setucontext_tDecl(Typedef->getDecl());
3272 else {
3273 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3274 assert(Tag && "Invalid ucontext_t type in AST file");
3275 Context.setucontext_tDecl(Tag->getDecl());
3276 }
3277 }
3278 }
3279 }
3280
3281 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3282
3283 // If there were any CUDA special declarations, deserialize them.
3284 if (!CUDASpecialDeclRefs.empty()) {
3285 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3286 Context.setcudaConfigureCallDecl(
3287 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3288 }
3289
3290 // Re-export any modules that were imported by a non-module AST file.
3291 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3292 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003293 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003294 /*ImportLoc=*/SourceLocation(),
3295 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003296 }
3297 ImportedModules.clear();
3298}
3299
3300void ASTReader::finalizeForWriting() {
3301 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3302 HiddenEnd = HiddenNamesMap.end();
3303 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003304 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003305 }
3306 HiddenNamesMap.clear();
3307}
3308
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003309/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan
3310/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning
3311/// false on success and true on failure.
3312static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) {
3313 while (1) {
3314 llvm::BitstreamEntry Entry = Cursor.advance();
3315 switch (Entry.Kind) {
3316 case llvm::BitstreamEntry::Error:
3317 case llvm::BitstreamEntry::EndBlock:
3318 return true;
3319
3320 case llvm::BitstreamEntry::Record:
3321 // Ignore top-level records.
3322 Cursor.skipRecord(Entry.ID);
3323 break;
3324
3325 case llvm::BitstreamEntry::SubBlock:
3326 if (Entry.ID == CONTROL_BLOCK_ID) {
3327 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID))
3328 return true;
3329 // Found it!
3330 return false;
3331 }
3332
3333 if (Cursor.SkipBlock())
3334 return true;
3335 }
3336 }
3337}
3338
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003339/// \brief Retrieve the name of the original source file name
3340/// directly from the AST file, without actually loading the AST
3341/// file.
3342std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3343 FileManager &FileMgr,
3344 DiagnosticsEngine &Diags) {
3345 // Open the AST file.
3346 std::string ErrStr;
3347 OwningPtr<llvm::MemoryBuffer> Buffer;
3348 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3349 if (!Buffer) {
3350 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3351 return std::string();
3352 }
3353
3354 // Initialize the stream
3355 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003356 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003357 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3358 (const unsigned char *)Buffer->getBufferEnd());
3359 Stream.init(StreamFile);
3360
3361 // Sniff for the signature.
3362 if (Stream.Read(8) != 'C' ||
3363 Stream.Read(8) != 'P' ||
3364 Stream.Read(8) != 'C' ||
3365 Stream.Read(8) != 'H') {
3366 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3367 return std::string();
3368 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003369
Chris Lattner88bde502013-01-19 21:39:22 +00003370 // Scan for the CONTROL_BLOCK_ID block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003371 if (SkipCursorToControlBlock(Stream)) {
3372 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3373 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003374 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003375
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003376 // Scan for ORIGINAL_FILE inside the control block.
3377 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003378 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003379 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003380 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3381 return std::string();
3382
3383 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3384 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3385 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003386 }
Chris Lattner88bde502013-01-19 21:39:22 +00003387
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003388 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003389 StringRef Blob;
3390 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3391 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003392 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003393}
3394
3395namespace {
3396 class SimplePCHValidator : public ASTReaderListener {
3397 const LangOptions &ExistingLangOpts;
3398 const TargetOptions &ExistingTargetOpts;
3399 const PreprocessorOptions &ExistingPPOpts;
3400 FileManager &FileMgr;
3401
3402 public:
3403 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3404 const TargetOptions &ExistingTargetOpts,
3405 const PreprocessorOptions &ExistingPPOpts,
3406 FileManager &FileMgr)
3407 : ExistingLangOpts(ExistingLangOpts),
3408 ExistingTargetOpts(ExistingTargetOpts),
3409 ExistingPPOpts(ExistingPPOpts),
3410 FileMgr(FileMgr)
3411 {
3412 }
3413
3414 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3415 bool Complain) {
3416 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3417 }
3418 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3419 bool Complain) {
3420 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3421 }
3422 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3423 bool Complain,
3424 std::string &SuggestedPredefines) {
3425 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3426 SuggestedPredefines);
3427 }
3428 };
3429}
3430
3431bool ASTReader::readASTFileControlBlock(StringRef Filename,
3432 FileManager &FileMgr,
3433 ASTReaderListener &Listener) {
3434 // Open the AST file.
3435 std::string ErrStr;
3436 OwningPtr<llvm::MemoryBuffer> Buffer;
3437 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3438 if (!Buffer) {
3439 return true;
3440 }
3441
3442 // Initialize the stream
3443 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003444 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003445 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3446 (const unsigned char *)Buffer->getBufferEnd());
3447 Stream.init(StreamFile);
3448
3449 // Sniff for the signature.
3450 if (Stream.Read(8) != 'C' ||
3451 Stream.Read(8) != 'P' ||
3452 Stream.Read(8) != 'C' ||
3453 Stream.Read(8) != 'H') {
3454 return true;
3455 }
3456
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003457 // Scan for the CONTROL_BLOCK_ID block.
3458 if (SkipCursorToControlBlock(Stream))
3459 return true;
3460
3461 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003462 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003463 while (1) {
3464 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3465 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3466 return false;
3467
3468 if (Entry.Kind != llvm::BitstreamEntry::Record)
3469 return true;
3470
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003471 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003472 StringRef Blob;
3473 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003474 switch ((ControlRecordTypes)RecCode) {
3475 case METADATA: {
3476 if (Record[0] != VERSION_MAJOR)
3477 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003478
Douglas Gregorc544ba02013-03-27 16:47:18 +00003479 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003480 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00003481
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003482 break;
3483 }
3484 case LANGUAGE_OPTIONS:
3485 if (ParseLanguageOptions(Record, false, Listener))
3486 return true;
3487 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003488
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003489 case TARGET_OPTIONS:
3490 if (ParseTargetOptions(Record, false, Listener))
3491 return true;
3492 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003493
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003494 case DIAGNOSTIC_OPTIONS:
3495 if (ParseDiagnosticOptions(Record, false, Listener))
3496 return true;
3497 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003498
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003499 case FILE_SYSTEM_OPTIONS:
3500 if (ParseFileSystemOptions(Record, false, Listener))
3501 return true;
3502 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003503
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003504 case HEADER_SEARCH_OPTIONS:
3505 if (ParseHeaderSearchOptions(Record, false, Listener))
3506 return true;
3507 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003508
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003509 case PREPROCESSOR_OPTIONS: {
3510 std::string IgnoredSuggestedPredefines;
3511 if (ParsePreprocessorOptions(Record, false, Listener,
3512 IgnoredSuggestedPredefines))
3513 return true;
3514 break;
3515 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003516
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003517 default:
3518 // No other validation to perform.
3519 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003520 }
3521 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003522}
3523
3524
3525bool ASTReader::isAcceptableASTFile(StringRef Filename,
3526 FileManager &FileMgr,
3527 const LangOptions &LangOpts,
3528 const TargetOptions &TargetOpts,
3529 const PreprocessorOptions &PPOpts) {
3530 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3531 return !readASTFileControlBlock(Filename, FileMgr, validator);
3532}
3533
3534bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3535 // Enter the submodule block.
3536 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3537 Error("malformed submodule block record in AST file");
3538 return true;
3539 }
3540
3541 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3542 bool First = true;
3543 Module *CurrentModule = 0;
3544 RecordData Record;
3545 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003546 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3547
3548 switch (Entry.Kind) {
3549 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3550 case llvm::BitstreamEntry::Error:
3551 Error("malformed block record in AST file");
3552 return true;
3553 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003554 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003555 case llvm::BitstreamEntry::Record:
3556 // The interesting case.
3557 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003558 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003559
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003560 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003561 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003562 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003563 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003564 default: // Default behavior: ignore.
3565 break;
3566
3567 case SUBMODULE_DEFINITION: {
3568 if (First) {
3569 Error("missing submodule metadata record at beginning of block");
3570 return true;
3571 }
3572
Douglas Gregor970e4412013-03-20 03:59:18 +00003573 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003574 Error("malformed module definition");
3575 return true;
3576 }
3577
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003578 StringRef Name = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003579 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3580 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3581 bool IsFramework = Record[2];
3582 bool IsExplicit = Record[3];
3583 bool IsSystem = Record[4];
3584 bool InferSubmodules = Record[5];
3585 bool InferExplicitSubmodules = Record[6];
3586 bool InferExportWildcard = Record[7];
Douglas Gregor970e4412013-03-20 03:59:18 +00003587 bool ConfigMacrosExhaustive = Record[8];
3588
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003589 Module *ParentModule = 0;
3590 if (Parent)
3591 ParentModule = getSubmodule(Parent);
3592
3593 // Retrieve this (sub)module from the module map, creating it if
3594 // necessary.
3595 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3596 IsFramework,
3597 IsExplicit).first;
3598 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3599 if (GlobalIndex >= SubmodulesLoaded.size() ||
3600 SubmodulesLoaded[GlobalIndex]) {
3601 Error("too many submodules");
3602 return true;
3603 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003604
Douglas Gregor677e15f2013-03-19 00:28:20 +00003605 if (!ParentModule) {
3606 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3607 if (CurFile != F.File) {
3608 if (!Diags.isDiagnosticInFlight()) {
3609 Diag(diag::err_module_file_conflict)
3610 << CurrentModule->getTopLevelModuleName()
3611 << CurFile->getName()
3612 << F.File->getName();
3613 }
3614 return true;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003615 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003616 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003617
3618 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003619 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003620
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003621 CurrentModule->IsFromModuleFile = true;
3622 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3623 CurrentModule->InferSubmodules = InferSubmodules;
3624 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3625 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00003626 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003627 if (DeserializationListener)
3628 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3629
3630 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003631
Douglas Gregor906d66a2013-03-20 21:10:35 +00003632 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003633 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00003634 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00003635 CurrentModule->UnresolvedConflicts.clear();
3636 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003637 break;
3638 }
3639
3640 case SUBMODULE_UMBRELLA_HEADER: {
3641 if (First) {
3642 Error("missing submodule metadata record at beginning of block");
3643 return true;
3644 }
3645
3646 if (!CurrentModule)
3647 break;
3648
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003649 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003650 if (!CurrentModule->getUmbrellaHeader())
3651 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3652 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3653 Error("mismatched umbrella headers in submodule");
3654 return true;
3655 }
3656 }
3657 break;
3658 }
3659
3660 case SUBMODULE_HEADER: {
3661 if (First) {
3662 Error("missing submodule metadata record at beginning of block");
3663 return true;
3664 }
3665
3666 if (!CurrentModule)
3667 break;
3668
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003669 // We lazily associate headers with their modules via the HeaderInfoTable.
3670 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3671 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003672 break;
3673 }
3674
3675 case SUBMODULE_EXCLUDED_HEADER: {
3676 if (First) {
3677 Error("missing submodule metadata record at beginning of block");
3678 return true;
3679 }
3680
3681 if (!CurrentModule)
3682 break;
3683
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003684 // We lazily associate headers with their modules via the HeaderInfoTable.
3685 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3686 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003687 break;
3688 }
3689
3690 case SUBMODULE_TOPHEADER: {
3691 if (First) {
3692 Error("missing submodule metadata record at beginning of block");
3693 return true;
3694 }
3695
3696 if (!CurrentModule)
3697 break;
3698
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00003699 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003700 break;
3701 }
3702
3703 case SUBMODULE_UMBRELLA_DIR: {
3704 if (First) {
3705 Error("missing submodule metadata record at beginning of block");
3706 return true;
3707 }
3708
3709 if (!CurrentModule)
3710 break;
3711
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003712 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003713 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003714 if (!CurrentModule->getUmbrellaDir())
3715 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3716 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3717 Error("mismatched umbrella directories in submodule");
3718 return true;
3719 }
3720 }
3721 break;
3722 }
3723
3724 case SUBMODULE_METADATA: {
3725 if (!First) {
3726 Error("submodule metadata record not at beginning of block");
3727 return true;
3728 }
3729 First = false;
3730
3731 F.BaseSubmoduleID = getTotalNumSubmodules();
3732 F.LocalNumSubmodules = Record[0];
3733 unsigned LocalBaseSubmoduleID = Record[1];
3734 if (F.LocalNumSubmodules > 0) {
3735 // Introduce the global -> local mapping for submodules within this
3736 // module.
3737 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3738
3739 // Introduce the local -> global mapping for submodules within this
3740 // module.
3741 F.SubmoduleRemap.insertOrReplace(
3742 std::make_pair(LocalBaseSubmoduleID,
3743 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3744
3745 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3746 }
3747 break;
3748 }
3749
3750 case SUBMODULE_IMPORTS: {
3751 if (First) {
3752 Error("missing submodule metadata record at beginning of block");
3753 return true;
3754 }
3755
3756 if (!CurrentModule)
3757 break;
3758
3759 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003760 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003761 Unresolved.File = &F;
3762 Unresolved.Mod = CurrentModule;
3763 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003764 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003765 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003766 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003767 }
3768 break;
3769 }
3770
3771 case SUBMODULE_EXPORTS: {
3772 if (First) {
3773 Error("missing submodule metadata record at beginning of block");
3774 return true;
3775 }
3776
3777 if (!CurrentModule)
3778 break;
3779
3780 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003781 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003782 Unresolved.File = &F;
3783 Unresolved.Mod = CurrentModule;
3784 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003785 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003786 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003787 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003788 }
3789
3790 // Once we've loaded the set of exports, there's no reason to keep
3791 // the parsed, unresolved exports around.
3792 CurrentModule->UnresolvedExports.clear();
3793 break;
3794 }
3795 case SUBMODULE_REQUIRES: {
3796 if (First) {
3797 Error("missing submodule metadata record at beginning of block");
3798 return true;
3799 }
3800
3801 if (!CurrentModule)
3802 break;
3803
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003804 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003805 Context.getTargetInfo());
3806 break;
3807 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003808
3809 case SUBMODULE_LINK_LIBRARY:
3810 if (First) {
3811 Error("missing submodule metadata record at beginning of block");
3812 return true;
3813 }
3814
3815 if (!CurrentModule)
3816 break;
3817
3818 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003819 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003820 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00003821
3822 case SUBMODULE_CONFIG_MACRO:
3823 if (First) {
3824 Error("missing submodule metadata record at beginning of block");
3825 return true;
3826 }
3827
3828 if (!CurrentModule)
3829 break;
3830
3831 CurrentModule->ConfigMacros.push_back(Blob.str());
3832 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003833
3834 case SUBMODULE_CONFLICT: {
3835 if (First) {
3836 Error("missing submodule metadata record at beginning of block");
3837 return true;
3838 }
3839
3840 if (!CurrentModule)
3841 break;
3842
3843 UnresolvedModuleRef Unresolved;
3844 Unresolved.File = &F;
3845 Unresolved.Mod = CurrentModule;
3846 Unresolved.ID = Record[0];
3847 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3848 Unresolved.IsWildcard = false;
3849 Unresolved.String = Blob;
3850 UnresolvedModuleRefs.push_back(Unresolved);
3851 break;
3852 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003853 }
3854 }
3855}
3856
3857/// \brief Parse the record that corresponds to a LangOptions data
3858/// structure.
3859///
3860/// This routine parses the language options from the AST file and then gives
3861/// them to the AST listener if one is set.
3862///
3863/// \returns true if the listener deems the file unacceptable, false otherwise.
3864bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3865 bool Complain,
3866 ASTReaderListener &Listener) {
3867 LangOptions LangOpts;
3868 unsigned Idx = 0;
3869#define LANGOPT(Name, Bits, Default, Description) \
3870 LangOpts.Name = Record[Idx++];
3871#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3872 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3873#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003874#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3875#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003876
3877 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3878 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3879 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3880
3881 unsigned Length = Record[Idx++];
3882 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3883 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003884
3885 Idx += Length;
3886
3887 // Comment options.
3888 for (unsigned N = Record[Idx++]; N; --N) {
3889 LangOpts.CommentOpts.BlockCommandNames.push_back(
3890 ReadString(Record, Idx));
3891 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00003892 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003893
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003894 return Listener.ReadLanguageOptions(LangOpts, Complain);
3895}
3896
3897bool ASTReader::ParseTargetOptions(const RecordData &Record,
3898 bool Complain,
3899 ASTReaderListener &Listener) {
3900 unsigned Idx = 0;
3901 TargetOptions TargetOpts;
3902 TargetOpts.Triple = ReadString(Record, Idx);
3903 TargetOpts.CPU = ReadString(Record, Idx);
3904 TargetOpts.ABI = ReadString(Record, Idx);
3905 TargetOpts.CXXABI = ReadString(Record, Idx);
3906 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3907 for (unsigned N = Record[Idx++]; N; --N) {
3908 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3909 }
3910 for (unsigned N = Record[Idx++]; N; --N) {
3911 TargetOpts.Features.push_back(ReadString(Record, Idx));
3912 }
3913
3914 return Listener.ReadTargetOptions(TargetOpts, Complain);
3915}
3916
3917bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3918 ASTReaderListener &Listener) {
3919 DiagnosticOptions DiagOpts;
3920 unsigned Idx = 0;
3921#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3922#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3923 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3924#include "clang/Basic/DiagnosticOptions.def"
3925
3926 for (unsigned N = Record[Idx++]; N; --N) {
3927 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3928 }
3929
3930 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3931}
3932
3933bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3934 ASTReaderListener &Listener) {
3935 FileSystemOptions FSOpts;
3936 unsigned Idx = 0;
3937 FSOpts.WorkingDir = ReadString(Record, Idx);
3938 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3939}
3940
3941bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3942 bool Complain,
3943 ASTReaderListener &Listener) {
3944 HeaderSearchOptions HSOpts;
3945 unsigned Idx = 0;
3946 HSOpts.Sysroot = ReadString(Record, Idx);
3947
3948 // Include entries.
3949 for (unsigned N = Record[Idx++]; N; --N) {
3950 std::string Path = ReadString(Record, Idx);
3951 frontend::IncludeDirGroup Group
3952 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003953 bool IsFramework = Record[Idx++];
3954 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003955 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00003956 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003957 }
3958
3959 // System header prefixes.
3960 for (unsigned N = Record[Idx++]; N; --N) {
3961 std::string Prefix = ReadString(Record, Idx);
3962 bool IsSystemHeader = Record[Idx++];
3963 HSOpts.SystemHeaderPrefixes.push_back(
3964 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3965 }
3966
3967 HSOpts.ResourceDir = ReadString(Record, Idx);
3968 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3969 HSOpts.DisableModuleHash = Record[Idx++];
3970 HSOpts.UseBuiltinIncludes = Record[Idx++];
3971 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3972 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3973 HSOpts.UseLibcxx = Record[Idx++];
3974
3975 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3976}
3977
3978bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3979 bool Complain,
3980 ASTReaderListener &Listener,
3981 std::string &SuggestedPredefines) {
3982 PreprocessorOptions PPOpts;
3983 unsigned Idx = 0;
3984
3985 // Macro definitions/undefs
3986 for (unsigned N = Record[Idx++]; N; --N) {
3987 std::string Macro = ReadString(Record, Idx);
3988 bool IsUndef = Record[Idx++];
3989 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3990 }
3991
3992 // Includes
3993 for (unsigned N = Record[Idx++]; N; --N) {
3994 PPOpts.Includes.push_back(ReadString(Record, Idx));
3995 }
3996
3997 // Macro Includes
3998 for (unsigned N = Record[Idx++]; N; --N) {
3999 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4000 }
4001
4002 PPOpts.UsePredefines = Record[Idx++];
4003 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4004 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4005 PPOpts.ObjCXXARCStandardLibrary =
4006 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4007 SuggestedPredefines.clear();
4008 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4009 SuggestedPredefines);
4010}
4011
4012std::pair<ModuleFile *, unsigned>
4013ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4014 GlobalPreprocessedEntityMapType::iterator
4015 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4016 assert(I != GlobalPreprocessedEntityMap.end() &&
4017 "Corrupted global preprocessed entity map");
4018 ModuleFile *M = I->second;
4019 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4020 return std::make_pair(M, LocalIndex);
4021}
4022
4023std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4024ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4025 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4026 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4027 Mod.NumPreprocessedEntities);
4028
4029 return std::make_pair(PreprocessingRecord::iterator(),
4030 PreprocessingRecord::iterator());
4031}
4032
4033std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4034ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4035 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4036 ModuleDeclIterator(this, &Mod,
4037 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4038}
4039
4040PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4041 PreprocessedEntityID PPID = Index+1;
4042 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4043 ModuleFile &M = *PPInfo.first;
4044 unsigned LocalIndex = PPInfo.second;
4045 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4046
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004047 if (!PP.getPreprocessingRecord()) {
4048 Error("no preprocessing record");
4049 return 0;
4050 }
4051
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004052 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4053 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4054
4055 llvm::BitstreamEntry Entry =
4056 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4057 if (Entry.Kind != llvm::BitstreamEntry::Record)
4058 return 0;
4059
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004060 // Read the record.
4061 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4062 ReadSourceLocation(M, PPOffs.End));
4063 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004064 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004065 RecordData Record;
4066 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004067 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4068 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004069 switch (RecType) {
4070 case PPD_MACRO_EXPANSION: {
4071 bool isBuiltin = Record[0];
4072 IdentifierInfo *Name = 0;
4073 MacroDefinition *Def = 0;
4074 if (isBuiltin)
4075 Name = getLocalIdentifier(M, Record[1]);
4076 else {
4077 PreprocessedEntityID
4078 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4079 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4080 }
4081
4082 MacroExpansion *ME;
4083 if (isBuiltin)
4084 ME = new (PPRec) MacroExpansion(Name, Range);
4085 else
4086 ME = new (PPRec) MacroExpansion(Def, Range);
4087
4088 return ME;
4089 }
4090
4091 case PPD_MACRO_DEFINITION: {
4092 // Decode the identifier info and then check again; if the macro is
4093 // still defined and associated with the identifier,
4094 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4095 MacroDefinition *MD
4096 = new (PPRec) MacroDefinition(II, Range);
4097
4098 if (DeserializationListener)
4099 DeserializationListener->MacroDefinitionRead(PPID, MD);
4100
4101 return MD;
4102 }
4103
4104 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004105 const char *FullFileNameStart = Blob.data() + Record[0];
4106 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004107 const FileEntry *File = 0;
4108 if (!FullFileName.empty())
4109 File = PP.getFileManager().getFile(FullFileName);
4110
4111 // FIXME: Stable encoding
4112 InclusionDirective::InclusionKind Kind
4113 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4114 InclusionDirective *ID
4115 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004116 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004117 Record[1], Record[3],
4118 File,
4119 Range);
4120 return ID;
4121 }
4122 }
4123
4124 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4125}
4126
4127/// \brief \arg SLocMapI points at a chunk of a module that contains no
4128/// preprocessed entities or the entities it contains are not the ones we are
4129/// looking for. Find the next module that contains entities and return the ID
4130/// of the first entry.
4131PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4132 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4133 ++SLocMapI;
4134 for (GlobalSLocOffsetMapType::const_iterator
4135 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4136 ModuleFile &M = *SLocMapI->second;
4137 if (M.NumPreprocessedEntities)
4138 return M.BasePreprocessedEntityID;
4139 }
4140
4141 return getTotalNumPreprocessedEntities();
4142}
4143
4144namespace {
4145
4146template <unsigned PPEntityOffset::*PPLoc>
4147struct PPEntityComp {
4148 const ASTReader &Reader;
4149 ModuleFile &M;
4150
4151 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4152
4153 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4154 SourceLocation LHS = getLoc(L);
4155 SourceLocation RHS = getLoc(R);
4156 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4157 }
4158
4159 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4160 SourceLocation LHS = getLoc(L);
4161 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4162 }
4163
4164 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4165 SourceLocation RHS = getLoc(R);
4166 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4167 }
4168
4169 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4170 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4171 }
4172};
4173
4174}
4175
4176/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4177PreprocessedEntityID
4178ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4179 if (SourceMgr.isLocalSourceLocation(BLoc))
4180 return getTotalNumPreprocessedEntities();
4181
4182 GlobalSLocOffsetMapType::const_iterator
4183 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004184 BLoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004185 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4186 "Corrupted global sloc offset map");
4187
4188 if (SLocMapI->second->NumPreprocessedEntities == 0)
4189 return findNextPreprocessedEntity(SLocMapI);
4190
4191 ModuleFile &M = *SLocMapI->second;
4192 typedef const PPEntityOffset *pp_iterator;
4193 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4194 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4195
4196 size_t Count = M.NumPreprocessedEntities;
4197 size_t Half;
4198 pp_iterator First = pp_begin;
4199 pp_iterator PPI;
4200
4201 // Do a binary search manually instead of using std::lower_bound because
4202 // The end locations of entities may be unordered (when a macro expansion
4203 // is inside another macro argument), but for this case it is not important
4204 // whether we get the first macro expansion or its containing macro.
4205 while (Count > 0) {
4206 Half = Count/2;
4207 PPI = First;
4208 std::advance(PPI, Half);
4209 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4210 BLoc)){
4211 First = PPI;
4212 ++First;
4213 Count = Count - Half - 1;
4214 } else
4215 Count = Half;
4216 }
4217
4218 if (PPI == pp_end)
4219 return findNextPreprocessedEntity(SLocMapI);
4220
4221 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4222}
4223
4224/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4225PreprocessedEntityID
4226ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4227 if (SourceMgr.isLocalSourceLocation(ELoc))
4228 return getTotalNumPreprocessedEntities();
4229
4230 GlobalSLocOffsetMapType::const_iterator
4231 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004232 ELoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004233 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4234 "Corrupted global sloc offset map");
4235
4236 if (SLocMapI->second->NumPreprocessedEntities == 0)
4237 return findNextPreprocessedEntity(SLocMapI);
4238
4239 ModuleFile &M = *SLocMapI->second;
4240 typedef const PPEntityOffset *pp_iterator;
4241 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4242 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4243 pp_iterator PPI =
4244 std::upper_bound(pp_begin, pp_end, ELoc,
4245 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4246
4247 if (PPI == pp_end)
4248 return findNextPreprocessedEntity(SLocMapI);
4249
4250 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4251}
4252
4253/// \brief Returns a pair of [Begin, End) indices of preallocated
4254/// preprocessed entities that \arg Range encompasses.
4255std::pair<unsigned, unsigned>
4256 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4257 if (Range.isInvalid())
4258 return std::make_pair(0,0);
4259 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4260
4261 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4262 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4263 return std::make_pair(BeginID, EndID);
4264}
4265
4266/// \brief Optionally returns true or false if the preallocated preprocessed
4267/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00004268Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004269 FileID FID) {
4270 if (FID.isInvalid())
4271 return false;
4272
4273 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4274 ModuleFile &M = *PPInfo.first;
4275 unsigned LocalIndex = PPInfo.second;
4276 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4277
4278 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4279 if (Loc.isInvalid())
4280 return false;
4281
4282 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4283 return true;
4284 else
4285 return false;
4286}
4287
4288namespace {
4289 /// \brief Visitor used to search for information about a header file.
4290 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004291 const FileEntry *FE;
4292
David Blaikiedc84cd52013-02-20 22:23:23 +00004293 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004294
4295 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004296 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4297 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004298
4299 static bool visit(ModuleFile &M, void *UserData) {
4300 HeaderFileInfoVisitor *This
4301 = static_cast<HeaderFileInfoVisitor *>(UserData);
4302
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004303 HeaderFileInfoLookupTable *Table
4304 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4305 if (!Table)
4306 return false;
4307
4308 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00004309 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004310 if (Pos == Table->end())
4311 return false;
4312
4313 This->HFI = *Pos;
4314 return true;
4315 }
4316
David Blaikiedc84cd52013-02-20 22:23:23 +00004317 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004318 };
4319}
4320
4321HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004322 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004323 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
David Blaikiedc84cd52013-02-20 22:23:23 +00004324 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004325 if (Listener)
4326 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4327 return *HFI;
4328 }
4329
4330 return HeaderFileInfo();
4331}
4332
4333void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4334 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004335 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004336 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4337 ModuleFile &F = *(*I);
4338 unsigned Idx = 0;
4339 DiagStates.clear();
4340 assert(!Diag.DiagStates.empty());
4341 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4342 while (Idx < F.PragmaDiagMappings.size()) {
4343 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4344 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4345 if (DiagStateID != 0) {
4346 Diag.DiagStatePoints.push_back(
4347 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4348 FullSourceLoc(Loc, SourceMgr)));
4349 continue;
4350 }
4351
4352 assert(DiagStateID == 0);
4353 // A new DiagState was created here.
4354 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4355 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4356 DiagStates.push_back(NewState);
4357 Diag.DiagStatePoints.push_back(
4358 DiagnosticsEngine::DiagStatePoint(NewState,
4359 FullSourceLoc(Loc, SourceMgr)));
4360 while (1) {
4361 assert(Idx < F.PragmaDiagMappings.size() &&
4362 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4363 if (Idx >= F.PragmaDiagMappings.size()) {
4364 break; // Something is messed up but at least avoid infinite loop in
4365 // release build.
4366 }
4367 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4368 if (DiagID == (unsigned)-1) {
4369 break; // no more diag/map pairs for this location.
4370 }
4371 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4372 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4373 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4374 }
4375 }
4376 }
4377}
4378
4379/// \brief Get the correct cursor and offset for loading a type.
4380ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4381 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4382 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4383 ModuleFile *M = I->second;
4384 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4385}
4386
4387/// \brief Read and return the type with the given index..
4388///
4389/// The index is the type ID, shifted and minus the number of predefs. This
4390/// routine actually reads the record corresponding to the type at the given
4391/// location. It is a helper routine for GetType, which deals with reading type
4392/// IDs.
4393QualType ASTReader::readTypeRecord(unsigned Index) {
4394 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004395 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004396
4397 // Keep track of where we are in the stream, then jump back there
4398 // after reading this type.
4399 SavedStreamPosition SavedPosition(DeclsCursor);
4400
4401 ReadingKindTracker ReadingKind(Read_Type, *this);
4402
4403 // Note that we are loading a type record.
4404 Deserializing AType(this);
4405
4406 unsigned Idx = 0;
4407 DeclsCursor.JumpToBit(Loc.Offset);
4408 RecordData Record;
4409 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004410 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004411 case TYPE_EXT_QUAL: {
4412 if (Record.size() != 2) {
4413 Error("Incorrect encoding of extended qualifier type");
4414 return QualType();
4415 }
4416 QualType Base = readType(*Loc.F, Record, Idx);
4417 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4418 return Context.getQualifiedType(Base, Quals);
4419 }
4420
4421 case TYPE_COMPLEX: {
4422 if (Record.size() != 1) {
4423 Error("Incorrect encoding of complex type");
4424 return QualType();
4425 }
4426 QualType ElemType = readType(*Loc.F, Record, Idx);
4427 return Context.getComplexType(ElemType);
4428 }
4429
4430 case TYPE_POINTER: {
4431 if (Record.size() != 1) {
4432 Error("Incorrect encoding of pointer type");
4433 return QualType();
4434 }
4435 QualType PointeeType = readType(*Loc.F, Record, Idx);
4436 return Context.getPointerType(PointeeType);
4437 }
4438
4439 case TYPE_BLOCK_POINTER: {
4440 if (Record.size() != 1) {
4441 Error("Incorrect encoding of block pointer type");
4442 return QualType();
4443 }
4444 QualType PointeeType = readType(*Loc.F, Record, Idx);
4445 return Context.getBlockPointerType(PointeeType);
4446 }
4447
4448 case TYPE_LVALUE_REFERENCE: {
4449 if (Record.size() != 2) {
4450 Error("Incorrect encoding of lvalue reference type");
4451 return QualType();
4452 }
4453 QualType PointeeType = readType(*Loc.F, Record, Idx);
4454 return Context.getLValueReferenceType(PointeeType, Record[1]);
4455 }
4456
4457 case TYPE_RVALUE_REFERENCE: {
4458 if (Record.size() != 1) {
4459 Error("Incorrect encoding of rvalue reference type");
4460 return QualType();
4461 }
4462 QualType PointeeType = readType(*Loc.F, Record, Idx);
4463 return Context.getRValueReferenceType(PointeeType);
4464 }
4465
4466 case TYPE_MEMBER_POINTER: {
4467 if (Record.size() != 2) {
4468 Error("Incorrect encoding of member pointer type");
4469 return QualType();
4470 }
4471 QualType PointeeType = readType(*Loc.F, Record, Idx);
4472 QualType ClassType = readType(*Loc.F, Record, Idx);
4473 if (PointeeType.isNull() || ClassType.isNull())
4474 return QualType();
4475
4476 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4477 }
4478
4479 case TYPE_CONSTANT_ARRAY: {
4480 QualType ElementType = readType(*Loc.F, Record, Idx);
4481 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4482 unsigned IndexTypeQuals = Record[2];
4483 unsigned Idx = 3;
4484 llvm::APInt Size = ReadAPInt(Record, Idx);
4485 return Context.getConstantArrayType(ElementType, Size,
4486 ASM, IndexTypeQuals);
4487 }
4488
4489 case TYPE_INCOMPLETE_ARRAY: {
4490 QualType ElementType = readType(*Loc.F, Record, Idx);
4491 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4492 unsigned IndexTypeQuals = Record[2];
4493 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4494 }
4495
4496 case TYPE_VARIABLE_ARRAY: {
4497 QualType ElementType = readType(*Loc.F, Record, Idx);
4498 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4499 unsigned IndexTypeQuals = Record[2];
4500 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4501 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4502 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4503 ASM, IndexTypeQuals,
4504 SourceRange(LBLoc, RBLoc));
4505 }
4506
4507 case TYPE_VECTOR: {
4508 if (Record.size() != 3) {
4509 Error("incorrect encoding of vector type in AST file");
4510 return QualType();
4511 }
4512
4513 QualType ElementType = readType(*Loc.F, Record, Idx);
4514 unsigned NumElements = Record[1];
4515 unsigned VecKind = Record[2];
4516 return Context.getVectorType(ElementType, NumElements,
4517 (VectorType::VectorKind)VecKind);
4518 }
4519
4520 case TYPE_EXT_VECTOR: {
4521 if (Record.size() != 3) {
4522 Error("incorrect encoding of extended vector type in AST file");
4523 return QualType();
4524 }
4525
4526 QualType ElementType = readType(*Loc.F, Record, Idx);
4527 unsigned NumElements = Record[1];
4528 return Context.getExtVectorType(ElementType, NumElements);
4529 }
4530
4531 case TYPE_FUNCTION_NO_PROTO: {
4532 if (Record.size() != 6) {
4533 Error("incorrect encoding of no-proto function type");
4534 return QualType();
4535 }
4536 QualType ResultType = readType(*Loc.F, Record, Idx);
4537 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4538 (CallingConv)Record[4], Record[5]);
4539 return Context.getFunctionNoProtoType(ResultType, Info);
4540 }
4541
4542 case TYPE_FUNCTION_PROTO: {
4543 QualType ResultType = readType(*Loc.F, Record, Idx);
4544
4545 FunctionProtoType::ExtProtoInfo EPI;
4546 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4547 /*hasregparm*/ Record[2],
4548 /*regparm*/ Record[3],
4549 static_cast<CallingConv>(Record[4]),
4550 /*produces*/ Record[5]);
4551
4552 unsigned Idx = 6;
4553 unsigned NumParams = Record[Idx++];
4554 SmallVector<QualType, 16> ParamTypes;
4555 for (unsigned I = 0; I != NumParams; ++I)
4556 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4557
4558 EPI.Variadic = Record[Idx++];
4559 EPI.HasTrailingReturn = Record[Idx++];
4560 EPI.TypeQuals = Record[Idx++];
4561 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4562 ExceptionSpecificationType EST =
4563 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4564 EPI.ExceptionSpecType = EST;
4565 SmallVector<QualType, 2> Exceptions;
4566 if (EST == EST_Dynamic) {
4567 EPI.NumExceptions = Record[Idx++];
4568 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4569 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4570 EPI.Exceptions = Exceptions.data();
4571 } else if (EST == EST_ComputedNoexcept) {
4572 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4573 } else if (EST == EST_Uninstantiated) {
4574 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4575 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4576 } else if (EST == EST_Unevaluated) {
4577 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4578 }
Jordan Rosebea522f2013-03-08 21:51:21 +00004579 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004580 }
4581
4582 case TYPE_UNRESOLVED_USING: {
4583 unsigned Idx = 0;
4584 return Context.getTypeDeclType(
4585 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4586 }
4587
4588 case TYPE_TYPEDEF: {
4589 if (Record.size() != 2) {
4590 Error("incorrect encoding of typedef type");
4591 return QualType();
4592 }
4593 unsigned Idx = 0;
4594 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4595 QualType Canonical = readType(*Loc.F, Record, Idx);
4596 if (!Canonical.isNull())
4597 Canonical = Context.getCanonicalType(Canonical);
4598 return Context.getTypedefType(Decl, Canonical);
4599 }
4600
4601 case TYPE_TYPEOF_EXPR:
4602 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4603
4604 case TYPE_TYPEOF: {
4605 if (Record.size() != 1) {
4606 Error("incorrect encoding of typeof(type) in AST file");
4607 return QualType();
4608 }
4609 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4610 return Context.getTypeOfType(UnderlyingType);
4611 }
4612
4613 case TYPE_DECLTYPE: {
4614 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4615 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4616 }
4617
4618 case TYPE_UNARY_TRANSFORM: {
4619 QualType BaseType = readType(*Loc.F, Record, Idx);
4620 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4621 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4622 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4623 }
4624
4625 case TYPE_AUTO:
4626 return Context.getAutoType(readType(*Loc.F, Record, Idx));
4627
4628 case TYPE_RECORD: {
4629 if (Record.size() != 2) {
4630 Error("incorrect encoding of record type");
4631 return QualType();
4632 }
4633 unsigned Idx = 0;
4634 bool IsDependent = Record[Idx++];
4635 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4636 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4637 QualType T = Context.getRecordType(RD);
4638 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4639 return T;
4640 }
4641
4642 case TYPE_ENUM: {
4643 if (Record.size() != 2) {
4644 Error("incorrect encoding of enum type");
4645 return QualType();
4646 }
4647 unsigned Idx = 0;
4648 bool IsDependent = Record[Idx++];
4649 QualType T
4650 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4651 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4652 return T;
4653 }
4654
4655 case TYPE_ATTRIBUTED: {
4656 if (Record.size() != 3) {
4657 Error("incorrect encoding of attributed type");
4658 return QualType();
4659 }
4660 QualType modifiedType = readType(*Loc.F, Record, Idx);
4661 QualType equivalentType = readType(*Loc.F, Record, Idx);
4662 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4663 return Context.getAttributedType(kind, modifiedType, equivalentType);
4664 }
4665
4666 case TYPE_PAREN: {
4667 if (Record.size() != 1) {
4668 Error("incorrect encoding of paren type");
4669 return QualType();
4670 }
4671 QualType InnerType = readType(*Loc.F, Record, Idx);
4672 return Context.getParenType(InnerType);
4673 }
4674
4675 case TYPE_PACK_EXPANSION: {
4676 if (Record.size() != 2) {
4677 Error("incorrect encoding of pack expansion type");
4678 return QualType();
4679 }
4680 QualType Pattern = readType(*Loc.F, Record, Idx);
4681 if (Pattern.isNull())
4682 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00004683 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004684 if (Record[1])
4685 NumExpansions = Record[1] - 1;
4686 return Context.getPackExpansionType(Pattern, NumExpansions);
4687 }
4688
4689 case TYPE_ELABORATED: {
4690 unsigned Idx = 0;
4691 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4692 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4693 QualType NamedType = readType(*Loc.F, Record, Idx);
4694 return Context.getElaboratedType(Keyword, NNS, NamedType);
4695 }
4696
4697 case TYPE_OBJC_INTERFACE: {
4698 unsigned Idx = 0;
4699 ObjCInterfaceDecl *ItfD
4700 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4701 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4702 }
4703
4704 case TYPE_OBJC_OBJECT: {
4705 unsigned Idx = 0;
4706 QualType Base = readType(*Loc.F, Record, Idx);
4707 unsigned NumProtos = Record[Idx++];
4708 SmallVector<ObjCProtocolDecl*, 4> Protos;
4709 for (unsigned I = 0; I != NumProtos; ++I)
4710 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4711 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4712 }
4713
4714 case TYPE_OBJC_OBJECT_POINTER: {
4715 unsigned Idx = 0;
4716 QualType Pointee = readType(*Loc.F, Record, Idx);
4717 return Context.getObjCObjectPointerType(Pointee);
4718 }
4719
4720 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4721 unsigned Idx = 0;
4722 QualType Parm = readType(*Loc.F, Record, Idx);
4723 QualType Replacement = readType(*Loc.F, Record, Idx);
4724 return
4725 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4726 Replacement);
4727 }
4728
4729 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4730 unsigned Idx = 0;
4731 QualType Parm = readType(*Loc.F, Record, Idx);
4732 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4733 return Context.getSubstTemplateTypeParmPackType(
4734 cast<TemplateTypeParmType>(Parm),
4735 ArgPack);
4736 }
4737
4738 case TYPE_INJECTED_CLASS_NAME: {
4739 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4740 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4741 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4742 // for AST reading, too much interdependencies.
4743 return
4744 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4745 }
4746
4747 case TYPE_TEMPLATE_TYPE_PARM: {
4748 unsigned Idx = 0;
4749 unsigned Depth = Record[Idx++];
4750 unsigned Index = Record[Idx++];
4751 bool Pack = Record[Idx++];
4752 TemplateTypeParmDecl *D
4753 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4754 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4755 }
4756
4757 case TYPE_DEPENDENT_NAME: {
4758 unsigned Idx = 0;
4759 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4760 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4761 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4762 QualType Canon = readType(*Loc.F, Record, Idx);
4763 if (!Canon.isNull())
4764 Canon = Context.getCanonicalType(Canon);
4765 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4766 }
4767
4768 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4769 unsigned Idx = 0;
4770 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4771 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4772 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4773 unsigned NumArgs = Record[Idx++];
4774 SmallVector<TemplateArgument, 8> Args;
4775 Args.reserve(NumArgs);
4776 while (NumArgs--)
4777 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4778 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4779 Args.size(), Args.data());
4780 }
4781
4782 case TYPE_DEPENDENT_SIZED_ARRAY: {
4783 unsigned Idx = 0;
4784
4785 // ArrayType
4786 QualType ElementType = readType(*Loc.F, Record, Idx);
4787 ArrayType::ArraySizeModifier ASM
4788 = (ArrayType::ArraySizeModifier)Record[Idx++];
4789 unsigned IndexTypeQuals = Record[Idx++];
4790
4791 // DependentSizedArrayType
4792 Expr *NumElts = ReadExpr(*Loc.F);
4793 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4794
4795 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4796 IndexTypeQuals, Brackets);
4797 }
4798
4799 case TYPE_TEMPLATE_SPECIALIZATION: {
4800 unsigned Idx = 0;
4801 bool IsDependent = Record[Idx++];
4802 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4803 SmallVector<TemplateArgument, 8> Args;
4804 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4805 QualType Underlying = readType(*Loc.F, Record, Idx);
4806 QualType T;
4807 if (Underlying.isNull())
4808 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4809 Args.size());
4810 else
4811 T = Context.getTemplateSpecializationType(Name, Args.data(),
4812 Args.size(), Underlying);
4813 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4814 return T;
4815 }
4816
4817 case TYPE_ATOMIC: {
4818 if (Record.size() != 1) {
4819 Error("Incorrect encoding of atomic type");
4820 return QualType();
4821 }
4822 QualType ValueType = readType(*Loc.F, Record, Idx);
4823 return Context.getAtomicType(ValueType);
4824 }
4825 }
4826 llvm_unreachable("Invalid TypeCode!");
4827}
4828
4829class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4830 ASTReader &Reader;
4831 ModuleFile &F;
4832 const ASTReader::RecordData &Record;
4833 unsigned &Idx;
4834
4835 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4836 unsigned &I) {
4837 return Reader.ReadSourceLocation(F, R, I);
4838 }
4839
4840 template<typename T>
4841 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4842 return Reader.ReadDeclAs<T>(F, Record, Idx);
4843 }
4844
4845public:
4846 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4847 const ASTReader::RecordData &Record, unsigned &Idx)
4848 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4849 { }
4850
4851 // We want compile-time assurance that we've enumerated all of
4852 // these, so unfortunately we have to declare them first, then
4853 // define them out-of-line.
4854#define ABSTRACT_TYPELOC(CLASS, PARENT)
4855#define TYPELOC(CLASS, PARENT) \
4856 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4857#include "clang/AST/TypeLocNodes.def"
4858
4859 void VisitFunctionTypeLoc(FunctionTypeLoc);
4860 void VisitArrayTypeLoc(ArrayTypeLoc);
4861};
4862
4863void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4864 // nothing to do
4865}
4866void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4867 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4868 if (TL.needsExtraLocalData()) {
4869 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4870 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4871 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4872 TL.setModeAttr(Record[Idx++]);
4873 }
4874}
4875void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4876 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4877}
4878void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4879 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4880}
4881void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4882 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4883}
4884void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4885 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4886}
4887void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4888 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4889}
4890void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4891 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4892 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4893}
4894void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4895 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4896 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4897 if (Record[Idx++])
4898 TL.setSizeExpr(Reader.ReadExpr(F));
4899 else
4900 TL.setSizeExpr(0);
4901}
4902void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4903 VisitArrayTypeLoc(TL);
4904}
4905void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4906 VisitArrayTypeLoc(TL);
4907}
4908void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4909 VisitArrayTypeLoc(TL);
4910}
4911void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4912 DependentSizedArrayTypeLoc TL) {
4913 VisitArrayTypeLoc(TL);
4914}
4915void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4916 DependentSizedExtVectorTypeLoc TL) {
4917 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4918}
4919void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4920 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4921}
4922void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4923 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4924}
4925void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4926 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4927 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4928 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4929 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4930 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4931 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4932 }
4933}
4934void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4935 VisitFunctionTypeLoc(TL);
4936}
4937void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4938 VisitFunctionTypeLoc(TL);
4939}
4940void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4941 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4942}
4943void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4944 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4945}
4946void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4947 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4948 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4949 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4950}
4951void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4952 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4953 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4954 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4955 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4956}
4957void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4958 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4959}
4960void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4961 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4962 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4963 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4964 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4965}
4966void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4967 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4968}
4969void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4970 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4971}
4972void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4973 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4974}
4975void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4976 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4977 if (TL.hasAttrOperand()) {
4978 SourceRange range;
4979 range.setBegin(ReadSourceLocation(Record, Idx));
4980 range.setEnd(ReadSourceLocation(Record, Idx));
4981 TL.setAttrOperandParensRange(range);
4982 }
4983 if (TL.hasAttrExprOperand()) {
4984 if (Record[Idx++])
4985 TL.setAttrExprOperand(Reader.ReadExpr(F));
4986 else
4987 TL.setAttrExprOperand(0);
4988 } else if (TL.hasAttrEnumOperand())
4989 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4990}
4991void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4992 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4993}
4994void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4995 SubstTemplateTypeParmTypeLoc TL) {
4996 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4997}
4998void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4999 SubstTemplateTypeParmPackTypeLoc TL) {
5000 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5001}
5002void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5003 TemplateSpecializationTypeLoc TL) {
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::VisitParenTypeLoc(ParenTypeLoc TL) {
5015 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5016 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5017}
5018void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5019 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5020 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5021}
5022void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5023 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5024}
5025void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5026 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5027 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5028 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5029}
5030void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5031 DependentTemplateSpecializationTypeLoc TL) {
5032 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5033 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5034 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5035 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5036 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5037 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5038 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5039 TL.setArgLocInfo(I,
5040 Reader.GetTemplateArgumentLocInfo(F,
5041 TL.getTypePtr()->getArg(I).getKind(),
5042 Record, Idx));
5043}
5044void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5045 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5046}
5047void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5048 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5049}
5050void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5051 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5052 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5053 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5054 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5055 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5056}
5057void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5058 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5059}
5060void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5061 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5062 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5063 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5064}
5065
5066TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5067 const RecordData &Record,
5068 unsigned &Idx) {
5069 QualType InfoTy = readType(F, Record, Idx);
5070 if (InfoTy.isNull())
5071 return 0;
5072
5073 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5074 TypeLocReader TLR(*this, F, Record, Idx);
5075 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5076 TLR.Visit(TL);
5077 return TInfo;
5078}
5079
5080QualType ASTReader::GetType(TypeID ID) {
5081 unsigned FastQuals = ID & Qualifiers::FastMask;
5082 unsigned Index = ID >> Qualifiers::FastWidth;
5083
5084 if (Index < NUM_PREDEF_TYPE_IDS) {
5085 QualType T;
5086 switch ((PredefinedTypeIDs)Index) {
5087 case PREDEF_TYPE_NULL_ID: return QualType();
5088 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5089 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5090
5091 case PREDEF_TYPE_CHAR_U_ID:
5092 case PREDEF_TYPE_CHAR_S_ID:
5093 // FIXME: Check that the signedness of CharTy is correct!
5094 T = Context.CharTy;
5095 break;
5096
5097 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5098 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5099 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5100 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5101 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5102 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5103 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5104 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5105 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5106 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5107 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5108 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5109 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5110 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5111 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5112 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5113 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5114 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5115 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5116 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5117 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5118 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5119 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5120 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5121 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5122 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5123 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5124 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005125 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5126 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5127 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5128 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5129 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5130 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005131 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005132 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005133 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5134
5135 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5136 T = Context.getAutoRRefDeductType();
5137 break;
5138
5139 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5140 T = Context.ARCUnbridgedCastTy;
5141 break;
5142
5143 case PREDEF_TYPE_VA_LIST_TAG:
5144 T = Context.getVaListTagType();
5145 break;
5146
5147 case PREDEF_TYPE_BUILTIN_FN:
5148 T = Context.BuiltinFnTy;
5149 break;
5150 }
5151
5152 assert(!T.isNull() && "Unknown predefined type");
5153 return T.withFastQualifiers(FastQuals);
5154 }
5155
5156 Index -= NUM_PREDEF_TYPE_IDS;
5157 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5158 if (TypesLoaded[Index].isNull()) {
5159 TypesLoaded[Index] = readTypeRecord(Index);
5160 if (TypesLoaded[Index].isNull())
5161 return QualType();
5162
5163 TypesLoaded[Index]->setFromAST();
5164 if (DeserializationListener)
5165 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5166 TypesLoaded[Index]);
5167 }
5168
5169 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5170}
5171
5172QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5173 return GetType(getGlobalTypeID(F, LocalID));
5174}
5175
5176serialization::TypeID
5177ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5178 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5179 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5180
5181 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5182 return LocalID;
5183
5184 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5185 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5186 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5187
5188 unsigned GlobalIndex = LocalIndex + I->second;
5189 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5190}
5191
5192TemplateArgumentLocInfo
5193ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5194 TemplateArgument::ArgKind Kind,
5195 const RecordData &Record,
5196 unsigned &Index) {
5197 switch (Kind) {
5198 case TemplateArgument::Expression:
5199 return ReadExpr(F);
5200 case TemplateArgument::Type:
5201 return GetTypeSourceInfo(F, Record, Index);
5202 case TemplateArgument::Template: {
5203 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5204 Index);
5205 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5206 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5207 SourceLocation());
5208 }
5209 case TemplateArgument::TemplateExpansion: {
5210 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5211 Index);
5212 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5213 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5214 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5215 EllipsisLoc);
5216 }
5217 case TemplateArgument::Null:
5218 case TemplateArgument::Integral:
5219 case TemplateArgument::Declaration:
5220 case TemplateArgument::NullPtr:
5221 case TemplateArgument::Pack:
5222 // FIXME: Is this right?
5223 return TemplateArgumentLocInfo();
5224 }
5225 llvm_unreachable("unexpected template argument loc");
5226}
5227
5228TemplateArgumentLoc
5229ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5230 const RecordData &Record, unsigned &Index) {
5231 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5232
5233 if (Arg.getKind() == TemplateArgument::Expression) {
5234 if (Record[Index++]) // bool InfoHasSameExpr.
5235 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5236 }
5237 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5238 Record, Index));
5239}
5240
5241Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5242 return GetDecl(ID);
5243}
5244
5245uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5246 unsigned &Idx){
5247 if (Idx >= Record.size())
5248 return 0;
5249
5250 unsigned LocalID = Record[Idx++];
5251 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5252}
5253
5254CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5255 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005256 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005257 SavedStreamPosition SavedPosition(Cursor);
5258 Cursor.JumpToBit(Loc.Offset);
5259 ReadingKindTracker ReadingKind(Read_Decl, *this);
5260 RecordData Record;
5261 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005262 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005263 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5264 Error("Malformed AST file: missing C++ base specifiers");
5265 return 0;
5266 }
5267
5268 unsigned Idx = 0;
5269 unsigned NumBases = Record[Idx++];
5270 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5271 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5272 for (unsigned I = 0; I != NumBases; ++I)
5273 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5274 return Bases;
5275}
5276
5277serialization::DeclID
5278ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5279 if (LocalID < NUM_PREDEF_DECL_IDS)
5280 return LocalID;
5281
5282 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5283 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5284 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5285
5286 return LocalID + I->second;
5287}
5288
5289bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5290 ModuleFile &M) const {
5291 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5292 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5293 return &M == I->second;
5294}
5295
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005296ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005297 if (!D->isFromASTFile())
5298 return 0;
5299 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5300 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5301 return I->second;
5302}
5303
5304SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5305 if (ID < NUM_PREDEF_DECL_IDS)
5306 return SourceLocation();
5307
5308 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5309
5310 if (Index > DeclsLoaded.size()) {
5311 Error("declaration ID out-of-range for AST file");
5312 return SourceLocation();
5313 }
5314
5315 if (Decl *D = DeclsLoaded[Index])
5316 return D->getLocation();
5317
5318 unsigned RawLocation = 0;
5319 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5320 return ReadSourceLocation(*Rec.F, RawLocation);
5321}
5322
5323Decl *ASTReader::GetDecl(DeclID ID) {
5324 if (ID < NUM_PREDEF_DECL_IDS) {
5325 switch ((PredefinedDeclIDs)ID) {
5326 case PREDEF_DECL_NULL_ID:
5327 return 0;
5328
5329 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5330 return Context.getTranslationUnitDecl();
5331
5332 case PREDEF_DECL_OBJC_ID_ID:
5333 return Context.getObjCIdDecl();
5334
5335 case PREDEF_DECL_OBJC_SEL_ID:
5336 return Context.getObjCSelDecl();
5337
5338 case PREDEF_DECL_OBJC_CLASS_ID:
5339 return Context.getObjCClassDecl();
5340
5341 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5342 return Context.getObjCProtocolDecl();
5343
5344 case PREDEF_DECL_INT_128_ID:
5345 return Context.getInt128Decl();
5346
5347 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5348 return Context.getUInt128Decl();
5349
5350 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5351 return Context.getObjCInstanceTypeDecl();
5352
5353 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5354 return Context.getBuiltinVaListDecl();
5355 }
5356 }
5357
5358 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5359
5360 if (Index >= DeclsLoaded.size()) {
5361 assert(0 && "declaration ID out-of-range for AST file");
5362 Error("declaration ID out-of-range for AST file");
5363 return 0;
5364 }
5365
5366 if (!DeclsLoaded[Index]) {
5367 ReadDeclRecord(ID);
5368 if (DeserializationListener)
5369 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5370 }
5371
5372 return DeclsLoaded[Index];
5373}
5374
5375DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5376 DeclID GlobalID) {
5377 if (GlobalID < NUM_PREDEF_DECL_IDS)
5378 return GlobalID;
5379
5380 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5381 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5382 ModuleFile *Owner = I->second;
5383
5384 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5385 = M.GlobalToLocalDeclIDs.find(Owner);
5386 if (Pos == M.GlobalToLocalDeclIDs.end())
5387 return 0;
5388
5389 return GlobalID - Owner->BaseDeclID + Pos->second;
5390}
5391
5392serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5393 const RecordData &Record,
5394 unsigned &Idx) {
5395 if (Idx >= Record.size()) {
5396 Error("Corrupted AST file");
5397 return 0;
5398 }
5399
5400 return getGlobalDeclID(F, Record[Idx++]);
5401}
5402
5403/// \brief Resolve the offset of a statement into a statement.
5404///
5405/// This operation will read a new statement from the external
5406/// source each time it is called, and is meant to be used via a
5407/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5408Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5409 // Switch case IDs are per Decl.
5410 ClearSwitchCaseIDs();
5411
5412 // Offset here is a global offset across the entire chain.
5413 RecordLocation Loc = getLocalBitOffset(Offset);
5414 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5415 return ReadStmtFromStream(*Loc.F);
5416}
5417
5418namespace {
5419 class FindExternalLexicalDeclsVisitor {
5420 ASTReader &Reader;
5421 const DeclContext *DC;
5422 bool (*isKindWeWant)(Decl::Kind);
5423
5424 SmallVectorImpl<Decl*> &Decls;
5425 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5426
5427 public:
5428 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5429 bool (*isKindWeWant)(Decl::Kind),
5430 SmallVectorImpl<Decl*> &Decls)
5431 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5432 {
5433 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5434 PredefsVisited[I] = false;
5435 }
5436
5437 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5438 if (Preorder)
5439 return false;
5440
5441 FindExternalLexicalDeclsVisitor *This
5442 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5443
5444 ModuleFile::DeclContextInfosMap::iterator Info
5445 = M.DeclContextInfos.find(This->DC);
5446 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5447 return false;
5448
5449 // Load all of the declaration IDs
5450 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5451 *IDE = ID + Info->second.NumLexicalDecls;
5452 ID != IDE; ++ID) {
5453 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5454 continue;
5455
5456 // Don't add predefined declarations to the lexical context more
5457 // than once.
5458 if (ID->second < NUM_PREDEF_DECL_IDS) {
5459 if (This->PredefsVisited[ID->second])
5460 continue;
5461
5462 This->PredefsVisited[ID->second] = true;
5463 }
5464
5465 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5466 if (!This->DC->isDeclInLexicalTraversal(D))
5467 This->Decls.push_back(D);
5468 }
5469 }
5470
5471 return false;
5472 }
5473 };
5474}
5475
5476ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5477 bool (*isKindWeWant)(Decl::Kind),
5478 SmallVectorImpl<Decl*> &Decls) {
5479 // There might be lexical decls in multiple modules, for the TU at
5480 // least. Walk all of the modules in the order they were loaded.
5481 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5482 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5483 ++NumLexicalDeclContextsRead;
5484 return ELR_Success;
5485}
5486
5487namespace {
5488
5489class DeclIDComp {
5490 ASTReader &Reader;
5491 ModuleFile &Mod;
5492
5493public:
5494 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5495
5496 bool operator()(LocalDeclID L, LocalDeclID R) const {
5497 SourceLocation LHS = getLocation(L);
5498 SourceLocation RHS = getLocation(R);
5499 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5500 }
5501
5502 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5503 SourceLocation RHS = getLocation(R);
5504 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5505 }
5506
5507 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5508 SourceLocation LHS = getLocation(L);
5509 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5510 }
5511
5512 SourceLocation getLocation(LocalDeclID ID) const {
5513 return Reader.getSourceManager().getFileLoc(
5514 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5515 }
5516};
5517
5518}
5519
5520void ASTReader::FindFileRegionDecls(FileID File,
5521 unsigned Offset, unsigned Length,
5522 SmallVectorImpl<Decl *> &Decls) {
5523 SourceManager &SM = getSourceManager();
5524
5525 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5526 if (I == FileDeclIDs.end())
5527 return;
5528
5529 FileDeclsInfo &DInfo = I->second;
5530 if (DInfo.Decls.empty())
5531 return;
5532
5533 SourceLocation
5534 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5535 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5536
5537 DeclIDComp DIDComp(*this, *DInfo.Mod);
5538 ArrayRef<serialization::LocalDeclID>::iterator
5539 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5540 BeginLoc, DIDComp);
5541 if (BeginIt != DInfo.Decls.begin())
5542 --BeginIt;
5543
5544 // If we are pointing at a top-level decl inside an objc container, we need
5545 // to backtrack until we find it otherwise we will fail to report that the
5546 // region overlaps with an objc container.
5547 while (BeginIt != DInfo.Decls.begin() &&
5548 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5549 ->isTopLevelDeclInObjCContainer())
5550 --BeginIt;
5551
5552 ArrayRef<serialization::LocalDeclID>::iterator
5553 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5554 EndLoc, DIDComp);
5555 if (EndIt != DInfo.Decls.end())
5556 ++EndIt;
5557
5558 for (ArrayRef<serialization::LocalDeclID>::iterator
5559 DIt = BeginIt; DIt != EndIt; ++DIt)
5560 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5561}
5562
5563namespace {
5564 /// \brief ModuleFile visitor used to perform name lookup into a
5565 /// declaration context.
5566 class DeclContextNameLookupVisitor {
5567 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005568 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005569 DeclarationName Name;
5570 SmallVectorImpl<NamedDecl *> &Decls;
5571
5572 public:
5573 DeclContextNameLookupVisitor(ASTReader &Reader,
5574 SmallVectorImpl<const DeclContext *> &Contexts,
5575 DeclarationName Name,
5576 SmallVectorImpl<NamedDecl *> &Decls)
5577 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5578
5579 static bool visit(ModuleFile &M, void *UserData) {
5580 DeclContextNameLookupVisitor *This
5581 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5582
5583 // Check whether we have any visible declaration information for
5584 // this context in this module.
5585 ModuleFile::DeclContextInfosMap::iterator Info;
5586 bool FoundInfo = false;
5587 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5588 Info = M.DeclContextInfos.find(This->Contexts[I]);
5589 if (Info != M.DeclContextInfos.end() &&
5590 Info->second.NameLookupTableData) {
5591 FoundInfo = true;
5592 break;
5593 }
5594 }
5595
5596 if (!FoundInfo)
5597 return false;
5598
5599 // Look for this name within this module.
5600 ASTDeclContextNameLookupTable *LookupTable =
5601 Info->second.NameLookupTableData;
5602 ASTDeclContextNameLookupTable::iterator Pos
5603 = LookupTable->find(This->Name);
5604 if (Pos == LookupTable->end())
5605 return false;
5606
5607 bool FoundAnything = false;
5608 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5609 for (; Data.first != Data.second; ++Data.first) {
5610 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5611 if (!ND)
5612 continue;
5613
5614 if (ND->getDeclName() != This->Name) {
5615 // A name might be null because the decl's redeclarable part is
5616 // currently read before reading its name. The lookup is triggered by
5617 // building that decl (likely indirectly), and so it is later in the
5618 // sense of "already existing" and can be ignored here.
5619 continue;
5620 }
5621
5622 // Record this declaration.
5623 FoundAnything = true;
5624 This->Decls.push_back(ND);
5625 }
5626
5627 return FoundAnything;
5628 }
5629 };
5630}
5631
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005632/// \brief Retrieve the "definitive" module file for the definition of the
5633/// given declaration context, if there is one.
5634///
5635/// The "definitive" module file is the only place where we need to look to
5636/// find information about the declarations within the given declaration
5637/// context. For example, C++ and Objective-C classes, C structs/unions, and
5638/// Objective-C protocols, categories, and extensions are all defined in a
5639/// single place in the source code, so they have definitive module files
5640/// associated with them. C++ namespaces, on the other hand, can have
5641/// definitions in multiple different module files.
5642///
5643/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5644/// NDEBUG checking.
5645static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5646 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005647 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5648 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005649
5650 return 0;
5651}
5652
Richard Smith3646c682013-02-07 03:30:24 +00005653bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005654ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5655 DeclarationName Name) {
5656 assert(DC->hasExternalVisibleStorage() &&
5657 "DeclContext has no visible decls in storage");
5658 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00005659 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005660
5661 SmallVector<NamedDecl *, 64> Decls;
5662
5663 // Compute the declaration contexts we need to look into. Multiple such
5664 // declaration contexts occur when two declaration contexts from disjoint
5665 // modules get merged, e.g., when two namespaces with the same name are
5666 // independently defined in separate modules.
5667 SmallVector<const DeclContext *, 2> Contexts;
5668 Contexts.push_back(DC);
5669
5670 if (DC->isNamespace()) {
5671 MergedDeclsMap::iterator Merged
5672 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5673 if (Merged != MergedDecls.end()) {
5674 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5675 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5676 }
5677 }
5678
5679 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005680
5681 // If we can definitively determine which module file to look into,
5682 // only look there. Otherwise, look in all module files.
5683 ModuleFile *Definitive;
5684 if (Contexts.size() == 1 &&
5685 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5686 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5687 } else {
5688 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5689 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005690 ++NumVisibleDeclContextsRead;
5691 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00005692 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005693}
5694
5695namespace {
5696 /// \brief ModuleFile visitor used to retrieve all visible names in a
5697 /// declaration context.
5698 class DeclContextAllNamesVisitor {
5699 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005700 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005701 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005702 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005703
5704 public:
5705 DeclContextAllNamesVisitor(ASTReader &Reader,
5706 SmallVectorImpl<const DeclContext *> &Contexts,
5707 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005708 SmallVector<NamedDecl *, 8> > &Decls,
5709 bool VisitAll)
5710 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005711
5712 static bool visit(ModuleFile &M, void *UserData) {
5713 DeclContextAllNamesVisitor *This
5714 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5715
5716 // Check whether we have any visible declaration information for
5717 // this context in this module.
5718 ModuleFile::DeclContextInfosMap::iterator Info;
5719 bool FoundInfo = false;
5720 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5721 Info = M.DeclContextInfos.find(This->Contexts[I]);
5722 if (Info != M.DeclContextInfos.end() &&
5723 Info->second.NameLookupTableData) {
5724 FoundInfo = true;
5725 break;
5726 }
5727 }
5728
5729 if (!FoundInfo)
5730 return false;
5731
5732 ASTDeclContextNameLookupTable *LookupTable =
5733 Info->second.NameLookupTableData;
5734 bool FoundAnything = false;
5735 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005736 I = LookupTable->data_begin(), E = LookupTable->data_end();
5737 I != E;
5738 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005739 ASTDeclContextNameLookupTrait::data_type Data = *I;
5740 for (; Data.first != Data.second; ++Data.first) {
5741 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5742 *Data.first);
5743 if (!ND)
5744 continue;
5745
5746 // Record this declaration.
5747 FoundAnything = true;
5748 This->Decls[ND->getDeclName()].push_back(ND);
5749 }
5750 }
5751
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005752 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005753 }
5754 };
5755}
5756
5757void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5758 if (!DC->hasExternalVisibleStorage())
5759 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005760 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005761
5762 // Compute the declaration contexts we need to look into. Multiple such
5763 // declaration contexts occur when two declaration contexts from disjoint
5764 // modules get merged, e.g., when two namespaces with the same name are
5765 // independently defined in separate modules.
5766 SmallVector<const DeclContext *, 2> Contexts;
5767 Contexts.push_back(DC);
5768
5769 if (DC->isNamespace()) {
5770 MergedDeclsMap::iterator Merged
5771 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5772 if (Merged != MergedDecls.end()) {
5773 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5774 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5775 }
5776 }
5777
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005778 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5779 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005780 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5781 ++NumVisibleDeclContextsRead;
5782
5783 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005784 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005785 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5786 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5787 }
5788 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5789}
5790
5791/// \brief Under non-PCH compilation the consumer receives the objc methods
5792/// before receiving the implementation, and codegen depends on this.
5793/// We simulate this by deserializing and passing to consumer the methods of the
5794/// implementation before passing the deserialized implementation decl.
5795static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5796 ASTConsumer *Consumer) {
5797 assert(ImplD && Consumer);
5798
5799 for (ObjCImplDecl::method_iterator
5800 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5801 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5802
5803 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5804}
5805
5806void ASTReader::PassInterestingDeclsToConsumer() {
5807 assert(Consumer);
5808 while (!InterestingDecls.empty()) {
5809 Decl *D = InterestingDecls.front();
5810 InterestingDecls.pop_front();
5811
5812 PassInterestingDeclToConsumer(D);
5813 }
5814}
5815
5816void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5817 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5818 PassObjCImplDeclToConsumer(ImplD, Consumer);
5819 else
5820 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5821}
5822
5823void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5824 this->Consumer = Consumer;
5825
5826 if (!Consumer)
5827 return;
5828
5829 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5830 // Force deserialization of this decl, which will cause it to be queued for
5831 // passing to the consumer.
5832 GetDecl(ExternalDefinitions[I]);
5833 }
5834 ExternalDefinitions.clear();
5835
5836 PassInterestingDeclsToConsumer();
5837}
5838
5839void ASTReader::PrintStats() {
5840 std::fprintf(stderr, "*** AST File Statistics:\n");
5841
5842 unsigned NumTypesLoaded
5843 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5844 QualType());
5845 unsigned NumDeclsLoaded
5846 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5847 (Decl *)0);
5848 unsigned NumIdentifiersLoaded
5849 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5850 IdentifiersLoaded.end(),
5851 (IdentifierInfo *)0);
5852 unsigned NumMacrosLoaded
5853 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5854 MacrosLoaded.end(),
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005855 (MacroInfo *)0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005856 unsigned NumSelectorsLoaded
5857 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5858 SelectorsLoaded.end(),
5859 Selector());
5860
5861 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5862 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5863 NumSLocEntriesRead, TotalNumSLocEntries,
5864 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5865 if (!TypesLoaded.empty())
5866 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5867 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5868 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5869 if (!DeclsLoaded.empty())
5870 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5871 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5872 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5873 if (!IdentifiersLoaded.empty())
5874 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5875 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5876 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5877 if (!MacrosLoaded.empty())
5878 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5879 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5880 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5881 if (!SelectorsLoaded.empty())
5882 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5883 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5884 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5885 if (TotalNumStatements)
5886 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5887 NumStatementsRead, TotalNumStatements,
5888 ((float)NumStatementsRead/TotalNumStatements * 100));
5889 if (TotalNumMacros)
5890 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5891 NumMacrosRead, TotalNumMacros,
5892 ((float)NumMacrosRead/TotalNumMacros * 100));
5893 if (TotalLexicalDeclContexts)
5894 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5895 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5896 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5897 * 100));
5898 if (TotalVisibleDeclContexts)
5899 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5900 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5901 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5902 * 100));
5903 if (TotalNumMethodPoolEntries) {
5904 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5905 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5906 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5907 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005908 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00005909 if (NumMethodPoolLookups) {
5910 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5911 NumMethodPoolHits, NumMethodPoolLookups,
5912 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5913 }
5914 if (NumMethodPoolTableLookups) {
5915 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5916 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5917 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5918 * 100.0));
5919 }
5920
Douglas Gregore1698072013-01-25 00:38:33 +00005921 if (NumIdentifierLookupHits) {
5922 std::fprintf(stderr,
5923 " %u / %u identifier table lookups succeeded (%f%%)\n",
5924 NumIdentifierLookupHits, NumIdentifierLookups,
5925 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
5926 }
5927
Douglas Gregor1a49d972013-01-25 01:03:03 +00005928 if (GlobalIndex) {
5929 std::fprintf(stderr, "\n");
5930 GlobalIndex->printStats();
5931 }
5932
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005933 std::fprintf(stderr, "\n");
5934 dump();
5935 std::fprintf(stderr, "\n");
5936}
5937
5938template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5939static void
5940dumpModuleIDMap(StringRef Name,
5941 const ContinuousRangeMap<Key, ModuleFile *,
5942 InitialCapacity> &Map) {
5943 if (Map.begin() == Map.end())
5944 return;
5945
5946 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5947 llvm::errs() << Name << ":\n";
5948 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5949 I != IEnd; ++I) {
5950 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5951 << "\n";
5952 }
5953}
5954
5955void ASTReader::dump() {
5956 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5957 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5958 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5959 dumpModuleIDMap("Global type map", GlobalTypeMap);
5960 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5961 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5962 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5963 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5964 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5965 dumpModuleIDMap("Global preprocessed entity map",
5966 GlobalPreprocessedEntityMap);
5967
5968 llvm::errs() << "\n*** PCH/Modules Loaded:";
5969 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5970 MEnd = ModuleMgr.end();
5971 M != MEnd; ++M)
5972 (*M)->dump();
5973}
5974
5975/// Return the amount of memory used by memory buffers, breaking down
5976/// by heap-backed versus mmap'ed memory.
5977void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5978 for (ModuleConstIterator I = ModuleMgr.begin(),
5979 E = ModuleMgr.end(); I != E; ++I) {
5980 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5981 size_t bytes = buf->getBufferSize();
5982 switch (buf->getBufferKind()) {
5983 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5984 sizes.malloc_bytes += bytes;
5985 break;
5986 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5987 sizes.mmap_bytes += bytes;
5988 break;
5989 }
5990 }
5991 }
5992}
5993
5994void ASTReader::InitializeSema(Sema &S) {
5995 SemaObj = &S;
5996 S.addExternalSource(this);
5997
5998 // Makes sure any declarations that were deserialized "too early"
5999 // still get added to the identifier's declaration chains.
6000 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006001 NamedDecl *ND = cast<NamedDecl>(PreloadedDecls[I]->getMostRecentDecl());
6002 SemaObj->pushExternalDeclIntoScope(ND, PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006003 }
6004 PreloadedDecls.clear();
6005
6006 // Load the offsets of the declarations that Sema references.
6007 // They will be lazily deserialized when needed.
6008 if (!SemaDeclRefs.empty()) {
6009 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6010 if (!SemaObj->StdNamespace)
6011 SemaObj->StdNamespace = SemaDeclRefs[0];
6012 if (!SemaObj->StdBadAlloc)
6013 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6014 }
6015
6016 if (!FPPragmaOptions.empty()) {
6017 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6018 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6019 }
6020
6021 if (!OpenCLExtensions.empty()) {
6022 unsigned I = 0;
6023#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6024#include "clang/Basic/OpenCLExtensions.def"
6025
6026 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6027 }
6028}
6029
6030IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6031 // Note that we are loading an identifier.
6032 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006033 StringRef Name(NameStart, NameEnd - NameStart);
6034
6035 // If there is a global index, look there first to determine which modules
6036 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006037 GlobalModuleIndex::HitSet Hits;
6038 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006039 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006040 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6041 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006042 }
6043 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006044 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006045 NumIdentifierLookups,
6046 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006047 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006048 IdentifierInfo *II = Visitor.getIdentifierInfo();
6049 markIdentifierUpToDate(II);
6050 return II;
6051}
6052
6053namespace clang {
6054 /// \brief An identifier-lookup iterator that enumerates all of the
6055 /// identifiers stored within a set of AST files.
6056 class ASTIdentifierIterator : public IdentifierIterator {
6057 /// \brief The AST reader whose identifiers are being enumerated.
6058 const ASTReader &Reader;
6059
6060 /// \brief The current index into the chain of AST files stored in
6061 /// the AST reader.
6062 unsigned Index;
6063
6064 /// \brief The current position within the identifier lookup table
6065 /// of the current AST file.
6066 ASTIdentifierLookupTable::key_iterator Current;
6067
6068 /// \brief The end position within the identifier lookup table of
6069 /// the current AST file.
6070 ASTIdentifierLookupTable::key_iterator End;
6071
6072 public:
6073 explicit ASTIdentifierIterator(const ASTReader &Reader);
6074
6075 virtual StringRef Next();
6076 };
6077}
6078
6079ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6080 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6081 ASTIdentifierLookupTable *IdTable
6082 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6083 Current = IdTable->key_begin();
6084 End = IdTable->key_end();
6085}
6086
6087StringRef ASTIdentifierIterator::Next() {
6088 while (Current == End) {
6089 // If we have exhausted all of our AST files, we're done.
6090 if (Index == 0)
6091 return StringRef();
6092
6093 --Index;
6094 ASTIdentifierLookupTable *IdTable
6095 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6096 IdentifierLookupTable;
6097 Current = IdTable->key_begin();
6098 End = IdTable->key_end();
6099 }
6100
6101 // We have any identifiers remaining in the current AST file; return
6102 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006103 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006104 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006105 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006106}
6107
6108IdentifierIterator *ASTReader::getIdentifiers() const {
6109 return new ASTIdentifierIterator(*this);
6110}
6111
6112namespace clang { namespace serialization {
6113 class ReadMethodPoolVisitor {
6114 ASTReader &Reader;
6115 Selector Sel;
6116 unsigned PriorGeneration;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006117 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6118 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006119
6120 public:
6121 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6122 unsigned PriorGeneration)
6123 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
6124
6125 static bool visit(ModuleFile &M, void *UserData) {
6126 ReadMethodPoolVisitor *This
6127 = static_cast<ReadMethodPoolVisitor *>(UserData);
6128
6129 if (!M.SelectorLookupTable)
6130 return false;
6131
6132 // If we've already searched this module file, skip it now.
6133 if (M.Generation <= This->PriorGeneration)
6134 return true;
6135
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006136 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006137 ASTSelectorLookupTable *PoolTable
6138 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6139 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6140 if (Pos == PoolTable->end())
6141 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006142
6143 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006144 ++This->Reader.NumSelectorsRead;
6145 // FIXME: Not quite happy with the statistics here. We probably should
6146 // disable this tracking when called via LoadSelector.
6147 // Also, should entries without methods count as misses?
6148 ++This->Reader.NumMethodPoolEntriesRead;
6149 ASTSelectorLookupTrait::data_type Data = *Pos;
6150 if (This->Reader.DeserializationListener)
6151 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6152 This->Sel);
6153
6154 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6155 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6156 return true;
6157 }
6158
6159 /// \brief Retrieve the instance methods found by this visitor.
6160 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6161 return InstanceMethods;
6162 }
6163
6164 /// \brief Retrieve the instance methods found by this visitor.
6165 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6166 return FactoryMethods;
6167 }
6168 };
6169} } // end namespace clang::serialization
6170
6171/// \brief Add the given set of methods to the method list.
6172static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6173 ObjCMethodList &List) {
6174 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6175 S.addMethodToGlobalList(&List, Methods[I]);
6176 }
6177}
6178
6179void ASTReader::ReadMethodPool(Selector Sel) {
6180 // Get the selector generation and update it to the current generation.
6181 unsigned &Generation = SelectorGeneration[Sel];
6182 unsigned PriorGeneration = Generation;
6183 Generation = CurrentGeneration;
6184
6185 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006186 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006187 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6188 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6189
6190 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006191 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006192 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006193
6194 ++NumMethodPoolHits;
6195
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006196 if (!getSema())
6197 return;
6198
6199 Sema &S = *getSema();
6200 Sema::GlobalMethodPool::iterator Pos
6201 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6202
6203 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6204 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
6205}
6206
6207void ASTReader::ReadKnownNamespaces(
6208 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6209 Namespaces.clear();
6210
6211 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6212 if (NamespaceDecl *Namespace
6213 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6214 Namespaces.push_back(Namespace);
6215 }
6216}
6217
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006218void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00006219 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006220 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6221 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00006222 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006223 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00006224 Undefined.insert(std::make_pair(D, Loc));
6225 }
6226}
Nick Lewycky01a41142013-01-26 00:35:08 +00006227
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006228void ASTReader::ReadTentativeDefinitions(
6229 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6230 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6231 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6232 if (Var)
6233 TentativeDefs.push_back(Var);
6234 }
6235 TentativeDefinitions.clear();
6236}
6237
6238void ASTReader::ReadUnusedFileScopedDecls(
6239 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6240 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6241 DeclaratorDecl *D
6242 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6243 if (D)
6244 Decls.push_back(D);
6245 }
6246 UnusedFileScopedDecls.clear();
6247}
6248
6249void ASTReader::ReadDelegatingConstructors(
6250 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6251 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6252 CXXConstructorDecl *D
6253 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6254 if (D)
6255 Decls.push_back(D);
6256 }
6257 DelegatingCtorDecls.clear();
6258}
6259
6260void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6261 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6262 TypedefNameDecl *D
6263 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6264 if (D)
6265 Decls.push_back(D);
6266 }
6267 ExtVectorDecls.clear();
6268}
6269
6270void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6271 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6272 CXXRecordDecl *D
6273 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6274 if (D)
6275 Decls.push_back(D);
6276 }
6277 DynamicClasses.clear();
6278}
6279
6280void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006281ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6282 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6283 NamedDecl *D
6284 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006285 if (D)
6286 Decls.push_back(D);
6287 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006288 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006289}
6290
6291void ASTReader::ReadReferencedSelectors(
6292 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6293 if (ReferencedSelectorsData.empty())
6294 return;
6295
6296 // If there are @selector references added them to its pool. This is for
6297 // implementation of -Wselector.
6298 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6299 unsigned I = 0;
6300 while (I < DataSize) {
6301 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6302 SourceLocation SelLoc
6303 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6304 Sels.push_back(std::make_pair(Sel, SelLoc));
6305 }
6306 ReferencedSelectorsData.clear();
6307}
6308
6309void ASTReader::ReadWeakUndeclaredIdentifiers(
6310 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6311 if (WeakUndeclaredIdentifiers.empty())
6312 return;
6313
6314 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6315 IdentifierInfo *WeakId
6316 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6317 IdentifierInfo *AliasId
6318 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6319 SourceLocation Loc
6320 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6321 bool Used = WeakUndeclaredIdentifiers[I++];
6322 WeakInfo WI(AliasId, Loc);
6323 WI.setUsed(Used);
6324 WeakIDs.push_back(std::make_pair(WeakId, WI));
6325 }
6326 WeakUndeclaredIdentifiers.clear();
6327}
6328
6329void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6330 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6331 ExternalVTableUse VT;
6332 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6333 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6334 VT.DefinitionRequired = VTableUses[Idx++];
6335 VTables.push_back(VT);
6336 }
6337
6338 VTableUses.clear();
6339}
6340
6341void ASTReader::ReadPendingInstantiations(
6342 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6343 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6344 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6345 SourceLocation Loc
6346 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6347
6348 Pending.push_back(std::make_pair(D, Loc));
6349 }
6350 PendingInstantiations.clear();
6351}
6352
6353void ASTReader::LoadSelector(Selector Sel) {
6354 // It would be complicated to avoid reading the methods anyway. So don't.
6355 ReadMethodPool(Sel);
6356}
6357
6358void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6359 assert(ID && "Non-zero identifier ID required");
6360 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6361 IdentifiersLoaded[ID - 1] = II;
6362 if (DeserializationListener)
6363 DeserializationListener->IdentifierRead(ID, II);
6364}
6365
6366/// \brief Set the globally-visible declarations associated with the given
6367/// identifier.
6368///
6369/// If the AST reader is currently in a state where the given declaration IDs
6370/// cannot safely be resolved, they are queued until it is safe to resolve
6371/// them.
6372///
6373/// \param II an IdentifierInfo that refers to one or more globally-visible
6374/// declarations.
6375///
6376/// \param DeclIDs the set of declaration IDs with the name @p II that are
6377/// visible at global scope.
6378///
Douglas Gregoraa945902013-02-18 15:53:43 +00006379/// \param Decls if non-null, this vector will be populated with the set of
6380/// deserialized declarations. These declarations will not be pushed into
6381/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006382void
6383ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6384 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00006385 SmallVectorImpl<Decl *> *Decls) {
6386 if (NumCurrentElementsDeserializing && !Decls) {
6387 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006388 return;
6389 }
6390
6391 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6392 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6393 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006394 // If we're simply supposed to record the declarations, do so now.
6395 if (Decls) {
6396 Decls->push_back(D);
6397 continue;
6398 }
6399
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006400 // Introduce this declaration into the translation-unit scope
6401 // and add it to the declaration chain for this identifier, so
6402 // that (unqualified) name lookup will find it.
Douglas Gregoraa945902013-02-18 15:53:43 +00006403 NamedDecl *ND = cast<NamedDecl>(D->getMostRecentDecl());
6404 SemaObj->pushExternalDeclIntoScope(ND, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006405 } else {
6406 // Queue this declaration so that it will be added to the
6407 // translation unit scope and identifier's declaration chain
6408 // once a Sema object is known.
6409 PreloadedDecls.push_back(D);
6410 }
6411 }
6412}
6413
Douglas Gregor8222b892013-01-21 16:52:34 +00006414IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006415 if (ID == 0)
6416 return 0;
6417
6418 if (IdentifiersLoaded.empty()) {
6419 Error("no identifier table in AST file");
6420 return 0;
6421 }
6422
6423 ID -= 1;
6424 if (!IdentifiersLoaded[ID]) {
6425 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6426 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6427 ModuleFile *M = I->second;
6428 unsigned Index = ID - M->BaseIdentifierID;
6429 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6430
6431 // All of the strings in the AST file are preceded by a 16-bit length.
6432 // Extract that 16-bit length to avoid having to execute strlen().
6433 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6434 // unsigned integers. This is important to avoid integer overflow when
6435 // we cast them to 'unsigned'.
6436 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6437 unsigned StrLen = (((unsigned) StrLenPtr[0])
6438 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006439 IdentifiersLoaded[ID]
6440 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006441 if (DeserializationListener)
6442 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6443 }
6444
6445 return IdentifiersLoaded[ID];
6446}
6447
Douglas Gregor8222b892013-01-21 16:52:34 +00006448IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6449 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006450}
6451
6452IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6453 if (LocalID < NUM_PREDEF_IDENT_IDS)
6454 return LocalID;
6455
6456 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6457 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6458 assert(I != M.IdentifierRemap.end()
6459 && "Invalid index into identifier index remap");
6460
6461 return LocalID + I->second;
6462}
6463
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006464MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006465 if (ID == 0)
6466 return 0;
6467
6468 if (MacrosLoaded.empty()) {
6469 Error("no macro table in AST file");
6470 return 0;
6471 }
6472
6473 ID -= NUM_PREDEF_MACRO_IDS;
6474 if (!MacrosLoaded[ID]) {
6475 GlobalMacroMapType::iterator I
6476 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6477 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6478 ModuleFile *M = I->second;
6479 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006480 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6481
6482 if (DeserializationListener)
6483 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6484 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006485 }
6486
6487 return MacrosLoaded[ID];
6488}
6489
6490MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6491 if (LocalID < NUM_PREDEF_MACRO_IDS)
6492 return LocalID;
6493
6494 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6495 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6496 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6497
6498 return LocalID + I->second;
6499}
6500
6501serialization::SubmoduleID
6502ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6503 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6504 return LocalID;
6505
6506 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6507 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6508 assert(I != M.SubmoduleRemap.end()
6509 && "Invalid index into submodule index remap");
6510
6511 return LocalID + I->second;
6512}
6513
6514Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6515 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6516 assert(GlobalID == 0 && "Unhandled global submodule ID");
6517 return 0;
6518 }
6519
6520 if (GlobalID > SubmodulesLoaded.size()) {
6521 Error("submodule ID out of range in AST file");
6522 return 0;
6523 }
6524
6525 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6526}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006527
6528Module *ASTReader::getModule(unsigned ID) {
6529 return getSubmodule(ID);
6530}
6531
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006532Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6533 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6534}
6535
6536Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6537 if (ID == 0)
6538 return Selector();
6539
6540 if (ID > SelectorsLoaded.size()) {
6541 Error("selector ID out of range in AST file");
6542 return Selector();
6543 }
6544
6545 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6546 // Load this selector from the selector table.
6547 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6548 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6549 ModuleFile &M = *I->second;
6550 ASTSelectorLookupTrait Trait(*this, M);
6551 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6552 SelectorsLoaded[ID - 1] =
6553 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6554 if (DeserializationListener)
6555 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6556 }
6557
6558 return SelectorsLoaded[ID - 1];
6559}
6560
6561Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6562 return DecodeSelector(ID);
6563}
6564
6565uint32_t ASTReader::GetNumExternalSelectors() {
6566 // ID 0 (the null selector) is considered an external selector.
6567 return getTotalNumSelectors() + 1;
6568}
6569
6570serialization::SelectorID
6571ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6572 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6573 return LocalID;
6574
6575 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6576 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6577 assert(I != M.SelectorRemap.end()
6578 && "Invalid index into selector index remap");
6579
6580 return LocalID + I->second;
6581}
6582
6583DeclarationName
6584ASTReader::ReadDeclarationName(ModuleFile &F,
6585 const RecordData &Record, unsigned &Idx) {
6586 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6587 switch (Kind) {
6588 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006589 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006590
6591 case DeclarationName::ObjCZeroArgSelector:
6592 case DeclarationName::ObjCOneArgSelector:
6593 case DeclarationName::ObjCMultiArgSelector:
6594 return DeclarationName(ReadSelector(F, Record, Idx));
6595
6596 case DeclarationName::CXXConstructorName:
6597 return Context.DeclarationNames.getCXXConstructorName(
6598 Context.getCanonicalType(readType(F, Record, Idx)));
6599
6600 case DeclarationName::CXXDestructorName:
6601 return Context.DeclarationNames.getCXXDestructorName(
6602 Context.getCanonicalType(readType(F, Record, Idx)));
6603
6604 case DeclarationName::CXXConversionFunctionName:
6605 return Context.DeclarationNames.getCXXConversionFunctionName(
6606 Context.getCanonicalType(readType(F, Record, Idx)));
6607
6608 case DeclarationName::CXXOperatorName:
6609 return Context.DeclarationNames.getCXXOperatorName(
6610 (OverloadedOperatorKind)Record[Idx++]);
6611
6612 case DeclarationName::CXXLiteralOperatorName:
6613 return Context.DeclarationNames.getCXXLiteralOperatorName(
6614 GetIdentifierInfo(F, Record, Idx));
6615
6616 case DeclarationName::CXXUsingDirective:
6617 return DeclarationName::getUsingDirectiveName();
6618 }
6619
6620 llvm_unreachable("Invalid NameKind!");
6621}
6622
6623void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6624 DeclarationNameLoc &DNLoc,
6625 DeclarationName Name,
6626 const RecordData &Record, unsigned &Idx) {
6627 switch (Name.getNameKind()) {
6628 case DeclarationName::CXXConstructorName:
6629 case DeclarationName::CXXDestructorName:
6630 case DeclarationName::CXXConversionFunctionName:
6631 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6632 break;
6633
6634 case DeclarationName::CXXOperatorName:
6635 DNLoc.CXXOperatorName.BeginOpNameLoc
6636 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6637 DNLoc.CXXOperatorName.EndOpNameLoc
6638 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6639 break;
6640
6641 case DeclarationName::CXXLiteralOperatorName:
6642 DNLoc.CXXLiteralOperatorName.OpNameLoc
6643 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6644 break;
6645
6646 case DeclarationName::Identifier:
6647 case DeclarationName::ObjCZeroArgSelector:
6648 case DeclarationName::ObjCOneArgSelector:
6649 case DeclarationName::ObjCMultiArgSelector:
6650 case DeclarationName::CXXUsingDirective:
6651 break;
6652 }
6653}
6654
6655void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6656 DeclarationNameInfo &NameInfo,
6657 const RecordData &Record, unsigned &Idx) {
6658 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6659 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6660 DeclarationNameLoc DNLoc;
6661 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6662 NameInfo.setInfo(DNLoc);
6663}
6664
6665void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6666 const RecordData &Record, unsigned &Idx) {
6667 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6668 unsigned NumTPLists = Record[Idx++];
6669 Info.NumTemplParamLists = NumTPLists;
6670 if (NumTPLists) {
6671 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6672 for (unsigned i=0; i != NumTPLists; ++i)
6673 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6674 }
6675}
6676
6677TemplateName
6678ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6679 unsigned &Idx) {
6680 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6681 switch (Kind) {
6682 case TemplateName::Template:
6683 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6684
6685 case TemplateName::OverloadedTemplate: {
6686 unsigned size = Record[Idx++];
6687 UnresolvedSet<8> Decls;
6688 while (size--)
6689 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6690
6691 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6692 }
6693
6694 case TemplateName::QualifiedTemplate: {
6695 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6696 bool hasTemplKeyword = Record[Idx++];
6697 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6698 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6699 }
6700
6701 case TemplateName::DependentTemplate: {
6702 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6703 if (Record[Idx++]) // isIdentifier
6704 return Context.getDependentTemplateName(NNS,
6705 GetIdentifierInfo(F, Record,
6706 Idx));
6707 return Context.getDependentTemplateName(NNS,
6708 (OverloadedOperatorKind)Record[Idx++]);
6709 }
6710
6711 case TemplateName::SubstTemplateTemplateParm: {
6712 TemplateTemplateParmDecl *param
6713 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6714 if (!param) return TemplateName();
6715 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6716 return Context.getSubstTemplateTemplateParm(param, replacement);
6717 }
6718
6719 case TemplateName::SubstTemplateTemplateParmPack: {
6720 TemplateTemplateParmDecl *Param
6721 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6722 if (!Param)
6723 return TemplateName();
6724
6725 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6726 if (ArgPack.getKind() != TemplateArgument::Pack)
6727 return TemplateName();
6728
6729 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6730 }
6731 }
6732
6733 llvm_unreachable("Unhandled template name kind!");
6734}
6735
6736TemplateArgument
6737ASTReader::ReadTemplateArgument(ModuleFile &F,
6738 const RecordData &Record, unsigned &Idx) {
6739 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6740 switch (Kind) {
6741 case TemplateArgument::Null:
6742 return TemplateArgument();
6743 case TemplateArgument::Type:
6744 return TemplateArgument(readType(F, Record, Idx));
6745 case TemplateArgument::Declaration: {
6746 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6747 bool ForReferenceParam = Record[Idx++];
6748 return TemplateArgument(D, ForReferenceParam);
6749 }
6750 case TemplateArgument::NullPtr:
6751 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6752 case TemplateArgument::Integral: {
6753 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6754 QualType T = readType(F, Record, Idx);
6755 return TemplateArgument(Context, Value, T);
6756 }
6757 case TemplateArgument::Template:
6758 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6759 case TemplateArgument::TemplateExpansion: {
6760 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00006761 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006762 if (unsigned NumExpansions = Record[Idx++])
6763 NumTemplateExpansions = NumExpansions - 1;
6764 return TemplateArgument(Name, NumTemplateExpansions);
6765 }
6766 case TemplateArgument::Expression:
6767 return TemplateArgument(ReadExpr(F));
6768 case TemplateArgument::Pack: {
6769 unsigned NumArgs = Record[Idx++];
6770 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6771 for (unsigned I = 0; I != NumArgs; ++I)
6772 Args[I] = ReadTemplateArgument(F, Record, Idx);
6773 return TemplateArgument(Args, NumArgs);
6774 }
6775 }
6776
6777 llvm_unreachable("Unhandled template argument kind!");
6778}
6779
6780TemplateParameterList *
6781ASTReader::ReadTemplateParameterList(ModuleFile &F,
6782 const RecordData &Record, unsigned &Idx) {
6783 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6784 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6785 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6786
6787 unsigned NumParams = Record[Idx++];
6788 SmallVector<NamedDecl *, 16> Params;
6789 Params.reserve(NumParams);
6790 while (NumParams--)
6791 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6792
6793 TemplateParameterList* TemplateParams =
6794 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6795 Params.data(), Params.size(), RAngleLoc);
6796 return TemplateParams;
6797}
6798
6799void
6800ASTReader::
6801ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6802 ModuleFile &F, const RecordData &Record,
6803 unsigned &Idx) {
6804 unsigned NumTemplateArgs = Record[Idx++];
6805 TemplArgs.reserve(NumTemplateArgs);
6806 while (NumTemplateArgs--)
6807 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6808}
6809
6810/// \brief Read a UnresolvedSet structure.
6811void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6812 const RecordData &Record, unsigned &Idx) {
6813 unsigned NumDecls = Record[Idx++];
6814 Set.reserve(Context, NumDecls);
6815 while (NumDecls--) {
6816 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6817 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6818 Set.addDecl(Context, D, AS);
6819 }
6820}
6821
6822CXXBaseSpecifier
6823ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6824 const RecordData &Record, unsigned &Idx) {
6825 bool isVirtual = static_cast<bool>(Record[Idx++]);
6826 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6827 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6828 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6829 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6830 SourceRange Range = ReadSourceRange(F, Record, Idx);
6831 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6832 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6833 EllipsisLoc);
6834 Result.setInheritConstructors(inheritConstructors);
6835 return Result;
6836}
6837
6838std::pair<CXXCtorInitializer **, unsigned>
6839ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6840 unsigned &Idx) {
6841 CXXCtorInitializer **CtorInitializers = 0;
6842 unsigned NumInitializers = Record[Idx++];
6843 if (NumInitializers) {
6844 CtorInitializers
6845 = new (Context) CXXCtorInitializer*[NumInitializers];
6846 for (unsigned i=0; i != NumInitializers; ++i) {
6847 TypeSourceInfo *TInfo = 0;
6848 bool IsBaseVirtual = false;
6849 FieldDecl *Member = 0;
6850 IndirectFieldDecl *IndirectMember = 0;
6851
6852 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6853 switch (Type) {
6854 case CTOR_INITIALIZER_BASE:
6855 TInfo = GetTypeSourceInfo(F, Record, Idx);
6856 IsBaseVirtual = Record[Idx++];
6857 break;
6858
6859 case CTOR_INITIALIZER_DELEGATING:
6860 TInfo = GetTypeSourceInfo(F, Record, Idx);
6861 break;
6862
6863 case CTOR_INITIALIZER_MEMBER:
6864 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6865 break;
6866
6867 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6868 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6869 break;
6870 }
6871
6872 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6873 Expr *Init = ReadExpr(F);
6874 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6875 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6876 bool IsWritten = Record[Idx++];
6877 unsigned SourceOrderOrNumArrayIndices;
6878 SmallVector<VarDecl *, 8> Indices;
6879 if (IsWritten) {
6880 SourceOrderOrNumArrayIndices = Record[Idx++];
6881 } else {
6882 SourceOrderOrNumArrayIndices = Record[Idx++];
6883 Indices.reserve(SourceOrderOrNumArrayIndices);
6884 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6885 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6886 }
6887
6888 CXXCtorInitializer *BOMInit;
6889 if (Type == CTOR_INITIALIZER_BASE) {
6890 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6891 LParenLoc, Init, RParenLoc,
6892 MemberOrEllipsisLoc);
6893 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6894 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6895 Init, RParenLoc);
6896 } else if (IsWritten) {
6897 if (Member)
6898 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6899 LParenLoc, Init, RParenLoc);
6900 else
6901 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6902 MemberOrEllipsisLoc, LParenLoc,
6903 Init, RParenLoc);
6904 } else {
6905 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6906 LParenLoc, Init, RParenLoc,
6907 Indices.data(), Indices.size());
6908 }
6909
6910 if (IsWritten)
6911 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6912 CtorInitializers[i] = BOMInit;
6913 }
6914 }
6915
6916 return std::make_pair(CtorInitializers, NumInitializers);
6917}
6918
6919NestedNameSpecifier *
6920ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6921 const RecordData &Record, unsigned &Idx) {
6922 unsigned N = Record[Idx++];
6923 NestedNameSpecifier *NNS = 0, *Prev = 0;
6924 for (unsigned I = 0; I != N; ++I) {
6925 NestedNameSpecifier::SpecifierKind Kind
6926 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6927 switch (Kind) {
6928 case NestedNameSpecifier::Identifier: {
6929 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6930 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6931 break;
6932 }
6933
6934 case NestedNameSpecifier::Namespace: {
6935 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6936 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6937 break;
6938 }
6939
6940 case NestedNameSpecifier::NamespaceAlias: {
6941 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6942 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6943 break;
6944 }
6945
6946 case NestedNameSpecifier::TypeSpec:
6947 case NestedNameSpecifier::TypeSpecWithTemplate: {
6948 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6949 if (!T)
6950 return 0;
6951
6952 bool Template = Record[Idx++];
6953 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6954 break;
6955 }
6956
6957 case NestedNameSpecifier::Global: {
6958 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6959 // No associated value, and there can't be a prefix.
6960 break;
6961 }
6962 }
6963 Prev = NNS;
6964 }
6965 return NNS;
6966}
6967
6968NestedNameSpecifierLoc
6969ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6970 unsigned &Idx) {
6971 unsigned N = Record[Idx++];
6972 NestedNameSpecifierLocBuilder Builder;
6973 for (unsigned I = 0; I != N; ++I) {
6974 NestedNameSpecifier::SpecifierKind Kind
6975 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6976 switch (Kind) {
6977 case NestedNameSpecifier::Identifier: {
6978 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6979 SourceRange Range = ReadSourceRange(F, Record, Idx);
6980 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6981 break;
6982 }
6983
6984 case NestedNameSpecifier::Namespace: {
6985 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6986 SourceRange Range = ReadSourceRange(F, Record, Idx);
6987 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
6988 break;
6989 }
6990
6991 case NestedNameSpecifier::NamespaceAlias: {
6992 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6993 SourceRange Range = ReadSourceRange(F, Record, Idx);
6994 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
6995 break;
6996 }
6997
6998 case NestedNameSpecifier::TypeSpec:
6999 case NestedNameSpecifier::TypeSpecWithTemplate: {
7000 bool Template = Record[Idx++];
7001 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7002 if (!T)
7003 return NestedNameSpecifierLoc();
7004 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7005
7006 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7007 Builder.Extend(Context,
7008 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7009 T->getTypeLoc(), ColonColonLoc);
7010 break;
7011 }
7012
7013 case NestedNameSpecifier::Global: {
7014 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7015 Builder.MakeGlobal(Context, ColonColonLoc);
7016 break;
7017 }
7018 }
7019 }
7020
7021 return Builder.getWithLocInContext(Context);
7022}
7023
7024SourceRange
7025ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7026 unsigned &Idx) {
7027 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7028 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7029 return SourceRange(beg, end);
7030}
7031
7032/// \brief Read an integral value
7033llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7034 unsigned BitWidth = Record[Idx++];
7035 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7036 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7037 Idx += NumWords;
7038 return Result;
7039}
7040
7041/// \brief Read a signed integral value
7042llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7043 bool isUnsigned = Record[Idx++];
7044 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7045}
7046
7047/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007048llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7049 const llvm::fltSemantics &Sem,
7050 unsigned &Idx) {
7051 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007052}
7053
7054// \brief Read a string
7055std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7056 unsigned Len = Record[Idx++];
7057 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7058 Idx += Len;
7059 return Result;
7060}
7061
7062VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7063 unsigned &Idx) {
7064 unsigned Major = Record[Idx++];
7065 unsigned Minor = Record[Idx++];
7066 unsigned Subminor = Record[Idx++];
7067 if (Minor == 0)
7068 return VersionTuple(Major);
7069 if (Subminor == 0)
7070 return VersionTuple(Major, Minor - 1);
7071 return VersionTuple(Major, Minor - 1, Subminor - 1);
7072}
7073
7074CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7075 const RecordData &Record,
7076 unsigned &Idx) {
7077 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7078 return CXXTemporary::Create(Context, Decl);
7079}
7080
7081DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7082 return Diag(SourceLocation(), DiagID);
7083}
7084
7085DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7086 return Diags.Report(Loc, DiagID);
7087}
7088
7089/// \brief Retrieve the identifier table associated with the
7090/// preprocessor.
7091IdentifierTable &ASTReader::getIdentifierTable() {
7092 return PP.getIdentifierTable();
7093}
7094
7095/// \brief Record that the given ID maps to the given switch-case
7096/// statement.
7097void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7098 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7099 "Already have a SwitchCase with this ID");
7100 (*CurrSwitchCaseStmts)[ID] = SC;
7101}
7102
7103/// \brief Retrieve the switch-case statement with the given ID.
7104SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7105 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7106 return (*CurrSwitchCaseStmts)[ID];
7107}
7108
7109void ASTReader::ClearSwitchCaseIDs() {
7110 CurrSwitchCaseStmts->clear();
7111}
7112
7113void ASTReader::ReadComments() {
7114 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007115 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007116 serialization::ModuleFile *> >::iterator
7117 I = CommentsCursors.begin(),
7118 E = CommentsCursors.end();
7119 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007120 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007121 serialization::ModuleFile &F = *I->second;
7122 SavedStreamPosition SavedPosition(Cursor);
7123
7124 RecordData Record;
7125 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007126 llvm::BitstreamEntry Entry =
7127 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7128
7129 switch (Entry.Kind) {
7130 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7131 case llvm::BitstreamEntry::Error:
7132 Error("malformed block record in AST file");
7133 return;
7134 case llvm::BitstreamEntry::EndBlock:
7135 goto NextCursor;
7136 case llvm::BitstreamEntry::Record:
7137 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007138 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007139 }
7140
7141 // Read a record.
7142 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007143 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007144 case COMMENTS_RAW_COMMENT: {
7145 unsigned Idx = 0;
7146 SourceRange SR = ReadSourceRange(F, Record, Idx);
7147 RawComment::CommentKind Kind =
7148 (RawComment::CommentKind) Record[Idx++];
7149 bool IsTrailingComment = Record[Idx++];
7150 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00007151 Comments.push_back(new (Context) RawComment(
7152 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7153 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007154 break;
7155 }
7156 }
7157 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007158 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007159 }
7160 Context.Comments.addCommentsToFront(Comments);
7161}
7162
7163void ASTReader::finishPendingActions() {
7164 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007165 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007166 // If any identifiers with corresponding top-level declarations have
7167 // been loaded, load those declarations now.
Douglas Gregoraa945902013-02-18 15:53:43 +00007168 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007169 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007170 // FIXME: std::move
7171 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7172 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00007173 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00007174
7175 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007176 }
7177
7178 // Load pending declaration chains.
7179 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7180 loadPendingDeclChain(PendingDeclChains[I]);
7181 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7182 }
7183 PendingDeclChains.clear();
7184
Douglas Gregoraa945902013-02-18 15:53:43 +00007185 // Make the most recent of the top-level declarations visible.
7186 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7187 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7188 TLD != TLDEnd; ++TLD) {
7189 IdentifierInfo *II = TLD->first;
7190 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
7191 NamedDecl *ND = cast<NamedDecl>(TLD->second[I]->getMostRecentDecl());
7192 SemaObj->pushExternalDeclIntoScope(ND, II);
7193 }
7194 }
7195
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007196 // Load any pending macro definitions.
7197 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007198 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7199 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7200 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7201 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00007202 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7203 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007204 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7205 if (Info.M->Kind != MK_Module)
7206 resolvePendingMacro(II, Info);
7207 }
7208 // Handle module imports.
7209 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7210 ++IDIdx) {
7211 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7212 if (Info.M->Kind == MK_Module)
7213 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007214 }
7215 }
7216 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007217
7218 // Wire up the DeclContexts for Decls that we delayed setting until
7219 // recursive loading is completed.
7220 while (!PendingDeclContextInfos.empty()) {
7221 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7222 PendingDeclContextInfos.pop_front();
7223 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7224 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7225 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7226 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007227 }
7228
7229 // If we deserialized any C++ or Objective-C class definitions, any
7230 // Objective-C protocol definitions, or any redeclarable templates, make sure
7231 // that all redeclarations point to the definitions. Note that this can only
7232 // happen now, after the redeclaration chains have been fully wired.
7233 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7234 DEnd = PendingDefinitions.end();
7235 D != DEnd; ++D) {
7236 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7237 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7238 // Make sure that the TagType points at the definition.
7239 const_cast<TagType*>(TagT)->decl = TD;
7240 }
7241
7242 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7243 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7244 REnd = RD->redecls_end();
7245 R != REnd; ++R)
7246 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7247
7248 }
7249
7250 continue;
7251 }
7252
7253 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7254 // Make sure that the ObjCInterfaceType points at the definition.
7255 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7256 ->Decl = ID;
7257
7258 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7259 REnd = ID->redecls_end();
7260 R != REnd; ++R)
7261 R->Data = ID->Data;
7262
7263 continue;
7264 }
7265
7266 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7267 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7268 REnd = PD->redecls_end();
7269 R != REnd; ++R)
7270 R->Data = PD->Data;
7271
7272 continue;
7273 }
7274
7275 RedeclarableTemplateDecl *RTD
7276 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7277 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7278 REnd = RTD->redecls_end();
7279 R != REnd; ++R)
7280 R->Common = RTD->Common;
7281 }
7282 PendingDefinitions.clear();
7283
7284 // Load the bodies of any functions or methods we've encountered. We do
7285 // this now (delayed) so that we can be sure that the declaration chains
7286 // have been fully wired up.
7287 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7288 PBEnd = PendingBodies.end();
7289 PB != PBEnd; ++PB) {
7290 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7291 // FIXME: Check for =delete/=default?
7292 // FIXME: Complain about ODR violations here?
7293 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7294 FD->setLazyBody(PB->second);
7295 continue;
7296 }
7297
7298 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7299 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7300 MD->setLazyBody(PB->second);
7301 }
7302 PendingBodies.clear();
7303}
7304
7305void ASTReader::FinishedDeserializing() {
7306 assert(NumCurrentElementsDeserializing &&
7307 "FinishedDeserializing not paired with StartedDeserializing");
7308 if (NumCurrentElementsDeserializing == 1) {
7309 // We decrease NumCurrentElementsDeserializing only after pending actions
7310 // are finished, to avoid recursively re-calling finishPendingActions().
7311 finishPendingActions();
7312 }
7313 --NumCurrentElementsDeserializing;
7314
7315 if (NumCurrentElementsDeserializing == 0 &&
7316 Consumer && !PassingDeclsToConsumer) {
7317 // Guard variable to avoid recursively redoing the process of passing
7318 // decls to consumer.
7319 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7320 true);
7321
7322 while (!InterestingDecls.empty()) {
7323 // We are not in recursive loading, so it's safe to pass the "interesting"
7324 // decls to the consumer.
7325 Decl *D = InterestingDecls.front();
7326 InterestingDecls.pop_front();
7327 PassInterestingDeclToConsumer(D);
7328 }
7329 }
7330}
7331
7332ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7333 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007334 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007335 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7336 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7337 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7338 Consumer(0), ModuleMgr(PP.getFileManager()),
7339 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007340 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007341 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007342 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7343 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007344 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7345 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7346 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007347 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7348 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7349 TotalNumMethodPoolEntries(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007350 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7351 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7352 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7353 PassingDeclsToConsumer(false),
7354 NumCXXBaseSpecifiersLoaded(0)
7355{
7356 SourceMgr.setExternalSLocEntrySource(this);
7357}
7358
7359ASTReader::~ASTReader() {
7360 for (DeclContextVisibleUpdatesPending::iterator
7361 I = PendingVisibleUpdates.begin(),
7362 E = PendingVisibleUpdates.end();
7363 I != E; ++I) {
7364 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7365 F = I->second.end();
7366 J != F; ++J)
7367 delete J->first;
7368 }
7369}