blob: 17f01380ed04ea7696beea6f4fd2a4985a32a13b [file] [log] [blame]
Nick Lewycky995e26b2013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTReader class, which reads AST files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTReader.h"
15#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
17#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/Type.h"
24#include "clang/AST/TypeLocVisitor.h"
25#include "clang/Basic/FileManager.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000026#include "clang/Basic/SourceManager.h"
27#include "clang/Basic/SourceManagerInternals.h"
28#include "clang/Basic/TargetInfo.h"
29#include "clang/Basic/TargetOptions.h"
30#include "clang/Basic/Version.h"
31#include "clang/Basic/VersionTuple.h"
32#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/HeaderSearchOptions.h"
34#include "clang/Lex/MacroInfo.h"
35#include "clang/Lex/PreprocessingRecord.h"
36#include "clang/Lex/Preprocessor.h"
37#include "clang/Lex/PreprocessorOptions.h"
38#include "clang/Sema/Scope.h"
39#include "clang/Sema/Sema.h"
40#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor1a49d972013-01-25 01:03:03 +000041#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000042#include "clang/Serialization/ModuleManager.h"
43#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000044#include "llvm/ADT/Hashing.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000045#include "llvm/ADT/StringExtras.h"
46#include "llvm/Bitcode/BitstreamReader.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/FileSystem.h"
49#include "llvm/Support/MemoryBuffer.h"
50#include "llvm/Support/Path.h"
51#include "llvm/Support/SaveAndRestore.h"
52#include "llvm/Support/system_error.h"
53#include <algorithm>
Chris Lattnere4e4a882013-01-20 00:57:52 +000054#include <cstdio>
Guy Benyei7f92f2d2012-12-18 14:30:41 +000055#include <iterator>
56
57using namespace clang;
58using namespace clang::serialization;
59using namespace clang::serialization::reader;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +000060using llvm::BitstreamCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000061
62//===----------------------------------------------------------------------===//
63// PCH validator implementation
64//===----------------------------------------------------------------------===//
65
66ASTReaderListener::~ASTReaderListener() {}
67
68/// \brief Compare the given set of language options against an existing set of
69/// language options.
70///
71/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
72///
73/// \returns true if the languagae options mis-match, false otherwise.
74static bool checkLanguageOptions(const LangOptions &LangOpts,
75 const LangOptions &ExistingLangOpts,
76 DiagnosticsEngine *Diags) {
77#define LANGOPT(Name, Bits, Default, Description) \
78 if (ExistingLangOpts.Name != LangOpts.Name) { \
79 if (Diags) \
80 Diags->Report(diag::err_pch_langopt_mismatch) \
81 << Description << LangOpts.Name << ExistingLangOpts.Name; \
82 return true; \
83 }
84
85#define VALUE_LANGOPT(Name, Bits, Default, Description) \
86 if (ExistingLangOpts.Name != LangOpts.Name) { \
87 if (Diags) \
88 Diags->Report(diag::err_pch_langopt_value_mismatch) \
89 << Description; \
90 return true; \
91 }
92
93#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
95 if (Diags) \
96 Diags->Report(diag::err_pch_langopt_value_mismatch) \
97 << Description; \
98 return true; \
99 }
100
101#define BENIGN_LANGOPT(Name, Bits, Default, Description)
102#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
103#include "clang/Basic/LangOptions.def"
104
105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
106 if (Diags)
107 Diags->Report(diag::err_pch_langopt_value_mismatch)
108 << "target Objective-C runtime";
109 return true;
110 }
111
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +0000112 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
113 LangOpts.CommentOpts.BlockCommandNames) {
114 if (Diags)
115 Diags->Report(diag::err_pch_langopt_value_mismatch)
116 << "block command names";
117 return true;
118 }
119
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000120 return false;
121}
122
123/// \brief Compare the given set of target options against an existing set of
124/// target options.
125///
126/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
127///
128/// \returns true if the target options mis-match, false otherwise.
129static bool checkTargetOptions(const TargetOptions &TargetOpts,
130 const TargetOptions &ExistingTargetOpts,
131 DiagnosticsEngine *Diags) {
132#define CHECK_TARGET_OPT(Field, Name) \
133 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
134 if (Diags) \
135 Diags->Report(diag::err_pch_targetopt_mismatch) \
136 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
137 return true; \
138 }
139
140 CHECK_TARGET_OPT(Triple, "target");
141 CHECK_TARGET_OPT(CPU, "target CPU");
142 CHECK_TARGET_OPT(ABI, "target ABI");
143 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
144 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
145#undef CHECK_TARGET_OPT
146
147 // Compare feature sets.
148 SmallVector<StringRef, 4> ExistingFeatures(
149 ExistingTargetOpts.FeaturesAsWritten.begin(),
150 ExistingTargetOpts.FeaturesAsWritten.end());
151 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
152 TargetOpts.FeaturesAsWritten.end());
153 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
154 std::sort(ReadFeatures.begin(), ReadFeatures.end());
155
156 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
157 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
158 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
159 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
160 ++ExistingIdx;
161 ++ReadIdx;
162 continue;
163 }
164
165 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
166 if (Diags)
167 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
168 << false << ReadFeatures[ReadIdx];
169 return true;
170 }
171
172 if (Diags)
173 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
174 << true << ExistingFeatures[ExistingIdx];
175 return true;
176 }
177
178 if (ExistingIdx < ExistingN) {
179 if (Diags)
180 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
181 << true << ExistingFeatures[ExistingIdx];
182 return true;
183 }
184
185 if (ReadIdx < ReadN) {
186 if (Diags)
187 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
188 << false << ReadFeatures[ReadIdx];
189 return true;
190 }
191
192 return false;
193}
194
195bool
196PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
197 bool Complain) {
198 const LangOptions &ExistingLangOpts = PP.getLangOpts();
199 return checkLanguageOptions(LangOpts, ExistingLangOpts,
200 Complain? &Reader.Diags : 0);
201}
202
203bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
204 bool Complain) {
205 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
206 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
207 Complain? &Reader.Diags : 0);
208}
209
210namespace {
211 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
212 MacroDefinitionsMap;
213}
214
215/// \brief Collect the macro definitions provided by the given preprocessor
216/// options.
217static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
218 MacroDefinitionsMap &Macros,
219 SmallVectorImpl<StringRef> *MacroNames = 0){
220 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
221 StringRef Macro = PPOpts.Macros[I].first;
222 bool IsUndef = PPOpts.Macros[I].second;
223
224 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
225 StringRef MacroName = MacroPair.first;
226 StringRef MacroBody = MacroPair.second;
227
228 // For an #undef'd macro, we only care about the name.
229 if (IsUndef) {
230 if (MacroNames && !Macros.count(MacroName))
231 MacroNames->push_back(MacroName);
232
233 Macros[MacroName] = std::make_pair("", true);
234 continue;
235 }
236
237 // For a #define'd macro, figure out the actual definition.
238 if (MacroName.size() == Macro.size())
239 MacroBody = "1";
240 else {
241 // Note: GCC drops anything following an end-of-line character.
242 StringRef::size_type End = MacroBody.find_first_of("\n\r");
243 MacroBody = MacroBody.substr(0, End);
244 }
245
246 if (MacroNames && !Macros.count(MacroName))
247 MacroNames->push_back(MacroName);
248 Macros[MacroName] = std::make_pair(MacroBody, false);
249 }
250}
251
252/// \brief Check the preprocessor options deserialized from the control block
253/// against the preprocessor options in an existing preprocessor.
254///
255/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
256static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
257 const PreprocessorOptions &ExistingPPOpts,
258 DiagnosticsEngine *Diags,
259 FileManager &FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000260 std::string &SuggestedPredefines,
261 const LangOptions &LangOpts) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000262 // Check macro definitions.
263 MacroDefinitionsMap ASTFileMacros;
264 collectMacroDefinitions(PPOpts, ASTFileMacros);
265 MacroDefinitionsMap ExistingMacros;
266 SmallVector<StringRef, 4> ExistingMacroNames;
267 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
268
269 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
270 // Dig out the macro definition in the existing preprocessor options.
271 StringRef MacroName = ExistingMacroNames[I];
272 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
273
274 // Check whether we know anything about this macro name or not.
275 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
276 = ASTFileMacros.find(MacroName);
277 if (Known == ASTFileMacros.end()) {
278 // FIXME: Check whether this identifier was referenced anywhere in the
279 // AST file. If so, we should reject the AST file. Unfortunately, this
280 // information isn't in the control block. What shall we do about it?
281
282 if (Existing.second) {
283 SuggestedPredefines += "#undef ";
284 SuggestedPredefines += MacroName.str();
285 SuggestedPredefines += '\n';
286 } else {
287 SuggestedPredefines += "#define ";
288 SuggestedPredefines += MacroName.str();
289 SuggestedPredefines += ' ';
290 SuggestedPredefines += Existing.first.str();
291 SuggestedPredefines += '\n';
292 }
293 continue;
294 }
295
296 // If the macro was defined in one but undef'd in the other, we have a
297 // conflict.
298 if (Existing.second != Known->second.second) {
299 if (Diags) {
300 Diags->Report(diag::err_pch_macro_def_undef)
301 << MacroName << Known->second.second;
302 }
303 return true;
304 }
305
306 // If the macro was #undef'd in both, or if the macro bodies are identical,
307 // it's fine.
308 if (Existing.second || Existing.first == Known->second.first)
309 continue;
310
311 // The macro bodies differ; complain.
312 if (Diags) {
313 Diags->Report(diag::err_pch_macro_def_conflict)
314 << MacroName << Known->second.first << Existing.first;
315 }
316 return true;
317 }
318
319 // Check whether we're using predefines.
320 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
321 if (Diags) {
322 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
323 }
324 return true;
325 }
326
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000327 // Detailed record is important since it is used for the module cache hash.
328 if (LangOpts.Modules &&
329 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
330 if (Diags) {
331 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
332 }
333 return true;
334 }
335
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000336 // Compute the #include and #include_macros lines we need.
337 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
338 StringRef File = ExistingPPOpts.Includes[I];
339 if (File == ExistingPPOpts.ImplicitPCHInclude)
340 continue;
341
342 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
343 != PPOpts.Includes.end())
344 continue;
345
346 SuggestedPredefines += "#include \"";
347 SuggestedPredefines +=
348 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
349 SuggestedPredefines += "\"\n";
350 }
351
352 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
353 StringRef File = ExistingPPOpts.MacroIncludes[I];
354 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
355 File)
356 != PPOpts.MacroIncludes.end())
357 continue;
358
359 SuggestedPredefines += "#__include_macros \"";
360 SuggestedPredefines +=
361 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
362 SuggestedPredefines += "\"\n##\n";
363 }
364
365 return false;
366}
367
368bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
369 bool Complain,
370 std::string &SuggestedPredefines) {
371 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
372
373 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
374 Complain? &Reader.Diags : 0,
375 PP.getFileManager(),
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000376 SuggestedPredefines,
377 PP.getLangOpts());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000378}
379
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000380void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
381 PP.setCounterValue(Value);
382}
383
384//===----------------------------------------------------------------------===//
385// AST reader implementation
386//===----------------------------------------------------------------------===//
387
388void
389ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
390 DeserializationListener = Listener;
391}
392
393
394
395unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
396 return serialization::ComputeHash(Sel);
397}
398
399
400std::pair<unsigned, unsigned>
401ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
402 using namespace clang::io;
403 unsigned KeyLen = ReadUnalignedLE16(d);
404 unsigned DataLen = ReadUnalignedLE16(d);
405 return std::make_pair(KeyLen, DataLen);
406}
407
408ASTSelectorLookupTrait::internal_key_type
409ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
410 using namespace clang::io;
411 SelectorTable &SelTable = Reader.getContext().Selectors;
412 unsigned N = ReadUnalignedLE16(d);
413 IdentifierInfo *FirstII
Douglas Gregor8222b892013-01-21 16:52:34 +0000414 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000415 if (N == 0)
416 return SelTable.getNullarySelector(FirstII);
417 else if (N == 1)
418 return SelTable.getUnarySelector(FirstII);
419
420 SmallVector<IdentifierInfo *, 16> Args;
421 Args.push_back(FirstII);
422 for (unsigned I = 1; I != N; ++I)
Douglas Gregor8222b892013-01-21 16:52:34 +0000423 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000424
425 return SelTable.getSelector(N, Args.data());
426}
427
428ASTSelectorLookupTrait::data_type
429ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
430 unsigned DataLen) {
431 using namespace clang::io;
432
433 data_type Result;
434
435 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +0000436 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
437 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
438 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
439 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
440 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
441 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000442
443 // Load instance methods
444 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
445 if (ObjCMethodDecl *Method
446 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
447 Result.Instance.push_back(Method);
448 }
449
450 // Load factory methods
451 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
452 if (ObjCMethodDecl *Method
453 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
454 Result.Factory.push_back(Method);
455 }
456
457 return Result;
458}
459
Douglas Gregor479633c2013-01-23 18:53:14 +0000460unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
461 return llvm::HashString(a);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000462}
463
464std::pair<unsigned, unsigned>
Douglas Gregor479633c2013-01-23 18:53:14 +0000465ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000466 using namespace clang::io;
467 unsigned DataLen = ReadUnalignedLE16(d);
468 unsigned KeyLen = ReadUnalignedLE16(d);
469 return std::make_pair(KeyLen, DataLen);
470}
471
Douglas Gregor479633c2013-01-23 18:53:14 +0000472ASTIdentifierLookupTraitBase::internal_key_type
473ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000474 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregor479633c2013-01-23 18:53:14 +0000475 return StringRef((const char*) d, n-1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000476}
477
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000478/// \brief Whether the given identifier is "interesting".
479static bool isInterestingIdentifier(IdentifierInfo &II) {
480 return II.isPoisoned() ||
481 II.isExtensionToken() ||
482 II.getObjCOrBuiltinID() ||
483 II.hasRevertedTokenIDToIdentifier() ||
484 II.hadMacroDefinition() ||
485 II.getFETokenInfo<void>();
486}
487
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000488IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
489 const unsigned char* d,
490 unsigned DataLen) {
491 using namespace clang::io;
492 unsigned RawID = ReadUnalignedLE32(d);
493 bool IsInteresting = RawID & 0x01;
494
495 // Wipe out the "is interesting" bit.
496 RawID = RawID >> 1;
497
498 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
499 if (!IsInteresting) {
500 // For uninteresting identifiers, just build the IdentifierInfo
501 // and associate it with the persistent ID.
502 IdentifierInfo *II = KnownII;
503 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000504 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000505 KnownII = II;
506 }
507 Reader.SetIdentifierInfo(ID, II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000508 if (!II->isFromAST()) {
509 bool WasInteresting = isInterestingIdentifier(*II);
510 II->setIsFromAST();
511 if (WasInteresting)
512 II->setChangedSinceDeserialization();
513 }
514 Reader.markIdentifierUpToDate(II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000515 return II;
516 }
517
518 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
519 unsigned Bits = ReadUnalignedLE16(d);
520 bool CPlusPlusOperatorKeyword = Bits & 0x01;
521 Bits >>= 1;
522 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
523 Bits >>= 1;
524 bool Poisoned = Bits & 0x01;
525 Bits >>= 1;
526 bool ExtensionToken = Bits & 0x01;
527 Bits >>= 1;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000528 bool hasSubmoduleMacros = Bits & 0x01;
529 Bits >>= 1;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000530 bool hadMacroDefinition = Bits & 0x01;
531 Bits >>= 1;
532
533 assert(Bits == 0 && "Extra bits in the identifier?");
534 DataLen -= 8;
535
536 // Build the IdentifierInfo itself and link the identifier ID with
537 // the new IdentifierInfo.
538 IdentifierInfo *II = KnownII;
539 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000540 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000541 KnownII = II;
542 }
543 Reader.markIdentifierUpToDate(II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000544 if (!II->isFromAST()) {
545 bool WasInteresting = isInterestingIdentifier(*II);
546 II->setIsFromAST();
547 if (WasInteresting)
548 II->setChangedSinceDeserialization();
549 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000550
551 // Set or check the various bits in the IdentifierInfo structure.
552 // Token IDs are read-only.
Argyrios Kyrtzidis1ebefc72013-02-27 01:13:51 +0000553 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000554 II->RevertTokenIDToIdentifier();
555 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
556 assert(II->isExtensionToken() == ExtensionToken &&
557 "Incorrect extension token flag");
558 (void)ExtensionToken;
559 if (Poisoned)
560 II->setIsPoisoned(true);
561 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
562 "Incorrect C++ operator keyword flag");
563 (void)CPlusPlusOperatorKeyword;
564
565 // If this identifier is a macro, deserialize the macro
566 // definition.
567 if (hadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000568 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
569 DataLen -= 4;
570 SmallVector<uint32_t, 8> LocalMacroIDs;
571 if (hasSubmoduleMacros) {
572 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
573 DataLen -= 4;
574 LocalMacroIDs.push_back(LocalMacroID);
575 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000576 DataLen -= 4;
577 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000578
579 if (F.Kind == MK_Module) {
580 for (SmallVectorImpl<uint32_t>::iterator
581 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
582 MacroID MacID = Reader.getGlobalMacroID(F, *I);
583 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
584 }
585 } else {
586 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
587 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000588 }
589
590 Reader.SetIdentifierInfo(ID, II);
591
592 // Read all of the declarations visible at global scope with this
593 // name.
594 if (DataLen > 0) {
595 SmallVector<uint32_t, 4> DeclIDs;
596 for (; DataLen > 0; DataLen -= 4)
597 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
598 Reader.SetGloballyVisibleDecls(II, DeclIDs);
599 }
600
601 return II;
602}
603
604unsigned
605ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
606 llvm::FoldingSetNodeID ID;
607 ID.AddInteger(Key.Kind);
608
609 switch (Key.Kind) {
610 case DeclarationName::Identifier:
611 case DeclarationName::CXXLiteralOperatorName:
612 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
613 break;
614 case DeclarationName::ObjCZeroArgSelector:
615 case DeclarationName::ObjCOneArgSelector:
616 case DeclarationName::ObjCMultiArgSelector:
617 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
618 break;
619 case DeclarationName::CXXOperatorName:
620 ID.AddInteger((OverloadedOperatorKind)Key.Data);
621 break;
622 case DeclarationName::CXXConstructorName:
623 case DeclarationName::CXXDestructorName:
624 case DeclarationName::CXXConversionFunctionName:
625 case DeclarationName::CXXUsingDirective:
626 break;
627 }
628
629 return ID.ComputeHash();
630}
631
632ASTDeclContextNameLookupTrait::internal_key_type
633ASTDeclContextNameLookupTrait::GetInternalKey(
634 const external_key_type& Name) const {
635 DeclNameKey Key;
636 Key.Kind = Name.getNameKind();
637 switch (Name.getNameKind()) {
638 case DeclarationName::Identifier:
639 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
640 break;
641 case DeclarationName::ObjCZeroArgSelector:
642 case DeclarationName::ObjCOneArgSelector:
643 case DeclarationName::ObjCMultiArgSelector:
644 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
645 break;
646 case DeclarationName::CXXOperatorName:
647 Key.Data = Name.getCXXOverloadedOperator();
648 break;
649 case DeclarationName::CXXLiteralOperatorName:
650 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
651 break;
652 case DeclarationName::CXXConstructorName:
653 case DeclarationName::CXXDestructorName:
654 case DeclarationName::CXXConversionFunctionName:
655 case DeclarationName::CXXUsingDirective:
656 Key.Data = 0;
657 break;
658 }
659
660 return Key;
661}
662
663std::pair<unsigned, unsigned>
664ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
665 using namespace clang::io;
666 unsigned KeyLen = ReadUnalignedLE16(d);
667 unsigned DataLen = ReadUnalignedLE16(d);
668 return std::make_pair(KeyLen, DataLen);
669}
670
671ASTDeclContextNameLookupTrait::internal_key_type
672ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
673 using namespace clang::io;
674
675 DeclNameKey Key;
676 Key.Kind = (DeclarationName::NameKind)*d++;
677 switch (Key.Kind) {
678 case DeclarationName::Identifier:
679 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
680 break;
681 case DeclarationName::ObjCZeroArgSelector:
682 case DeclarationName::ObjCOneArgSelector:
683 case DeclarationName::ObjCMultiArgSelector:
684 Key.Data =
685 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
686 .getAsOpaquePtr();
687 break;
688 case DeclarationName::CXXOperatorName:
689 Key.Data = *d++; // OverloadedOperatorKind
690 break;
691 case DeclarationName::CXXLiteralOperatorName:
692 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
693 break;
694 case DeclarationName::CXXConstructorName:
695 case DeclarationName::CXXDestructorName:
696 case DeclarationName::CXXConversionFunctionName:
697 case DeclarationName::CXXUsingDirective:
698 Key.Data = 0;
699 break;
700 }
701
702 return Key;
703}
704
705ASTDeclContextNameLookupTrait::data_type
706ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
707 const unsigned char* d,
708 unsigned DataLen) {
709 using namespace clang::io;
710 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidise8b61cf2013-01-11 22:29:49 +0000711 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
712 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000713 return std::make_pair(Start, Start + NumDecls);
714}
715
716bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000717 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000718 const std::pair<uint64_t, uint64_t> &Offsets,
719 DeclContextInfo &Info) {
720 SavedStreamPosition SavedPosition(Cursor);
721 // First the lexical decls.
722 if (Offsets.first != 0) {
723 Cursor.JumpToBit(Offsets.first);
724
725 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000726 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000727 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000728 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000729 if (RecCode != DECL_CONTEXT_LEXICAL) {
730 Error("Expected lexical block");
731 return true;
732 }
733
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000734 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
735 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000736 }
737
738 // Now the lookup table.
739 if (Offsets.second != 0) {
740 Cursor.JumpToBit(Offsets.second);
741
742 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000743 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000744 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000745 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000746 if (RecCode != DECL_CONTEXT_VISIBLE) {
747 Error("Expected visible lookup table block");
748 return true;
749 }
750 Info.NameLookupTableData
751 = ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000752 (const unsigned char *)Blob.data() + Record[0],
753 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000754 ASTDeclContextNameLookupTrait(*this, M));
755 }
756
757 return false;
758}
759
760void ASTReader::Error(StringRef Msg) {
761 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregorc1478612013-05-10 22:15:13 +0000762 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
763 Diag(diag::note_module_cache_path)
764 << PP.getHeaderSearchInfo().getModuleCachePath();
765 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000766}
767
768void ASTReader::Error(unsigned DiagID,
769 StringRef Arg1, StringRef Arg2) {
770 if (Diags.isDiagnosticInFlight())
771 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
772 else
773 Diag(DiagID) << Arg1 << Arg2;
774}
775
776//===----------------------------------------------------------------------===//
777// Source Manager Deserialization
778//===----------------------------------------------------------------------===//
779
780/// \brief Read the line table in the source manager block.
781/// \returns true if there was an error.
782bool ASTReader::ParseLineTable(ModuleFile &F,
783 SmallVectorImpl<uint64_t> &Record) {
784 unsigned Idx = 0;
785 LineTableInfo &LineTable = SourceMgr.getLineTable();
786
787 // Parse the file names
788 std::map<int, int> FileIDs;
789 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
790 // Extract the file name
791 unsigned FilenameLen = Record[Idx++];
792 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
793 Idx += FilenameLen;
794 MaybeAddSystemRootToFilename(F, Filename);
795 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
796 }
797
798 // Parse the line entries
799 std::vector<LineEntry> Entries;
800 while (Idx < Record.size()) {
801 int FID = Record[Idx++];
802 assert(FID >= 0 && "Serialized line entries for non-local file.");
803 // Remap FileID from 1-based old view.
804 FID += F.SLocEntryBaseID - 1;
805
806 // Extract the line entries
807 unsigned NumEntries = Record[Idx++];
808 assert(NumEntries && "Numentries is 00000");
809 Entries.clear();
810 Entries.reserve(NumEntries);
811 for (unsigned I = 0; I != NumEntries; ++I) {
812 unsigned FileOffset = Record[Idx++];
813 unsigned LineNo = Record[Idx++];
814 int FilenameID = FileIDs[Record[Idx++]];
815 SrcMgr::CharacteristicKind FileKind
816 = (SrcMgr::CharacteristicKind)Record[Idx++];
817 unsigned IncludeOffset = Record[Idx++];
818 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
819 FileKind, IncludeOffset));
820 }
821 LineTable.AddEntry(FileID::get(FID), Entries);
822 }
823
824 return false;
825}
826
827/// \brief Read a source manager block
828bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
829 using namespace SrcMgr;
830
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000831 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000832
833 // Set the source-location entry cursor to the current position in
834 // the stream. This cursor will be used to read the contents of the
835 // source manager block initially, and then lazily read
836 // source-location entries as needed.
837 SLocEntryCursor = F.Stream;
838
839 // The stream itself is going to skip over the source manager block.
840 if (F.Stream.SkipBlock()) {
841 Error("malformed block record in AST file");
842 return true;
843 }
844
845 // Enter the source manager block.
846 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
847 Error("malformed source manager block record in AST file");
848 return true;
849 }
850
851 RecordData Record;
852 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +0000853 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
854
855 switch (E.Kind) {
856 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
857 case llvm::BitstreamEntry::Error:
858 Error("malformed block record in AST file");
859 return true;
860 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000861 return false;
Chris Lattner88bde502013-01-19 21:39:22 +0000862 case llvm::BitstreamEntry::Record:
863 // The interesting case.
864 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000865 }
Chris Lattner88bde502013-01-19 21:39:22 +0000866
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000867 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000868 Record.clear();
Chris Lattner125eb3e2013-01-21 18:28:26 +0000869 StringRef Blob;
870 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000871 default: // Default behavior: ignore.
872 break;
873
874 case SM_SLOC_FILE_ENTRY:
875 case SM_SLOC_BUFFER_ENTRY:
876 case SM_SLOC_EXPANSION_ENTRY:
877 // Once we hit one of the source location entries, we're done.
878 return false;
879 }
880 }
881}
882
883/// \brief If a header file is not found at the path that we expect it to be
884/// and the PCH file was moved from its original location, try to resolve the
885/// file by assuming that header+PCH were moved together and the header is in
886/// the same place relative to the PCH.
887static std::string
888resolveFileRelativeToOriginalDir(const std::string &Filename,
889 const std::string &OriginalDir,
890 const std::string &CurrDir) {
891 assert(OriginalDir != CurrDir &&
892 "No point trying to resolve the file if the PCH dir didn't change");
893 using namespace llvm::sys;
894 SmallString<128> filePath(Filename);
895 fs::make_absolute(filePath);
896 assert(path::is_absolute(OriginalDir));
897 SmallString<128> currPCHPath(CurrDir);
898
899 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
900 fileDirE = path::end(path::parent_path(filePath));
901 path::const_iterator origDirI = path::begin(OriginalDir),
902 origDirE = path::end(OriginalDir);
903 // Skip the common path components from filePath and OriginalDir.
904 while (fileDirI != fileDirE && origDirI != origDirE &&
905 *fileDirI == *origDirI) {
906 ++fileDirI;
907 ++origDirI;
908 }
909 for (; origDirI != origDirE; ++origDirI)
910 path::append(currPCHPath, "..");
911 path::append(currPCHPath, fileDirI, fileDirE);
912 path::append(currPCHPath, path::filename(Filename));
913 return currPCHPath.str();
914}
915
916bool ASTReader::ReadSLocEntry(int ID) {
917 if (ID == 0)
918 return false;
919
920 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
921 Error("source location entry ID out-of-range for AST file");
922 return true;
923 }
924
925 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
926 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000927 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000928 unsigned BaseOffset = F->SLocEntryBaseOffset;
929
930 ++NumSLocEntriesRead;
Chris Lattner88bde502013-01-19 21:39:22 +0000931 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
932 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000933 Error("incorrectly-formatted source location entry in AST file");
934 return true;
935 }
Chris Lattner88bde502013-01-19 21:39:22 +0000936
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000937 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000938 StringRef Blob;
939 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000940 default:
941 Error("incorrectly-formatted source location entry in AST file");
942 return true;
943
944 case SM_SLOC_FILE_ENTRY: {
945 // We will detect whether a file changed and return 'Failure' for it, but
946 // we will also try to fail gracefully by setting up the SLocEntry.
947 unsigned InputID = Record[4];
948 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +0000949 const FileEntry *File = IF.getFile();
950 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000951
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +0000952 // Note that we only check if a File was returned. If it was out-of-date
953 // we have complained but we will continue creating a FileID to recover
954 // gracefully.
955 if (!File)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000956 return true;
957
958 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
959 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
960 // This is the module's main file.
961 IncludeLoc = getImportLocation(F);
962 }
963 SrcMgr::CharacteristicKind
964 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
965 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
966 ID, BaseOffset + Record[0]);
967 SrcMgr::FileInfo &FileInfo =
968 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
969 FileInfo.NumCreatedFIDs = Record[5];
970 if (Record[3])
971 FileInfo.setHasLineDirectives();
972
973 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
974 unsigned NumFileDecls = Record[7];
975 if (NumFileDecls) {
976 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
977 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
978 NumFileDecls));
979 }
980
981 const SrcMgr::ContentCache *ContentCache
982 = SourceMgr.getOrCreateContentCache(File,
983 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
984 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
985 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
986 unsigned Code = SLocEntryCursor.ReadCode();
987 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000988 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000989
990 if (RecCode != SM_SLOC_BUFFER_BLOB) {
991 Error("AST record has invalid code");
992 return true;
993 }
994
995 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000996 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000997 SourceMgr.overrideFileContents(File, Buffer);
998 }
999
1000 break;
1001 }
1002
1003 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001004 const char *Name = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001005 unsigned Offset = Record[0];
1006 SrcMgr::CharacteristicKind
1007 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1008 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1009 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1010 IncludeLoc = getImportLocation(F);
1011 }
1012 unsigned Code = SLocEntryCursor.ReadCode();
1013 Record.clear();
1014 unsigned RecCode
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001015 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001016
1017 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1018 Error("AST record has invalid code");
1019 return true;
1020 }
1021
1022 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001023 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001024 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1025 BaseOffset + Offset, IncludeLoc);
1026 break;
1027 }
1028
1029 case SM_SLOC_EXPANSION_ENTRY: {
1030 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1031 SourceMgr.createExpansionLoc(SpellingLoc,
1032 ReadSourceLocation(*F, Record[2]),
1033 ReadSourceLocation(*F, Record[3]),
1034 Record[4],
1035 ID,
1036 BaseOffset + Record[0]);
1037 break;
1038 }
1039 }
1040
1041 return false;
1042}
1043
1044std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1045 if (ID == 0)
1046 return std::make_pair(SourceLocation(), "");
1047
1048 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1049 Error("source location entry ID out-of-range for AST file");
1050 return std::make_pair(SourceLocation(), "");
1051 }
1052
1053 // Find which module file this entry lands in.
1054 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1055 if (M->Kind != MK_Module)
1056 return std::make_pair(SourceLocation(), "");
1057
1058 // FIXME: Can we map this down to a particular submodule? That would be
1059 // ideal.
1060 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1061}
1062
1063/// \brief Find the location where the module F is imported.
1064SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1065 if (F->ImportLoc.isValid())
1066 return F->ImportLoc;
1067
1068 // Otherwise we have a PCH. It's considered to be "imported" at the first
1069 // location of its includer.
1070 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1071 // Main file is the importer. We assume that it is the first entry in the
1072 // entry table. We can't ask the manager, because at the time of PCH loading
1073 // the main file entry doesn't exist yet.
1074 // The very first entry is the invalid instantiation loc, which takes up
1075 // offsets 0 and 1.
1076 return SourceLocation::getFromRawEncoding(2U);
1077 }
1078 //return F->Loaders[0]->FirstLoc;
1079 return F->ImportedBy[0]->FirstLoc;
1080}
1081
1082/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1083/// specified cursor. Read the abbreviations that are at the top of the block
1084/// and then leave the cursor pointing into the block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001085bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001086 if (Cursor.EnterSubBlock(BlockID)) {
1087 Error("malformed block record in AST file");
1088 return Failure;
1089 }
1090
1091 while (true) {
1092 uint64_t Offset = Cursor.GetCurrentBitNo();
1093 unsigned Code = Cursor.ReadCode();
1094
1095 // We expect all abbrevs to be at the start of the block.
1096 if (Code != llvm::bitc::DEFINE_ABBREV) {
1097 Cursor.JumpToBit(Offset);
1098 return false;
1099 }
1100 Cursor.ReadAbbrevRecord();
1101 }
1102}
1103
John McCallaeeacf72013-05-03 00:10:13 +00001104Token ASTReader::ReadToken(ModuleFile &F, const RecordData &Record,
1105 unsigned &Idx) {
1106 Token Tok;
1107 Tok.startToken();
1108 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1109 Tok.setLength(Record[Idx++]);
1110 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1111 Tok.setIdentifierInfo(II);
1112 Tok.setKind((tok::TokenKind)Record[Idx++]);
1113 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1114 return Tok;
1115}
1116
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001117MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001118 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001119
1120 // Keep track of where we are in the stream, then jump back there
1121 // after reading this macro.
1122 SavedStreamPosition SavedPosition(Stream);
1123
1124 Stream.JumpToBit(Offset);
1125 RecordData Record;
1126 SmallVector<IdentifierInfo*, 16> MacroArgs;
1127 MacroInfo *Macro = 0;
1128
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001129 while (true) {
Chris Lattner99a5af02013-01-20 00:00:22 +00001130 // Advance to the next record, but if we get to the end of the block, don't
1131 // pop it (removing all the abbreviations from the cursor) since we want to
1132 // be able to reseek within the block and read entries.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001133 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattner99a5af02013-01-20 00:00:22 +00001134 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1135
1136 switch (Entry.Kind) {
1137 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1138 case llvm::BitstreamEntry::Error:
1139 Error("malformed block record in AST file");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001140 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001141 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001142 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001143 case llvm::BitstreamEntry::Record:
1144 // The interesting case.
1145 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001146 }
1147
1148 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001149 Record.clear();
1150 PreprocessorRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001151 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001152 switch (RecType) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001153 case PP_MACRO_DIRECTIVE_HISTORY:
1154 return Macro;
1155
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001156 case PP_MACRO_OBJECT_LIKE:
1157 case PP_MACRO_FUNCTION_LIKE: {
1158 // If we already have a macro, that means that we've hit the end
1159 // of the definition of the macro we were looking for. We're
1160 // done.
1161 if (Macro)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001162 return Macro;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001163
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001164 unsigned NextIndex = 1; // Skip identifier ID.
1165 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001166 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001167 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001168 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001169 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001170
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001171 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1172 // Decode function-like macro info.
1173 bool isC99VarArgs = Record[NextIndex++];
1174 bool isGNUVarArgs = Record[NextIndex++];
1175 bool hasCommaPasting = Record[NextIndex++];
1176 MacroArgs.clear();
1177 unsigned NumArgs = Record[NextIndex++];
1178 for (unsigned i = 0; i != NumArgs; ++i)
1179 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1180
1181 // Install function-like macro info.
1182 MI->setIsFunctionLike();
1183 if (isC99VarArgs) MI->setIsC99Varargs();
1184 if (isGNUVarArgs) MI->setIsGNUVarargs();
1185 if (hasCommaPasting) MI->setHasCommaPasting();
1186 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1187 PP.getPreprocessorAllocator());
1188 }
1189
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001190 // Remember that we saw this macro last so that we add the tokens that
1191 // form its body to it.
1192 Macro = MI;
1193
1194 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1195 Record[NextIndex]) {
1196 // We have a macro definition. Register the association
1197 PreprocessedEntityID
1198 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1199 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +00001200 PreprocessingRecord::PPEntityID
1201 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1202 MacroDefinition *PPDef =
1203 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1204 if (PPDef)
1205 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001206 }
1207
1208 ++NumMacrosRead;
1209 break;
1210 }
1211
1212 case PP_TOKEN: {
1213 // If we see a TOKEN before a PP_MACRO_*, then the file is
1214 // erroneous, just pretend we didn't see this.
1215 if (Macro == 0) break;
1216
John McCallaeeacf72013-05-03 00:10:13 +00001217 unsigned Idx = 0;
1218 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001219 Macro->AddTokenToBody(Tok);
1220 break;
1221 }
1222 }
1223 }
1224}
1225
1226PreprocessedEntityID
1227ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1228 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1229 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1230 assert(I != M.PreprocessedEntityRemap.end()
1231 && "Invalid index into preprocessed entity index remap");
1232
1233 return LocalID + I->second;
1234}
1235
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001236unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1237 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001238}
1239
1240HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001241HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1242 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1243 FE->getName() };
1244 return ikey;
1245}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001246
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001247bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1248 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001249 return false;
1250
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001251 if (strcmp(a.Filename, b.Filename) == 0)
1252 return true;
1253
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001254 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001255 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001256 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1257 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001258 return (FEA && FEA == FEB);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001259}
1260
1261std::pair<unsigned, unsigned>
1262HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1263 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1264 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001265 return std::make_pair(KeyLen, DataLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001266}
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001267
1268HeaderFileInfoTrait::internal_key_type
1269HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1270 internal_key_type ikey;
1271 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1272 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1273 ikey.Filename = (const char *)d;
1274 return ikey;
1275}
1276
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001277HeaderFileInfoTrait::data_type
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001278HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001279 unsigned DataLen) {
1280 const unsigned char *End = d + DataLen;
1281 using namespace clang::io;
1282 HeaderFileInfo HFI;
1283 unsigned Flags = *d++;
1284 HFI.isImport = (Flags >> 5) & 0x01;
1285 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1286 HFI.DirInfo = (Flags >> 2) & 0x03;
1287 HFI.Resolved = (Flags >> 1) & 0x01;
1288 HFI.IndexHeaderMapHeader = Flags & 0x01;
1289 HFI.NumIncludes = ReadUnalignedLE16(d);
1290 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1291 ReadUnalignedLE32(d));
1292 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1293 // The framework offset is 1 greater than the actual offset,
1294 // since 0 is used as an indicator for "no framework name".
1295 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1296 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1297 }
1298
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001299 if (d != End) {
1300 uint32_t LocalSMID = ReadUnalignedLE32(d);
1301 if (LocalSMID) {
1302 // This header is part of a module. Associate it with the module to enable
1303 // implicit module import.
1304 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1305 Module *Mod = Reader.getSubmodule(GlobalSMID);
1306 HFI.isModuleHeader = true;
1307 FileManager &FileMgr = Reader.getFileManager();
1308 ModuleMap &ModMap =
1309 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1310 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false);
1311 }
1312 }
1313
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001314 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1315 (void)End;
1316
1317 // This HeaderFileInfo was externally loaded.
1318 HFI.External = true;
1319 return HFI;
1320}
1321
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001322void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1323 ModuleFile *M,
1324 GlobalMacroID GMacID,
1325 SourceLocation ImportLoc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001326 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001327 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1328}
1329
1330void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1331 ModuleFile *M,
1332 uint64_t MacroDirectivesOffset) {
1333 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1334 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001335}
1336
1337void ASTReader::ReadDefinedMacros() {
1338 // Note that we are loading defined macros.
1339 Deserializing Macros(this);
1340
1341 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1342 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001343 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001344
1345 // If there was no preprocessor block, skip this file.
1346 if (!MacroCursor.getBitStreamReader())
1347 continue;
1348
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001349 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001350 Cursor.JumpToBit((*I)->MacroStartOffset);
1351
1352 RecordData Record;
1353 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001354 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1355
1356 switch (E.Kind) {
1357 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1358 case llvm::BitstreamEntry::Error:
1359 Error("malformed block record in AST file");
1360 return;
1361 case llvm::BitstreamEntry::EndBlock:
1362 goto NextCursor;
1363
1364 case llvm::BitstreamEntry::Record:
Chris Lattner88bde502013-01-19 21:39:22 +00001365 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001366 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001367 default: // Default behavior: ignore.
1368 break;
1369
1370 case PP_MACRO_OBJECT_LIKE:
1371 case PP_MACRO_FUNCTION_LIKE:
1372 getLocalIdentifier(**I, Record[0]);
1373 break;
1374
1375 case PP_TOKEN:
1376 // Ignore tokens.
1377 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001378 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001379 break;
1380 }
1381 }
Chris Lattner88bde502013-01-19 21:39:22 +00001382 NextCursor: ;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001383 }
1384}
1385
1386namespace {
1387 /// \brief Visitor class used to look up identifirs in an AST file.
1388 class IdentifierLookupVisitor {
1389 StringRef Name;
1390 unsigned PriorGeneration;
Douglas Gregore1698072013-01-25 00:38:33 +00001391 unsigned &NumIdentifierLookups;
1392 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001393 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001394
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001395 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001396 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1397 unsigned &NumIdentifierLookups,
1398 unsigned &NumIdentifierLookupHits)
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001399 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregore1698072013-01-25 00:38:33 +00001400 NumIdentifierLookups(NumIdentifierLookups),
1401 NumIdentifierLookupHits(NumIdentifierLookupHits),
1402 Found()
1403 {
1404 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001405
1406 static bool visit(ModuleFile &M, void *UserData) {
1407 IdentifierLookupVisitor *This
1408 = static_cast<IdentifierLookupVisitor *>(UserData);
1409
1410 // If we've already searched this module file, skip it now.
1411 if (M.Generation <= This->PriorGeneration)
1412 return true;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001413
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001414 ASTIdentifierLookupTable *IdTable
1415 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1416 if (!IdTable)
1417 return false;
1418
1419 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1420 M, This->Found);
Douglas Gregore1698072013-01-25 00:38:33 +00001421 ++This->NumIdentifierLookups;
1422 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001423 if (Pos == IdTable->end())
1424 return false;
1425
1426 // Dereferencing the iterator has the effect of building the
1427 // IdentifierInfo node and populating it with the various
1428 // declarations it needs.
Douglas Gregore1698072013-01-25 00:38:33 +00001429 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001430 This->Found = *Pos;
1431 return true;
1432 }
1433
1434 // \brief Retrieve the identifier info found within the module
1435 // files.
1436 IdentifierInfo *getIdentifierInfo() const { return Found; }
1437 };
1438}
1439
1440void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1441 // Note that we are loading an identifier.
1442 Deserializing AnIdentifier(this);
1443
1444 unsigned PriorGeneration = 0;
1445 if (getContext().getLangOpts().Modules)
1446 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregor1a49d972013-01-25 01:03:03 +00001447
1448 // If there is a global index, look there first to determine which modules
1449 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001450 GlobalModuleIndex::HitSet Hits;
1451 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001452 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001453 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1454 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001455 }
1456 }
1457
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001458 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregore1698072013-01-25 00:38:33 +00001459 NumIdentifierLookups,
1460 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001461 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001462 markIdentifierUpToDate(&II);
1463}
1464
1465void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1466 if (!II)
1467 return;
1468
1469 II->setOutOfDate(false);
1470
1471 // Update the generation for this identifier.
1472 if (getContext().getLangOpts().Modules)
1473 IdentifierGeneration[II] = CurrentGeneration;
1474}
1475
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001476void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1477 const PendingMacroInfo &PMInfo) {
1478 assert(II);
1479
1480 if (PMInfo.M->Kind != MK_Module) {
1481 installPCHMacroDirectives(II, *PMInfo.M,
1482 PMInfo.PCHMacroData.MacroDirectivesOffset);
1483 return;
1484 }
1485
1486 // Module Macro.
1487
1488 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1489 SourceLocation ImportLoc =
1490 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1491
1492 assert(GMacID);
1493 // If this macro has already been loaded, don't do so again.
1494 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1495 return;
1496
1497 MacroInfo *MI = getMacro(GMacID);
1498 SubmoduleID SubModID = MI->getOwningModuleID();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001499 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1500 /*isImported=*/true);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001501
1502 // Determine whether this macro definition is visible.
1503 bool Hidden = false;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001504 Module *Owner = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001505 if (SubModID) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001506 if ((Owner = getSubmodule(SubModID))) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001507 if (Owner->NameVisibility == Module::Hidden) {
1508 // The owning module is not visible, and this macro definition
1509 // should not be, either.
1510 Hidden = true;
1511
1512 // Note that this macro definition was hidden because its owning
1513 // module is not yet visible.
1514 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1515 }
1516 }
1517 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001518
1519 if (!Hidden)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001520 installImportedMacro(II, MD, Owner);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001521}
1522
1523void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1524 ModuleFile &M, uint64_t Offset) {
1525 assert(M.Kind != MK_Module);
1526
1527 BitstreamCursor &Cursor = M.MacroCursor;
1528 SavedStreamPosition SavedPosition(Cursor);
1529 Cursor.JumpToBit(Offset);
1530
1531 llvm::BitstreamEntry Entry =
1532 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1533 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1534 Error("malformed block record in AST file");
1535 return;
1536 }
1537
1538 RecordData Record;
1539 PreprocessorRecordTypes RecType =
1540 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1541 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1542 Error("malformed block record in AST file");
1543 return;
1544 }
1545
1546 // Deserialize the macro directives history in reverse source-order.
1547 MacroDirective *Latest = 0, *Earliest = 0;
1548 unsigned Idx = 0, N = Record.size();
1549 while (Idx < N) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001550 MacroDirective *MD = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001551 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001552 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1553 switch (K) {
1554 case MacroDirective::MD_Define: {
1555 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1556 MacroInfo *MI = getMacro(GMacID);
1557 bool isImported = Record[Idx++];
1558 bool isAmbiguous = Record[Idx++];
1559 DefMacroDirective *DefMD =
1560 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1561 DefMD->setAmbiguous(isAmbiguous);
1562 MD = DefMD;
1563 break;
1564 }
1565 case MacroDirective::MD_Undefine:
1566 MD = PP.AllocateUndefMacroDirective(Loc);
1567 break;
1568 case MacroDirective::MD_Visibility: {
1569 bool isPublic = Record[Idx++];
1570 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1571 break;
1572 }
1573 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001574
1575 if (!Latest)
1576 Latest = MD;
1577 if (Earliest)
1578 Earliest->setPrevious(MD);
1579 Earliest = MD;
1580 }
1581
1582 PP.setLoadedMacroDirective(II, Latest);
1583}
1584
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001585/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor7332ae42013-04-12 21:00:54 +00001586/// modules.
1587static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1588 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001589 assert(PrevMI && NewMI);
1590 if (!NewOwner)
1591 return false;
1592 Module *PrevOwner = 0;
1593 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1594 PrevOwner = Reader.getSubmodule(PrevModID);
1595 if (!PrevOwner)
1596 return false;
1597 if (PrevOwner == NewOwner)
1598 return false;
Douglas Gregor7332ae42013-04-12 21:00:54 +00001599 return PrevOwner->IsSystem && NewOwner->IsSystem;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001600}
1601
1602void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1603 Module *Owner) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001604 assert(II && MD);
1605
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001606 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001607 MacroDirective *Prev = PP.getMacroDirective(II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001608 if (Prev) {
1609 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001610 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1611 MacroInfo *NewMI = DefMD->getInfo();
Argyrios Kyrtzidisbd25ff82013-04-03 17:39:30 +00001612 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1613 /*Syntactically=*/true)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001614 // Before marking the macros as ambiguous, check if this is a case where
Douglas Gregor7332ae42013-04-12 21:00:54 +00001615 // both macros are in system headers. If so, we trust that the system
1616 // did not get it wrong. This also handles cases where Clang's own
1617 // headers have a different spelling of certain system macros:
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001618 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1619 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
Douglas Gregor7332ae42013-04-12 21:00:54 +00001620 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001621 PrevDef.getDirective()->setAmbiguous(true);
1622 DefMD->setAmbiguous(true);
1623 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001624 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001625 }
1626
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001627 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001628}
1629
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001630InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001631 // If this ID is bogus, just return an empty input file.
1632 if (ID == 0 || ID > F.InputFilesLoaded.size())
1633 return InputFile();
1634
1635 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001636 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001637 return F.InputFilesLoaded[ID-1];
1638
1639 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001640 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001641 SavedStreamPosition SavedPosition(Cursor);
1642 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1643
1644 unsigned Code = Cursor.ReadCode();
1645 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001646 StringRef Blob;
1647 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001648 case INPUT_FILE: {
1649 unsigned StoredID = Record[0];
1650 assert(ID == StoredID && "Bogus stored ID or offset");
1651 (void)StoredID;
1652 off_t StoredSize = (off_t)Record[1];
1653 time_t StoredTime = (time_t)Record[2];
1654 bool Overridden = (bool)Record[3];
1655
1656 // Get the file entry for this input file.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001657 StringRef OrigFilename = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001658 std::string Filename = OrigFilename;
1659 MaybeAddSystemRootToFilename(F, Filename);
1660 const FileEntry *File
1661 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1662 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1663
1664 // If we didn't find the file, resolve it relative to the
1665 // original directory from which this AST file was created.
1666 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1667 F.OriginalDir != CurrentDir) {
1668 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1669 F.OriginalDir,
1670 CurrentDir);
1671 if (!Resolved.empty())
1672 File = FileMgr.getFile(Resolved);
1673 }
1674
1675 // For an overridden file, create a virtual file with the stored
1676 // size/timestamp.
1677 if (Overridden && File == 0) {
1678 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1679 }
1680
1681 if (File == 0) {
1682 if (Complain) {
1683 std::string ErrorStr = "could not find file '";
1684 ErrorStr += Filename;
1685 ErrorStr += "' referenced by AST file";
1686 Error(ErrorStr.c_str());
1687 }
1688 return InputFile();
1689 }
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001690
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001691 // Check if there was a request to override the contents of the file
1692 // that was part of the precompiled header. Overridding such a file
1693 // can lead to problems when lexing using the source locations from the
1694 // PCH.
1695 SourceManager &SM = getSourceManager();
1696 if (!Overridden && SM.isFileOverridden(File)) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001697 if (Complain)
1698 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001699 // After emitting the diagnostic, recover by disabling the override so
1700 // that the original file will be used.
1701 SM.disableFileContentsOverride(File);
1702 // The FileEntry is a virtual file entry with the size of the contents
1703 // that would override the original contents. Set it to the original's
1704 // size/time.
1705 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1706 StoredSize, StoredTime);
1707 }
1708
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001709 bool IsOutOfDate = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001710
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001711 // For an overridden file, there is nothing to validate.
1712 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001713#if !defined(LLVM_ON_WIN32)
1714 // In our regression testing, the Windows file system seems to
1715 // have inconsistent modification times that sometimes
1716 // erroneously trigger this error-handling path.
1717 || StoredTime != File->getModificationTime()
1718#endif
1719 )) {
Douglas Gregor677e15f2013-03-19 00:28:20 +00001720 if (Complain) {
Argyrios Kyrtzidisf8f373f2013-03-08 20:42:38 +00001721 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregorc1478612013-05-10 22:15:13 +00001722 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1723 Diag(diag::note_module_cache_path)
1724 << PP.getHeaderSearchInfo().getModuleCachePath();
1725 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00001726 }
1727
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001728 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001729 }
1730
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001731 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1732
1733 // Note that we've loaded this input file.
1734 F.InputFilesLoaded[ID-1] = IF;
1735 return IF;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001736 }
1737 }
1738
1739 return InputFile();
1740}
1741
1742const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1743 ModuleFile &M = ModuleMgr.getPrimaryModule();
1744 std::string Filename = filenameStrRef;
1745 MaybeAddSystemRootToFilename(M, Filename);
1746 const FileEntry *File = FileMgr.getFile(Filename);
1747 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1748 M.OriginalDir != CurrentDir) {
1749 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1750 M.OriginalDir,
1751 CurrentDir);
1752 if (!resolved.empty())
1753 File = FileMgr.getFile(resolved);
1754 }
1755
1756 return File;
1757}
1758
1759/// \brief If we are loading a relocatable PCH file, and the filename is
1760/// not an absolute path, add the system root to the beginning of the file
1761/// name.
1762void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1763 std::string &Filename) {
1764 // If this is not a relocatable PCH file, there's nothing to do.
1765 if (!M.RelocatablePCH)
1766 return;
1767
1768 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1769 return;
1770
1771 if (isysroot.empty()) {
1772 // If no system root was given, default to '/'
1773 Filename.insert(Filename.begin(), '/');
1774 return;
1775 }
1776
1777 unsigned Length = isysroot.size();
1778 if (isysroot[Length - 1] != '/')
1779 Filename.insert(Filename.begin(), '/');
1780
1781 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1782}
1783
1784ASTReader::ASTReadResult
1785ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001786 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001787 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001788 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001789
1790 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1791 Error("malformed block record in AST file");
1792 return Failure;
1793 }
1794
1795 // Read all of the records and blocks in the control block.
1796 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001797 while (1) {
1798 llvm::BitstreamEntry Entry = Stream.advance();
1799
1800 switch (Entry.Kind) {
1801 case llvm::BitstreamEntry::Error:
1802 Error("malformed block record in AST file");
1803 return Failure;
1804 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001805 // Validate all of the non-system input files.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001806 if (!DisableValidation) {
1807 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001808 // All user input files reside at the index range [0, Record[1]).
1809 // Record is the one from INPUT_FILE_OFFSETS.
1810 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001811 InputFile IF = getInputFile(F, I+1, Complain);
1812 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001813 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001814 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001815 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001816 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001817
1818 case llvm::BitstreamEntry::SubBlock:
1819 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001820 case INPUT_FILES_BLOCK_ID:
1821 F.InputFilesCursor = Stream;
1822 if (Stream.SkipBlock() || // Skip with the main cursor
1823 // Read the abbreviations
1824 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1825 Error("malformed block record in AST file");
1826 return Failure;
1827 }
1828 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001829
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001830 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001831 if (Stream.SkipBlock()) {
1832 Error("malformed block record in AST file");
1833 return Failure;
1834 }
1835 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001836 }
Chris Lattner88bde502013-01-19 21:39:22 +00001837
1838 case llvm::BitstreamEntry::Record:
1839 // The interesting case.
1840 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001841 }
1842
1843 // Read and process a record.
1844 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001845 StringRef Blob;
1846 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001847 case METADATA: {
1848 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1849 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1850 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1851 : diag::warn_pch_version_too_new);
1852 return VersionMismatch;
1853 }
1854
1855 bool hasErrors = Record[5];
1856 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1857 Diag(diag::err_pch_with_compiler_errors);
1858 return HadErrors;
1859 }
1860
1861 F.RelocatablePCH = Record[4];
1862
1863 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001864 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001865 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1866 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1867 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1868 return VersionMismatch;
1869 }
1870 break;
1871 }
1872
1873 case IMPORTS: {
1874 // Load each of the imported PCH files.
1875 unsigned Idx = 0, N = Record.size();
1876 while (Idx < N) {
1877 // Read information about the AST file.
1878 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1879 // The import location will be the local one for now; we will adjust
1880 // all import locations of module imports after the global source
1881 // location info are setup.
1882 SourceLocation ImportLoc =
1883 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001884 off_t StoredSize = (off_t)Record[Idx++];
1885 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001886 unsigned Length = Record[Idx++];
1887 SmallString<128> ImportedFile(Record.begin() + Idx,
1888 Record.begin() + Idx + Length);
1889 Idx += Length;
1890
1891 // Load the AST file.
1892 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00001893 StoredSize, StoredModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001894 ClientLoadCapabilities)) {
1895 case Failure: return Failure;
1896 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorac39f132013-03-19 00:38:50 +00001897 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001898 case OutOfDate: return OutOfDate;
1899 case VersionMismatch: return VersionMismatch;
1900 case ConfigurationMismatch: return ConfigurationMismatch;
1901 case HadErrors: return HadErrors;
1902 case Success: break;
1903 }
1904 }
1905 break;
1906 }
1907
1908 case LANGUAGE_OPTIONS: {
1909 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1910 if (Listener && &F == *ModuleMgr.begin() &&
1911 ParseLanguageOptions(Record, Complain, *Listener) &&
1912 !DisableValidation)
1913 return ConfigurationMismatch;
1914 break;
1915 }
1916
1917 case TARGET_OPTIONS: {
1918 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1919 if (Listener && &F == *ModuleMgr.begin() &&
1920 ParseTargetOptions(Record, Complain, *Listener) &&
1921 !DisableValidation)
1922 return ConfigurationMismatch;
1923 break;
1924 }
1925
1926 case DIAGNOSTIC_OPTIONS: {
1927 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1928 if (Listener && &F == *ModuleMgr.begin() &&
1929 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1930 !DisableValidation)
1931 return ConfigurationMismatch;
1932 break;
1933 }
1934
1935 case FILE_SYSTEM_OPTIONS: {
1936 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1937 if (Listener && &F == *ModuleMgr.begin() &&
1938 ParseFileSystemOptions(Record, Complain, *Listener) &&
1939 !DisableValidation)
1940 return ConfigurationMismatch;
1941 break;
1942 }
1943
1944 case HEADER_SEARCH_OPTIONS: {
1945 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1946 if (Listener && &F == *ModuleMgr.begin() &&
1947 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1948 !DisableValidation)
1949 return ConfigurationMismatch;
1950 break;
1951 }
1952
1953 case PREPROCESSOR_OPTIONS: {
1954 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1955 if (Listener && &F == *ModuleMgr.begin() &&
1956 ParsePreprocessorOptions(Record, Complain, *Listener,
1957 SuggestedPredefines) &&
1958 !DisableValidation)
1959 return ConfigurationMismatch;
1960 break;
1961 }
1962
1963 case ORIGINAL_FILE:
1964 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001965 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001966 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1967 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1968 break;
1969
1970 case ORIGINAL_FILE_ID:
1971 F.OriginalSourceFileID = FileID::get(Record[0]);
1972 break;
1973
1974 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001975 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001976 break;
1977
1978 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001979 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001980 F.InputFilesLoaded.resize(Record[0]);
1981 break;
1982 }
1983 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001984}
1985
1986bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001987 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001988
1989 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1990 Error("malformed block record in AST file");
1991 return true;
1992 }
1993
1994 // Read all of the records and blocks for the AST file.
1995 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001996 while (1) {
1997 llvm::BitstreamEntry Entry = Stream.advance();
1998
1999 switch (Entry.Kind) {
2000 case llvm::BitstreamEntry::Error:
2001 Error("error at end of module block in AST file");
2002 return true;
2003 case llvm::BitstreamEntry::EndBlock: {
Richard Smith43828672013-04-03 22:49:41 +00002004 // Outside of C++, we do not store a lookup map for the translation unit.
2005 // Instead, mark it as needing a lookup map to be built if this module
2006 // contains any declarations lexically within it (which it always does!).
2007 // This usually has no cost, since we very rarely need the lookup map for
2008 // the translation unit outside C++.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002009 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smith43828672013-04-03 22:49:41 +00002010 if (DC->hasExternalLexicalStorage() &&
2011 !getContext().getLangOpts().CPlusPlus)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002012 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00002013
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002014 return false;
2015 }
Chris Lattner88bde502013-01-19 21:39:22 +00002016 case llvm::BitstreamEntry::SubBlock:
2017 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002018 case DECLTYPES_BLOCK_ID:
2019 // We lazily load the decls block, but we want to set up the
2020 // DeclsCursor cursor to point into it. Clone our current bitcode
2021 // cursor to it, enter the block and read the abbrevs in that block.
2022 // With the main cursor, we just skip over it.
2023 F.DeclsCursor = Stream;
2024 if (Stream.SkipBlock() || // Skip with the main cursor.
2025 // Read the abbrevs.
2026 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2027 Error("malformed block record in AST file");
2028 return true;
2029 }
2030 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002031
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002032 case DECL_UPDATES_BLOCK_ID:
2033 if (Stream.SkipBlock()) {
2034 Error("malformed block record in AST file");
2035 return true;
2036 }
2037 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002038
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002039 case PREPROCESSOR_BLOCK_ID:
2040 F.MacroCursor = Stream;
2041 if (!PP.getExternalSource())
2042 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00002043
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002044 if (Stream.SkipBlock() ||
2045 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2046 Error("malformed block record in AST file");
2047 return true;
2048 }
2049 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2050 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002051
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002052 case PREPROCESSOR_DETAIL_BLOCK_ID:
2053 F.PreprocessorDetailCursor = Stream;
2054 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00002055 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002056 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00002057 Error("malformed preprocessor detail record in AST file");
2058 return true;
2059 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002060 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002061 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2062
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002063 if (!PP.getPreprocessingRecord())
2064 PP.createPreprocessingRecord();
2065 if (!PP.getPreprocessingRecord()->getExternalSource())
2066 PP.getPreprocessingRecord()->SetExternalSource(*this);
2067 break;
2068
2069 case SOURCE_MANAGER_BLOCK_ID:
2070 if (ReadSourceManagerBlock(F))
2071 return true;
2072 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002073
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002074 case SUBMODULE_BLOCK_ID:
2075 if (ReadSubmoduleBlock(F))
2076 return true;
2077 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002078
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002079 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002080 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002081 if (Stream.SkipBlock() ||
2082 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2083 Error("malformed comments block in AST file");
2084 return true;
2085 }
2086 CommentsCursors.push_back(std::make_pair(C, &F));
2087 break;
2088 }
Chris Lattner88bde502013-01-19 21:39:22 +00002089
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002090 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002091 if (Stream.SkipBlock()) {
2092 Error("malformed block record in AST file");
2093 return true;
2094 }
2095 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002096 }
2097 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002098
2099 case llvm::BitstreamEntry::Record:
2100 // The interesting case.
2101 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002102 }
2103
2104 // Read and process a record.
2105 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002106 StringRef Blob;
2107 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002108 default: // Default behavior: ignore.
2109 break;
2110
2111 case TYPE_OFFSET: {
2112 if (F.LocalNumTypes != 0) {
2113 Error("duplicate TYPE_OFFSET record in AST file");
2114 return true;
2115 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002116 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002117 F.LocalNumTypes = Record[0];
2118 unsigned LocalBaseTypeIndex = Record[1];
2119 F.BaseTypeIndex = getTotalNumTypes();
2120
2121 if (F.LocalNumTypes > 0) {
2122 // Introduce the global -> local mapping for types within this module.
2123 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2124
2125 // Introduce the local -> global mapping for types within this module.
2126 F.TypeRemap.insertOrReplace(
2127 std::make_pair(LocalBaseTypeIndex,
2128 F.BaseTypeIndex - LocalBaseTypeIndex));
2129
2130 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2131 }
2132 break;
2133 }
2134
2135 case DECL_OFFSET: {
2136 if (F.LocalNumDecls != 0) {
2137 Error("duplicate DECL_OFFSET record in AST file");
2138 return true;
2139 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002140 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002141 F.LocalNumDecls = Record[0];
2142 unsigned LocalBaseDeclID = Record[1];
2143 F.BaseDeclID = getTotalNumDecls();
2144
2145 if (F.LocalNumDecls > 0) {
2146 // Introduce the global -> local mapping for declarations within this
2147 // module.
2148 GlobalDeclMap.insert(
2149 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2150
2151 // Introduce the local -> global mapping for declarations within this
2152 // module.
2153 F.DeclRemap.insertOrReplace(
2154 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2155
2156 // Introduce the global -> local mapping for declarations within this
2157 // module.
2158 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2159
2160 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2161 }
2162 break;
2163 }
2164
2165 case TU_UPDATE_LEXICAL: {
2166 DeclContext *TU = Context.getTranslationUnitDecl();
2167 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002168 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002169 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002170 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002171 TU->setHasExternalLexicalStorage(true);
2172 break;
2173 }
2174
2175 case UPDATE_VISIBLE: {
2176 unsigned Idx = 0;
2177 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2178 ASTDeclContextNameLookupTable *Table =
2179 ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002180 (const unsigned char *)Blob.data() + Record[Idx++],
2181 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002182 ASTDeclContextNameLookupTrait(*this, F));
2183 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2184 DeclContext *TU = Context.getTranslationUnitDecl();
2185 F.DeclContextInfos[TU].NameLookupTableData = Table;
2186 TU->setHasExternalVisibleStorage(true);
2187 } else
2188 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2189 break;
2190 }
2191
2192 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002193 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002194 if (Record[0]) {
2195 F.IdentifierLookupTable
2196 = ASTIdentifierLookupTable::Create(
2197 (const unsigned char *)F.IdentifierTableData + Record[0],
2198 (const unsigned char *)F.IdentifierTableData,
2199 ASTIdentifierLookupTrait(*this, F));
2200
2201 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2202 }
2203 break;
2204
2205 case IDENTIFIER_OFFSET: {
2206 if (F.LocalNumIdentifiers != 0) {
2207 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2208 return true;
2209 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002210 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002211 F.LocalNumIdentifiers = Record[0];
2212 unsigned LocalBaseIdentifierID = Record[1];
2213 F.BaseIdentifierID = getTotalNumIdentifiers();
2214
2215 if (F.LocalNumIdentifiers > 0) {
2216 // Introduce the global -> local mapping for identifiers within this
2217 // module.
2218 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2219 &F));
2220
2221 // Introduce the local -> global mapping for identifiers within this
2222 // module.
2223 F.IdentifierRemap.insertOrReplace(
2224 std::make_pair(LocalBaseIdentifierID,
2225 F.BaseIdentifierID - LocalBaseIdentifierID));
2226
2227 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2228 + F.LocalNumIdentifiers);
2229 }
2230 break;
2231 }
2232
2233 case EXTERNAL_DEFINITIONS:
2234 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2235 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2236 break;
2237
2238 case SPECIAL_TYPES:
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002239 if (SpecialTypes.empty()) {
2240 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2241 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2242 break;
2243 }
2244
2245 if (SpecialTypes.size() != Record.size()) {
2246 Error("invalid special-types record");
2247 return true;
2248 }
2249
2250 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2251 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2252 if (!SpecialTypes[I])
2253 SpecialTypes[I] = ID;
2254 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2255 // merge step?
2256 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002257 break;
2258
2259 case STATISTICS:
2260 TotalNumStatements += Record[0];
2261 TotalNumMacros += Record[1];
2262 TotalLexicalDeclContexts += Record[2];
2263 TotalVisibleDeclContexts += Record[3];
2264 break;
2265
2266 case UNUSED_FILESCOPED_DECLS:
2267 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2268 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2269 break;
2270
2271 case DELEGATING_CTORS:
2272 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2273 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2274 break;
2275
2276 case WEAK_UNDECLARED_IDENTIFIERS:
2277 if (Record.size() % 4 != 0) {
2278 Error("invalid weak identifiers record");
2279 return true;
2280 }
2281
2282 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2283 // files. This isn't the way to do it :)
2284 WeakUndeclaredIdentifiers.clear();
2285
2286 // Translate the weak, undeclared identifiers into global IDs.
2287 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2288 WeakUndeclaredIdentifiers.push_back(
2289 getGlobalIdentifierID(F, Record[I++]));
2290 WeakUndeclaredIdentifiers.push_back(
2291 getGlobalIdentifierID(F, Record[I++]));
2292 WeakUndeclaredIdentifiers.push_back(
2293 ReadSourceLocation(F, Record, I).getRawEncoding());
2294 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2295 }
2296 break;
2297
Richard Smith5ea6ef42013-01-10 23:43:47 +00002298 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002299 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002300 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002301 break;
2302
2303 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002304 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002305 F.LocalNumSelectors = Record[0];
2306 unsigned LocalBaseSelectorID = Record[1];
2307 F.BaseSelectorID = getTotalNumSelectors();
2308
2309 if (F.LocalNumSelectors > 0) {
2310 // Introduce the global -> local mapping for selectors within this
2311 // module.
2312 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2313
2314 // Introduce the local -> global mapping for selectors within this
2315 // module.
2316 F.SelectorRemap.insertOrReplace(
2317 std::make_pair(LocalBaseSelectorID,
2318 F.BaseSelectorID - LocalBaseSelectorID));
2319
2320 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2321 }
2322 break;
2323 }
2324
2325 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002326 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002327 if (Record[0])
2328 F.SelectorLookupTable
2329 = ASTSelectorLookupTable::Create(
2330 F.SelectorLookupTableData + Record[0],
2331 F.SelectorLookupTableData,
2332 ASTSelectorLookupTrait(*this, F));
2333 TotalNumMethodPoolEntries += Record[1];
2334 break;
2335
2336 case REFERENCED_SELECTOR_POOL:
2337 if (!Record.empty()) {
2338 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2339 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2340 Record[Idx++]));
2341 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2342 getRawEncoding());
2343 }
2344 }
2345 break;
2346
2347 case PP_COUNTER_VALUE:
2348 if (!Record.empty() && Listener)
2349 Listener->ReadCounter(F, Record[0]);
2350 break;
2351
2352 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002353 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002354 F.NumFileSortedDecls = Record[0];
2355 break;
2356
2357 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002358 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002359 F.LocalNumSLocEntries = Record[0];
2360 unsigned SLocSpaceSize = Record[1];
2361 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2362 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2363 SLocSpaceSize);
2364 // Make our entry in the range map. BaseID is negative and growing, so
2365 // we invert it. Because we invert it, though, we need the other end of
2366 // the range.
2367 unsigned RangeStart =
2368 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2369 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2370 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2371
2372 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2373 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2374 GlobalSLocOffsetMap.insert(
2375 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2376 - SLocSpaceSize,&F));
2377
2378 // Initialize the remapping table.
2379 // Invalid stays invalid.
2380 F.SLocRemap.insert(std::make_pair(0U, 0));
2381 // This module. Base was 2 when being compiled.
2382 F.SLocRemap.insert(std::make_pair(2U,
2383 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2384
2385 TotalNumSLocEntries += F.LocalNumSLocEntries;
2386 break;
2387 }
2388
2389 case MODULE_OFFSET_MAP: {
2390 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002391 const unsigned char *Data = (const unsigned char*)Blob.data();
2392 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002393
2394 // Continuous range maps we may be updating in our module.
2395 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2396 ContinuousRangeMap<uint32_t, int, 2>::Builder
2397 IdentifierRemap(F.IdentifierRemap);
2398 ContinuousRangeMap<uint32_t, int, 2>::Builder
2399 MacroRemap(F.MacroRemap);
2400 ContinuousRangeMap<uint32_t, int, 2>::Builder
2401 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2402 ContinuousRangeMap<uint32_t, int, 2>::Builder
2403 SubmoduleRemap(F.SubmoduleRemap);
2404 ContinuousRangeMap<uint32_t, int, 2>::Builder
2405 SelectorRemap(F.SelectorRemap);
2406 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2407 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2408
2409 while(Data < DataEnd) {
2410 uint16_t Len = io::ReadUnalignedLE16(Data);
2411 StringRef Name = StringRef((const char*)Data, Len);
2412 Data += Len;
2413 ModuleFile *OM = ModuleMgr.lookup(Name);
2414 if (!OM) {
2415 Error("SourceLocation remap refers to unknown module");
2416 return true;
2417 }
2418
2419 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2420 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2421 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2422 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2423 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2424 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2425 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2426 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2427
2428 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2429 SLocRemap.insert(std::make_pair(SLocOffset,
2430 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2431 IdentifierRemap.insert(
2432 std::make_pair(IdentifierIDOffset,
2433 OM->BaseIdentifierID - IdentifierIDOffset));
2434 MacroRemap.insert(std::make_pair(MacroIDOffset,
2435 OM->BaseMacroID - MacroIDOffset));
2436 PreprocessedEntityRemap.insert(
2437 std::make_pair(PreprocessedEntityIDOffset,
2438 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2439 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2440 OM->BaseSubmoduleID - SubmoduleIDOffset));
2441 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2442 OM->BaseSelectorID - SelectorIDOffset));
2443 DeclRemap.insert(std::make_pair(DeclIDOffset,
2444 OM->BaseDeclID - DeclIDOffset));
2445
2446 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2447 OM->BaseTypeIndex - TypeIndexOffset));
2448
2449 // Global -> local mappings.
2450 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2451 }
2452 break;
2453 }
2454
2455 case SOURCE_MANAGER_LINE_TABLE:
2456 if (ParseLineTable(F, Record))
2457 return true;
2458 break;
2459
2460 case SOURCE_LOCATION_PRELOADS: {
2461 // Need to transform from the local view (1-based IDs) to the global view,
2462 // which is based off F.SLocEntryBaseID.
2463 if (!F.PreloadSLocEntries.empty()) {
2464 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2465 return true;
2466 }
2467
2468 F.PreloadSLocEntries.swap(Record);
2469 break;
2470 }
2471
2472 case EXT_VECTOR_DECLS:
2473 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2474 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2475 break;
2476
2477 case VTABLE_USES:
2478 if (Record.size() % 3 != 0) {
2479 Error("Invalid VTABLE_USES record");
2480 return true;
2481 }
2482
2483 // Later tables overwrite earlier ones.
2484 // FIXME: Modules will have some trouble with this. This is clearly not
2485 // the right way to do this.
2486 VTableUses.clear();
2487
2488 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2489 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2490 VTableUses.push_back(
2491 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2492 VTableUses.push_back(Record[Idx++]);
2493 }
2494 break;
2495
2496 case DYNAMIC_CLASSES:
2497 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2498 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2499 break;
2500
2501 case PENDING_IMPLICIT_INSTANTIATIONS:
2502 if (PendingInstantiations.size() % 2 != 0) {
2503 Error("Invalid existing PendingInstantiations");
2504 return true;
2505 }
2506
2507 if (Record.size() % 2 != 0) {
2508 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2509 return true;
2510 }
2511
2512 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2513 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2514 PendingInstantiations.push_back(
2515 ReadSourceLocation(F, Record, I).getRawEncoding());
2516 }
2517 break;
2518
2519 case SEMA_DECL_REFS:
2520 // Later tables overwrite earlier ones.
2521 // FIXME: Modules will have some trouble with this.
2522 SemaDeclRefs.clear();
2523 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2524 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2525 break;
2526
2527 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002528 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2529 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2530 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002531
2532 unsigned LocalBasePreprocessedEntityID = Record[0];
2533
2534 unsigned StartingID;
2535 if (!PP.getPreprocessingRecord())
2536 PP.createPreprocessingRecord();
2537 if (!PP.getPreprocessingRecord()->getExternalSource())
2538 PP.getPreprocessingRecord()->SetExternalSource(*this);
2539 StartingID
2540 = PP.getPreprocessingRecord()
2541 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2542 F.BasePreprocessedEntityID = StartingID;
2543
2544 if (F.NumPreprocessedEntities > 0) {
2545 // Introduce the global -> local mapping for preprocessed entities in
2546 // this module.
2547 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2548
2549 // Introduce the local -> global mapping for preprocessed entities in
2550 // this module.
2551 F.PreprocessedEntityRemap.insertOrReplace(
2552 std::make_pair(LocalBasePreprocessedEntityID,
2553 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2554 }
2555
2556 break;
2557 }
2558
2559 case DECL_UPDATE_OFFSETS: {
2560 if (Record.size() % 2 != 0) {
2561 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2562 return true;
2563 }
2564 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2565 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2566 .push_back(std::make_pair(&F, Record[I+1]));
2567 break;
2568 }
2569
2570 case DECL_REPLACEMENTS: {
2571 if (Record.size() % 3 != 0) {
2572 Error("invalid DECL_REPLACEMENTS block in AST file");
2573 return true;
2574 }
2575 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2576 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2577 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2578 break;
2579 }
2580
2581 case OBJC_CATEGORIES_MAP: {
2582 if (F.LocalNumObjCCategoriesInMap != 0) {
2583 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2584 return true;
2585 }
2586
2587 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002588 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002589 break;
2590 }
2591
2592 case OBJC_CATEGORIES:
2593 F.ObjCCategories.swap(Record);
2594 break;
2595
2596 case CXX_BASE_SPECIFIER_OFFSETS: {
2597 if (F.LocalNumCXXBaseSpecifiers != 0) {
2598 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2599 return true;
2600 }
2601
2602 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002603 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002604 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2605 break;
2606 }
2607
2608 case DIAG_PRAGMA_MAPPINGS:
2609 if (F.PragmaDiagMappings.empty())
2610 F.PragmaDiagMappings.swap(Record);
2611 else
2612 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2613 Record.begin(), Record.end());
2614 break;
2615
2616 case CUDA_SPECIAL_DECL_REFS:
2617 // Later tables overwrite earlier ones.
2618 // FIXME: Modules will have trouble with this.
2619 CUDASpecialDeclRefs.clear();
2620 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2621 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2622 break;
2623
2624 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002625 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002626 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002627 if (Record[0]) {
2628 F.HeaderFileInfoTable
2629 = HeaderFileInfoLookupTable::Create(
2630 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2631 (const unsigned char *)F.HeaderFileInfoTableData,
2632 HeaderFileInfoTrait(*this, F,
2633 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002634 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002635
2636 PP.getHeaderSearchInfo().SetExternalSource(this);
2637 if (!PP.getHeaderSearchInfo().getExternalLookup())
2638 PP.getHeaderSearchInfo().SetExternalLookup(this);
2639 }
2640 break;
2641 }
2642
2643 case FP_PRAGMA_OPTIONS:
2644 // Later tables overwrite earlier ones.
2645 FPPragmaOptions.swap(Record);
2646 break;
2647
2648 case OPENCL_EXTENSIONS:
2649 // Later tables overwrite earlier ones.
2650 OpenCLExtensions.swap(Record);
2651 break;
2652
2653 case TENTATIVE_DEFINITIONS:
2654 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2655 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2656 break;
2657
2658 case KNOWN_NAMESPACES:
2659 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2660 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2661 break;
Nick Lewycky01a41142013-01-26 00:35:08 +00002662
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002663 case UNDEFINED_BUT_USED:
2664 if (UndefinedButUsed.size() % 2 != 0) {
2665 Error("Invalid existing UndefinedButUsed");
Nick Lewycky01a41142013-01-26 00:35:08 +00002666 return true;
2667 }
2668
2669 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002670 Error("invalid undefined-but-used record");
Nick Lewycky01a41142013-01-26 00:35:08 +00002671 return true;
2672 }
2673 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002674 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2675 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00002676 ReadSourceLocation(F, Record, I).getRawEncoding());
2677 }
2678 break;
2679
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002680 case IMPORTED_MODULES: {
2681 if (F.Kind != MK_Module) {
2682 // If we aren't loading a module (which has its own exports), make
2683 // all of the imported modules visible.
2684 // FIXME: Deal with macros-only imports.
2685 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2686 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2687 ImportedModules.push_back(GlobalID);
2688 }
2689 }
2690 break;
2691 }
2692
2693 case LOCAL_REDECLARATIONS: {
2694 F.RedeclarationChains.swap(Record);
2695 break;
2696 }
2697
2698 case LOCAL_REDECLARATIONS_MAP: {
2699 if (F.LocalNumRedeclarationsInMap != 0) {
2700 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2701 return true;
2702 }
2703
2704 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002705 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002706 break;
2707 }
2708
2709 case MERGED_DECLARATIONS: {
2710 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2711 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2712 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2713 for (unsigned N = Record[Idx++]; N > 0; --N)
2714 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2715 }
2716 break;
2717 }
2718
2719 case MACRO_OFFSET: {
2720 if (F.LocalNumMacros != 0) {
2721 Error("duplicate MACRO_OFFSET record in AST file");
2722 return true;
2723 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002724 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002725 F.LocalNumMacros = Record[0];
2726 unsigned LocalBaseMacroID = Record[1];
2727 F.BaseMacroID = getTotalNumMacros();
2728
2729 if (F.LocalNumMacros > 0) {
2730 // Introduce the global -> local mapping for macros within this module.
2731 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2732
2733 // Introduce the local -> global mapping for macros within this module.
2734 F.MacroRemap.insertOrReplace(
2735 std::make_pair(LocalBaseMacroID,
2736 F.BaseMacroID - LocalBaseMacroID));
2737
2738 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2739 }
2740 break;
2741 }
2742
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002743 case MACRO_TABLE: {
2744 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002745 break;
2746 }
2747 }
2748 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002749}
2750
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002751/// \brief Move the given method to the back of the global list of methods.
2752static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2753 // Find the entry for this selector in the method pool.
2754 Sema::GlobalMethodPool::iterator Known
2755 = S.MethodPool.find(Method->getSelector());
2756 if (Known == S.MethodPool.end())
2757 return;
2758
2759 // Retrieve the appropriate method list.
2760 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2761 : Known->second.second;
2762 bool Found = false;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002763 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002764 if (!Found) {
2765 if (List->Method == Method) {
2766 Found = true;
2767 } else {
2768 // Keep searching.
2769 continue;
2770 }
2771 }
2772
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002773 if (List->getNext())
2774 List->Method = List->getNext()->Method;
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002775 else
2776 List->Method = Method;
2777 }
2778}
2779
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002780void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002781 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2782 switch (Names[I].getKind()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002783 case HiddenName::Declaration: {
2784 Decl *D = Names[I].getDecl();
2785 bool wasHidden = D->Hidden;
2786 D->Hidden = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002787
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002788 if (wasHidden && SemaObj) {
2789 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2790 moveMethodToBackOfGlobalList(*SemaObj, Method);
2791 }
2792 }
2793 break;
2794 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002795 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002796 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002797 installImportedMacro(Macro.first, Macro.second, Owner);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002798 break;
2799 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002800 }
2801 }
2802}
2803
2804void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00002805 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00002806 SourceLocation ImportLoc,
2807 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002808 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002809 SmallVector<Module *, 4> Stack;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002810 Stack.push_back(Mod);
2811 while (!Stack.empty()) {
2812 Mod = Stack.back();
2813 Stack.pop_back();
2814
2815 if (NameVisibility <= Mod->NameVisibility) {
2816 // This module already has this level of visibility (or greater), so
2817 // there is nothing more to do.
2818 continue;
2819 }
2820
2821 if (!Mod->isAvailable()) {
2822 // Modules that aren't available cannot be made visible.
2823 continue;
2824 }
2825
2826 // Update the module's name visibility.
2827 Mod->NameVisibility = NameVisibility;
2828
2829 // If we've already deserialized any names from this module,
2830 // mark them as visible.
2831 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2832 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002833 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002834 HiddenNamesMap.erase(Hidden);
2835 }
2836
2837 // Push any non-explicit submodules onto the stack to be marked as
2838 // visible.
2839 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2840 SubEnd = Mod->submodule_end();
2841 Sub != SubEnd; ++Sub) {
2842 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2843 Stack.push_back(*Sub);
2844 }
2845
2846 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00002847 SmallVector<Module *, 16> Exports;
2848 Mod->getExportedModules(Exports);
2849 for (SmallVectorImpl<Module *>::iterator
2850 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2851 Module *Exported = *I;
2852 if (Visited.insert(Exported))
2853 Stack.push_back(Exported);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002854 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002855
2856 // Detect any conflicts.
2857 if (Complain) {
2858 assert(ImportLoc.isValid() && "Missing import location");
2859 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2860 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2861 Diag(ImportLoc, diag::warn_module_conflict)
2862 << Mod->getFullModuleName()
2863 << Mod->Conflicts[I].Other->getFullModuleName()
2864 << Mod->Conflicts[I].Message;
2865 // FIXME: Need note where the other module was imported.
2866 }
2867 }
2868 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002869 }
2870}
2871
Douglas Gregor1a49d972013-01-25 01:03:03 +00002872bool ASTReader::loadGlobalIndex() {
2873 if (GlobalIndex)
2874 return false;
2875
2876 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2877 !Context.getLangOpts().Modules)
2878 return true;
2879
2880 // Try to load the global index.
2881 TriedLoadingGlobalIndex = true;
2882 StringRef ModuleCachePath
2883 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2884 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor677e15f2013-03-19 00:28:20 +00002885 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002886 if (!Result.first)
2887 return true;
2888
2889 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002890 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-01-25 01:03:03 +00002891 return false;
2892}
2893
2894bool ASTReader::isGlobalIndexUnavailable() const {
2895 return Context.getLangOpts().Modules && UseGlobalIndex &&
2896 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2897}
2898
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002899ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2900 ModuleKind Type,
2901 SourceLocation ImportLoc,
2902 unsigned ClientLoadCapabilities) {
2903 // Bump the generation number.
2904 unsigned PreviousGeneration = CurrentGeneration++;
2905
2906 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002907 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002908 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2909 /*ImportedBy=*/0, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002910 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002911 ClientLoadCapabilities)) {
2912 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002913 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002914 case OutOfDate:
2915 case VersionMismatch:
2916 case ConfigurationMismatch:
2917 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002918 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2919 Context.getLangOpts().Modules
2920 ? &PP.getHeaderSearchInfo().getModuleMap()
2921 : 0);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002922
2923 // If we find that any modules are unusable, the global index is going
2924 // to be out-of-date. Just remove it.
2925 GlobalIndex.reset();
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002926 ModuleMgr.setGlobalIndex(0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002927 return ReadResult;
2928
2929 case Success:
2930 break;
2931 }
2932
2933 // Here comes stuff that we only do once the entire chain is loaded.
2934
2935 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002936 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2937 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002938 M != MEnd; ++M) {
2939 ModuleFile &F = *M->Mod;
2940
2941 // Read the AST block.
2942 if (ReadASTBlock(F))
2943 return Failure;
2944
2945 // Once read, set the ModuleFile bit base offset and update the size in
2946 // bits of all files we've seen.
2947 F.GlobalBitOffset = TotalModulesSizeInBits;
2948 TotalModulesSizeInBits += F.SizeInBits;
2949 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2950
2951 // Preload SLocEntries.
2952 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2953 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2954 // Load it through the SourceManager and don't call ReadSLocEntry()
2955 // directly because the entry may have already been loaded in which case
2956 // calling ReadSLocEntry() directly would trigger an assertion in
2957 // SourceManager.
2958 SourceMgr.getLoadedSLocEntryByID(Index);
2959 }
2960 }
2961
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002962 // Setup the import locations and notify the module manager that we've
2963 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002964 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2965 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002966 M != MEnd; ++M) {
2967 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002968
2969 ModuleMgr.moduleFileAccepted(&F);
2970
2971 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00002972 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002973 if (!M->ImportedBy)
2974 F.ImportLoc = M->ImportLoc;
2975 else
2976 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2977 M->ImportLoc.getRawEncoding());
2978 }
2979
2980 // Mark all of the identifiers in the identifier table as being out of date,
2981 // so that various accessors know to check the loaded modules when the
2982 // identifier is used.
2983 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2984 IdEnd = PP.getIdentifierTable().end();
2985 Id != IdEnd; ++Id)
2986 Id->second->setOutOfDate(true);
2987
2988 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00002989 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2990 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002991 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2992 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00002993
2994 switch (Unresolved.Kind) {
2995 case UnresolvedModuleRef::Conflict:
2996 if (ResolvedMod) {
2997 Module::Conflict Conflict;
2998 Conflict.Other = ResolvedMod;
2999 Conflict.Message = Unresolved.String.str();
3000 Unresolved.Mod->Conflicts.push_back(Conflict);
3001 }
3002 continue;
3003
3004 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003005 if (ResolvedMod)
3006 Unresolved.Mod->Imports.push_back(ResolvedMod);
3007 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003008
Douglas Gregor906d66a2013-03-20 21:10:35 +00003009 case UnresolvedModuleRef::Export:
3010 if (ResolvedMod || Unresolved.IsWildcard)
3011 Unresolved.Mod->Exports.push_back(
3012 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3013 continue;
3014 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003015 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003016 UnresolvedModuleRefs.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003017
3018 InitializeContext();
3019
3020 if (DeserializationListener)
3021 DeserializationListener->ReaderInitialized(this);
3022
3023 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3024 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3025 PrimaryModule.OriginalSourceFileID
3026 = FileID::get(PrimaryModule.SLocEntryBaseID
3027 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3028
3029 // If this AST file is a precompiled preamble, then set the
3030 // preamble file ID of the source manager to the file source file
3031 // from which the preamble was built.
3032 if (Type == MK_Preamble) {
3033 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3034 } else if (Type == MK_MainFile) {
3035 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3036 }
3037 }
3038
3039 // For any Objective-C class definitions we have already loaded, make sure
3040 // that we load any additional categories.
3041 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3042 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3043 ObjCClassesLoaded[I],
3044 PreviousGeneration);
3045 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00003046
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003047 return Success;
3048}
3049
3050ASTReader::ASTReadResult
3051ASTReader::ReadASTCore(StringRef FileName,
3052 ModuleKind Type,
3053 SourceLocation ImportLoc,
3054 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003055 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003056 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003057 unsigned ClientLoadCapabilities) {
3058 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003059 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003060 ModuleManager::AddModuleResult AddResult
3061 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3062 CurrentGeneration, ExpectedSize, ExpectedModTime,
3063 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003064
Douglas Gregor677e15f2013-03-19 00:28:20 +00003065 switch (AddResult) {
3066 case ModuleManager::AlreadyLoaded:
3067 return Success;
3068
3069 case ModuleManager::NewlyLoaded:
3070 // Load module file below.
3071 break;
3072
3073 case ModuleManager::Missing:
3074 // The module file was missing; if the client handle handle, that, return
3075 // it.
3076 if (ClientLoadCapabilities & ARR_Missing)
3077 return Missing;
3078
3079 // Otherwise, return an error.
3080 {
3081 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3082 + ErrorStr;
3083 Error(Msg);
3084 }
3085 return Failure;
3086
3087 case ModuleManager::OutOfDate:
3088 // We couldn't load the module file because it is out-of-date. If the
3089 // client can handle out-of-date, return it.
3090 if (ClientLoadCapabilities & ARR_OutOfDate)
3091 return OutOfDate;
3092
3093 // Otherwise, return an error.
3094 {
3095 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3096 + ErrorStr;
3097 Error(Msg);
3098 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003099 return Failure;
3100 }
3101
Douglas Gregor677e15f2013-03-19 00:28:20 +00003102 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003103
3104 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3105 // module?
3106 if (FileName != "-") {
3107 CurrentDir = llvm::sys::path::parent_path(FileName);
3108 if (CurrentDir.empty()) CurrentDir = ".";
3109 }
3110
3111 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003112 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003113 Stream.init(F.StreamFile);
3114 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3115
3116 // Sniff for the signature.
3117 if (Stream.Read(8) != 'C' ||
3118 Stream.Read(8) != 'P' ||
3119 Stream.Read(8) != 'C' ||
3120 Stream.Read(8) != 'H') {
3121 Diag(diag::err_not_a_pch_file) << FileName;
3122 return Failure;
3123 }
3124
3125 // This is used for compatibility with older PCH formats.
3126 bool HaveReadControlBlock = false;
3127
Chris Lattner99a5af02013-01-20 00:00:22 +00003128 while (1) {
3129 llvm::BitstreamEntry Entry = Stream.advance();
3130
3131 switch (Entry.Kind) {
3132 case llvm::BitstreamEntry::Error:
3133 case llvm::BitstreamEntry::EndBlock:
3134 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003135 Error("invalid record at top-level of AST file");
3136 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003137
3138 case llvm::BitstreamEntry::SubBlock:
3139 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003140 }
3141
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003142 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003143 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003144 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3145 if (Stream.ReadBlockInfoBlock()) {
3146 Error("malformed BlockInfoBlock in AST file");
3147 return Failure;
3148 }
3149 break;
3150 case CONTROL_BLOCK_ID:
3151 HaveReadControlBlock = true;
3152 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3153 case Success:
3154 break;
3155
3156 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003157 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003158 case OutOfDate: return OutOfDate;
3159 case VersionMismatch: return VersionMismatch;
3160 case ConfigurationMismatch: return ConfigurationMismatch;
3161 case HadErrors: return HadErrors;
3162 }
3163 break;
3164 case AST_BLOCK_ID:
3165 if (!HaveReadControlBlock) {
3166 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3167 Diag(diag::warn_pch_version_too_old);
3168 return VersionMismatch;
3169 }
3170
3171 // Record that we've loaded this module.
3172 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3173 return Success;
3174
3175 default:
3176 if (Stream.SkipBlock()) {
3177 Error("malformed block record in AST file");
3178 return Failure;
3179 }
3180 break;
3181 }
3182 }
3183
3184 return Success;
3185}
3186
3187void ASTReader::InitializeContext() {
3188 // If there's a listener, notify them that we "read" the translation unit.
3189 if (DeserializationListener)
3190 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3191 Context.getTranslationUnitDecl());
3192
3193 // Make sure we load the declaration update records for the translation unit,
3194 // if there are any.
3195 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3196 Context.getTranslationUnitDecl());
3197
3198 // FIXME: Find a better way to deal with collisions between these
3199 // built-in types. Right now, we just ignore the problem.
3200
3201 // Load the special types.
3202 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3203 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3204 if (!Context.CFConstantStringTypeDecl)
3205 Context.setCFConstantStringType(GetType(String));
3206 }
3207
3208 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3209 QualType FileType = GetType(File);
3210 if (FileType.isNull()) {
3211 Error("FILE type is NULL");
3212 return;
3213 }
3214
3215 if (!Context.FILEDecl) {
3216 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3217 Context.setFILEDecl(Typedef->getDecl());
3218 else {
3219 const TagType *Tag = FileType->getAs<TagType>();
3220 if (!Tag) {
3221 Error("Invalid FILE type in AST file");
3222 return;
3223 }
3224 Context.setFILEDecl(Tag->getDecl());
3225 }
3226 }
3227 }
3228
3229 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3230 QualType Jmp_bufType = GetType(Jmp_buf);
3231 if (Jmp_bufType.isNull()) {
3232 Error("jmp_buf type is NULL");
3233 return;
3234 }
3235
3236 if (!Context.jmp_bufDecl) {
3237 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3238 Context.setjmp_bufDecl(Typedef->getDecl());
3239 else {
3240 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3241 if (!Tag) {
3242 Error("Invalid jmp_buf type in AST file");
3243 return;
3244 }
3245 Context.setjmp_bufDecl(Tag->getDecl());
3246 }
3247 }
3248 }
3249
3250 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3251 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3252 if (Sigjmp_bufType.isNull()) {
3253 Error("sigjmp_buf type is NULL");
3254 return;
3255 }
3256
3257 if (!Context.sigjmp_bufDecl) {
3258 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3259 Context.setsigjmp_bufDecl(Typedef->getDecl());
3260 else {
3261 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3262 assert(Tag && "Invalid sigjmp_buf type in AST file");
3263 Context.setsigjmp_bufDecl(Tag->getDecl());
3264 }
3265 }
3266 }
3267
3268 if (unsigned ObjCIdRedef
3269 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3270 if (Context.ObjCIdRedefinitionType.isNull())
3271 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3272 }
3273
3274 if (unsigned ObjCClassRedef
3275 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3276 if (Context.ObjCClassRedefinitionType.isNull())
3277 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3278 }
3279
3280 if (unsigned ObjCSelRedef
3281 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3282 if (Context.ObjCSelRedefinitionType.isNull())
3283 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3284 }
3285
3286 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3287 QualType Ucontext_tType = GetType(Ucontext_t);
3288 if (Ucontext_tType.isNull()) {
3289 Error("ucontext_t type is NULL");
3290 return;
3291 }
3292
3293 if (!Context.ucontext_tDecl) {
3294 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3295 Context.setucontext_tDecl(Typedef->getDecl());
3296 else {
3297 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3298 assert(Tag && "Invalid ucontext_t type in AST file");
3299 Context.setucontext_tDecl(Tag->getDecl());
3300 }
3301 }
3302 }
3303 }
3304
3305 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3306
3307 // If there were any CUDA special declarations, deserialize them.
3308 if (!CUDASpecialDeclRefs.empty()) {
3309 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3310 Context.setcudaConfigureCallDecl(
3311 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3312 }
3313
3314 // Re-export any modules that were imported by a non-module AST file.
3315 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3316 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003317 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003318 /*ImportLoc=*/SourceLocation(),
3319 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003320 }
3321 ImportedModules.clear();
3322}
3323
3324void ASTReader::finalizeForWriting() {
3325 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3326 HiddenEnd = HiddenNamesMap.end();
3327 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003328 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003329 }
3330 HiddenNamesMap.clear();
3331}
3332
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003333/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3334/// cursor into the start of the given block ID, returning false on success and
3335/// true on failure.
3336static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003337 while (1) {
3338 llvm::BitstreamEntry Entry = Cursor.advance();
3339 switch (Entry.Kind) {
3340 case llvm::BitstreamEntry::Error:
3341 case llvm::BitstreamEntry::EndBlock:
3342 return true;
3343
3344 case llvm::BitstreamEntry::Record:
3345 // Ignore top-level records.
3346 Cursor.skipRecord(Entry.ID);
3347 break;
3348
3349 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003350 if (Entry.ID == BlockID) {
3351 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003352 return true;
3353 // Found it!
3354 return false;
3355 }
3356
3357 if (Cursor.SkipBlock())
3358 return true;
3359 }
3360 }
3361}
3362
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003363/// \brief Retrieve the name of the original source file name
3364/// directly from the AST file, without actually loading the AST
3365/// file.
3366std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3367 FileManager &FileMgr,
3368 DiagnosticsEngine &Diags) {
3369 // Open the AST file.
3370 std::string ErrStr;
3371 OwningPtr<llvm::MemoryBuffer> Buffer;
3372 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3373 if (!Buffer) {
3374 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3375 return std::string();
3376 }
3377
3378 // Initialize the stream
3379 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003380 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003381 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3382 (const unsigned char *)Buffer->getBufferEnd());
3383 Stream.init(StreamFile);
3384
3385 // Sniff for the signature.
3386 if (Stream.Read(8) != 'C' ||
3387 Stream.Read(8) != 'P' ||
3388 Stream.Read(8) != 'C' ||
3389 Stream.Read(8) != 'H') {
3390 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3391 return std::string();
3392 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003393
Chris Lattner88bde502013-01-19 21:39:22 +00003394 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003395 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003396 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3397 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003398 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003399
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003400 // Scan for ORIGINAL_FILE inside the control block.
3401 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003402 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003403 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003404 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3405 return std::string();
3406
3407 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3408 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3409 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003410 }
Chris Lattner88bde502013-01-19 21:39:22 +00003411
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003412 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003413 StringRef Blob;
3414 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3415 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003416 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003417}
3418
3419namespace {
3420 class SimplePCHValidator : public ASTReaderListener {
3421 const LangOptions &ExistingLangOpts;
3422 const TargetOptions &ExistingTargetOpts;
3423 const PreprocessorOptions &ExistingPPOpts;
3424 FileManager &FileMgr;
3425
3426 public:
3427 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3428 const TargetOptions &ExistingTargetOpts,
3429 const PreprocessorOptions &ExistingPPOpts,
3430 FileManager &FileMgr)
3431 : ExistingLangOpts(ExistingLangOpts),
3432 ExistingTargetOpts(ExistingTargetOpts),
3433 ExistingPPOpts(ExistingPPOpts),
3434 FileMgr(FileMgr)
3435 {
3436 }
3437
3438 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3439 bool Complain) {
3440 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3441 }
3442 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3443 bool Complain) {
3444 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3445 }
3446 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3447 bool Complain,
3448 std::string &SuggestedPredefines) {
3449 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00003450 SuggestedPredefines, ExistingLangOpts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003451 }
3452 };
3453}
3454
3455bool ASTReader::readASTFileControlBlock(StringRef Filename,
3456 FileManager &FileMgr,
3457 ASTReaderListener &Listener) {
3458 // Open the AST file.
3459 std::string ErrStr;
3460 OwningPtr<llvm::MemoryBuffer> Buffer;
3461 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3462 if (!Buffer) {
3463 return true;
3464 }
3465
3466 // Initialize the stream
3467 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003468 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003469 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3470 (const unsigned char *)Buffer->getBufferEnd());
3471 Stream.init(StreamFile);
3472
3473 // Sniff for the signature.
3474 if (Stream.Read(8) != 'C' ||
3475 Stream.Read(8) != 'P' ||
3476 Stream.Read(8) != 'C' ||
3477 Stream.Read(8) != 'H') {
3478 return true;
3479 }
3480
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003481 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003482 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003483 return true;
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003484
3485 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3486 BitstreamCursor InputFilesCursor;
3487 if (NeedsInputFiles) {
3488 InputFilesCursor = Stream;
3489 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3490 return true;
3491
3492 // Read the abbreviations
3493 while (true) {
3494 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3495 unsigned Code = InputFilesCursor.ReadCode();
3496
3497 // We expect all abbrevs to be at the start of the block.
3498 if (Code != llvm::bitc::DEFINE_ABBREV) {
3499 InputFilesCursor.JumpToBit(Offset);
3500 break;
3501 }
3502 InputFilesCursor.ReadAbbrevRecord();
3503 }
3504 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003505
3506 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003507 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003508 while (1) {
3509 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3510 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3511 return false;
3512
3513 if (Entry.Kind != llvm::BitstreamEntry::Record)
3514 return true;
3515
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003516 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003517 StringRef Blob;
3518 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003519 switch ((ControlRecordTypes)RecCode) {
3520 case METADATA: {
3521 if (Record[0] != VERSION_MAJOR)
3522 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003523
Douglas Gregorc544ba02013-03-27 16:47:18 +00003524 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003525 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00003526
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003527 break;
3528 }
3529 case LANGUAGE_OPTIONS:
3530 if (ParseLanguageOptions(Record, false, Listener))
3531 return true;
3532 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003533
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003534 case TARGET_OPTIONS:
3535 if (ParseTargetOptions(Record, false, Listener))
3536 return true;
3537 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003538
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003539 case DIAGNOSTIC_OPTIONS:
3540 if (ParseDiagnosticOptions(Record, false, Listener))
3541 return true;
3542 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003543
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003544 case FILE_SYSTEM_OPTIONS:
3545 if (ParseFileSystemOptions(Record, false, Listener))
3546 return true;
3547 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003548
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003549 case HEADER_SEARCH_OPTIONS:
3550 if (ParseHeaderSearchOptions(Record, false, Listener))
3551 return true;
3552 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003553
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003554 case PREPROCESSOR_OPTIONS: {
3555 std::string IgnoredSuggestedPredefines;
3556 if (ParsePreprocessorOptions(Record, false, Listener,
3557 IgnoredSuggestedPredefines))
3558 return true;
3559 break;
3560 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003561
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003562 case INPUT_FILE_OFFSETS: {
3563 if (!NeedsInputFiles)
3564 break;
3565
3566 unsigned NumInputFiles = Record[0];
3567 unsigned NumUserFiles = Record[1];
3568 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3569 for (unsigned I = 0; I != NumInputFiles; ++I) {
3570 // Go find this input file.
3571 bool isSystemFile = I >= NumUserFiles;
3572 BitstreamCursor &Cursor = InputFilesCursor;
3573 SavedStreamPosition SavedPosition(Cursor);
3574 Cursor.JumpToBit(InputFileOffs[I]);
3575
3576 unsigned Code = Cursor.ReadCode();
3577 RecordData Record;
3578 StringRef Blob;
3579 bool shouldContinue = false;
3580 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3581 case INPUT_FILE:
3582 shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3583 break;
3584 }
3585 if (!shouldContinue)
3586 break;
3587 }
3588 break;
3589 }
3590
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003591 default:
3592 // No other validation to perform.
3593 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003594 }
3595 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003596}
3597
3598
3599bool ASTReader::isAcceptableASTFile(StringRef Filename,
3600 FileManager &FileMgr,
3601 const LangOptions &LangOpts,
3602 const TargetOptions &TargetOpts,
3603 const PreprocessorOptions &PPOpts) {
3604 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3605 return !readASTFileControlBlock(Filename, FileMgr, validator);
3606}
3607
3608bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3609 // Enter the submodule block.
3610 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3611 Error("malformed submodule block record in AST file");
3612 return true;
3613 }
3614
3615 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3616 bool First = true;
3617 Module *CurrentModule = 0;
3618 RecordData Record;
3619 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003620 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3621
3622 switch (Entry.Kind) {
3623 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3624 case llvm::BitstreamEntry::Error:
3625 Error("malformed block record in AST file");
3626 return true;
3627 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003628 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003629 case llvm::BitstreamEntry::Record:
3630 // The interesting case.
3631 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003632 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003633
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003634 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003635 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003636 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003637 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003638 default: // Default behavior: ignore.
3639 break;
3640
3641 case SUBMODULE_DEFINITION: {
3642 if (First) {
3643 Error("missing submodule metadata record at beginning of block");
3644 return true;
3645 }
3646
Douglas Gregor970e4412013-03-20 03:59:18 +00003647 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003648 Error("malformed module definition");
3649 return true;
3650 }
3651
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003652 StringRef Name = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003653 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3654 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3655 bool IsFramework = Record[2];
3656 bool IsExplicit = Record[3];
3657 bool IsSystem = Record[4];
3658 bool InferSubmodules = Record[5];
3659 bool InferExplicitSubmodules = Record[6];
3660 bool InferExportWildcard = Record[7];
Douglas Gregor970e4412013-03-20 03:59:18 +00003661 bool ConfigMacrosExhaustive = Record[8];
3662
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003663 Module *ParentModule = 0;
3664 if (Parent)
3665 ParentModule = getSubmodule(Parent);
3666
3667 // Retrieve this (sub)module from the module map, creating it if
3668 // necessary.
3669 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3670 IsFramework,
3671 IsExplicit).first;
3672 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3673 if (GlobalIndex >= SubmodulesLoaded.size() ||
3674 SubmodulesLoaded[GlobalIndex]) {
3675 Error("too many submodules");
3676 return true;
3677 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003678
Douglas Gregor677e15f2013-03-19 00:28:20 +00003679 if (!ParentModule) {
3680 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3681 if (CurFile != F.File) {
3682 if (!Diags.isDiagnosticInFlight()) {
3683 Diag(diag::err_module_file_conflict)
3684 << CurrentModule->getTopLevelModuleName()
3685 << CurFile->getName()
3686 << F.File->getName();
3687 }
3688 return true;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003689 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003690 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003691
3692 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003693 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003694
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003695 CurrentModule->IsFromModuleFile = true;
3696 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3697 CurrentModule->InferSubmodules = InferSubmodules;
3698 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3699 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00003700 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003701 if (DeserializationListener)
3702 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3703
3704 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003705
Douglas Gregor906d66a2013-03-20 21:10:35 +00003706 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003707 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00003708 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00003709 CurrentModule->UnresolvedConflicts.clear();
3710 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003711 break;
3712 }
3713
3714 case SUBMODULE_UMBRELLA_HEADER: {
3715 if (First) {
3716 Error("missing submodule metadata record at beginning of block");
3717 return true;
3718 }
3719
3720 if (!CurrentModule)
3721 break;
3722
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003723 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003724 if (!CurrentModule->getUmbrellaHeader())
3725 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3726 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3727 Error("mismatched umbrella headers in submodule");
3728 return true;
3729 }
3730 }
3731 break;
3732 }
3733
3734 case SUBMODULE_HEADER: {
3735 if (First) {
3736 Error("missing submodule metadata record at beginning of block");
3737 return true;
3738 }
3739
3740 if (!CurrentModule)
3741 break;
3742
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003743 // We lazily associate headers with their modules via the HeaderInfoTable.
3744 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3745 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003746 break;
3747 }
3748
3749 case SUBMODULE_EXCLUDED_HEADER: {
3750 if (First) {
3751 Error("missing submodule metadata record at beginning of block");
3752 return true;
3753 }
3754
3755 if (!CurrentModule)
3756 break;
3757
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003758 // We lazily associate headers with their modules via the HeaderInfoTable.
3759 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3760 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003761 break;
3762 }
3763
3764 case SUBMODULE_TOPHEADER: {
3765 if (First) {
3766 Error("missing submodule metadata record at beginning of block");
3767 return true;
3768 }
3769
3770 if (!CurrentModule)
3771 break;
3772
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00003773 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003774 break;
3775 }
3776
3777 case SUBMODULE_UMBRELLA_DIR: {
3778 if (First) {
3779 Error("missing submodule metadata record at beginning of block");
3780 return true;
3781 }
3782
3783 if (!CurrentModule)
3784 break;
3785
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003786 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003787 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003788 if (!CurrentModule->getUmbrellaDir())
3789 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3790 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3791 Error("mismatched umbrella directories in submodule");
3792 return true;
3793 }
3794 }
3795 break;
3796 }
3797
3798 case SUBMODULE_METADATA: {
3799 if (!First) {
3800 Error("submodule metadata record not at beginning of block");
3801 return true;
3802 }
3803 First = false;
3804
3805 F.BaseSubmoduleID = getTotalNumSubmodules();
3806 F.LocalNumSubmodules = Record[0];
3807 unsigned LocalBaseSubmoduleID = Record[1];
3808 if (F.LocalNumSubmodules > 0) {
3809 // Introduce the global -> local mapping for submodules within this
3810 // module.
3811 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3812
3813 // Introduce the local -> global mapping for submodules within this
3814 // module.
3815 F.SubmoduleRemap.insertOrReplace(
3816 std::make_pair(LocalBaseSubmoduleID,
3817 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3818
3819 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3820 }
3821 break;
3822 }
3823
3824 case SUBMODULE_IMPORTS: {
3825 if (First) {
3826 Error("missing submodule metadata record at beginning of block");
3827 return true;
3828 }
3829
3830 if (!CurrentModule)
3831 break;
3832
3833 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003834 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003835 Unresolved.File = &F;
3836 Unresolved.Mod = CurrentModule;
3837 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003838 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003839 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003840 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003841 }
3842 break;
3843 }
3844
3845 case SUBMODULE_EXPORTS: {
3846 if (First) {
3847 Error("missing submodule metadata record at beginning of block");
3848 return true;
3849 }
3850
3851 if (!CurrentModule)
3852 break;
3853
3854 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003855 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003856 Unresolved.File = &F;
3857 Unresolved.Mod = CurrentModule;
3858 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003859 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003860 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003861 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003862 }
3863
3864 // Once we've loaded the set of exports, there's no reason to keep
3865 // the parsed, unresolved exports around.
3866 CurrentModule->UnresolvedExports.clear();
3867 break;
3868 }
3869 case SUBMODULE_REQUIRES: {
3870 if (First) {
3871 Error("missing submodule metadata record at beginning of block");
3872 return true;
3873 }
3874
3875 if (!CurrentModule)
3876 break;
3877
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003878 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003879 Context.getTargetInfo());
3880 break;
3881 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003882
3883 case SUBMODULE_LINK_LIBRARY:
3884 if (First) {
3885 Error("missing submodule metadata record at beginning of block");
3886 return true;
3887 }
3888
3889 if (!CurrentModule)
3890 break;
3891
3892 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003893 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003894 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00003895
3896 case SUBMODULE_CONFIG_MACRO:
3897 if (First) {
3898 Error("missing submodule metadata record at beginning of block");
3899 return true;
3900 }
3901
3902 if (!CurrentModule)
3903 break;
3904
3905 CurrentModule->ConfigMacros.push_back(Blob.str());
3906 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003907
3908 case SUBMODULE_CONFLICT: {
3909 if (First) {
3910 Error("missing submodule metadata record at beginning of block");
3911 return true;
3912 }
3913
3914 if (!CurrentModule)
3915 break;
3916
3917 UnresolvedModuleRef Unresolved;
3918 Unresolved.File = &F;
3919 Unresolved.Mod = CurrentModule;
3920 Unresolved.ID = Record[0];
3921 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3922 Unresolved.IsWildcard = false;
3923 Unresolved.String = Blob;
3924 UnresolvedModuleRefs.push_back(Unresolved);
3925 break;
3926 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003927 }
3928 }
3929}
3930
3931/// \brief Parse the record that corresponds to a LangOptions data
3932/// structure.
3933///
3934/// This routine parses the language options from the AST file and then gives
3935/// them to the AST listener if one is set.
3936///
3937/// \returns true if the listener deems the file unacceptable, false otherwise.
3938bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3939 bool Complain,
3940 ASTReaderListener &Listener) {
3941 LangOptions LangOpts;
3942 unsigned Idx = 0;
3943#define LANGOPT(Name, Bits, Default, Description) \
3944 LangOpts.Name = Record[Idx++];
3945#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3946 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3947#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003948#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3949#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003950
3951 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3952 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3953 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3954
3955 unsigned Length = Record[Idx++];
3956 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3957 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003958
3959 Idx += Length;
3960
3961 // Comment options.
3962 for (unsigned N = Record[Idx++]; N; --N) {
3963 LangOpts.CommentOpts.BlockCommandNames.push_back(
3964 ReadString(Record, Idx));
3965 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00003966 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003967
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003968 return Listener.ReadLanguageOptions(LangOpts, Complain);
3969}
3970
3971bool ASTReader::ParseTargetOptions(const RecordData &Record,
3972 bool Complain,
3973 ASTReaderListener &Listener) {
3974 unsigned Idx = 0;
3975 TargetOptions TargetOpts;
3976 TargetOpts.Triple = ReadString(Record, Idx);
3977 TargetOpts.CPU = ReadString(Record, Idx);
3978 TargetOpts.ABI = ReadString(Record, Idx);
3979 TargetOpts.CXXABI = ReadString(Record, Idx);
3980 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3981 for (unsigned N = Record[Idx++]; N; --N) {
3982 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3983 }
3984 for (unsigned N = Record[Idx++]; N; --N) {
3985 TargetOpts.Features.push_back(ReadString(Record, Idx));
3986 }
3987
3988 return Listener.ReadTargetOptions(TargetOpts, Complain);
3989}
3990
3991bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3992 ASTReaderListener &Listener) {
3993 DiagnosticOptions DiagOpts;
3994 unsigned Idx = 0;
3995#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3996#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3997 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3998#include "clang/Basic/DiagnosticOptions.def"
3999
4000 for (unsigned N = Record[Idx++]; N; --N) {
4001 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4002 }
4003
4004 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4005}
4006
4007bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4008 ASTReaderListener &Listener) {
4009 FileSystemOptions FSOpts;
4010 unsigned Idx = 0;
4011 FSOpts.WorkingDir = ReadString(Record, Idx);
4012 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4013}
4014
4015bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4016 bool Complain,
4017 ASTReaderListener &Listener) {
4018 HeaderSearchOptions HSOpts;
4019 unsigned Idx = 0;
4020 HSOpts.Sysroot = ReadString(Record, Idx);
4021
4022 // Include entries.
4023 for (unsigned N = Record[Idx++]; N; --N) {
4024 std::string Path = ReadString(Record, Idx);
4025 frontend::IncludeDirGroup Group
4026 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004027 bool IsFramework = Record[Idx++];
4028 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004029 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00004030 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004031 }
4032
4033 // System header prefixes.
4034 for (unsigned N = Record[Idx++]; N; --N) {
4035 std::string Prefix = ReadString(Record, Idx);
4036 bool IsSystemHeader = Record[Idx++];
4037 HSOpts.SystemHeaderPrefixes.push_back(
4038 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4039 }
4040
4041 HSOpts.ResourceDir = ReadString(Record, Idx);
4042 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4043 HSOpts.DisableModuleHash = Record[Idx++];
4044 HSOpts.UseBuiltinIncludes = Record[Idx++];
4045 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4046 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4047 HSOpts.UseLibcxx = Record[Idx++];
4048
4049 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4050}
4051
4052bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4053 bool Complain,
4054 ASTReaderListener &Listener,
4055 std::string &SuggestedPredefines) {
4056 PreprocessorOptions PPOpts;
4057 unsigned Idx = 0;
4058
4059 // Macro definitions/undefs
4060 for (unsigned N = Record[Idx++]; N; --N) {
4061 std::string Macro = ReadString(Record, Idx);
4062 bool IsUndef = Record[Idx++];
4063 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4064 }
4065
4066 // Includes
4067 for (unsigned N = Record[Idx++]; N; --N) {
4068 PPOpts.Includes.push_back(ReadString(Record, Idx));
4069 }
4070
4071 // Macro Includes
4072 for (unsigned N = Record[Idx++]; N; --N) {
4073 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4074 }
4075
4076 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004077 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004078 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4079 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4080 PPOpts.ObjCXXARCStandardLibrary =
4081 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4082 SuggestedPredefines.clear();
4083 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4084 SuggestedPredefines);
4085}
4086
4087std::pair<ModuleFile *, unsigned>
4088ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4089 GlobalPreprocessedEntityMapType::iterator
4090 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4091 assert(I != GlobalPreprocessedEntityMap.end() &&
4092 "Corrupted global preprocessed entity map");
4093 ModuleFile *M = I->second;
4094 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4095 return std::make_pair(M, LocalIndex);
4096}
4097
4098std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4099ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4100 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4101 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4102 Mod.NumPreprocessedEntities);
4103
4104 return std::make_pair(PreprocessingRecord::iterator(),
4105 PreprocessingRecord::iterator());
4106}
4107
4108std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4109ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4110 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4111 ModuleDeclIterator(this, &Mod,
4112 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4113}
4114
4115PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4116 PreprocessedEntityID PPID = Index+1;
4117 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4118 ModuleFile &M = *PPInfo.first;
4119 unsigned LocalIndex = PPInfo.second;
4120 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4121
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004122 if (!PP.getPreprocessingRecord()) {
4123 Error("no preprocessing record");
4124 return 0;
4125 }
4126
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004127 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4128 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4129
4130 llvm::BitstreamEntry Entry =
4131 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4132 if (Entry.Kind != llvm::BitstreamEntry::Record)
4133 return 0;
4134
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004135 // Read the record.
4136 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4137 ReadSourceLocation(M, PPOffs.End));
4138 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004139 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004140 RecordData Record;
4141 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004142 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4143 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004144 switch (RecType) {
4145 case PPD_MACRO_EXPANSION: {
4146 bool isBuiltin = Record[0];
4147 IdentifierInfo *Name = 0;
4148 MacroDefinition *Def = 0;
4149 if (isBuiltin)
4150 Name = getLocalIdentifier(M, Record[1]);
4151 else {
4152 PreprocessedEntityID
4153 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4154 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4155 }
4156
4157 MacroExpansion *ME;
4158 if (isBuiltin)
4159 ME = new (PPRec) MacroExpansion(Name, Range);
4160 else
4161 ME = new (PPRec) MacroExpansion(Def, Range);
4162
4163 return ME;
4164 }
4165
4166 case PPD_MACRO_DEFINITION: {
4167 // Decode the identifier info and then check again; if the macro is
4168 // still defined and associated with the identifier,
4169 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4170 MacroDefinition *MD
4171 = new (PPRec) MacroDefinition(II, Range);
4172
4173 if (DeserializationListener)
4174 DeserializationListener->MacroDefinitionRead(PPID, MD);
4175
4176 return MD;
4177 }
4178
4179 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004180 const char *FullFileNameStart = Blob.data() + Record[0];
4181 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004182 const FileEntry *File = 0;
4183 if (!FullFileName.empty())
4184 File = PP.getFileManager().getFile(FullFileName);
4185
4186 // FIXME: Stable encoding
4187 InclusionDirective::InclusionKind Kind
4188 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4189 InclusionDirective *ID
4190 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004191 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004192 Record[1], Record[3],
4193 File,
4194 Range);
4195 return ID;
4196 }
4197 }
4198
4199 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4200}
4201
4202/// \brief \arg SLocMapI points at a chunk of a module that contains no
4203/// preprocessed entities or the entities it contains are not the ones we are
4204/// looking for. Find the next module that contains entities and return the ID
4205/// of the first entry.
4206PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4207 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4208 ++SLocMapI;
4209 for (GlobalSLocOffsetMapType::const_iterator
4210 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4211 ModuleFile &M = *SLocMapI->second;
4212 if (M.NumPreprocessedEntities)
4213 return M.BasePreprocessedEntityID;
4214 }
4215
4216 return getTotalNumPreprocessedEntities();
4217}
4218
4219namespace {
4220
4221template <unsigned PPEntityOffset::*PPLoc>
4222struct PPEntityComp {
4223 const ASTReader &Reader;
4224 ModuleFile &M;
4225
4226 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4227
4228 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4229 SourceLocation LHS = getLoc(L);
4230 SourceLocation RHS = getLoc(R);
4231 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4232 }
4233
4234 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4235 SourceLocation LHS = getLoc(L);
4236 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4237 }
4238
4239 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4240 SourceLocation RHS = getLoc(R);
4241 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4242 }
4243
4244 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4245 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4246 }
4247};
4248
4249}
4250
4251/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4252PreprocessedEntityID
4253ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4254 if (SourceMgr.isLocalSourceLocation(BLoc))
4255 return getTotalNumPreprocessedEntities();
4256
4257 GlobalSLocOffsetMapType::const_iterator
4258 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004259 BLoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004260 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4261 "Corrupted global sloc offset map");
4262
4263 if (SLocMapI->second->NumPreprocessedEntities == 0)
4264 return findNextPreprocessedEntity(SLocMapI);
4265
4266 ModuleFile &M = *SLocMapI->second;
4267 typedef const PPEntityOffset *pp_iterator;
4268 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4269 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4270
4271 size_t Count = M.NumPreprocessedEntities;
4272 size_t Half;
4273 pp_iterator First = pp_begin;
4274 pp_iterator PPI;
4275
4276 // Do a binary search manually instead of using std::lower_bound because
4277 // The end locations of entities may be unordered (when a macro expansion
4278 // is inside another macro argument), but for this case it is not important
4279 // whether we get the first macro expansion or its containing macro.
4280 while (Count > 0) {
4281 Half = Count/2;
4282 PPI = First;
4283 std::advance(PPI, Half);
4284 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4285 BLoc)){
4286 First = PPI;
4287 ++First;
4288 Count = Count - Half - 1;
4289 } else
4290 Count = Half;
4291 }
4292
4293 if (PPI == pp_end)
4294 return findNextPreprocessedEntity(SLocMapI);
4295
4296 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4297}
4298
4299/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4300PreprocessedEntityID
4301ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4302 if (SourceMgr.isLocalSourceLocation(ELoc))
4303 return getTotalNumPreprocessedEntities();
4304
4305 GlobalSLocOffsetMapType::const_iterator
4306 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004307 ELoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004308 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4309 "Corrupted global sloc offset map");
4310
4311 if (SLocMapI->second->NumPreprocessedEntities == 0)
4312 return findNextPreprocessedEntity(SLocMapI);
4313
4314 ModuleFile &M = *SLocMapI->second;
4315 typedef const PPEntityOffset *pp_iterator;
4316 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4317 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4318 pp_iterator PPI =
4319 std::upper_bound(pp_begin, pp_end, ELoc,
4320 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4321
4322 if (PPI == pp_end)
4323 return findNextPreprocessedEntity(SLocMapI);
4324
4325 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4326}
4327
4328/// \brief Returns a pair of [Begin, End) indices of preallocated
4329/// preprocessed entities that \arg Range encompasses.
4330std::pair<unsigned, unsigned>
4331 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4332 if (Range.isInvalid())
4333 return std::make_pair(0,0);
4334 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4335
4336 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4337 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4338 return std::make_pair(BeginID, EndID);
4339}
4340
4341/// \brief Optionally returns true or false if the preallocated preprocessed
4342/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00004343Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004344 FileID FID) {
4345 if (FID.isInvalid())
4346 return false;
4347
4348 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4349 ModuleFile &M = *PPInfo.first;
4350 unsigned LocalIndex = PPInfo.second;
4351 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4352
4353 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4354 if (Loc.isInvalid())
4355 return false;
4356
4357 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4358 return true;
4359 else
4360 return false;
4361}
4362
4363namespace {
4364 /// \brief Visitor used to search for information about a header file.
4365 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004366 const FileEntry *FE;
4367
David Blaikiedc84cd52013-02-20 22:23:23 +00004368 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004369
4370 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004371 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4372 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004373
4374 static bool visit(ModuleFile &M, void *UserData) {
4375 HeaderFileInfoVisitor *This
4376 = static_cast<HeaderFileInfoVisitor *>(UserData);
4377
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004378 HeaderFileInfoLookupTable *Table
4379 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4380 if (!Table)
4381 return false;
4382
4383 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00004384 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004385 if (Pos == Table->end())
4386 return false;
4387
4388 This->HFI = *Pos;
4389 return true;
4390 }
4391
David Blaikiedc84cd52013-02-20 22:23:23 +00004392 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004393 };
4394}
4395
4396HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004397 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004398 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidisf9ba8512013-05-08 23:46:55 +00004399 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004400 return *HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004401
4402 return HeaderFileInfo();
4403}
4404
4405void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4406 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004407 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004408 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4409 ModuleFile &F = *(*I);
4410 unsigned Idx = 0;
4411 DiagStates.clear();
4412 assert(!Diag.DiagStates.empty());
4413 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4414 while (Idx < F.PragmaDiagMappings.size()) {
4415 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4416 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4417 if (DiagStateID != 0) {
4418 Diag.DiagStatePoints.push_back(
4419 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4420 FullSourceLoc(Loc, SourceMgr)));
4421 continue;
4422 }
4423
4424 assert(DiagStateID == 0);
4425 // A new DiagState was created here.
4426 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4427 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4428 DiagStates.push_back(NewState);
4429 Diag.DiagStatePoints.push_back(
4430 DiagnosticsEngine::DiagStatePoint(NewState,
4431 FullSourceLoc(Loc, SourceMgr)));
4432 while (1) {
4433 assert(Idx < F.PragmaDiagMappings.size() &&
4434 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4435 if (Idx >= F.PragmaDiagMappings.size()) {
4436 break; // Something is messed up but at least avoid infinite loop in
4437 // release build.
4438 }
4439 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4440 if (DiagID == (unsigned)-1) {
4441 break; // no more diag/map pairs for this location.
4442 }
4443 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4444 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4445 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4446 }
4447 }
4448 }
4449}
4450
4451/// \brief Get the correct cursor and offset for loading a type.
4452ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4453 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4454 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4455 ModuleFile *M = I->second;
4456 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4457}
4458
4459/// \brief Read and return the type with the given index..
4460///
4461/// The index is the type ID, shifted and minus the number of predefs. This
4462/// routine actually reads the record corresponding to the type at the given
4463/// location. It is a helper routine for GetType, which deals with reading type
4464/// IDs.
4465QualType ASTReader::readTypeRecord(unsigned Index) {
4466 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004467 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004468
4469 // Keep track of where we are in the stream, then jump back there
4470 // after reading this type.
4471 SavedStreamPosition SavedPosition(DeclsCursor);
4472
4473 ReadingKindTracker ReadingKind(Read_Type, *this);
4474
4475 // Note that we are loading a type record.
4476 Deserializing AType(this);
4477
4478 unsigned Idx = 0;
4479 DeclsCursor.JumpToBit(Loc.Offset);
4480 RecordData Record;
4481 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004482 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004483 case TYPE_EXT_QUAL: {
4484 if (Record.size() != 2) {
4485 Error("Incorrect encoding of extended qualifier type");
4486 return QualType();
4487 }
4488 QualType Base = readType(*Loc.F, Record, Idx);
4489 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4490 return Context.getQualifiedType(Base, Quals);
4491 }
4492
4493 case TYPE_COMPLEX: {
4494 if (Record.size() != 1) {
4495 Error("Incorrect encoding of complex type");
4496 return QualType();
4497 }
4498 QualType ElemType = readType(*Loc.F, Record, Idx);
4499 return Context.getComplexType(ElemType);
4500 }
4501
4502 case TYPE_POINTER: {
4503 if (Record.size() != 1) {
4504 Error("Incorrect encoding of pointer type");
4505 return QualType();
4506 }
4507 QualType PointeeType = readType(*Loc.F, Record, Idx);
4508 return Context.getPointerType(PointeeType);
4509 }
4510
4511 case TYPE_BLOCK_POINTER: {
4512 if (Record.size() != 1) {
4513 Error("Incorrect encoding of block pointer type");
4514 return QualType();
4515 }
4516 QualType PointeeType = readType(*Loc.F, Record, Idx);
4517 return Context.getBlockPointerType(PointeeType);
4518 }
4519
4520 case TYPE_LVALUE_REFERENCE: {
4521 if (Record.size() != 2) {
4522 Error("Incorrect encoding of lvalue reference type");
4523 return QualType();
4524 }
4525 QualType PointeeType = readType(*Loc.F, Record, Idx);
4526 return Context.getLValueReferenceType(PointeeType, Record[1]);
4527 }
4528
4529 case TYPE_RVALUE_REFERENCE: {
4530 if (Record.size() != 1) {
4531 Error("Incorrect encoding of rvalue reference type");
4532 return QualType();
4533 }
4534 QualType PointeeType = readType(*Loc.F, Record, Idx);
4535 return Context.getRValueReferenceType(PointeeType);
4536 }
4537
4538 case TYPE_MEMBER_POINTER: {
4539 if (Record.size() != 2) {
4540 Error("Incorrect encoding of member pointer type");
4541 return QualType();
4542 }
4543 QualType PointeeType = readType(*Loc.F, Record, Idx);
4544 QualType ClassType = readType(*Loc.F, Record, Idx);
4545 if (PointeeType.isNull() || ClassType.isNull())
4546 return QualType();
4547
4548 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4549 }
4550
4551 case TYPE_CONSTANT_ARRAY: {
4552 QualType ElementType = readType(*Loc.F, Record, Idx);
4553 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4554 unsigned IndexTypeQuals = Record[2];
4555 unsigned Idx = 3;
4556 llvm::APInt Size = ReadAPInt(Record, Idx);
4557 return Context.getConstantArrayType(ElementType, Size,
4558 ASM, IndexTypeQuals);
4559 }
4560
4561 case TYPE_INCOMPLETE_ARRAY: {
4562 QualType ElementType = readType(*Loc.F, Record, Idx);
4563 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4564 unsigned IndexTypeQuals = Record[2];
4565 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4566 }
4567
4568 case TYPE_VARIABLE_ARRAY: {
4569 QualType ElementType = readType(*Loc.F, Record, Idx);
4570 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4571 unsigned IndexTypeQuals = Record[2];
4572 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4573 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4574 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4575 ASM, IndexTypeQuals,
4576 SourceRange(LBLoc, RBLoc));
4577 }
4578
4579 case TYPE_VECTOR: {
4580 if (Record.size() != 3) {
4581 Error("incorrect encoding of vector type in AST file");
4582 return QualType();
4583 }
4584
4585 QualType ElementType = readType(*Loc.F, Record, Idx);
4586 unsigned NumElements = Record[1];
4587 unsigned VecKind = Record[2];
4588 return Context.getVectorType(ElementType, NumElements,
4589 (VectorType::VectorKind)VecKind);
4590 }
4591
4592 case TYPE_EXT_VECTOR: {
4593 if (Record.size() != 3) {
4594 Error("incorrect encoding of extended vector type in AST file");
4595 return QualType();
4596 }
4597
4598 QualType ElementType = readType(*Loc.F, Record, Idx);
4599 unsigned NumElements = Record[1];
4600 return Context.getExtVectorType(ElementType, NumElements);
4601 }
4602
4603 case TYPE_FUNCTION_NO_PROTO: {
4604 if (Record.size() != 6) {
4605 Error("incorrect encoding of no-proto function type");
4606 return QualType();
4607 }
4608 QualType ResultType = readType(*Loc.F, Record, Idx);
4609 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4610 (CallingConv)Record[4], Record[5]);
4611 return Context.getFunctionNoProtoType(ResultType, Info);
4612 }
4613
4614 case TYPE_FUNCTION_PROTO: {
4615 QualType ResultType = readType(*Loc.F, Record, Idx);
4616
4617 FunctionProtoType::ExtProtoInfo EPI;
4618 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4619 /*hasregparm*/ Record[2],
4620 /*regparm*/ Record[3],
4621 static_cast<CallingConv>(Record[4]),
4622 /*produces*/ Record[5]);
4623
4624 unsigned Idx = 6;
4625 unsigned NumParams = Record[Idx++];
4626 SmallVector<QualType, 16> ParamTypes;
4627 for (unsigned I = 0; I != NumParams; ++I)
4628 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4629
4630 EPI.Variadic = Record[Idx++];
4631 EPI.HasTrailingReturn = Record[Idx++];
4632 EPI.TypeQuals = Record[Idx++];
4633 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4634 ExceptionSpecificationType EST =
4635 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4636 EPI.ExceptionSpecType = EST;
4637 SmallVector<QualType, 2> Exceptions;
4638 if (EST == EST_Dynamic) {
4639 EPI.NumExceptions = Record[Idx++];
4640 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4641 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4642 EPI.Exceptions = Exceptions.data();
4643 } else if (EST == EST_ComputedNoexcept) {
4644 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4645 } else if (EST == EST_Uninstantiated) {
4646 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4647 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4648 } else if (EST == EST_Unevaluated) {
4649 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4650 }
Jordan Rosebea522f2013-03-08 21:51:21 +00004651 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004652 }
4653
4654 case TYPE_UNRESOLVED_USING: {
4655 unsigned Idx = 0;
4656 return Context.getTypeDeclType(
4657 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4658 }
4659
4660 case TYPE_TYPEDEF: {
4661 if (Record.size() != 2) {
4662 Error("incorrect encoding of typedef type");
4663 return QualType();
4664 }
4665 unsigned Idx = 0;
4666 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4667 QualType Canonical = readType(*Loc.F, Record, Idx);
4668 if (!Canonical.isNull())
4669 Canonical = Context.getCanonicalType(Canonical);
4670 return Context.getTypedefType(Decl, Canonical);
4671 }
4672
4673 case TYPE_TYPEOF_EXPR:
4674 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4675
4676 case TYPE_TYPEOF: {
4677 if (Record.size() != 1) {
4678 Error("incorrect encoding of typeof(type) in AST file");
4679 return QualType();
4680 }
4681 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4682 return Context.getTypeOfType(UnderlyingType);
4683 }
4684
4685 case TYPE_DECLTYPE: {
4686 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4687 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4688 }
4689
4690 case TYPE_UNARY_TRANSFORM: {
4691 QualType BaseType = readType(*Loc.F, Record, Idx);
4692 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4693 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4694 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4695 }
4696
Richard Smitha2c36462013-04-26 16:15:35 +00004697 case TYPE_AUTO: {
4698 QualType Deduced = readType(*Loc.F, Record, Idx);
4699 bool IsDecltypeAuto = Record[Idx++];
Richard Smithdc7a4f52013-04-30 13:56:41 +00004700 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
4701 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smitha2c36462013-04-26 16:15:35 +00004702 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004703
4704 case TYPE_RECORD: {
4705 if (Record.size() != 2) {
4706 Error("incorrect encoding of record type");
4707 return QualType();
4708 }
4709 unsigned Idx = 0;
4710 bool IsDependent = Record[Idx++];
4711 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4712 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4713 QualType T = Context.getRecordType(RD);
4714 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4715 return T;
4716 }
4717
4718 case TYPE_ENUM: {
4719 if (Record.size() != 2) {
4720 Error("incorrect encoding of enum type");
4721 return QualType();
4722 }
4723 unsigned Idx = 0;
4724 bool IsDependent = Record[Idx++];
4725 QualType T
4726 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4727 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4728 return T;
4729 }
4730
4731 case TYPE_ATTRIBUTED: {
4732 if (Record.size() != 3) {
4733 Error("incorrect encoding of attributed type");
4734 return QualType();
4735 }
4736 QualType modifiedType = readType(*Loc.F, Record, Idx);
4737 QualType equivalentType = readType(*Loc.F, Record, Idx);
4738 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4739 return Context.getAttributedType(kind, modifiedType, equivalentType);
4740 }
4741
4742 case TYPE_PAREN: {
4743 if (Record.size() != 1) {
4744 Error("incorrect encoding of paren type");
4745 return QualType();
4746 }
4747 QualType InnerType = readType(*Loc.F, Record, Idx);
4748 return Context.getParenType(InnerType);
4749 }
4750
4751 case TYPE_PACK_EXPANSION: {
4752 if (Record.size() != 2) {
4753 Error("incorrect encoding of pack expansion type");
4754 return QualType();
4755 }
4756 QualType Pattern = readType(*Loc.F, Record, Idx);
4757 if (Pattern.isNull())
4758 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00004759 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004760 if (Record[1])
4761 NumExpansions = Record[1] - 1;
4762 return Context.getPackExpansionType(Pattern, NumExpansions);
4763 }
4764
4765 case TYPE_ELABORATED: {
4766 unsigned Idx = 0;
4767 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4768 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4769 QualType NamedType = readType(*Loc.F, Record, Idx);
4770 return Context.getElaboratedType(Keyword, NNS, NamedType);
4771 }
4772
4773 case TYPE_OBJC_INTERFACE: {
4774 unsigned Idx = 0;
4775 ObjCInterfaceDecl *ItfD
4776 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4777 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4778 }
4779
4780 case TYPE_OBJC_OBJECT: {
4781 unsigned Idx = 0;
4782 QualType Base = readType(*Loc.F, Record, Idx);
4783 unsigned NumProtos = Record[Idx++];
4784 SmallVector<ObjCProtocolDecl*, 4> Protos;
4785 for (unsigned I = 0; I != NumProtos; ++I)
4786 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4787 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4788 }
4789
4790 case TYPE_OBJC_OBJECT_POINTER: {
4791 unsigned Idx = 0;
4792 QualType Pointee = readType(*Loc.F, Record, Idx);
4793 return Context.getObjCObjectPointerType(Pointee);
4794 }
4795
4796 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4797 unsigned Idx = 0;
4798 QualType Parm = readType(*Loc.F, Record, Idx);
4799 QualType Replacement = readType(*Loc.F, Record, Idx);
4800 return
4801 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4802 Replacement);
4803 }
4804
4805 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4806 unsigned Idx = 0;
4807 QualType Parm = readType(*Loc.F, Record, Idx);
4808 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4809 return Context.getSubstTemplateTypeParmPackType(
4810 cast<TemplateTypeParmType>(Parm),
4811 ArgPack);
4812 }
4813
4814 case TYPE_INJECTED_CLASS_NAME: {
4815 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4816 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4817 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4818 // for AST reading, too much interdependencies.
4819 return
4820 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4821 }
4822
4823 case TYPE_TEMPLATE_TYPE_PARM: {
4824 unsigned Idx = 0;
4825 unsigned Depth = Record[Idx++];
4826 unsigned Index = Record[Idx++];
4827 bool Pack = Record[Idx++];
4828 TemplateTypeParmDecl *D
4829 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4830 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4831 }
4832
4833 case TYPE_DEPENDENT_NAME: {
4834 unsigned Idx = 0;
4835 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4836 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4837 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4838 QualType Canon = readType(*Loc.F, Record, Idx);
4839 if (!Canon.isNull())
4840 Canon = Context.getCanonicalType(Canon);
4841 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4842 }
4843
4844 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4845 unsigned Idx = 0;
4846 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4847 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4848 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4849 unsigned NumArgs = Record[Idx++];
4850 SmallVector<TemplateArgument, 8> Args;
4851 Args.reserve(NumArgs);
4852 while (NumArgs--)
4853 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4854 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4855 Args.size(), Args.data());
4856 }
4857
4858 case TYPE_DEPENDENT_SIZED_ARRAY: {
4859 unsigned Idx = 0;
4860
4861 // ArrayType
4862 QualType ElementType = readType(*Loc.F, Record, Idx);
4863 ArrayType::ArraySizeModifier ASM
4864 = (ArrayType::ArraySizeModifier)Record[Idx++];
4865 unsigned IndexTypeQuals = Record[Idx++];
4866
4867 // DependentSizedArrayType
4868 Expr *NumElts = ReadExpr(*Loc.F);
4869 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4870
4871 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4872 IndexTypeQuals, Brackets);
4873 }
4874
4875 case TYPE_TEMPLATE_SPECIALIZATION: {
4876 unsigned Idx = 0;
4877 bool IsDependent = Record[Idx++];
4878 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4879 SmallVector<TemplateArgument, 8> Args;
4880 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4881 QualType Underlying = readType(*Loc.F, Record, Idx);
4882 QualType T;
4883 if (Underlying.isNull())
4884 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4885 Args.size());
4886 else
4887 T = Context.getTemplateSpecializationType(Name, Args.data(),
4888 Args.size(), Underlying);
4889 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4890 return T;
4891 }
4892
4893 case TYPE_ATOMIC: {
4894 if (Record.size() != 1) {
4895 Error("Incorrect encoding of atomic type");
4896 return QualType();
4897 }
4898 QualType ValueType = readType(*Loc.F, Record, Idx);
4899 return Context.getAtomicType(ValueType);
4900 }
4901 }
4902 llvm_unreachable("Invalid TypeCode!");
4903}
4904
4905class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4906 ASTReader &Reader;
4907 ModuleFile &F;
4908 const ASTReader::RecordData &Record;
4909 unsigned &Idx;
4910
4911 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4912 unsigned &I) {
4913 return Reader.ReadSourceLocation(F, R, I);
4914 }
4915
4916 template<typename T>
4917 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4918 return Reader.ReadDeclAs<T>(F, Record, Idx);
4919 }
4920
4921public:
4922 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4923 const ASTReader::RecordData &Record, unsigned &Idx)
4924 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4925 { }
4926
4927 // We want compile-time assurance that we've enumerated all of
4928 // these, so unfortunately we have to declare them first, then
4929 // define them out-of-line.
4930#define ABSTRACT_TYPELOC(CLASS, PARENT)
4931#define TYPELOC(CLASS, PARENT) \
4932 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4933#include "clang/AST/TypeLocNodes.def"
4934
4935 void VisitFunctionTypeLoc(FunctionTypeLoc);
4936 void VisitArrayTypeLoc(ArrayTypeLoc);
4937};
4938
4939void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4940 // nothing to do
4941}
4942void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4943 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4944 if (TL.needsExtraLocalData()) {
4945 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4946 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4947 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4948 TL.setModeAttr(Record[Idx++]);
4949 }
4950}
4951void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4952 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4953}
4954void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4955 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4956}
4957void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4958 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4959}
4960void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4961 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4962}
4963void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4964 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4965}
4966void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4967 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4968 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4969}
4970void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4971 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4972 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4973 if (Record[Idx++])
4974 TL.setSizeExpr(Reader.ReadExpr(F));
4975 else
4976 TL.setSizeExpr(0);
4977}
4978void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4979 VisitArrayTypeLoc(TL);
4980}
4981void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4982 VisitArrayTypeLoc(TL);
4983}
4984void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4985 VisitArrayTypeLoc(TL);
4986}
4987void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4988 DependentSizedArrayTypeLoc TL) {
4989 VisitArrayTypeLoc(TL);
4990}
4991void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4992 DependentSizedExtVectorTypeLoc TL) {
4993 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4994}
4995void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4996 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4997}
4998void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4999 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5000}
5001void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5002 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5003 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5004 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5005 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5006 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
5007 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5008 }
5009}
5010void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5011 VisitFunctionTypeLoc(TL);
5012}
5013void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5014 VisitFunctionTypeLoc(TL);
5015}
5016void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5017 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5018}
5019void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5020 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5021}
5022void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5023 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5024 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5025 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5026}
5027void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5028 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5029 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5030 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5031 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5032}
5033void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5034 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5035}
5036void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5037 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5038 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5039 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5040 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5041}
5042void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5043 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5044}
5045void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5046 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5047}
5048void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5049 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5050}
5051void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5052 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5053 if (TL.hasAttrOperand()) {
5054 SourceRange range;
5055 range.setBegin(ReadSourceLocation(Record, Idx));
5056 range.setEnd(ReadSourceLocation(Record, Idx));
5057 TL.setAttrOperandParensRange(range);
5058 }
5059 if (TL.hasAttrExprOperand()) {
5060 if (Record[Idx++])
5061 TL.setAttrExprOperand(Reader.ReadExpr(F));
5062 else
5063 TL.setAttrExprOperand(0);
5064 } else if (TL.hasAttrEnumOperand())
5065 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5066}
5067void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5068 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5069}
5070void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5071 SubstTemplateTypeParmTypeLoc TL) {
5072 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5073}
5074void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5075 SubstTemplateTypeParmPackTypeLoc TL) {
5076 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5077}
5078void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5079 TemplateSpecializationTypeLoc TL) {
5080 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5081 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5082 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5083 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5084 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5085 TL.setArgLocInfo(i,
5086 Reader.GetTemplateArgumentLocInfo(F,
5087 TL.getTypePtr()->getArg(i).getKind(),
5088 Record, Idx));
5089}
5090void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5091 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5092 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5093}
5094void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5095 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5096 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5097}
5098void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5099 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5100}
5101void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5102 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5103 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5104 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5105}
5106void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5107 DependentTemplateSpecializationTypeLoc TL) {
5108 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5109 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5110 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5111 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5112 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5113 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5114 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5115 TL.setArgLocInfo(I,
5116 Reader.GetTemplateArgumentLocInfo(F,
5117 TL.getTypePtr()->getArg(I).getKind(),
5118 Record, Idx));
5119}
5120void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5121 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5122}
5123void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5124 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5125}
5126void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5127 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5128 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5129 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5130 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5131 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5132}
5133void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5134 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5135}
5136void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5137 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5138 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5139 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5140}
5141
5142TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5143 const RecordData &Record,
5144 unsigned &Idx) {
5145 QualType InfoTy = readType(F, Record, Idx);
5146 if (InfoTy.isNull())
5147 return 0;
5148
5149 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5150 TypeLocReader TLR(*this, F, Record, Idx);
5151 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5152 TLR.Visit(TL);
5153 return TInfo;
5154}
5155
5156QualType ASTReader::GetType(TypeID ID) {
5157 unsigned FastQuals = ID & Qualifiers::FastMask;
5158 unsigned Index = ID >> Qualifiers::FastWidth;
5159
5160 if (Index < NUM_PREDEF_TYPE_IDS) {
5161 QualType T;
5162 switch ((PredefinedTypeIDs)Index) {
5163 case PREDEF_TYPE_NULL_ID: return QualType();
5164 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5165 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5166
5167 case PREDEF_TYPE_CHAR_U_ID:
5168 case PREDEF_TYPE_CHAR_S_ID:
5169 // FIXME: Check that the signedness of CharTy is correct!
5170 T = Context.CharTy;
5171 break;
5172
5173 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5174 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5175 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5176 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5177 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5178 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5179 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5180 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5181 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5182 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5183 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5184 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5185 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5186 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5187 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5188 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5189 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5190 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5191 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5192 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5193 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5194 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5195 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5196 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5197 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5198 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5199 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5200 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005201 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5202 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5203 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5204 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5205 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5206 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005207 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005208 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005209 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5210
5211 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5212 T = Context.getAutoRRefDeductType();
5213 break;
5214
5215 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5216 T = Context.ARCUnbridgedCastTy;
5217 break;
5218
5219 case PREDEF_TYPE_VA_LIST_TAG:
5220 T = Context.getVaListTagType();
5221 break;
5222
5223 case PREDEF_TYPE_BUILTIN_FN:
5224 T = Context.BuiltinFnTy;
5225 break;
5226 }
5227
5228 assert(!T.isNull() && "Unknown predefined type");
5229 return T.withFastQualifiers(FastQuals);
5230 }
5231
5232 Index -= NUM_PREDEF_TYPE_IDS;
5233 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5234 if (TypesLoaded[Index].isNull()) {
5235 TypesLoaded[Index] = readTypeRecord(Index);
5236 if (TypesLoaded[Index].isNull())
5237 return QualType();
5238
5239 TypesLoaded[Index]->setFromAST();
5240 if (DeserializationListener)
5241 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5242 TypesLoaded[Index]);
5243 }
5244
5245 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5246}
5247
5248QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5249 return GetType(getGlobalTypeID(F, LocalID));
5250}
5251
5252serialization::TypeID
5253ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5254 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5255 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5256
5257 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5258 return LocalID;
5259
5260 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5261 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5262 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5263
5264 unsigned GlobalIndex = LocalIndex + I->second;
5265 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5266}
5267
5268TemplateArgumentLocInfo
5269ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5270 TemplateArgument::ArgKind Kind,
5271 const RecordData &Record,
5272 unsigned &Index) {
5273 switch (Kind) {
5274 case TemplateArgument::Expression:
5275 return ReadExpr(F);
5276 case TemplateArgument::Type:
5277 return GetTypeSourceInfo(F, Record, Index);
5278 case TemplateArgument::Template: {
5279 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5280 Index);
5281 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5282 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5283 SourceLocation());
5284 }
5285 case TemplateArgument::TemplateExpansion: {
5286 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5287 Index);
5288 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5289 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5290 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5291 EllipsisLoc);
5292 }
5293 case TemplateArgument::Null:
5294 case TemplateArgument::Integral:
5295 case TemplateArgument::Declaration:
5296 case TemplateArgument::NullPtr:
5297 case TemplateArgument::Pack:
5298 // FIXME: Is this right?
5299 return TemplateArgumentLocInfo();
5300 }
5301 llvm_unreachable("unexpected template argument loc");
5302}
5303
5304TemplateArgumentLoc
5305ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5306 const RecordData &Record, unsigned &Index) {
5307 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5308
5309 if (Arg.getKind() == TemplateArgument::Expression) {
5310 if (Record[Index++]) // bool InfoHasSameExpr.
5311 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5312 }
5313 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5314 Record, Index));
5315}
5316
5317Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5318 return GetDecl(ID);
5319}
5320
5321uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5322 unsigned &Idx){
5323 if (Idx >= Record.size())
5324 return 0;
5325
5326 unsigned LocalID = Record[Idx++];
5327 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5328}
5329
5330CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5331 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005332 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005333 SavedStreamPosition SavedPosition(Cursor);
5334 Cursor.JumpToBit(Loc.Offset);
5335 ReadingKindTracker ReadingKind(Read_Decl, *this);
5336 RecordData Record;
5337 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005338 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005339 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5340 Error("Malformed AST file: missing C++ base specifiers");
5341 return 0;
5342 }
5343
5344 unsigned Idx = 0;
5345 unsigned NumBases = Record[Idx++];
5346 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5347 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5348 for (unsigned I = 0; I != NumBases; ++I)
5349 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5350 return Bases;
5351}
5352
5353serialization::DeclID
5354ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5355 if (LocalID < NUM_PREDEF_DECL_IDS)
5356 return LocalID;
5357
5358 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5359 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5360 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5361
5362 return LocalID + I->second;
5363}
5364
5365bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5366 ModuleFile &M) const {
5367 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5368 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5369 return &M == I->second;
5370}
5371
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005372ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005373 if (!D->isFromASTFile())
5374 return 0;
5375 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5376 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5377 return I->second;
5378}
5379
5380SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5381 if (ID < NUM_PREDEF_DECL_IDS)
5382 return SourceLocation();
5383
5384 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5385
5386 if (Index > DeclsLoaded.size()) {
5387 Error("declaration ID out-of-range for AST file");
5388 return SourceLocation();
5389 }
5390
5391 if (Decl *D = DeclsLoaded[Index])
5392 return D->getLocation();
5393
5394 unsigned RawLocation = 0;
5395 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5396 return ReadSourceLocation(*Rec.F, RawLocation);
5397}
5398
5399Decl *ASTReader::GetDecl(DeclID ID) {
5400 if (ID < NUM_PREDEF_DECL_IDS) {
5401 switch ((PredefinedDeclIDs)ID) {
5402 case PREDEF_DECL_NULL_ID:
5403 return 0;
5404
5405 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5406 return Context.getTranslationUnitDecl();
5407
5408 case PREDEF_DECL_OBJC_ID_ID:
5409 return Context.getObjCIdDecl();
5410
5411 case PREDEF_DECL_OBJC_SEL_ID:
5412 return Context.getObjCSelDecl();
5413
5414 case PREDEF_DECL_OBJC_CLASS_ID:
5415 return Context.getObjCClassDecl();
5416
5417 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5418 return Context.getObjCProtocolDecl();
5419
5420 case PREDEF_DECL_INT_128_ID:
5421 return Context.getInt128Decl();
5422
5423 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5424 return Context.getUInt128Decl();
5425
5426 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5427 return Context.getObjCInstanceTypeDecl();
5428
5429 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5430 return Context.getBuiltinVaListDecl();
5431 }
5432 }
5433
5434 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5435
5436 if (Index >= DeclsLoaded.size()) {
5437 assert(0 && "declaration ID out-of-range for AST file");
5438 Error("declaration ID out-of-range for AST file");
5439 return 0;
5440 }
5441
5442 if (!DeclsLoaded[Index]) {
5443 ReadDeclRecord(ID);
5444 if (DeserializationListener)
5445 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5446 }
5447
5448 return DeclsLoaded[Index];
5449}
5450
5451DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5452 DeclID GlobalID) {
5453 if (GlobalID < NUM_PREDEF_DECL_IDS)
5454 return GlobalID;
5455
5456 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5457 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5458 ModuleFile *Owner = I->second;
5459
5460 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5461 = M.GlobalToLocalDeclIDs.find(Owner);
5462 if (Pos == M.GlobalToLocalDeclIDs.end())
5463 return 0;
5464
5465 return GlobalID - Owner->BaseDeclID + Pos->second;
5466}
5467
5468serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5469 const RecordData &Record,
5470 unsigned &Idx) {
5471 if (Idx >= Record.size()) {
5472 Error("Corrupted AST file");
5473 return 0;
5474 }
5475
5476 return getGlobalDeclID(F, Record[Idx++]);
5477}
5478
5479/// \brief Resolve the offset of a statement into a statement.
5480///
5481/// This operation will read a new statement from the external
5482/// source each time it is called, and is meant to be used via a
5483/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5484Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5485 // Switch case IDs are per Decl.
5486 ClearSwitchCaseIDs();
5487
5488 // Offset here is a global offset across the entire chain.
5489 RecordLocation Loc = getLocalBitOffset(Offset);
5490 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5491 return ReadStmtFromStream(*Loc.F);
5492}
5493
5494namespace {
5495 class FindExternalLexicalDeclsVisitor {
5496 ASTReader &Reader;
5497 const DeclContext *DC;
5498 bool (*isKindWeWant)(Decl::Kind);
5499
5500 SmallVectorImpl<Decl*> &Decls;
5501 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5502
5503 public:
5504 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5505 bool (*isKindWeWant)(Decl::Kind),
5506 SmallVectorImpl<Decl*> &Decls)
5507 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5508 {
5509 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5510 PredefsVisited[I] = false;
5511 }
5512
5513 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5514 if (Preorder)
5515 return false;
5516
5517 FindExternalLexicalDeclsVisitor *This
5518 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5519
5520 ModuleFile::DeclContextInfosMap::iterator Info
5521 = M.DeclContextInfos.find(This->DC);
5522 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5523 return false;
5524
5525 // Load all of the declaration IDs
5526 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5527 *IDE = ID + Info->second.NumLexicalDecls;
5528 ID != IDE; ++ID) {
5529 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5530 continue;
5531
5532 // Don't add predefined declarations to the lexical context more
5533 // than once.
5534 if (ID->second < NUM_PREDEF_DECL_IDS) {
5535 if (This->PredefsVisited[ID->second])
5536 continue;
5537
5538 This->PredefsVisited[ID->second] = true;
5539 }
5540
5541 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5542 if (!This->DC->isDeclInLexicalTraversal(D))
5543 This->Decls.push_back(D);
5544 }
5545 }
5546
5547 return false;
5548 }
5549 };
5550}
5551
5552ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5553 bool (*isKindWeWant)(Decl::Kind),
5554 SmallVectorImpl<Decl*> &Decls) {
5555 // There might be lexical decls in multiple modules, for the TU at
5556 // least. Walk all of the modules in the order they were loaded.
5557 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5558 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5559 ++NumLexicalDeclContextsRead;
5560 return ELR_Success;
5561}
5562
5563namespace {
5564
5565class DeclIDComp {
5566 ASTReader &Reader;
5567 ModuleFile &Mod;
5568
5569public:
5570 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5571
5572 bool operator()(LocalDeclID L, LocalDeclID R) const {
5573 SourceLocation LHS = getLocation(L);
5574 SourceLocation RHS = getLocation(R);
5575 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5576 }
5577
5578 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5579 SourceLocation RHS = getLocation(R);
5580 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5581 }
5582
5583 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5584 SourceLocation LHS = getLocation(L);
5585 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5586 }
5587
5588 SourceLocation getLocation(LocalDeclID ID) const {
5589 return Reader.getSourceManager().getFileLoc(
5590 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5591 }
5592};
5593
5594}
5595
5596void ASTReader::FindFileRegionDecls(FileID File,
5597 unsigned Offset, unsigned Length,
5598 SmallVectorImpl<Decl *> &Decls) {
5599 SourceManager &SM = getSourceManager();
5600
5601 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5602 if (I == FileDeclIDs.end())
5603 return;
5604
5605 FileDeclsInfo &DInfo = I->second;
5606 if (DInfo.Decls.empty())
5607 return;
5608
5609 SourceLocation
5610 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5611 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5612
5613 DeclIDComp DIDComp(*this, *DInfo.Mod);
5614 ArrayRef<serialization::LocalDeclID>::iterator
5615 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5616 BeginLoc, DIDComp);
5617 if (BeginIt != DInfo.Decls.begin())
5618 --BeginIt;
5619
5620 // If we are pointing at a top-level decl inside an objc container, we need
5621 // to backtrack until we find it otherwise we will fail to report that the
5622 // region overlaps with an objc container.
5623 while (BeginIt != DInfo.Decls.begin() &&
5624 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5625 ->isTopLevelDeclInObjCContainer())
5626 --BeginIt;
5627
5628 ArrayRef<serialization::LocalDeclID>::iterator
5629 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5630 EndLoc, DIDComp);
5631 if (EndIt != DInfo.Decls.end())
5632 ++EndIt;
5633
5634 for (ArrayRef<serialization::LocalDeclID>::iterator
5635 DIt = BeginIt; DIt != EndIt; ++DIt)
5636 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5637}
5638
5639namespace {
5640 /// \brief ModuleFile visitor used to perform name lookup into a
5641 /// declaration context.
5642 class DeclContextNameLookupVisitor {
5643 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005644 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005645 DeclarationName Name;
5646 SmallVectorImpl<NamedDecl *> &Decls;
5647
5648 public:
5649 DeclContextNameLookupVisitor(ASTReader &Reader,
5650 SmallVectorImpl<const DeclContext *> &Contexts,
5651 DeclarationName Name,
5652 SmallVectorImpl<NamedDecl *> &Decls)
5653 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5654
5655 static bool visit(ModuleFile &M, void *UserData) {
5656 DeclContextNameLookupVisitor *This
5657 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5658
5659 // Check whether we have any visible declaration information for
5660 // this context in this module.
5661 ModuleFile::DeclContextInfosMap::iterator Info;
5662 bool FoundInfo = false;
5663 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5664 Info = M.DeclContextInfos.find(This->Contexts[I]);
5665 if (Info != M.DeclContextInfos.end() &&
5666 Info->second.NameLookupTableData) {
5667 FoundInfo = true;
5668 break;
5669 }
5670 }
5671
5672 if (!FoundInfo)
5673 return false;
5674
5675 // Look for this name within this module.
5676 ASTDeclContextNameLookupTable *LookupTable =
5677 Info->second.NameLookupTableData;
5678 ASTDeclContextNameLookupTable::iterator Pos
5679 = LookupTable->find(This->Name);
5680 if (Pos == LookupTable->end())
5681 return false;
5682
5683 bool FoundAnything = false;
5684 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5685 for (; Data.first != Data.second; ++Data.first) {
5686 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5687 if (!ND)
5688 continue;
5689
5690 if (ND->getDeclName() != This->Name) {
5691 // A name might be null because the decl's redeclarable part is
5692 // currently read before reading its name. The lookup is triggered by
5693 // building that decl (likely indirectly), and so it is later in the
5694 // sense of "already existing" and can be ignored here.
5695 continue;
5696 }
5697
5698 // Record this declaration.
5699 FoundAnything = true;
5700 This->Decls.push_back(ND);
5701 }
5702
5703 return FoundAnything;
5704 }
5705 };
5706}
5707
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005708/// \brief Retrieve the "definitive" module file for the definition of the
5709/// given declaration context, if there is one.
5710///
5711/// The "definitive" module file is the only place where we need to look to
5712/// find information about the declarations within the given declaration
5713/// context. For example, C++ and Objective-C classes, C structs/unions, and
5714/// Objective-C protocols, categories, and extensions are all defined in a
5715/// single place in the source code, so they have definitive module files
5716/// associated with them. C++ namespaces, on the other hand, can have
5717/// definitions in multiple different module files.
5718///
5719/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5720/// NDEBUG checking.
5721static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5722 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005723 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5724 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005725
5726 return 0;
5727}
5728
Richard Smith3646c682013-02-07 03:30:24 +00005729bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005730ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5731 DeclarationName Name) {
5732 assert(DC->hasExternalVisibleStorage() &&
5733 "DeclContext has no visible decls in storage");
5734 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00005735 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005736
5737 SmallVector<NamedDecl *, 64> Decls;
5738
5739 // Compute the declaration contexts we need to look into. Multiple such
5740 // declaration contexts occur when two declaration contexts from disjoint
5741 // modules get merged, e.g., when two namespaces with the same name are
5742 // independently defined in separate modules.
5743 SmallVector<const DeclContext *, 2> Contexts;
5744 Contexts.push_back(DC);
5745
5746 if (DC->isNamespace()) {
5747 MergedDeclsMap::iterator Merged
5748 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5749 if (Merged != MergedDecls.end()) {
5750 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5751 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5752 }
5753 }
5754
5755 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005756
5757 // If we can definitively determine which module file to look into,
5758 // only look there. Otherwise, look in all module files.
5759 ModuleFile *Definitive;
5760 if (Contexts.size() == 1 &&
5761 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5762 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5763 } else {
5764 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5765 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005766 ++NumVisibleDeclContextsRead;
5767 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00005768 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005769}
5770
5771namespace {
5772 /// \brief ModuleFile visitor used to retrieve all visible names in a
5773 /// declaration context.
5774 class DeclContextAllNamesVisitor {
5775 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005776 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005777 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005778 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005779
5780 public:
5781 DeclContextAllNamesVisitor(ASTReader &Reader,
5782 SmallVectorImpl<const DeclContext *> &Contexts,
5783 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005784 SmallVector<NamedDecl *, 8> > &Decls,
5785 bool VisitAll)
5786 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005787
5788 static bool visit(ModuleFile &M, void *UserData) {
5789 DeclContextAllNamesVisitor *This
5790 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5791
5792 // Check whether we have any visible declaration information for
5793 // this context in this module.
5794 ModuleFile::DeclContextInfosMap::iterator Info;
5795 bool FoundInfo = false;
5796 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5797 Info = M.DeclContextInfos.find(This->Contexts[I]);
5798 if (Info != M.DeclContextInfos.end() &&
5799 Info->second.NameLookupTableData) {
5800 FoundInfo = true;
5801 break;
5802 }
5803 }
5804
5805 if (!FoundInfo)
5806 return false;
5807
5808 ASTDeclContextNameLookupTable *LookupTable =
5809 Info->second.NameLookupTableData;
5810 bool FoundAnything = false;
5811 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005812 I = LookupTable->data_begin(), E = LookupTable->data_end();
5813 I != E;
5814 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005815 ASTDeclContextNameLookupTrait::data_type Data = *I;
5816 for (; Data.first != Data.second; ++Data.first) {
5817 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5818 *Data.first);
5819 if (!ND)
5820 continue;
5821
5822 // Record this declaration.
5823 FoundAnything = true;
5824 This->Decls[ND->getDeclName()].push_back(ND);
5825 }
5826 }
5827
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005828 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005829 }
5830 };
5831}
5832
5833void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5834 if (!DC->hasExternalVisibleStorage())
5835 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005836 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005837
5838 // Compute the declaration contexts we need to look into. Multiple such
5839 // declaration contexts occur when two declaration contexts from disjoint
5840 // modules get merged, e.g., when two namespaces with the same name are
5841 // independently defined in separate modules.
5842 SmallVector<const DeclContext *, 2> Contexts;
5843 Contexts.push_back(DC);
5844
5845 if (DC->isNamespace()) {
5846 MergedDeclsMap::iterator Merged
5847 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5848 if (Merged != MergedDecls.end()) {
5849 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5850 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5851 }
5852 }
5853
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005854 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5855 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005856 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5857 ++NumVisibleDeclContextsRead;
5858
5859 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005860 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005861 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5862 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5863 }
5864 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5865}
5866
5867/// \brief Under non-PCH compilation the consumer receives the objc methods
5868/// before receiving the implementation, and codegen depends on this.
5869/// We simulate this by deserializing and passing to consumer the methods of the
5870/// implementation before passing the deserialized implementation decl.
5871static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5872 ASTConsumer *Consumer) {
5873 assert(ImplD && Consumer);
5874
5875 for (ObjCImplDecl::method_iterator
5876 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5877 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5878
5879 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5880}
5881
5882void ASTReader::PassInterestingDeclsToConsumer() {
5883 assert(Consumer);
5884 while (!InterestingDecls.empty()) {
5885 Decl *D = InterestingDecls.front();
5886 InterestingDecls.pop_front();
5887
5888 PassInterestingDeclToConsumer(D);
5889 }
5890}
5891
5892void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5893 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5894 PassObjCImplDeclToConsumer(ImplD, Consumer);
5895 else
5896 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5897}
5898
5899void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5900 this->Consumer = Consumer;
5901
5902 if (!Consumer)
5903 return;
5904
5905 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5906 // Force deserialization of this decl, which will cause it to be queued for
5907 // passing to the consumer.
5908 GetDecl(ExternalDefinitions[I]);
5909 }
5910 ExternalDefinitions.clear();
5911
5912 PassInterestingDeclsToConsumer();
5913}
5914
5915void ASTReader::PrintStats() {
5916 std::fprintf(stderr, "*** AST File Statistics:\n");
5917
5918 unsigned NumTypesLoaded
5919 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5920 QualType());
5921 unsigned NumDeclsLoaded
5922 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5923 (Decl *)0);
5924 unsigned NumIdentifiersLoaded
5925 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5926 IdentifiersLoaded.end(),
5927 (IdentifierInfo *)0);
5928 unsigned NumMacrosLoaded
5929 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5930 MacrosLoaded.end(),
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005931 (MacroInfo *)0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005932 unsigned NumSelectorsLoaded
5933 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5934 SelectorsLoaded.end(),
5935 Selector());
5936
5937 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5938 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5939 NumSLocEntriesRead, TotalNumSLocEntries,
5940 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5941 if (!TypesLoaded.empty())
5942 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5943 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5944 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5945 if (!DeclsLoaded.empty())
5946 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5947 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5948 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5949 if (!IdentifiersLoaded.empty())
5950 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5951 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5952 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5953 if (!MacrosLoaded.empty())
5954 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5955 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5956 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5957 if (!SelectorsLoaded.empty())
5958 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5959 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5960 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5961 if (TotalNumStatements)
5962 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5963 NumStatementsRead, TotalNumStatements,
5964 ((float)NumStatementsRead/TotalNumStatements * 100));
5965 if (TotalNumMacros)
5966 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5967 NumMacrosRead, TotalNumMacros,
5968 ((float)NumMacrosRead/TotalNumMacros * 100));
5969 if (TotalLexicalDeclContexts)
5970 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5971 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5972 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5973 * 100));
5974 if (TotalVisibleDeclContexts)
5975 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5976 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5977 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5978 * 100));
5979 if (TotalNumMethodPoolEntries) {
5980 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5981 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5982 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5983 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005984 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00005985 if (NumMethodPoolLookups) {
5986 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5987 NumMethodPoolHits, NumMethodPoolLookups,
5988 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5989 }
5990 if (NumMethodPoolTableLookups) {
5991 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5992 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5993 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5994 * 100.0));
5995 }
5996
Douglas Gregore1698072013-01-25 00:38:33 +00005997 if (NumIdentifierLookupHits) {
5998 std::fprintf(stderr,
5999 " %u / %u identifier table lookups succeeded (%f%%)\n",
6000 NumIdentifierLookupHits, NumIdentifierLookups,
6001 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6002 }
6003
Douglas Gregor1a49d972013-01-25 01:03:03 +00006004 if (GlobalIndex) {
6005 std::fprintf(stderr, "\n");
6006 GlobalIndex->printStats();
6007 }
6008
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006009 std::fprintf(stderr, "\n");
6010 dump();
6011 std::fprintf(stderr, "\n");
6012}
6013
6014template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6015static void
6016dumpModuleIDMap(StringRef Name,
6017 const ContinuousRangeMap<Key, ModuleFile *,
6018 InitialCapacity> &Map) {
6019 if (Map.begin() == Map.end())
6020 return;
6021
6022 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6023 llvm::errs() << Name << ":\n";
6024 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6025 I != IEnd; ++I) {
6026 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6027 << "\n";
6028 }
6029}
6030
6031void ASTReader::dump() {
6032 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6033 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6034 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6035 dumpModuleIDMap("Global type map", GlobalTypeMap);
6036 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6037 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6038 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6039 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6040 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6041 dumpModuleIDMap("Global preprocessed entity map",
6042 GlobalPreprocessedEntityMap);
6043
6044 llvm::errs() << "\n*** PCH/Modules Loaded:";
6045 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6046 MEnd = ModuleMgr.end();
6047 M != MEnd; ++M)
6048 (*M)->dump();
6049}
6050
6051/// Return the amount of memory used by memory buffers, breaking down
6052/// by heap-backed versus mmap'ed memory.
6053void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6054 for (ModuleConstIterator I = ModuleMgr.begin(),
6055 E = ModuleMgr.end(); I != E; ++I) {
6056 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6057 size_t bytes = buf->getBufferSize();
6058 switch (buf->getBufferKind()) {
6059 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6060 sizes.malloc_bytes += bytes;
6061 break;
6062 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6063 sizes.mmap_bytes += bytes;
6064 break;
6065 }
6066 }
6067 }
6068}
6069
6070void ASTReader::InitializeSema(Sema &S) {
6071 SemaObj = &S;
6072 S.addExternalSource(this);
6073
6074 // Makes sure any declarations that were deserialized "too early"
6075 // still get added to the identifier's declaration chains.
6076 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006077 pushExternalDeclIntoScope(PreloadedDecls[I],
6078 PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006079 }
6080 PreloadedDecls.clear();
6081
6082 // Load the offsets of the declarations that Sema references.
6083 // They will be lazily deserialized when needed.
6084 if (!SemaDeclRefs.empty()) {
6085 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6086 if (!SemaObj->StdNamespace)
6087 SemaObj->StdNamespace = SemaDeclRefs[0];
6088 if (!SemaObj->StdBadAlloc)
6089 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6090 }
6091
6092 if (!FPPragmaOptions.empty()) {
6093 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6094 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6095 }
6096
6097 if (!OpenCLExtensions.empty()) {
6098 unsigned I = 0;
6099#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6100#include "clang/Basic/OpenCLExtensions.def"
6101
6102 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6103 }
6104}
6105
6106IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6107 // Note that we are loading an identifier.
6108 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006109 StringRef Name(NameStart, NameEnd - NameStart);
6110
6111 // If there is a global index, look there first to determine which modules
6112 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006113 GlobalModuleIndex::HitSet Hits;
6114 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006115 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006116 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6117 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006118 }
6119 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006120 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006121 NumIdentifierLookups,
6122 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006123 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006124 IdentifierInfo *II = Visitor.getIdentifierInfo();
6125 markIdentifierUpToDate(II);
6126 return II;
6127}
6128
6129namespace clang {
6130 /// \brief An identifier-lookup iterator that enumerates all of the
6131 /// identifiers stored within a set of AST files.
6132 class ASTIdentifierIterator : public IdentifierIterator {
6133 /// \brief The AST reader whose identifiers are being enumerated.
6134 const ASTReader &Reader;
6135
6136 /// \brief The current index into the chain of AST files stored in
6137 /// the AST reader.
6138 unsigned Index;
6139
6140 /// \brief The current position within the identifier lookup table
6141 /// of the current AST file.
6142 ASTIdentifierLookupTable::key_iterator Current;
6143
6144 /// \brief The end position within the identifier lookup table of
6145 /// the current AST file.
6146 ASTIdentifierLookupTable::key_iterator End;
6147
6148 public:
6149 explicit ASTIdentifierIterator(const ASTReader &Reader);
6150
6151 virtual StringRef Next();
6152 };
6153}
6154
6155ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6156 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6157 ASTIdentifierLookupTable *IdTable
6158 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6159 Current = IdTable->key_begin();
6160 End = IdTable->key_end();
6161}
6162
6163StringRef ASTIdentifierIterator::Next() {
6164 while (Current == End) {
6165 // If we have exhausted all of our AST files, we're done.
6166 if (Index == 0)
6167 return StringRef();
6168
6169 --Index;
6170 ASTIdentifierLookupTable *IdTable
6171 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6172 IdentifierLookupTable;
6173 Current = IdTable->key_begin();
6174 End = IdTable->key_end();
6175 }
6176
6177 // We have any identifiers remaining in the current AST file; return
6178 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006179 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006180 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006181 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006182}
6183
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +00006184IdentifierIterator *ASTReader::getIdentifiers() {
6185 if (!loadGlobalIndex())
6186 return GlobalIndex->createIdentifierIterator();
6187
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006188 return new ASTIdentifierIterator(*this);
6189}
6190
6191namespace clang { namespace serialization {
6192 class ReadMethodPoolVisitor {
6193 ASTReader &Reader;
6194 Selector Sel;
6195 unsigned PriorGeneration;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006196 unsigned InstanceBits;
6197 unsigned FactoryBits;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006198 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6199 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006200
6201 public:
6202 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6203 unsigned PriorGeneration)
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006204 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6205 InstanceBits(0), FactoryBits(0) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006206
6207 static bool visit(ModuleFile &M, void *UserData) {
6208 ReadMethodPoolVisitor *This
6209 = static_cast<ReadMethodPoolVisitor *>(UserData);
6210
6211 if (!M.SelectorLookupTable)
6212 return false;
6213
6214 // If we've already searched this module file, skip it now.
6215 if (M.Generation <= This->PriorGeneration)
6216 return true;
6217
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006218 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006219 ASTSelectorLookupTable *PoolTable
6220 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6221 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6222 if (Pos == PoolTable->end())
6223 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006224
6225 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006226 ++This->Reader.NumSelectorsRead;
6227 // FIXME: Not quite happy with the statistics here. We probably should
6228 // disable this tracking when called via LoadSelector.
6229 // Also, should entries without methods count as misses?
6230 ++This->Reader.NumMethodPoolEntriesRead;
6231 ASTSelectorLookupTrait::data_type Data = *Pos;
6232 if (This->Reader.DeserializationListener)
6233 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6234 This->Sel);
6235
6236 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6237 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006238 This->InstanceBits = Data.InstanceBits;
6239 This->FactoryBits = Data.FactoryBits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006240 return true;
6241 }
6242
6243 /// \brief Retrieve the instance methods found by this visitor.
6244 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6245 return InstanceMethods;
6246 }
6247
6248 /// \brief Retrieve the instance methods found by this visitor.
6249 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6250 return FactoryMethods;
6251 }
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006252
6253 unsigned getInstanceBits() const { return InstanceBits; }
6254 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006255 };
6256} } // end namespace clang::serialization
6257
6258/// \brief Add the given set of methods to the method list.
6259static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6260 ObjCMethodList &List) {
6261 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6262 S.addMethodToGlobalList(&List, Methods[I]);
6263 }
6264}
6265
6266void ASTReader::ReadMethodPool(Selector Sel) {
6267 // Get the selector generation and update it to the current generation.
6268 unsigned &Generation = SelectorGeneration[Sel];
6269 unsigned PriorGeneration = Generation;
6270 Generation = CurrentGeneration;
6271
6272 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006273 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006274 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6275 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6276
6277 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006278 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006279 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006280
6281 ++NumMethodPoolHits;
6282
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006283 if (!getSema())
6284 return;
6285
6286 Sema &S = *getSema();
6287 Sema::GlobalMethodPool::iterator Pos
6288 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6289
6290 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6291 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006292 Pos->second.first.setBits(Visitor.getInstanceBits());
6293 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006294}
6295
6296void ASTReader::ReadKnownNamespaces(
6297 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6298 Namespaces.clear();
6299
6300 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6301 if (NamespaceDecl *Namespace
6302 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6303 Namespaces.push_back(Namespace);
6304 }
6305}
6306
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006307void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00006308 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006309 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6310 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00006311 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006312 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00006313 Undefined.insert(std::make_pair(D, Loc));
6314 }
6315}
Nick Lewycky01a41142013-01-26 00:35:08 +00006316
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006317void ASTReader::ReadTentativeDefinitions(
6318 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6319 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6320 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6321 if (Var)
6322 TentativeDefs.push_back(Var);
6323 }
6324 TentativeDefinitions.clear();
6325}
6326
6327void ASTReader::ReadUnusedFileScopedDecls(
6328 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6329 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6330 DeclaratorDecl *D
6331 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6332 if (D)
6333 Decls.push_back(D);
6334 }
6335 UnusedFileScopedDecls.clear();
6336}
6337
6338void ASTReader::ReadDelegatingConstructors(
6339 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6340 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6341 CXXConstructorDecl *D
6342 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6343 if (D)
6344 Decls.push_back(D);
6345 }
6346 DelegatingCtorDecls.clear();
6347}
6348
6349void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6350 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6351 TypedefNameDecl *D
6352 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6353 if (D)
6354 Decls.push_back(D);
6355 }
6356 ExtVectorDecls.clear();
6357}
6358
6359void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6360 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6361 CXXRecordDecl *D
6362 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6363 if (D)
6364 Decls.push_back(D);
6365 }
6366 DynamicClasses.clear();
6367}
6368
6369void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006370ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6371 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6372 NamedDecl *D
6373 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006374 if (D)
6375 Decls.push_back(D);
6376 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006377 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006378}
6379
6380void ASTReader::ReadReferencedSelectors(
6381 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6382 if (ReferencedSelectorsData.empty())
6383 return;
6384
6385 // If there are @selector references added them to its pool. This is for
6386 // implementation of -Wselector.
6387 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6388 unsigned I = 0;
6389 while (I < DataSize) {
6390 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6391 SourceLocation SelLoc
6392 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6393 Sels.push_back(std::make_pair(Sel, SelLoc));
6394 }
6395 ReferencedSelectorsData.clear();
6396}
6397
6398void ASTReader::ReadWeakUndeclaredIdentifiers(
6399 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6400 if (WeakUndeclaredIdentifiers.empty())
6401 return;
6402
6403 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6404 IdentifierInfo *WeakId
6405 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6406 IdentifierInfo *AliasId
6407 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6408 SourceLocation Loc
6409 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6410 bool Used = WeakUndeclaredIdentifiers[I++];
6411 WeakInfo WI(AliasId, Loc);
6412 WI.setUsed(Used);
6413 WeakIDs.push_back(std::make_pair(WeakId, WI));
6414 }
6415 WeakUndeclaredIdentifiers.clear();
6416}
6417
6418void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6419 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6420 ExternalVTableUse VT;
6421 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6422 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6423 VT.DefinitionRequired = VTableUses[Idx++];
6424 VTables.push_back(VT);
6425 }
6426
6427 VTableUses.clear();
6428}
6429
6430void ASTReader::ReadPendingInstantiations(
6431 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6432 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6433 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6434 SourceLocation Loc
6435 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6436
6437 Pending.push_back(std::make_pair(D, Loc));
6438 }
6439 PendingInstantiations.clear();
6440}
6441
6442void ASTReader::LoadSelector(Selector Sel) {
6443 // It would be complicated to avoid reading the methods anyway. So don't.
6444 ReadMethodPool(Sel);
6445}
6446
6447void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6448 assert(ID && "Non-zero identifier ID required");
6449 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6450 IdentifiersLoaded[ID - 1] = II;
6451 if (DeserializationListener)
6452 DeserializationListener->IdentifierRead(ID, II);
6453}
6454
6455/// \brief Set the globally-visible declarations associated with the given
6456/// identifier.
6457///
6458/// If the AST reader is currently in a state where the given declaration IDs
6459/// cannot safely be resolved, they are queued until it is safe to resolve
6460/// them.
6461///
6462/// \param II an IdentifierInfo that refers to one or more globally-visible
6463/// declarations.
6464///
6465/// \param DeclIDs the set of declaration IDs with the name @p II that are
6466/// visible at global scope.
6467///
Douglas Gregoraa945902013-02-18 15:53:43 +00006468/// \param Decls if non-null, this vector will be populated with the set of
6469/// deserialized declarations. These declarations will not be pushed into
6470/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006471void
6472ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6473 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00006474 SmallVectorImpl<Decl *> *Decls) {
6475 if (NumCurrentElementsDeserializing && !Decls) {
6476 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006477 return;
6478 }
6479
6480 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6481 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6482 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006483 // If we're simply supposed to record the declarations, do so now.
6484 if (Decls) {
6485 Decls->push_back(D);
6486 continue;
6487 }
6488
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006489 // Introduce this declaration into the translation-unit scope
6490 // and add it to the declaration chain for this identifier, so
6491 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006492 pushExternalDeclIntoScope(D, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006493 } else {
6494 // Queue this declaration so that it will be added to the
6495 // translation unit scope and identifier's declaration chain
6496 // once a Sema object is known.
6497 PreloadedDecls.push_back(D);
6498 }
6499 }
6500}
6501
Douglas Gregor8222b892013-01-21 16:52:34 +00006502IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006503 if (ID == 0)
6504 return 0;
6505
6506 if (IdentifiersLoaded.empty()) {
6507 Error("no identifier table in AST file");
6508 return 0;
6509 }
6510
6511 ID -= 1;
6512 if (!IdentifiersLoaded[ID]) {
6513 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6514 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6515 ModuleFile *M = I->second;
6516 unsigned Index = ID - M->BaseIdentifierID;
6517 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6518
6519 // All of the strings in the AST file are preceded by a 16-bit length.
6520 // Extract that 16-bit length to avoid having to execute strlen().
6521 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6522 // unsigned integers. This is important to avoid integer overflow when
6523 // we cast them to 'unsigned'.
6524 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6525 unsigned StrLen = (((unsigned) StrLenPtr[0])
6526 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006527 IdentifiersLoaded[ID]
6528 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006529 if (DeserializationListener)
6530 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6531 }
6532
6533 return IdentifiersLoaded[ID];
6534}
6535
Douglas Gregor8222b892013-01-21 16:52:34 +00006536IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6537 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006538}
6539
6540IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6541 if (LocalID < NUM_PREDEF_IDENT_IDS)
6542 return LocalID;
6543
6544 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6545 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6546 assert(I != M.IdentifierRemap.end()
6547 && "Invalid index into identifier index remap");
6548
6549 return LocalID + I->second;
6550}
6551
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006552MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006553 if (ID == 0)
6554 return 0;
6555
6556 if (MacrosLoaded.empty()) {
6557 Error("no macro table in AST file");
6558 return 0;
6559 }
6560
6561 ID -= NUM_PREDEF_MACRO_IDS;
6562 if (!MacrosLoaded[ID]) {
6563 GlobalMacroMapType::iterator I
6564 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6565 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6566 ModuleFile *M = I->second;
6567 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006568 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6569
6570 if (DeserializationListener)
6571 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6572 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006573 }
6574
6575 return MacrosLoaded[ID];
6576}
6577
6578MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6579 if (LocalID < NUM_PREDEF_MACRO_IDS)
6580 return LocalID;
6581
6582 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6583 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6584 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6585
6586 return LocalID + I->second;
6587}
6588
6589serialization::SubmoduleID
6590ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6591 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6592 return LocalID;
6593
6594 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6595 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6596 assert(I != M.SubmoduleRemap.end()
6597 && "Invalid index into submodule index remap");
6598
6599 return LocalID + I->second;
6600}
6601
6602Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6603 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6604 assert(GlobalID == 0 && "Unhandled global submodule ID");
6605 return 0;
6606 }
6607
6608 if (GlobalID > SubmodulesLoaded.size()) {
6609 Error("submodule ID out of range in AST file");
6610 return 0;
6611 }
6612
6613 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6614}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006615
6616Module *ASTReader::getModule(unsigned ID) {
6617 return getSubmodule(ID);
6618}
6619
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006620Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6621 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6622}
6623
6624Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6625 if (ID == 0)
6626 return Selector();
6627
6628 if (ID > SelectorsLoaded.size()) {
6629 Error("selector ID out of range in AST file");
6630 return Selector();
6631 }
6632
6633 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6634 // Load this selector from the selector table.
6635 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6636 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6637 ModuleFile &M = *I->second;
6638 ASTSelectorLookupTrait Trait(*this, M);
6639 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6640 SelectorsLoaded[ID - 1] =
6641 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6642 if (DeserializationListener)
6643 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6644 }
6645
6646 return SelectorsLoaded[ID - 1];
6647}
6648
6649Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6650 return DecodeSelector(ID);
6651}
6652
6653uint32_t ASTReader::GetNumExternalSelectors() {
6654 // ID 0 (the null selector) is considered an external selector.
6655 return getTotalNumSelectors() + 1;
6656}
6657
6658serialization::SelectorID
6659ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6660 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6661 return LocalID;
6662
6663 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6664 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6665 assert(I != M.SelectorRemap.end()
6666 && "Invalid index into selector index remap");
6667
6668 return LocalID + I->second;
6669}
6670
6671DeclarationName
6672ASTReader::ReadDeclarationName(ModuleFile &F,
6673 const RecordData &Record, unsigned &Idx) {
6674 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6675 switch (Kind) {
6676 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006677 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006678
6679 case DeclarationName::ObjCZeroArgSelector:
6680 case DeclarationName::ObjCOneArgSelector:
6681 case DeclarationName::ObjCMultiArgSelector:
6682 return DeclarationName(ReadSelector(F, Record, Idx));
6683
6684 case DeclarationName::CXXConstructorName:
6685 return Context.DeclarationNames.getCXXConstructorName(
6686 Context.getCanonicalType(readType(F, Record, Idx)));
6687
6688 case DeclarationName::CXXDestructorName:
6689 return Context.DeclarationNames.getCXXDestructorName(
6690 Context.getCanonicalType(readType(F, Record, Idx)));
6691
6692 case DeclarationName::CXXConversionFunctionName:
6693 return Context.DeclarationNames.getCXXConversionFunctionName(
6694 Context.getCanonicalType(readType(F, Record, Idx)));
6695
6696 case DeclarationName::CXXOperatorName:
6697 return Context.DeclarationNames.getCXXOperatorName(
6698 (OverloadedOperatorKind)Record[Idx++]);
6699
6700 case DeclarationName::CXXLiteralOperatorName:
6701 return Context.DeclarationNames.getCXXLiteralOperatorName(
6702 GetIdentifierInfo(F, Record, Idx));
6703
6704 case DeclarationName::CXXUsingDirective:
6705 return DeclarationName::getUsingDirectiveName();
6706 }
6707
6708 llvm_unreachable("Invalid NameKind!");
6709}
6710
6711void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6712 DeclarationNameLoc &DNLoc,
6713 DeclarationName Name,
6714 const RecordData &Record, unsigned &Idx) {
6715 switch (Name.getNameKind()) {
6716 case DeclarationName::CXXConstructorName:
6717 case DeclarationName::CXXDestructorName:
6718 case DeclarationName::CXXConversionFunctionName:
6719 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6720 break;
6721
6722 case DeclarationName::CXXOperatorName:
6723 DNLoc.CXXOperatorName.BeginOpNameLoc
6724 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6725 DNLoc.CXXOperatorName.EndOpNameLoc
6726 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6727 break;
6728
6729 case DeclarationName::CXXLiteralOperatorName:
6730 DNLoc.CXXLiteralOperatorName.OpNameLoc
6731 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6732 break;
6733
6734 case DeclarationName::Identifier:
6735 case DeclarationName::ObjCZeroArgSelector:
6736 case DeclarationName::ObjCOneArgSelector:
6737 case DeclarationName::ObjCMultiArgSelector:
6738 case DeclarationName::CXXUsingDirective:
6739 break;
6740 }
6741}
6742
6743void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6744 DeclarationNameInfo &NameInfo,
6745 const RecordData &Record, unsigned &Idx) {
6746 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6747 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6748 DeclarationNameLoc DNLoc;
6749 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6750 NameInfo.setInfo(DNLoc);
6751}
6752
6753void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6754 const RecordData &Record, unsigned &Idx) {
6755 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6756 unsigned NumTPLists = Record[Idx++];
6757 Info.NumTemplParamLists = NumTPLists;
6758 if (NumTPLists) {
6759 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6760 for (unsigned i=0; i != NumTPLists; ++i)
6761 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6762 }
6763}
6764
6765TemplateName
6766ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6767 unsigned &Idx) {
6768 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6769 switch (Kind) {
6770 case TemplateName::Template:
6771 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6772
6773 case TemplateName::OverloadedTemplate: {
6774 unsigned size = Record[Idx++];
6775 UnresolvedSet<8> Decls;
6776 while (size--)
6777 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6778
6779 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6780 }
6781
6782 case TemplateName::QualifiedTemplate: {
6783 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6784 bool hasTemplKeyword = Record[Idx++];
6785 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6786 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6787 }
6788
6789 case TemplateName::DependentTemplate: {
6790 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6791 if (Record[Idx++]) // isIdentifier
6792 return Context.getDependentTemplateName(NNS,
6793 GetIdentifierInfo(F, Record,
6794 Idx));
6795 return Context.getDependentTemplateName(NNS,
6796 (OverloadedOperatorKind)Record[Idx++]);
6797 }
6798
6799 case TemplateName::SubstTemplateTemplateParm: {
6800 TemplateTemplateParmDecl *param
6801 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6802 if (!param) return TemplateName();
6803 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6804 return Context.getSubstTemplateTemplateParm(param, replacement);
6805 }
6806
6807 case TemplateName::SubstTemplateTemplateParmPack: {
6808 TemplateTemplateParmDecl *Param
6809 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6810 if (!Param)
6811 return TemplateName();
6812
6813 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6814 if (ArgPack.getKind() != TemplateArgument::Pack)
6815 return TemplateName();
6816
6817 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6818 }
6819 }
6820
6821 llvm_unreachable("Unhandled template name kind!");
6822}
6823
6824TemplateArgument
6825ASTReader::ReadTemplateArgument(ModuleFile &F,
6826 const RecordData &Record, unsigned &Idx) {
6827 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6828 switch (Kind) {
6829 case TemplateArgument::Null:
6830 return TemplateArgument();
6831 case TemplateArgument::Type:
6832 return TemplateArgument(readType(F, Record, Idx));
6833 case TemplateArgument::Declaration: {
6834 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6835 bool ForReferenceParam = Record[Idx++];
6836 return TemplateArgument(D, ForReferenceParam);
6837 }
6838 case TemplateArgument::NullPtr:
6839 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6840 case TemplateArgument::Integral: {
6841 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6842 QualType T = readType(F, Record, Idx);
6843 return TemplateArgument(Context, Value, T);
6844 }
6845 case TemplateArgument::Template:
6846 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6847 case TemplateArgument::TemplateExpansion: {
6848 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00006849 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006850 if (unsigned NumExpansions = Record[Idx++])
6851 NumTemplateExpansions = NumExpansions - 1;
6852 return TemplateArgument(Name, NumTemplateExpansions);
6853 }
6854 case TemplateArgument::Expression:
6855 return TemplateArgument(ReadExpr(F));
6856 case TemplateArgument::Pack: {
6857 unsigned NumArgs = Record[Idx++];
6858 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6859 for (unsigned I = 0; I != NumArgs; ++I)
6860 Args[I] = ReadTemplateArgument(F, Record, Idx);
6861 return TemplateArgument(Args, NumArgs);
6862 }
6863 }
6864
6865 llvm_unreachable("Unhandled template argument kind!");
6866}
6867
6868TemplateParameterList *
6869ASTReader::ReadTemplateParameterList(ModuleFile &F,
6870 const RecordData &Record, unsigned &Idx) {
6871 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6872 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6873 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6874
6875 unsigned NumParams = Record[Idx++];
6876 SmallVector<NamedDecl *, 16> Params;
6877 Params.reserve(NumParams);
6878 while (NumParams--)
6879 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6880
6881 TemplateParameterList* TemplateParams =
6882 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6883 Params.data(), Params.size(), RAngleLoc);
6884 return TemplateParams;
6885}
6886
6887void
6888ASTReader::
6889ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6890 ModuleFile &F, const RecordData &Record,
6891 unsigned &Idx) {
6892 unsigned NumTemplateArgs = Record[Idx++];
6893 TemplArgs.reserve(NumTemplateArgs);
6894 while (NumTemplateArgs--)
6895 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6896}
6897
6898/// \brief Read a UnresolvedSet structure.
6899void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6900 const RecordData &Record, unsigned &Idx) {
6901 unsigned NumDecls = Record[Idx++];
6902 Set.reserve(Context, NumDecls);
6903 while (NumDecls--) {
6904 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6905 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6906 Set.addDecl(Context, D, AS);
6907 }
6908}
6909
6910CXXBaseSpecifier
6911ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6912 const RecordData &Record, unsigned &Idx) {
6913 bool isVirtual = static_cast<bool>(Record[Idx++]);
6914 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6915 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6916 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6917 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6918 SourceRange Range = ReadSourceRange(F, Record, Idx);
6919 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6920 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6921 EllipsisLoc);
6922 Result.setInheritConstructors(inheritConstructors);
6923 return Result;
6924}
6925
6926std::pair<CXXCtorInitializer **, unsigned>
6927ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6928 unsigned &Idx) {
6929 CXXCtorInitializer **CtorInitializers = 0;
6930 unsigned NumInitializers = Record[Idx++];
6931 if (NumInitializers) {
6932 CtorInitializers
6933 = new (Context) CXXCtorInitializer*[NumInitializers];
6934 for (unsigned i=0; i != NumInitializers; ++i) {
6935 TypeSourceInfo *TInfo = 0;
6936 bool IsBaseVirtual = false;
6937 FieldDecl *Member = 0;
6938 IndirectFieldDecl *IndirectMember = 0;
6939
6940 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6941 switch (Type) {
6942 case CTOR_INITIALIZER_BASE:
6943 TInfo = GetTypeSourceInfo(F, Record, Idx);
6944 IsBaseVirtual = Record[Idx++];
6945 break;
6946
6947 case CTOR_INITIALIZER_DELEGATING:
6948 TInfo = GetTypeSourceInfo(F, Record, Idx);
6949 break;
6950
6951 case CTOR_INITIALIZER_MEMBER:
6952 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6953 break;
6954
6955 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6956 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6957 break;
6958 }
6959
6960 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6961 Expr *Init = ReadExpr(F);
6962 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6963 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6964 bool IsWritten = Record[Idx++];
6965 unsigned SourceOrderOrNumArrayIndices;
6966 SmallVector<VarDecl *, 8> Indices;
6967 if (IsWritten) {
6968 SourceOrderOrNumArrayIndices = Record[Idx++];
6969 } else {
6970 SourceOrderOrNumArrayIndices = Record[Idx++];
6971 Indices.reserve(SourceOrderOrNumArrayIndices);
6972 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6973 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6974 }
6975
6976 CXXCtorInitializer *BOMInit;
6977 if (Type == CTOR_INITIALIZER_BASE) {
6978 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6979 LParenLoc, Init, RParenLoc,
6980 MemberOrEllipsisLoc);
6981 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6982 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6983 Init, RParenLoc);
6984 } else if (IsWritten) {
6985 if (Member)
6986 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6987 LParenLoc, Init, RParenLoc);
6988 else
6989 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6990 MemberOrEllipsisLoc, LParenLoc,
6991 Init, RParenLoc);
6992 } else {
6993 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6994 LParenLoc, Init, RParenLoc,
6995 Indices.data(), Indices.size());
6996 }
6997
6998 if (IsWritten)
6999 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7000 CtorInitializers[i] = BOMInit;
7001 }
7002 }
7003
7004 return std::make_pair(CtorInitializers, NumInitializers);
7005}
7006
7007NestedNameSpecifier *
7008ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7009 const RecordData &Record, unsigned &Idx) {
7010 unsigned N = Record[Idx++];
7011 NestedNameSpecifier *NNS = 0, *Prev = 0;
7012 for (unsigned I = 0; I != N; ++I) {
7013 NestedNameSpecifier::SpecifierKind Kind
7014 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7015 switch (Kind) {
7016 case NestedNameSpecifier::Identifier: {
7017 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7018 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7019 break;
7020 }
7021
7022 case NestedNameSpecifier::Namespace: {
7023 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7024 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7025 break;
7026 }
7027
7028 case NestedNameSpecifier::NamespaceAlias: {
7029 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7030 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7031 break;
7032 }
7033
7034 case NestedNameSpecifier::TypeSpec:
7035 case NestedNameSpecifier::TypeSpecWithTemplate: {
7036 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7037 if (!T)
7038 return 0;
7039
7040 bool Template = Record[Idx++];
7041 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7042 break;
7043 }
7044
7045 case NestedNameSpecifier::Global: {
7046 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7047 // No associated value, and there can't be a prefix.
7048 break;
7049 }
7050 }
7051 Prev = NNS;
7052 }
7053 return NNS;
7054}
7055
7056NestedNameSpecifierLoc
7057ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7058 unsigned &Idx) {
7059 unsigned N = Record[Idx++];
7060 NestedNameSpecifierLocBuilder Builder;
7061 for (unsigned I = 0; I != N; ++I) {
7062 NestedNameSpecifier::SpecifierKind Kind
7063 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7064 switch (Kind) {
7065 case NestedNameSpecifier::Identifier: {
7066 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7067 SourceRange Range = ReadSourceRange(F, Record, Idx);
7068 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7069 break;
7070 }
7071
7072 case NestedNameSpecifier::Namespace: {
7073 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7074 SourceRange Range = ReadSourceRange(F, Record, Idx);
7075 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7076 break;
7077 }
7078
7079 case NestedNameSpecifier::NamespaceAlias: {
7080 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7081 SourceRange Range = ReadSourceRange(F, Record, Idx);
7082 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7083 break;
7084 }
7085
7086 case NestedNameSpecifier::TypeSpec:
7087 case NestedNameSpecifier::TypeSpecWithTemplate: {
7088 bool Template = Record[Idx++];
7089 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7090 if (!T)
7091 return NestedNameSpecifierLoc();
7092 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7093
7094 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7095 Builder.Extend(Context,
7096 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7097 T->getTypeLoc(), ColonColonLoc);
7098 break;
7099 }
7100
7101 case NestedNameSpecifier::Global: {
7102 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7103 Builder.MakeGlobal(Context, ColonColonLoc);
7104 break;
7105 }
7106 }
7107 }
7108
7109 return Builder.getWithLocInContext(Context);
7110}
7111
7112SourceRange
7113ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7114 unsigned &Idx) {
7115 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7116 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7117 return SourceRange(beg, end);
7118}
7119
7120/// \brief Read an integral value
7121llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7122 unsigned BitWidth = Record[Idx++];
7123 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7124 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7125 Idx += NumWords;
7126 return Result;
7127}
7128
7129/// \brief Read a signed integral value
7130llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7131 bool isUnsigned = Record[Idx++];
7132 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7133}
7134
7135/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007136llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7137 const llvm::fltSemantics &Sem,
7138 unsigned &Idx) {
7139 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007140}
7141
7142// \brief Read a string
7143std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7144 unsigned Len = Record[Idx++];
7145 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7146 Idx += Len;
7147 return Result;
7148}
7149
7150VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7151 unsigned &Idx) {
7152 unsigned Major = Record[Idx++];
7153 unsigned Minor = Record[Idx++];
7154 unsigned Subminor = Record[Idx++];
7155 if (Minor == 0)
7156 return VersionTuple(Major);
7157 if (Subminor == 0)
7158 return VersionTuple(Major, Minor - 1);
7159 return VersionTuple(Major, Minor - 1, Subminor - 1);
7160}
7161
7162CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7163 const RecordData &Record,
7164 unsigned &Idx) {
7165 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7166 return CXXTemporary::Create(Context, Decl);
7167}
7168
7169DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7170 return Diag(SourceLocation(), DiagID);
7171}
7172
7173DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7174 return Diags.Report(Loc, DiagID);
7175}
7176
7177/// \brief Retrieve the identifier table associated with the
7178/// preprocessor.
7179IdentifierTable &ASTReader::getIdentifierTable() {
7180 return PP.getIdentifierTable();
7181}
7182
7183/// \brief Record that the given ID maps to the given switch-case
7184/// statement.
7185void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7186 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7187 "Already have a SwitchCase with this ID");
7188 (*CurrSwitchCaseStmts)[ID] = SC;
7189}
7190
7191/// \brief Retrieve the switch-case statement with the given ID.
7192SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7193 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7194 return (*CurrSwitchCaseStmts)[ID];
7195}
7196
7197void ASTReader::ClearSwitchCaseIDs() {
7198 CurrSwitchCaseStmts->clear();
7199}
7200
7201void ASTReader::ReadComments() {
7202 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007203 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007204 serialization::ModuleFile *> >::iterator
7205 I = CommentsCursors.begin(),
7206 E = CommentsCursors.end();
7207 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007208 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007209 serialization::ModuleFile &F = *I->second;
7210 SavedStreamPosition SavedPosition(Cursor);
7211
7212 RecordData Record;
7213 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007214 llvm::BitstreamEntry Entry =
7215 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7216
7217 switch (Entry.Kind) {
7218 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7219 case llvm::BitstreamEntry::Error:
7220 Error("malformed block record in AST file");
7221 return;
7222 case llvm::BitstreamEntry::EndBlock:
7223 goto NextCursor;
7224 case llvm::BitstreamEntry::Record:
7225 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007226 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007227 }
7228
7229 // Read a record.
7230 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007231 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007232 case COMMENTS_RAW_COMMENT: {
7233 unsigned Idx = 0;
7234 SourceRange SR = ReadSourceRange(F, Record, Idx);
7235 RawComment::CommentKind Kind =
7236 (RawComment::CommentKind) Record[Idx++];
7237 bool IsTrailingComment = Record[Idx++];
7238 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00007239 Comments.push_back(new (Context) RawComment(
7240 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7241 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007242 break;
7243 }
7244 }
7245 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007246 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007247 }
7248 Context.Comments.addCommentsToFront(Comments);
7249}
7250
7251void ASTReader::finishPendingActions() {
7252 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007253 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007254 // If any identifiers with corresponding top-level declarations have
7255 // been loaded, load those declarations now.
Douglas Gregoraa945902013-02-18 15:53:43 +00007256 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007257 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007258 // FIXME: std::move
7259 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7260 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00007261 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00007262
7263 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007264 }
7265
7266 // Load pending declaration chains.
7267 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7268 loadPendingDeclChain(PendingDeclChains[I]);
7269 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7270 }
7271 PendingDeclChains.clear();
7272
Douglas Gregoraa945902013-02-18 15:53:43 +00007273 // Make the most recent of the top-level declarations visible.
7274 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7275 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7276 TLD != TLDEnd; ++TLD) {
7277 IdentifierInfo *II = TLD->first;
7278 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007279 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregoraa945902013-02-18 15:53:43 +00007280 }
7281 }
7282
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007283 // Load any pending macro definitions.
7284 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007285 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7286 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7287 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7288 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00007289 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7290 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007291 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7292 if (Info.M->Kind != MK_Module)
7293 resolvePendingMacro(II, Info);
7294 }
7295 // Handle module imports.
7296 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7297 ++IDIdx) {
7298 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7299 if (Info.M->Kind == MK_Module)
7300 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007301 }
7302 }
7303 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007304
7305 // Wire up the DeclContexts for Decls that we delayed setting until
7306 // recursive loading is completed.
7307 while (!PendingDeclContextInfos.empty()) {
7308 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7309 PendingDeclContextInfos.pop_front();
7310 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7311 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7312 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7313 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007314 }
7315
7316 // If we deserialized any C++ or Objective-C class definitions, any
7317 // Objective-C protocol definitions, or any redeclarable templates, make sure
7318 // that all redeclarations point to the definitions. Note that this can only
7319 // happen now, after the redeclaration chains have been fully wired.
7320 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7321 DEnd = PendingDefinitions.end();
7322 D != DEnd; ++D) {
7323 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7324 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7325 // Make sure that the TagType points at the definition.
7326 const_cast<TagType*>(TagT)->decl = TD;
7327 }
7328
7329 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7330 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7331 REnd = RD->redecls_end();
7332 R != REnd; ++R)
7333 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7334
7335 }
7336
7337 continue;
7338 }
7339
7340 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7341 // Make sure that the ObjCInterfaceType points at the definition.
7342 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7343 ->Decl = ID;
7344
7345 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7346 REnd = ID->redecls_end();
7347 R != REnd; ++R)
7348 R->Data = ID->Data;
7349
7350 continue;
7351 }
7352
7353 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7354 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7355 REnd = PD->redecls_end();
7356 R != REnd; ++R)
7357 R->Data = PD->Data;
7358
7359 continue;
7360 }
7361
7362 RedeclarableTemplateDecl *RTD
7363 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7364 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7365 REnd = RTD->redecls_end();
7366 R != REnd; ++R)
7367 R->Common = RTD->Common;
7368 }
7369 PendingDefinitions.clear();
7370
7371 // Load the bodies of any functions or methods we've encountered. We do
7372 // this now (delayed) so that we can be sure that the declaration chains
7373 // have been fully wired up.
7374 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7375 PBEnd = PendingBodies.end();
7376 PB != PBEnd; ++PB) {
7377 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7378 // FIXME: Check for =delete/=default?
7379 // FIXME: Complain about ODR violations here?
7380 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7381 FD->setLazyBody(PB->second);
7382 continue;
7383 }
7384
7385 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7386 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7387 MD->setLazyBody(PB->second);
7388 }
7389 PendingBodies.clear();
7390}
7391
7392void ASTReader::FinishedDeserializing() {
7393 assert(NumCurrentElementsDeserializing &&
7394 "FinishedDeserializing not paired with StartedDeserializing");
7395 if (NumCurrentElementsDeserializing == 1) {
7396 // We decrease NumCurrentElementsDeserializing only after pending actions
7397 // are finished, to avoid recursively re-calling finishPendingActions().
7398 finishPendingActions();
7399 }
7400 --NumCurrentElementsDeserializing;
7401
7402 if (NumCurrentElementsDeserializing == 0 &&
7403 Consumer && !PassingDeclsToConsumer) {
7404 // Guard variable to avoid recursively redoing the process of passing
7405 // decls to consumer.
7406 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7407 true);
7408
7409 while (!InterestingDecls.empty()) {
7410 // We are not in recursive loading, so it's safe to pass the "interesting"
7411 // decls to the consumer.
7412 Decl *D = InterestingDecls.front();
7413 InterestingDecls.pop_front();
7414 PassInterestingDeclToConsumer(D);
7415 }
7416 }
7417}
7418
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007419void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7420 D = cast<NamedDecl>(D->getMostRecentDecl());
7421
7422 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7423 SemaObj->TUScope->AddDecl(D);
7424 } else if (SemaObj->TUScope) {
7425 // Adding the decl to IdResolver may have failed because it was already in
7426 // (even though it was not added in scope). If it is already in, make sure
7427 // it gets in the scope as well.
7428 if (std::find(SemaObj->IdResolver.begin(Name),
7429 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7430 SemaObj->TUScope->AddDecl(D);
7431 }
7432}
7433
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007434ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7435 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007436 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007437 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7438 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7439 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7440 Consumer(0), ModuleMgr(PP.getFileManager()),
7441 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007442 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007443 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007444 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7445 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007446 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7447 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7448 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007449 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7450 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7451 TotalNumMethodPoolEntries(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007452 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7453 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7454 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7455 PassingDeclsToConsumer(false),
7456 NumCXXBaseSpecifiersLoaded(0)
7457{
7458 SourceMgr.setExternalSLocEntrySource(this);
7459}
7460
7461ASTReader::~ASTReader() {
7462 for (DeclContextVisibleUpdatesPending::iterator
7463 I = PendingVisibleUpdates.begin(),
7464 E = PendingVisibleUpdates.end();
7465 I != E; ++I) {
7466 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7467 F = I->second.end();
7468 J != F; ++J)
7469 delete J->first;
7470 }
7471}