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