blob: a4c8c353077cdb73265332570342cd4e379bee21 [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) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00002903 llvm::SaveAndRestore<SourceLocation>
2904 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
2905
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002906 // Bump the generation number.
2907 unsigned PreviousGeneration = CurrentGeneration++;
2908
2909 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002910 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002911 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2912 /*ImportedBy=*/0, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002913 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002914 ClientLoadCapabilities)) {
2915 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002916 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002917 case OutOfDate:
2918 case VersionMismatch:
2919 case ConfigurationMismatch:
2920 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002921 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2922 Context.getLangOpts().Modules
2923 ? &PP.getHeaderSearchInfo().getModuleMap()
2924 : 0);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002925
2926 // If we find that any modules are unusable, the global index is going
2927 // to be out-of-date. Just remove it.
2928 GlobalIndex.reset();
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002929 ModuleMgr.setGlobalIndex(0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002930 return ReadResult;
2931
2932 case Success:
2933 break;
2934 }
2935
2936 // Here comes stuff that we only do once the entire chain is loaded.
2937
2938 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002939 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2940 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002941 M != MEnd; ++M) {
2942 ModuleFile &F = *M->Mod;
2943
2944 // Read the AST block.
2945 if (ReadASTBlock(F))
2946 return Failure;
2947
2948 // Once read, set the ModuleFile bit base offset and update the size in
2949 // bits of all files we've seen.
2950 F.GlobalBitOffset = TotalModulesSizeInBits;
2951 TotalModulesSizeInBits += F.SizeInBits;
2952 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2953
2954 // Preload SLocEntries.
2955 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2956 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2957 // Load it through the SourceManager and don't call ReadSLocEntry()
2958 // directly because the entry may have already been loaded in which case
2959 // calling ReadSLocEntry() directly would trigger an assertion in
2960 // SourceManager.
2961 SourceMgr.getLoadedSLocEntryByID(Index);
2962 }
2963 }
2964
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002965 // Setup the import locations and notify the module manager that we've
2966 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002967 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2968 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002969 M != MEnd; ++M) {
2970 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002971
2972 ModuleMgr.moduleFileAccepted(&F);
2973
2974 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00002975 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002976 if (!M->ImportedBy)
2977 F.ImportLoc = M->ImportLoc;
2978 else
2979 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2980 M->ImportLoc.getRawEncoding());
2981 }
2982
2983 // Mark all of the identifiers in the identifier table as being out of date,
2984 // so that various accessors know to check the loaded modules when the
2985 // identifier is used.
2986 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2987 IdEnd = PP.getIdentifierTable().end();
2988 Id != IdEnd; ++Id)
2989 Id->second->setOutOfDate(true);
2990
2991 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00002992 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2993 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002994 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2995 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00002996
2997 switch (Unresolved.Kind) {
2998 case UnresolvedModuleRef::Conflict:
2999 if (ResolvedMod) {
3000 Module::Conflict Conflict;
3001 Conflict.Other = ResolvedMod;
3002 Conflict.Message = Unresolved.String.str();
3003 Unresolved.Mod->Conflicts.push_back(Conflict);
3004 }
3005 continue;
3006
3007 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003008 if (ResolvedMod)
3009 Unresolved.Mod->Imports.push_back(ResolvedMod);
3010 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003011
Douglas Gregor906d66a2013-03-20 21:10:35 +00003012 case UnresolvedModuleRef::Export:
3013 if (ResolvedMod || Unresolved.IsWildcard)
3014 Unresolved.Mod->Exports.push_back(
3015 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3016 continue;
3017 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003018 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003019 UnresolvedModuleRefs.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003020
3021 InitializeContext();
3022
3023 if (DeserializationListener)
3024 DeserializationListener->ReaderInitialized(this);
3025
3026 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3027 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3028 PrimaryModule.OriginalSourceFileID
3029 = FileID::get(PrimaryModule.SLocEntryBaseID
3030 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3031
3032 // If this AST file is a precompiled preamble, then set the
3033 // preamble file ID of the source manager to the file source file
3034 // from which the preamble was built.
3035 if (Type == MK_Preamble) {
3036 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3037 } else if (Type == MK_MainFile) {
3038 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3039 }
3040 }
3041
3042 // For any Objective-C class definitions we have already loaded, make sure
3043 // that we load any additional categories.
3044 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3045 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3046 ObjCClassesLoaded[I],
3047 PreviousGeneration);
3048 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00003049
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003050 return Success;
3051}
3052
3053ASTReader::ASTReadResult
3054ASTReader::ReadASTCore(StringRef FileName,
3055 ModuleKind Type,
3056 SourceLocation ImportLoc,
3057 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003058 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003059 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003060 unsigned ClientLoadCapabilities) {
3061 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003062 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003063 ModuleManager::AddModuleResult AddResult
3064 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3065 CurrentGeneration, ExpectedSize, ExpectedModTime,
3066 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003067
Douglas Gregor677e15f2013-03-19 00:28:20 +00003068 switch (AddResult) {
3069 case ModuleManager::AlreadyLoaded:
3070 return Success;
3071
3072 case ModuleManager::NewlyLoaded:
3073 // Load module file below.
3074 break;
3075
3076 case ModuleManager::Missing:
3077 // The module file was missing; if the client handle handle, that, return
3078 // it.
3079 if (ClientLoadCapabilities & ARR_Missing)
3080 return Missing;
3081
3082 // Otherwise, return an error.
3083 {
3084 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3085 + ErrorStr;
3086 Error(Msg);
3087 }
3088 return Failure;
3089
3090 case ModuleManager::OutOfDate:
3091 // We couldn't load the module file because it is out-of-date. If the
3092 // client can handle out-of-date, return it.
3093 if (ClientLoadCapabilities & ARR_OutOfDate)
3094 return OutOfDate;
3095
3096 // Otherwise, return an error.
3097 {
3098 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3099 + ErrorStr;
3100 Error(Msg);
3101 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003102 return Failure;
3103 }
3104
Douglas Gregor677e15f2013-03-19 00:28:20 +00003105 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003106
3107 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3108 // module?
3109 if (FileName != "-") {
3110 CurrentDir = llvm::sys::path::parent_path(FileName);
3111 if (CurrentDir.empty()) CurrentDir = ".";
3112 }
3113
3114 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003115 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003116 Stream.init(F.StreamFile);
3117 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3118
3119 // Sniff for the signature.
3120 if (Stream.Read(8) != 'C' ||
3121 Stream.Read(8) != 'P' ||
3122 Stream.Read(8) != 'C' ||
3123 Stream.Read(8) != 'H') {
3124 Diag(diag::err_not_a_pch_file) << FileName;
3125 return Failure;
3126 }
3127
3128 // This is used for compatibility with older PCH formats.
3129 bool HaveReadControlBlock = false;
3130
Chris Lattner99a5af02013-01-20 00:00:22 +00003131 while (1) {
3132 llvm::BitstreamEntry Entry = Stream.advance();
3133
3134 switch (Entry.Kind) {
3135 case llvm::BitstreamEntry::Error:
3136 case llvm::BitstreamEntry::EndBlock:
3137 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003138 Error("invalid record at top-level of AST file");
3139 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003140
3141 case llvm::BitstreamEntry::SubBlock:
3142 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003143 }
3144
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003145 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003146 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003147 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3148 if (Stream.ReadBlockInfoBlock()) {
3149 Error("malformed BlockInfoBlock in AST file");
3150 return Failure;
3151 }
3152 break;
3153 case CONTROL_BLOCK_ID:
3154 HaveReadControlBlock = true;
3155 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3156 case Success:
3157 break;
3158
3159 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003160 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003161 case OutOfDate: return OutOfDate;
3162 case VersionMismatch: return VersionMismatch;
3163 case ConfigurationMismatch: return ConfigurationMismatch;
3164 case HadErrors: return HadErrors;
3165 }
3166 break;
3167 case AST_BLOCK_ID:
3168 if (!HaveReadControlBlock) {
3169 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3170 Diag(diag::warn_pch_version_too_old);
3171 return VersionMismatch;
3172 }
3173
3174 // Record that we've loaded this module.
3175 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3176 return Success;
3177
3178 default:
3179 if (Stream.SkipBlock()) {
3180 Error("malformed block record in AST file");
3181 return Failure;
3182 }
3183 break;
3184 }
3185 }
3186
3187 return Success;
3188}
3189
3190void ASTReader::InitializeContext() {
3191 // If there's a listener, notify them that we "read" the translation unit.
3192 if (DeserializationListener)
3193 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3194 Context.getTranslationUnitDecl());
3195
3196 // Make sure we load the declaration update records for the translation unit,
3197 // if there are any.
3198 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3199 Context.getTranslationUnitDecl());
3200
3201 // FIXME: Find a better way to deal with collisions between these
3202 // built-in types. Right now, we just ignore the problem.
3203
3204 // Load the special types.
3205 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3206 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3207 if (!Context.CFConstantStringTypeDecl)
3208 Context.setCFConstantStringType(GetType(String));
3209 }
3210
3211 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3212 QualType FileType = GetType(File);
3213 if (FileType.isNull()) {
3214 Error("FILE type is NULL");
3215 return;
3216 }
3217
3218 if (!Context.FILEDecl) {
3219 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3220 Context.setFILEDecl(Typedef->getDecl());
3221 else {
3222 const TagType *Tag = FileType->getAs<TagType>();
3223 if (!Tag) {
3224 Error("Invalid FILE type in AST file");
3225 return;
3226 }
3227 Context.setFILEDecl(Tag->getDecl());
3228 }
3229 }
3230 }
3231
3232 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3233 QualType Jmp_bufType = GetType(Jmp_buf);
3234 if (Jmp_bufType.isNull()) {
3235 Error("jmp_buf type is NULL");
3236 return;
3237 }
3238
3239 if (!Context.jmp_bufDecl) {
3240 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3241 Context.setjmp_bufDecl(Typedef->getDecl());
3242 else {
3243 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3244 if (!Tag) {
3245 Error("Invalid jmp_buf type in AST file");
3246 return;
3247 }
3248 Context.setjmp_bufDecl(Tag->getDecl());
3249 }
3250 }
3251 }
3252
3253 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3254 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3255 if (Sigjmp_bufType.isNull()) {
3256 Error("sigjmp_buf type is NULL");
3257 return;
3258 }
3259
3260 if (!Context.sigjmp_bufDecl) {
3261 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3262 Context.setsigjmp_bufDecl(Typedef->getDecl());
3263 else {
3264 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3265 assert(Tag && "Invalid sigjmp_buf type in AST file");
3266 Context.setsigjmp_bufDecl(Tag->getDecl());
3267 }
3268 }
3269 }
3270
3271 if (unsigned ObjCIdRedef
3272 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3273 if (Context.ObjCIdRedefinitionType.isNull())
3274 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3275 }
3276
3277 if (unsigned ObjCClassRedef
3278 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3279 if (Context.ObjCClassRedefinitionType.isNull())
3280 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3281 }
3282
3283 if (unsigned ObjCSelRedef
3284 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3285 if (Context.ObjCSelRedefinitionType.isNull())
3286 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3287 }
3288
3289 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3290 QualType Ucontext_tType = GetType(Ucontext_t);
3291 if (Ucontext_tType.isNull()) {
3292 Error("ucontext_t type is NULL");
3293 return;
3294 }
3295
3296 if (!Context.ucontext_tDecl) {
3297 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3298 Context.setucontext_tDecl(Typedef->getDecl());
3299 else {
3300 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3301 assert(Tag && "Invalid ucontext_t type in AST file");
3302 Context.setucontext_tDecl(Tag->getDecl());
3303 }
3304 }
3305 }
3306 }
3307
3308 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3309
3310 // If there were any CUDA special declarations, deserialize them.
3311 if (!CUDASpecialDeclRefs.empty()) {
3312 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3313 Context.setcudaConfigureCallDecl(
3314 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3315 }
3316
3317 // Re-export any modules that were imported by a non-module AST file.
3318 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3319 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003320 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003321 /*ImportLoc=*/SourceLocation(),
3322 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003323 }
3324 ImportedModules.clear();
3325}
3326
3327void ASTReader::finalizeForWriting() {
3328 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3329 HiddenEnd = HiddenNamesMap.end();
3330 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003331 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003332 }
3333 HiddenNamesMap.clear();
3334}
3335
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003336/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3337/// cursor into the start of the given block ID, returning false on success and
3338/// true on failure.
3339static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003340 while (1) {
3341 llvm::BitstreamEntry Entry = Cursor.advance();
3342 switch (Entry.Kind) {
3343 case llvm::BitstreamEntry::Error:
3344 case llvm::BitstreamEntry::EndBlock:
3345 return true;
3346
3347 case llvm::BitstreamEntry::Record:
3348 // Ignore top-level records.
3349 Cursor.skipRecord(Entry.ID);
3350 break;
3351
3352 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003353 if (Entry.ID == BlockID) {
3354 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003355 return true;
3356 // Found it!
3357 return false;
3358 }
3359
3360 if (Cursor.SkipBlock())
3361 return true;
3362 }
3363 }
3364}
3365
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003366/// \brief Retrieve the name of the original source file name
3367/// directly from the AST file, without actually loading the AST
3368/// file.
3369std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3370 FileManager &FileMgr,
3371 DiagnosticsEngine &Diags) {
3372 // Open the AST file.
3373 std::string ErrStr;
3374 OwningPtr<llvm::MemoryBuffer> Buffer;
3375 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3376 if (!Buffer) {
3377 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3378 return std::string();
3379 }
3380
3381 // Initialize the stream
3382 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003383 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003384 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3385 (const unsigned char *)Buffer->getBufferEnd());
3386 Stream.init(StreamFile);
3387
3388 // Sniff for the signature.
3389 if (Stream.Read(8) != 'C' ||
3390 Stream.Read(8) != 'P' ||
3391 Stream.Read(8) != 'C' ||
3392 Stream.Read(8) != 'H') {
3393 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3394 return std::string();
3395 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003396
Chris Lattner88bde502013-01-19 21:39:22 +00003397 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003398 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003399 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3400 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003401 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003402
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003403 // Scan for ORIGINAL_FILE inside the control block.
3404 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003405 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003406 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003407 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3408 return std::string();
3409
3410 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3411 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3412 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003413 }
Chris Lattner88bde502013-01-19 21:39:22 +00003414
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003415 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003416 StringRef Blob;
3417 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3418 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003419 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003420}
3421
3422namespace {
3423 class SimplePCHValidator : public ASTReaderListener {
3424 const LangOptions &ExistingLangOpts;
3425 const TargetOptions &ExistingTargetOpts;
3426 const PreprocessorOptions &ExistingPPOpts;
3427 FileManager &FileMgr;
3428
3429 public:
3430 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3431 const TargetOptions &ExistingTargetOpts,
3432 const PreprocessorOptions &ExistingPPOpts,
3433 FileManager &FileMgr)
3434 : ExistingLangOpts(ExistingLangOpts),
3435 ExistingTargetOpts(ExistingTargetOpts),
3436 ExistingPPOpts(ExistingPPOpts),
3437 FileMgr(FileMgr)
3438 {
3439 }
3440
3441 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3442 bool Complain) {
3443 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3444 }
3445 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3446 bool Complain) {
3447 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3448 }
3449 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3450 bool Complain,
3451 std::string &SuggestedPredefines) {
3452 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00003453 SuggestedPredefines, ExistingLangOpts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003454 }
3455 };
3456}
3457
3458bool ASTReader::readASTFileControlBlock(StringRef Filename,
3459 FileManager &FileMgr,
3460 ASTReaderListener &Listener) {
3461 // Open the AST file.
3462 std::string ErrStr;
3463 OwningPtr<llvm::MemoryBuffer> Buffer;
3464 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3465 if (!Buffer) {
3466 return true;
3467 }
3468
3469 // Initialize the stream
3470 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003471 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003472 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3473 (const unsigned char *)Buffer->getBufferEnd());
3474 Stream.init(StreamFile);
3475
3476 // Sniff for the signature.
3477 if (Stream.Read(8) != 'C' ||
3478 Stream.Read(8) != 'P' ||
3479 Stream.Read(8) != 'C' ||
3480 Stream.Read(8) != 'H') {
3481 return true;
3482 }
3483
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003484 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003485 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003486 return true;
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003487
3488 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3489 BitstreamCursor InputFilesCursor;
3490 if (NeedsInputFiles) {
3491 InputFilesCursor = Stream;
3492 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3493 return true;
3494
3495 // Read the abbreviations
3496 while (true) {
3497 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3498 unsigned Code = InputFilesCursor.ReadCode();
3499
3500 // We expect all abbrevs to be at the start of the block.
3501 if (Code != llvm::bitc::DEFINE_ABBREV) {
3502 InputFilesCursor.JumpToBit(Offset);
3503 break;
3504 }
3505 InputFilesCursor.ReadAbbrevRecord();
3506 }
3507 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003508
3509 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003510 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003511 while (1) {
3512 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3513 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3514 return false;
3515
3516 if (Entry.Kind != llvm::BitstreamEntry::Record)
3517 return true;
3518
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003519 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003520 StringRef Blob;
3521 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003522 switch ((ControlRecordTypes)RecCode) {
3523 case METADATA: {
3524 if (Record[0] != VERSION_MAJOR)
3525 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003526
Douglas Gregorc544ba02013-03-27 16:47:18 +00003527 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003528 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00003529
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003530 break;
3531 }
3532 case LANGUAGE_OPTIONS:
3533 if (ParseLanguageOptions(Record, false, Listener))
3534 return true;
3535 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003536
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003537 case TARGET_OPTIONS:
3538 if (ParseTargetOptions(Record, false, Listener))
3539 return true;
3540 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003541
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003542 case DIAGNOSTIC_OPTIONS:
3543 if (ParseDiagnosticOptions(Record, false, Listener))
3544 return true;
3545 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003546
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003547 case FILE_SYSTEM_OPTIONS:
3548 if (ParseFileSystemOptions(Record, false, Listener))
3549 return true;
3550 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003551
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003552 case HEADER_SEARCH_OPTIONS:
3553 if (ParseHeaderSearchOptions(Record, false, Listener))
3554 return true;
3555 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003556
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003557 case PREPROCESSOR_OPTIONS: {
3558 std::string IgnoredSuggestedPredefines;
3559 if (ParsePreprocessorOptions(Record, false, Listener,
3560 IgnoredSuggestedPredefines))
3561 return true;
3562 break;
3563 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003564
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003565 case INPUT_FILE_OFFSETS: {
3566 if (!NeedsInputFiles)
3567 break;
3568
3569 unsigned NumInputFiles = Record[0];
3570 unsigned NumUserFiles = Record[1];
3571 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3572 for (unsigned I = 0; I != NumInputFiles; ++I) {
3573 // Go find this input file.
3574 bool isSystemFile = I >= NumUserFiles;
3575 BitstreamCursor &Cursor = InputFilesCursor;
3576 SavedStreamPosition SavedPosition(Cursor);
3577 Cursor.JumpToBit(InputFileOffs[I]);
3578
3579 unsigned Code = Cursor.ReadCode();
3580 RecordData Record;
3581 StringRef Blob;
3582 bool shouldContinue = false;
3583 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3584 case INPUT_FILE:
3585 shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3586 break;
3587 }
3588 if (!shouldContinue)
3589 break;
3590 }
3591 break;
3592 }
3593
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003594 default:
3595 // No other validation to perform.
3596 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003597 }
3598 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003599}
3600
3601
3602bool ASTReader::isAcceptableASTFile(StringRef Filename,
3603 FileManager &FileMgr,
3604 const LangOptions &LangOpts,
3605 const TargetOptions &TargetOpts,
3606 const PreprocessorOptions &PPOpts) {
3607 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3608 return !readASTFileControlBlock(Filename, FileMgr, validator);
3609}
3610
3611bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3612 // Enter the submodule block.
3613 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3614 Error("malformed submodule block record in AST file");
3615 return true;
3616 }
3617
3618 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3619 bool First = true;
3620 Module *CurrentModule = 0;
3621 RecordData Record;
3622 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003623 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3624
3625 switch (Entry.Kind) {
3626 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3627 case llvm::BitstreamEntry::Error:
3628 Error("malformed block record in AST file");
3629 return true;
3630 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003631 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003632 case llvm::BitstreamEntry::Record:
3633 // The interesting case.
3634 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003635 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003636
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003637 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003638 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003639 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003640 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003641 default: // Default behavior: ignore.
3642 break;
3643
3644 case SUBMODULE_DEFINITION: {
3645 if (First) {
3646 Error("missing submodule metadata record at beginning of block");
3647 return true;
3648 }
3649
Douglas Gregor970e4412013-03-20 03:59:18 +00003650 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003651 Error("malformed module definition");
3652 return true;
3653 }
3654
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003655 StringRef Name = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003656 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3657 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3658 bool IsFramework = Record[2];
3659 bool IsExplicit = Record[3];
3660 bool IsSystem = Record[4];
3661 bool InferSubmodules = Record[5];
3662 bool InferExplicitSubmodules = Record[6];
3663 bool InferExportWildcard = Record[7];
Douglas Gregor970e4412013-03-20 03:59:18 +00003664 bool ConfigMacrosExhaustive = Record[8];
3665
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003666 Module *ParentModule = 0;
3667 if (Parent)
3668 ParentModule = getSubmodule(Parent);
3669
3670 // Retrieve this (sub)module from the module map, creating it if
3671 // necessary.
3672 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3673 IsFramework,
3674 IsExplicit).first;
3675 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3676 if (GlobalIndex >= SubmodulesLoaded.size() ||
3677 SubmodulesLoaded[GlobalIndex]) {
3678 Error("too many submodules");
3679 return true;
3680 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003681
Douglas Gregor677e15f2013-03-19 00:28:20 +00003682 if (!ParentModule) {
3683 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3684 if (CurFile != F.File) {
3685 if (!Diags.isDiagnosticInFlight()) {
3686 Diag(diag::err_module_file_conflict)
3687 << CurrentModule->getTopLevelModuleName()
3688 << CurFile->getName()
3689 << F.File->getName();
3690 }
3691 return true;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003692 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003693 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003694
3695 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003696 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003697
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003698 CurrentModule->IsFromModuleFile = true;
3699 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3700 CurrentModule->InferSubmodules = InferSubmodules;
3701 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3702 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00003703 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003704 if (DeserializationListener)
3705 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3706
3707 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003708
Douglas Gregor906d66a2013-03-20 21:10:35 +00003709 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003710 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00003711 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00003712 CurrentModule->UnresolvedConflicts.clear();
3713 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003714 break;
3715 }
3716
3717 case SUBMODULE_UMBRELLA_HEADER: {
3718 if (First) {
3719 Error("missing submodule metadata record at beginning of block");
3720 return true;
3721 }
3722
3723 if (!CurrentModule)
3724 break;
3725
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003726 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003727 if (!CurrentModule->getUmbrellaHeader())
3728 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3729 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3730 Error("mismatched umbrella headers in submodule");
3731 return true;
3732 }
3733 }
3734 break;
3735 }
3736
3737 case SUBMODULE_HEADER: {
3738 if (First) {
3739 Error("missing submodule metadata record at beginning of block");
3740 return true;
3741 }
3742
3743 if (!CurrentModule)
3744 break;
3745
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003746 // We lazily associate headers with their modules via the HeaderInfoTable.
3747 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3748 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003749 break;
3750 }
3751
3752 case SUBMODULE_EXCLUDED_HEADER: {
3753 if (First) {
3754 Error("missing submodule metadata record at beginning of block");
3755 return true;
3756 }
3757
3758 if (!CurrentModule)
3759 break;
3760
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003761 // We lazily associate headers with their modules via the HeaderInfoTable.
3762 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3763 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003764 break;
3765 }
3766
3767 case SUBMODULE_TOPHEADER: {
3768 if (First) {
3769 Error("missing submodule metadata record at beginning of block");
3770 return true;
3771 }
3772
3773 if (!CurrentModule)
3774 break;
3775
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00003776 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003777 break;
3778 }
3779
3780 case SUBMODULE_UMBRELLA_DIR: {
3781 if (First) {
3782 Error("missing submodule metadata record at beginning of block");
3783 return true;
3784 }
3785
3786 if (!CurrentModule)
3787 break;
3788
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003789 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003790 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003791 if (!CurrentModule->getUmbrellaDir())
3792 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3793 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3794 Error("mismatched umbrella directories in submodule");
3795 return true;
3796 }
3797 }
3798 break;
3799 }
3800
3801 case SUBMODULE_METADATA: {
3802 if (!First) {
3803 Error("submodule metadata record not at beginning of block");
3804 return true;
3805 }
3806 First = false;
3807
3808 F.BaseSubmoduleID = getTotalNumSubmodules();
3809 F.LocalNumSubmodules = Record[0];
3810 unsigned LocalBaseSubmoduleID = Record[1];
3811 if (F.LocalNumSubmodules > 0) {
3812 // Introduce the global -> local mapping for submodules within this
3813 // module.
3814 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3815
3816 // Introduce the local -> global mapping for submodules within this
3817 // module.
3818 F.SubmoduleRemap.insertOrReplace(
3819 std::make_pair(LocalBaseSubmoduleID,
3820 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3821
3822 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3823 }
3824 break;
3825 }
3826
3827 case SUBMODULE_IMPORTS: {
3828 if (First) {
3829 Error("missing submodule metadata record at beginning of block");
3830 return true;
3831 }
3832
3833 if (!CurrentModule)
3834 break;
3835
3836 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003837 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003838 Unresolved.File = &F;
3839 Unresolved.Mod = CurrentModule;
3840 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003841 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003842 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003843 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003844 }
3845 break;
3846 }
3847
3848 case SUBMODULE_EXPORTS: {
3849 if (First) {
3850 Error("missing submodule metadata record at beginning of block");
3851 return true;
3852 }
3853
3854 if (!CurrentModule)
3855 break;
3856
3857 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003858 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003859 Unresolved.File = &F;
3860 Unresolved.Mod = CurrentModule;
3861 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003862 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003863 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003864 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003865 }
3866
3867 // Once we've loaded the set of exports, there's no reason to keep
3868 // the parsed, unresolved exports around.
3869 CurrentModule->UnresolvedExports.clear();
3870 break;
3871 }
3872 case SUBMODULE_REQUIRES: {
3873 if (First) {
3874 Error("missing submodule metadata record at beginning of block");
3875 return true;
3876 }
3877
3878 if (!CurrentModule)
3879 break;
3880
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003881 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003882 Context.getTargetInfo());
3883 break;
3884 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003885
3886 case SUBMODULE_LINK_LIBRARY:
3887 if (First) {
3888 Error("missing submodule metadata record at beginning of block");
3889 return true;
3890 }
3891
3892 if (!CurrentModule)
3893 break;
3894
3895 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003896 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003897 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00003898
3899 case SUBMODULE_CONFIG_MACRO:
3900 if (First) {
3901 Error("missing submodule metadata record at beginning of block");
3902 return true;
3903 }
3904
3905 if (!CurrentModule)
3906 break;
3907
3908 CurrentModule->ConfigMacros.push_back(Blob.str());
3909 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003910
3911 case SUBMODULE_CONFLICT: {
3912 if (First) {
3913 Error("missing submodule metadata record at beginning of block");
3914 return true;
3915 }
3916
3917 if (!CurrentModule)
3918 break;
3919
3920 UnresolvedModuleRef Unresolved;
3921 Unresolved.File = &F;
3922 Unresolved.Mod = CurrentModule;
3923 Unresolved.ID = Record[0];
3924 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3925 Unresolved.IsWildcard = false;
3926 Unresolved.String = Blob;
3927 UnresolvedModuleRefs.push_back(Unresolved);
3928 break;
3929 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003930 }
3931 }
3932}
3933
3934/// \brief Parse the record that corresponds to a LangOptions data
3935/// structure.
3936///
3937/// This routine parses the language options from the AST file and then gives
3938/// them to the AST listener if one is set.
3939///
3940/// \returns true if the listener deems the file unacceptable, false otherwise.
3941bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3942 bool Complain,
3943 ASTReaderListener &Listener) {
3944 LangOptions LangOpts;
3945 unsigned Idx = 0;
3946#define LANGOPT(Name, Bits, Default, Description) \
3947 LangOpts.Name = Record[Idx++];
3948#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3949 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3950#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003951#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3952#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003953
3954 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3955 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3956 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3957
3958 unsigned Length = Record[Idx++];
3959 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3960 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003961
3962 Idx += Length;
3963
3964 // Comment options.
3965 for (unsigned N = Record[Idx++]; N; --N) {
3966 LangOpts.CommentOpts.BlockCommandNames.push_back(
3967 ReadString(Record, Idx));
3968 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00003969 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003970
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003971 return Listener.ReadLanguageOptions(LangOpts, Complain);
3972}
3973
3974bool ASTReader::ParseTargetOptions(const RecordData &Record,
3975 bool Complain,
3976 ASTReaderListener &Listener) {
3977 unsigned Idx = 0;
3978 TargetOptions TargetOpts;
3979 TargetOpts.Triple = ReadString(Record, Idx);
3980 TargetOpts.CPU = ReadString(Record, Idx);
3981 TargetOpts.ABI = ReadString(Record, Idx);
3982 TargetOpts.CXXABI = ReadString(Record, Idx);
3983 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3984 for (unsigned N = Record[Idx++]; N; --N) {
3985 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3986 }
3987 for (unsigned N = Record[Idx++]; N; --N) {
3988 TargetOpts.Features.push_back(ReadString(Record, Idx));
3989 }
3990
3991 return Listener.ReadTargetOptions(TargetOpts, Complain);
3992}
3993
3994bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3995 ASTReaderListener &Listener) {
3996 DiagnosticOptions DiagOpts;
3997 unsigned Idx = 0;
3998#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3999#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4000 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4001#include "clang/Basic/DiagnosticOptions.def"
4002
4003 for (unsigned N = Record[Idx++]; N; --N) {
4004 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4005 }
4006
4007 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4008}
4009
4010bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4011 ASTReaderListener &Listener) {
4012 FileSystemOptions FSOpts;
4013 unsigned Idx = 0;
4014 FSOpts.WorkingDir = ReadString(Record, Idx);
4015 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4016}
4017
4018bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4019 bool Complain,
4020 ASTReaderListener &Listener) {
4021 HeaderSearchOptions HSOpts;
4022 unsigned Idx = 0;
4023 HSOpts.Sysroot = ReadString(Record, Idx);
4024
4025 // Include entries.
4026 for (unsigned N = Record[Idx++]; N; --N) {
4027 std::string Path = ReadString(Record, Idx);
4028 frontend::IncludeDirGroup Group
4029 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004030 bool IsFramework = Record[Idx++];
4031 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004032 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00004033 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004034 }
4035
4036 // System header prefixes.
4037 for (unsigned N = Record[Idx++]; N; --N) {
4038 std::string Prefix = ReadString(Record, Idx);
4039 bool IsSystemHeader = Record[Idx++];
4040 HSOpts.SystemHeaderPrefixes.push_back(
4041 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4042 }
4043
4044 HSOpts.ResourceDir = ReadString(Record, Idx);
4045 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4046 HSOpts.DisableModuleHash = Record[Idx++];
4047 HSOpts.UseBuiltinIncludes = Record[Idx++];
4048 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4049 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4050 HSOpts.UseLibcxx = Record[Idx++];
4051
4052 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4053}
4054
4055bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4056 bool Complain,
4057 ASTReaderListener &Listener,
4058 std::string &SuggestedPredefines) {
4059 PreprocessorOptions PPOpts;
4060 unsigned Idx = 0;
4061
4062 // Macro definitions/undefs
4063 for (unsigned N = Record[Idx++]; N; --N) {
4064 std::string Macro = ReadString(Record, Idx);
4065 bool IsUndef = Record[Idx++];
4066 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4067 }
4068
4069 // Includes
4070 for (unsigned N = Record[Idx++]; N; --N) {
4071 PPOpts.Includes.push_back(ReadString(Record, Idx));
4072 }
4073
4074 // Macro Includes
4075 for (unsigned N = Record[Idx++]; N; --N) {
4076 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4077 }
4078
4079 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004080 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004081 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4082 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4083 PPOpts.ObjCXXARCStandardLibrary =
4084 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4085 SuggestedPredefines.clear();
4086 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4087 SuggestedPredefines);
4088}
4089
4090std::pair<ModuleFile *, unsigned>
4091ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4092 GlobalPreprocessedEntityMapType::iterator
4093 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4094 assert(I != GlobalPreprocessedEntityMap.end() &&
4095 "Corrupted global preprocessed entity map");
4096 ModuleFile *M = I->second;
4097 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4098 return std::make_pair(M, LocalIndex);
4099}
4100
4101std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4102ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4103 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4104 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4105 Mod.NumPreprocessedEntities);
4106
4107 return std::make_pair(PreprocessingRecord::iterator(),
4108 PreprocessingRecord::iterator());
4109}
4110
4111std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4112ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4113 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4114 ModuleDeclIterator(this, &Mod,
4115 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4116}
4117
4118PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4119 PreprocessedEntityID PPID = Index+1;
4120 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4121 ModuleFile &M = *PPInfo.first;
4122 unsigned LocalIndex = PPInfo.second;
4123 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4124
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004125 if (!PP.getPreprocessingRecord()) {
4126 Error("no preprocessing record");
4127 return 0;
4128 }
4129
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004130 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4131 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4132
4133 llvm::BitstreamEntry Entry =
4134 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4135 if (Entry.Kind != llvm::BitstreamEntry::Record)
4136 return 0;
4137
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004138 // Read the record.
4139 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4140 ReadSourceLocation(M, PPOffs.End));
4141 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004142 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004143 RecordData Record;
4144 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004145 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4146 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004147 switch (RecType) {
4148 case PPD_MACRO_EXPANSION: {
4149 bool isBuiltin = Record[0];
4150 IdentifierInfo *Name = 0;
4151 MacroDefinition *Def = 0;
4152 if (isBuiltin)
4153 Name = getLocalIdentifier(M, Record[1]);
4154 else {
4155 PreprocessedEntityID
4156 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4157 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4158 }
4159
4160 MacroExpansion *ME;
4161 if (isBuiltin)
4162 ME = new (PPRec) MacroExpansion(Name, Range);
4163 else
4164 ME = new (PPRec) MacroExpansion(Def, Range);
4165
4166 return ME;
4167 }
4168
4169 case PPD_MACRO_DEFINITION: {
4170 // Decode the identifier info and then check again; if the macro is
4171 // still defined and associated with the identifier,
4172 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4173 MacroDefinition *MD
4174 = new (PPRec) MacroDefinition(II, Range);
4175
4176 if (DeserializationListener)
4177 DeserializationListener->MacroDefinitionRead(PPID, MD);
4178
4179 return MD;
4180 }
4181
4182 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004183 const char *FullFileNameStart = Blob.data() + Record[0];
4184 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004185 const FileEntry *File = 0;
4186 if (!FullFileName.empty())
4187 File = PP.getFileManager().getFile(FullFileName);
4188
4189 // FIXME: Stable encoding
4190 InclusionDirective::InclusionKind Kind
4191 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4192 InclusionDirective *ID
4193 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004194 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004195 Record[1], Record[3],
4196 File,
4197 Range);
4198 return ID;
4199 }
4200 }
4201
4202 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4203}
4204
4205/// \brief \arg SLocMapI points at a chunk of a module that contains no
4206/// preprocessed entities or the entities it contains are not the ones we are
4207/// looking for. Find the next module that contains entities and return the ID
4208/// of the first entry.
4209PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4210 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4211 ++SLocMapI;
4212 for (GlobalSLocOffsetMapType::const_iterator
4213 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4214 ModuleFile &M = *SLocMapI->second;
4215 if (M.NumPreprocessedEntities)
4216 return M.BasePreprocessedEntityID;
4217 }
4218
4219 return getTotalNumPreprocessedEntities();
4220}
4221
4222namespace {
4223
4224template <unsigned PPEntityOffset::*PPLoc>
4225struct PPEntityComp {
4226 const ASTReader &Reader;
4227 ModuleFile &M;
4228
4229 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4230
4231 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4232 SourceLocation LHS = getLoc(L);
4233 SourceLocation RHS = getLoc(R);
4234 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4235 }
4236
4237 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4238 SourceLocation LHS = getLoc(L);
4239 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4240 }
4241
4242 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4243 SourceLocation RHS = getLoc(R);
4244 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4245 }
4246
4247 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4248 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4249 }
4250};
4251
4252}
4253
4254/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4255PreprocessedEntityID
4256ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4257 if (SourceMgr.isLocalSourceLocation(BLoc))
4258 return getTotalNumPreprocessedEntities();
4259
4260 GlobalSLocOffsetMapType::const_iterator
4261 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004262 BLoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004263 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4264 "Corrupted global sloc offset map");
4265
4266 if (SLocMapI->second->NumPreprocessedEntities == 0)
4267 return findNextPreprocessedEntity(SLocMapI);
4268
4269 ModuleFile &M = *SLocMapI->second;
4270 typedef const PPEntityOffset *pp_iterator;
4271 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4272 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4273
4274 size_t Count = M.NumPreprocessedEntities;
4275 size_t Half;
4276 pp_iterator First = pp_begin;
4277 pp_iterator PPI;
4278
4279 // Do a binary search manually instead of using std::lower_bound because
4280 // The end locations of entities may be unordered (when a macro expansion
4281 // is inside another macro argument), but for this case it is not important
4282 // whether we get the first macro expansion or its containing macro.
4283 while (Count > 0) {
4284 Half = Count/2;
4285 PPI = First;
4286 std::advance(PPI, Half);
4287 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4288 BLoc)){
4289 First = PPI;
4290 ++First;
4291 Count = Count - Half - 1;
4292 } else
4293 Count = Half;
4294 }
4295
4296 if (PPI == pp_end)
4297 return findNextPreprocessedEntity(SLocMapI);
4298
4299 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4300}
4301
4302/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4303PreprocessedEntityID
4304ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4305 if (SourceMgr.isLocalSourceLocation(ELoc))
4306 return getTotalNumPreprocessedEntities();
4307
4308 GlobalSLocOffsetMapType::const_iterator
4309 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004310 ELoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004311 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4312 "Corrupted global sloc offset map");
4313
4314 if (SLocMapI->second->NumPreprocessedEntities == 0)
4315 return findNextPreprocessedEntity(SLocMapI);
4316
4317 ModuleFile &M = *SLocMapI->second;
4318 typedef const PPEntityOffset *pp_iterator;
4319 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4320 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4321 pp_iterator PPI =
4322 std::upper_bound(pp_begin, pp_end, ELoc,
4323 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4324
4325 if (PPI == pp_end)
4326 return findNextPreprocessedEntity(SLocMapI);
4327
4328 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4329}
4330
4331/// \brief Returns a pair of [Begin, End) indices of preallocated
4332/// preprocessed entities that \arg Range encompasses.
4333std::pair<unsigned, unsigned>
4334 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4335 if (Range.isInvalid())
4336 return std::make_pair(0,0);
4337 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4338
4339 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4340 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4341 return std::make_pair(BeginID, EndID);
4342}
4343
4344/// \brief Optionally returns true or false if the preallocated preprocessed
4345/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00004346Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004347 FileID FID) {
4348 if (FID.isInvalid())
4349 return false;
4350
4351 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4352 ModuleFile &M = *PPInfo.first;
4353 unsigned LocalIndex = PPInfo.second;
4354 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4355
4356 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4357 if (Loc.isInvalid())
4358 return false;
4359
4360 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4361 return true;
4362 else
4363 return false;
4364}
4365
4366namespace {
4367 /// \brief Visitor used to search for information about a header file.
4368 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004369 const FileEntry *FE;
4370
David Blaikiedc84cd52013-02-20 22:23:23 +00004371 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004372
4373 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004374 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4375 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004376
4377 static bool visit(ModuleFile &M, void *UserData) {
4378 HeaderFileInfoVisitor *This
4379 = static_cast<HeaderFileInfoVisitor *>(UserData);
4380
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004381 HeaderFileInfoLookupTable *Table
4382 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4383 if (!Table)
4384 return false;
4385
4386 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00004387 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004388 if (Pos == Table->end())
4389 return false;
4390
4391 This->HFI = *Pos;
4392 return true;
4393 }
4394
David Blaikiedc84cd52013-02-20 22:23:23 +00004395 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004396 };
4397}
4398
4399HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004400 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004401 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidisf9ba8512013-05-08 23:46:55 +00004402 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004403 return *HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004404
4405 return HeaderFileInfo();
4406}
4407
4408void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4409 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004410 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004411 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4412 ModuleFile &F = *(*I);
4413 unsigned Idx = 0;
4414 DiagStates.clear();
4415 assert(!Diag.DiagStates.empty());
4416 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4417 while (Idx < F.PragmaDiagMappings.size()) {
4418 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4419 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4420 if (DiagStateID != 0) {
4421 Diag.DiagStatePoints.push_back(
4422 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4423 FullSourceLoc(Loc, SourceMgr)));
4424 continue;
4425 }
4426
4427 assert(DiagStateID == 0);
4428 // A new DiagState was created here.
4429 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4430 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4431 DiagStates.push_back(NewState);
4432 Diag.DiagStatePoints.push_back(
4433 DiagnosticsEngine::DiagStatePoint(NewState,
4434 FullSourceLoc(Loc, SourceMgr)));
4435 while (1) {
4436 assert(Idx < F.PragmaDiagMappings.size() &&
4437 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4438 if (Idx >= F.PragmaDiagMappings.size()) {
4439 break; // Something is messed up but at least avoid infinite loop in
4440 // release build.
4441 }
4442 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4443 if (DiagID == (unsigned)-1) {
4444 break; // no more diag/map pairs for this location.
4445 }
4446 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4447 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4448 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4449 }
4450 }
4451 }
4452}
4453
4454/// \brief Get the correct cursor and offset for loading a type.
4455ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4456 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4457 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4458 ModuleFile *M = I->second;
4459 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4460}
4461
4462/// \brief Read and return the type with the given index..
4463///
4464/// The index is the type ID, shifted and minus the number of predefs. This
4465/// routine actually reads the record corresponding to the type at the given
4466/// location. It is a helper routine for GetType, which deals with reading type
4467/// IDs.
4468QualType ASTReader::readTypeRecord(unsigned Index) {
4469 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004470 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004471
4472 // Keep track of where we are in the stream, then jump back there
4473 // after reading this type.
4474 SavedStreamPosition SavedPosition(DeclsCursor);
4475
4476 ReadingKindTracker ReadingKind(Read_Type, *this);
4477
4478 // Note that we are loading a type record.
4479 Deserializing AType(this);
4480
4481 unsigned Idx = 0;
4482 DeclsCursor.JumpToBit(Loc.Offset);
4483 RecordData Record;
4484 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004485 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004486 case TYPE_EXT_QUAL: {
4487 if (Record.size() != 2) {
4488 Error("Incorrect encoding of extended qualifier type");
4489 return QualType();
4490 }
4491 QualType Base = readType(*Loc.F, Record, Idx);
4492 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4493 return Context.getQualifiedType(Base, Quals);
4494 }
4495
4496 case TYPE_COMPLEX: {
4497 if (Record.size() != 1) {
4498 Error("Incorrect encoding of complex type");
4499 return QualType();
4500 }
4501 QualType ElemType = readType(*Loc.F, Record, Idx);
4502 return Context.getComplexType(ElemType);
4503 }
4504
4505 case TYPE_POINTER: {
4506 if (Record.size() != 1) {
4507 Error("Incorrect encoding of pointer type");
4508 return QualType();
4509 }
4510 QualType PointeeType = readType(*Loc.F, Record, Idx);
4511 return Context.getPointerType(PointeeType);
4512 }
4513
4514 case TYPE_BLOCK_POINTER: {
4515 if (Record.size() != 1) {
4516 Error("Incorrect encoding of block pointer type");
4517 return QualType();
4518 }
4519 QualType PointeeType = readType(*Loc.F, Record, Idx);
4520 return Context.getBlockPointerType(PointeeType);
4521 }
4522
4523 case TYPE_LVALUE_REFERENCE: {
4524 if (Record.size() != 2) {
4525 Error("Incorrect encoding of lvalue reference type");
4526 return QualType();
4527 }
4528 QualType PointeeType = readType(*Loc.F, Record, Idx);
4529 return Context.getLValueReferenceType(PointeeType, Record[1]);
4530 }
4531
4532 case TYPE_RVALUE_REFERENCE: {
4533 if (Record.size() != 1) {
4534 Error("Incorrect encoding of rvalue reference type");
4535 return QualType();
4536 }
4537 QualType PointeeType = readType(*Loc.F, Record, Idx);
4538 return Context.getRValueReferenceType(PointeeType);
4539 }
4540
4541 case TYPE_MEMBER_POINTER: {
4542 if (Record.size() != 2) {
4543 Error("Incorrect encoding of member pointer type");
4544 return QualType();
4545 }
4546 QualType PointeeType = readType(*Loc.F, Record, Idx);
4547 QualType ClassType = readType(*Loc.F, Record, Idx);
4548 if (PointeeType.isNull() || ClassType.isNull())
4549 return QualType();
4550
4551 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4552 }
4553
4554 case TYPE_CONSTANT_ARRAY: {
4555 QualType ElementType = readType(*Loc.F, Record, Idx);
4556 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4557 unsigned IndexTypeQuals = Record[2];
4558 unsigned Idx = 3;
4559 llvm::APInt Size = ReadAPInt(Record, Idx);
4560 return Context.getConstantArrayType(ElementType, Size,
4561 ASM, IndexTypeQuals);
4562 }
4563
4564 case TYPE_INCOMPLETE_ARRAY: {
4565 QualType ElementType = readType(*Loc.F, Record, Idx);
4566 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4567 unsigned IndexTypeQuals = Record[2];
4568 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4569 }
4570
4571 case TYPE_VARIABLE_ARRAY: {
4572 QualType ElementType = readType(*Loc.F, Record, Idx);
4573 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4574 unsigned IndexTypeQuals = Record[2];
4575 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4576 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4577 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4578 ASM, IndexTypeQuals,
4579 SourceRange(LBLoc, RBLoc));
4580 }
4581
4582 case TYPE_VECTOR: {
4583 if (Record.size() != 3) {
4584 Error("incorrect encoding of vector type in AST file");
4585 return QualType();
4586 }
4587
4588 QualType ElementType = readType(*Loc.F, Record, Idx);
4589 unsigned NumElements = Record[1];
4590 unsigned VecKind = Record[2];
4591 return Context.getVectorType(ElementType, NumElements,
4592 (VectorType::VectorKind)VecKind);
4593 }
4594
4595 case TYPE_EXT_VECTOR: {
4596 if (Record.size() != 3) {
4597 Error("incorrect encoding of extended vector type in AST file");
4598 return QualType();
4599 }
4600
4601 QualType ElementType = readType(*Loc.F, Record, Idx);
4602 unsigned NumElements = Record[1];
4603 return Context.getExtVectorType(ElementType, NumElements);
4604 }
4605
4606 case TYPE_FUNCTION_NO_PROTO: {
4607 if (Record.size() != 6) {
4608 Error("incorrect encoding of no-proto function type");
4609 return QualType();
4610 }
4611 QualType ResultType = readType(*Loc.F, Record, Idx);
4612 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4613 (CallingConv)Record[4], Record[5]);
4614 return Context.getFunctionNoProtoType(ResultType, Info);
4615 }
4616
4617 case TYPE_FUNCTION_PROTO: {
4618 QualType ResultType = readType(*Loc.F, Record, Idx);
4619
4620 FunctionProtoType::ExtProtoInfo EPI;
4621 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4622 /*hasregparm*/ Record[2],
4623 /*regparm*/ Record[3],
4624 static_cast<CallingConv>(Record[4]),
4625 /*produces*/ Record[5]);
4626
4627 unsigned Idx = 6;
4628 unsigned NumParams = Record[Idx++];
4629 SmallVector<QualType, 16> ParamTypes;
4630 for (unsigned I = 0; I != NumParams; ++I)
4631 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4632
4633 EPI.Variadic = Record[Idx++];
4634 EPI.HasTrailingReturn = Record[Idx++];
4635 EPI.TypeQuals = Record[Idx++];
4636 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4637 ExceptionSpecificationType EST =
4638 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4639 EPI.ExceptionSpecType = EST;
4640 SmallVector<QualType, 2> Exceptions;
4641 if (EST == EST_Dynamic) {
4642 EPI.NumExceptions = Record[Idx++];
4643 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4644 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4645 EPI.Exceptions = Exceptions.data();
4646 } else if (EST == EST_ComputedNoexcept) {
4647 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4648 } else if (EST == EST_Uninstantiated) {
4649 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4650 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4651 } else if (EST == EST_Unevaluated) {
4652 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4653 }
Jordan Rosebea522f2013-03-08 21:51:21 +00004654 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004655 }
4656
4657 case TYPE_UNRESOLVED_USING: {
4658 unsigned Idx = 0;
4659 return Context.getTypeDeclType(
4660 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4661 }
4662
4663 case TYPE_TYPEDEF: {
4664 if (Record.size() != 2) {
4665 Error("incorrect encoding of typedef type");
4666 return QualType();
4667 }
4668 unsigned Idx = 0;
4669 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4670 QualType Canonical = readType(*Loc.F, Record, Idx);
4671 if (!Canonical.isNull())
4672 Canonical = Context.getCanonicalType(Canonical);
4673 return Context.getTypedefType(Decl, Canonical);
4674 }
4675
4676 case TYPE_TYPEOF_EXPR:
4677 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4678
4679 case TYPE_TYPEOF: {
4680 if (Record.size() != 1) {
4681 Error("incorrect encoding of typeof(type) in AST file");
4682 return QualType();
4683 }
4684 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4685 return Context.getTypeOfType(UnderlyingType);
4686 }
4687
4688 case TYPE_DECLTYPE: {
4689 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4690 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4691 }
4692
4693 case TYPE_UNARY_TRANSFORM: {
4694 QualType BaseType = readType(*Loc.F, Record, Idx);
4695 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4696 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4697 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4698 }
4699
Richard Smitha2c36462013-04-26 16:15:35 +00004700 case TYPE_AUTO: {
4701 QualType Deduced = readType(*Loc.F, Record, Idx);
4702 bool IsDecltypeAuto = Record[Idx++];
Richard Smithdc7a4f52013-04-30 13:56:41 +00004703 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
4704 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smitha2c36462013-04-26 16:15:35 +00004705 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004706
4707 case TYPE_RECORD: {
4708 if (Record.size() != 2) {
4709 Error("incorrect encoding of record type");
4710 return QualType();
4711 }
4712 unsigned Idx = 0;
4713 bool IsDependent = Record[Idx++];
4714 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4715 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4716 QualType T = Context.getRecordType(RD);
4717 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4718 return T;
4719 }
4720
4721 case TYPE_ENUM: {
4722 if (Record.size() != 2) {
4723 Error("incorrect encoding of enum type");
4724 return QualType();
4725 }
4726 unsigned Idx = 0;
4727 bool IsDependent = Record[Idx++];
4728 QualType T
4729 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4730 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4731 return T;
4732 }
4733
4734 case TYPE_ATTRIBUTED: {
4735 if (Record.size() != 3) {
4736 Error("incorrect encoding of attributed type");
4737 return QualType();
4738 }
4739 QualType modifiedType = readType(*Loc.F, Record, Idx);
4740 QualType equivalentType = readType(*Loc.F, Record, Idx);
4741 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4742 return Context.getAttributedType(kind, modifiedType, equivalentType);
4743 }
4744
4745 case TYPE_PAREN: {
4746 if (Record.size() != 1) {
4747 Error("incorrect encoding of paren type");
4748 return QualType();
4749 }
4750 QualType InnerType = readType(*Loc.F, Record, Idx);
4751 return Context.getParenType(InnerType);
4752 }
4753
4754 case TYPE_PACK_EXPANSION: {
4755 if (Record.size() != 2) {
4756 Error("incorrect encoding of pack expansion type");
4757 return QualType();
4758 }
4759 QualType Pattern = readType(*Loc.F, Record, Idx);
4760 if (Pattern.isNull())
4761 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00004762 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004763 if (Record[1])
4764 NumExpansions = Record[1] - 1;
4765 return Context.getPackExpansionType(Pattern, NumExpansions);
4766 }
4767
4768 case TYPE_ELABORATED: {
4769 unsigned Idx = 0;
4770 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4771 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4772 QualType NamedType = readType(*Loc.F, Record, Idx);
4773 return Context.getElaboratedType(Keyword, NNS, NamedType);
4774 }
4775
4776 case TYPE_OBJC_INTERFACE: {
4777 unsigned Idx = 0;
4778 ObjCInterfaceDecl *ItfD
4779 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4780 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4781 }
4782
4783 case TYPE_OBJC_OBJECT: {
4784 unsigned Idx = 0;
4785 QualType Base = readType(*Loc.F, Record, Idx);
4786 unsigned NumProtos = Record[Idx++];
4787 SmallVector<ObjCProtocolDecl*, 4> Protos;
4788 for (unsigned I = 0; I != NumProtos; ++I)
4789 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4790 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4791 }
4792
4793 case TYPE_OBJC_OBJECT_POINTER: {
4794 unsigned Idx = 0;
4795 QualType Pointee = readType(*Loc.F, Record, Idx);
4796 return Context.getObjCObjectPointerType(Pointee);
4797 }
4798
4799 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4800 unsigned Idx = 0;
4801 QualType Parm = readType(*Loc.F, Record, Idx);
4802 QualType Replacement = readType(*Loc.F, Record, Idx);
4803 return
4804 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4805 Replacement);
4806 }
4807
4808 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4809 unsigned Idx = 0;
4810 QualType Parm = readType(*Loc.F, Record, Idx);
4811 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4812 return Context.getSubstTemplateTypeParmPackType(
4813 cast<TemplateTypeParmType>(Parm),
4814 ArgPack);
4815 }
4816
4817 case TYPE_INJECTED_CLASS_NAME: {
4818 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4819 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4820 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4821 // for AST reading, too much interdependencies.
4822 return
4823 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4824 }
4825
4826 case TYPE_TEMPLATE_TYPE_PARM: {
4827 unsigned Idx = 0;
4828 unsigned Depth = Record[Idx++];
4829 unsigned Index = Record[Idx++];
4830 bool Pack = Record[Idx++];
4831 TemplateTypeParmDecl *D
4832 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4833 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4834 }
4835
4836 case TYPE_DEPENDENT_NAME: {
4837 unsigned Idx = 0;
4838 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4839 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4840 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4841 QualType Canon = readType(*Loc.F, Record, Idx);
4842 if (!Canon.isNull())
4843 Canon = Context.getCanonicalType(Canon);
4844 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4845 }
4846
4847 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4848 unsigned Idx = 0;
4849 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4850 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4851 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4852 unsigned NumArgs = Record[Idx++];
4853 SmallVector<TemplateArgument, 8> Args;
4854 Args.reserve(NumArgs);
4855 while (NumArgs--)
4856 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4857 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4858 Args.size(), Args.data());
4859 }
4860
4861 case TYPE_DEPENDENT_SIZED_ARRAY: {
4862 unsigned Idx = 0;
4863
4864 // ArrayType
4865 QualType ElementType = readType(*Loc.F, Record, Idx);
4866 ArrayType::ArraySizeModifier ASM
4867 = (ArrayType::ArraySizeModifier)Record[Idx++];
4868 unsigned IndexTypeQuals = Record[Idx++];
4869
4870 // DependentSizedArrayType
4871 Expr *NumElts = ReadExpr(*Loc.F);
4872 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4873
4874 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4875 IndexTypeQuals, Brackets);
4876 }
4877
4878 case TYPE_TEMPLATE_SPECIALIZATION: {
4879 unsigned Idx = 0;
4880 bool IsDependent = Record[Idx++];
4881 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4882 SmallVector<TemplateArgument, 8> Args;
4883 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4884 QualType Underlying = readType(*Loc.F, Record, Idx);
4885 QualType T;
4886 if (Underlying.isNull())
4887 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4888 Args.size());
4889 else
4890 T = Context.getTemplateSpecializationType(Name, Args.data(),
4891 Args.size(), Underlying);
4892 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4893 return T;
4894 }
4895
4896 case TYPE_ATOMIC: {
4897 if (Record.size() != 1) {
4898 Error("Incorrect encoding of atomic type");
4899 return QualType();
4900 }
4901 QualType ValueType = readType(*Loc.F, Record, Idx);
4902 return Context.getAtomicType(ValueType);
4903 }
4904 }
4905 llvm_unreachable("Invalid TypeCode!");
4906}
4907
4908class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4909 ASTReader &Reader;
4910 ModuleFile &F;
4911 const ASTReader::RecordData &Record;
4912 unsigned &Idx;
4913
4914 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4915 unsigned &I) {
4916 return Reader.ReadSourceLocation(F, R, I);
4917 }
4918
4919 template<typename T>
4920 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4921 return Reader.ReadDeclAs<T>(F, Record, Idx);
4922 }
4923
4924public:
4925 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4926 const ASTReader::RecordData &Record, unsigned &Idx)
4927 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4928 { }
4929
4930 // We want compile-time assurance that we've enumerated all of
4931 // these, so unfortunately we have to declare them first, then
4932 // define them out-of-line.
4933#define ABSTRACT_TYPELOC(CLASS, PARENT)
4934#define TYPELOC(CLASS, PARENT) \
4935 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4936#include "clang/AST/TypeLocNodes.def"
4937
4938 void VisitFunctionTypeLoc(FunctionTypeLoc);
4939 void VisitArrayTypeLoc(ArrayTypeLoc);
4940};
4941
4942void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4943 // nothing to do
4944}
4945void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4946 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4947 if (TL.needsExtraLocalData()) {
4948 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4949 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4950 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4951 TL.setModeAttr(Record[Idx++]);
4952 }
4953}
4954void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4955 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4956}
4957void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4958 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4959}
4960void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4961 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4962}
4963void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4964 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4965}
4966void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4967 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4968}
4969void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4970 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4971 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4972}
4973void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4974 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4975 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4976 if (Record[Idx++])
4977 TL.setSizeExpr(Reader.ReadExpr(F));
4978 else
4979 TL.setSizeExpr(0);
4980}
4981void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4982 VisitArrayTypeLoc(TL);
4983}
4984void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4985 VisitArrayTypeLoc(TL);
4986}
4987void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4988 VisitArrayTypeLoc(TL);
4989}
4990void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4991 DependentSizedArrayTypeLoc TL) {
4992 VisitArrayTypeLoc(TL);
4993}
4994void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4995 DependentSizedExtVectorTypeLoc TL) {
4996 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4997}
4998void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4999 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5000}
5001void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5002 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5003}
5004void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5005 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5006 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5007 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5008 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5009 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
5010 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5011 }
5012}
5013void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5014 VisitFunctionTypeLoc(TL);
5015}
5016void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5017 VisitFunctionTypeLoc(TL);
5018}
5019void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5020 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5021}
5022void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5023 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5024}
5025void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5026 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5027 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5028 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5029}
5030void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5031 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5032 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5033 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5034 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5035}
5036void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5037 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5038}
5039void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5040 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5041 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5042 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5043 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5044}
5045void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5046 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5047}
5048void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5049 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5050}
5051void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5052 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5053}
5054void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5055 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5056 if (TL.hasAttrOperand()) {
5057 SourceRange range;
5058 range.setBegin(ReadSourceLocation(Record, Idx));
5059 range.setEnd(ReadSourceLocation(Record, Idx));
5060 TL.setAttrOperandParensRange(range);
5061 }
5062 if (TL.hasAttrExprOperand()) {
5063 if (Record[Idx++])
5064 TL.setAttrExprOperand(Reader.ReadExpr(F));
5065 else
5066 TL.setAttrExprOperand(0);
5067 } else if (TL.hasAttrEnumOperand())
5068 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5069}
5070void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5071 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5072}
5073void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5074 SubstTemplateTypeParmTypeLoc TL) {
5075 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5076}
5077void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5078 SubstTemplateTypeParmPackTypeLoc TL) {
5079 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5080}
5081void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5082 TemplateSpecializationTypeLoc TL) {
5083 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5084 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5085 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5086 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5087 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5088 TL.setArgLocInfo(i,
5089 Reader.GetTemplateArgumentLocInfo(F,
5090 TL.getTypePtr()->getArg(i).getKind(),
5091 Record, Idx));
5092}
5093void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5094 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5095 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5096}
5097void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5098 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5099 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5100}
5101void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5102 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5103}
5104void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5105 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5106 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5107 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5108}
5109void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5110 DependentTemplateSpecializationTypeLoc TL) {
5111 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5112 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5113 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5114 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5115 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5116 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5117 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5118 TL.setArgLocInfo(I,
5119 Reader.GetTemplateArgumentLocInfo(F,
5120 TL.getTypePtr()->getArg(I).getKind(),
5121 Record, Idx));
5122}
5123void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5124 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5125}
5126void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5127 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5128}
5129void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5130 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5131 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5132 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5133 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5134 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5135}
5136void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5137 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5138}
5139void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5140 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5141 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5142 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5143}
5144
5145TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5146 const RecordData &Record,
5147 unsigned &Idx) {
5148 QualType InfoTy = readType(F, Record, Idx);
5149 if (InfoTy.isNull())
5150 return 0;
5151
5152 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5153 TypeLocReader TLR(*this, F, Record, Idx);
5154 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5155 TLR.Visit(TL);
5156 return TInfo;
5157}
5158
5159QualType ASTReader::GetType(TypeID ID) {
5160 unsigned FastQuals = ID & Qualifiers::FastMask;
5161 unsigned Index = ID >> Qualifiers::FastWidth;
5162
5163 if (Index < NUM_PREDEF_TYPE_IDS) {
5164 QualType T;
5165 switch ((PredefinedTypeIDs)Index) {
5166 case PREDEF_TYPE_NULL_ID: return QualType();
5167 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5168 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5169
5170 case PREDEF_TYPE_CHAR_U_ID:
5171 case PREDEF_TYPE_CHAR_S_ID:
5172 // FIXME: Check that the signedness of CharTy is correct!
5173 T = Context.CharTy;
5174 break;
5175
5176 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5177 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5178 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5179 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5180 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5181 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5182 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5183 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5184 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5185 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5186 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5187 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5188 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5189 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5190 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5191 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5192 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5193 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5194 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5195 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5196 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5197 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5198 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5199 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5200 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5201 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5202 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5203 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005204 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5205 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5206 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5207 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5208 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5209 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005210 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005211 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005212 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5213
5214 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5215 T = Context.getAutoRRefDeductType();
5216 break;
5217
5218 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5219 T = Context.ARCUnbridgedCastTy;
5220 break;
5221
5222 case PREDEF_TYPE_VA_LIST_TAG:
5223 T = Context.getVaListTagType();
5224 break;
5225
5226 case PREDEF_TYPE_BUILTIN_FN:
5227 T = Context.BuiltinFnTy;
5228 break;
5229 }
5230
5231 assert(!T.isNull() && "Unknown predefined type");
5232 return T.withFastQualifiers(FastQuals);
5233 }
5234
5235 Index -= NUM_PREDEF_TYPE_IDS;
5236 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5237 if (TypesLoaded[Index].isNull()) {
5238 TypesLoaded[Index] = readTypeRecord(Index);
5239 if (TypesLoaded[Index].isNull())
5240 return QualType();
5241
5242 TypesLoaded[Index]->setFromAST();
5243 if (DeserializationListener)
5244 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5245 TypesLoaded[Index]);
5246 }
5247
5248 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5249}
5250
5251QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5252 return GetType(getGlobalTypeID(F, LocalID));
5253}
5254
5255serialization::TypeID
5256ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5257 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5258 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5259
5260 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5261 return LocalID;
5262
5263 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5264 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5265 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5266
5267 unsigned GlobalIndex = LocalIndex + I->second;
5268 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5269}
5270
5271TemplateArgumentLocInfo
5272ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5273 TemplateArgument::ArgKind Kind,
5274 const RecordData &Record,
5275 unsigned &Index) {
5276 switch (Kind) {
5277 case TemplateArgument::Expression:
5278 return ReadExpr(F);
5279 case TemplateArgument::Type:
5280 return GetTypeSourceInfo(F, Record, Index);
5281 case TemplateArgument::Template: {
5282 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5283 Index);
5284 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5285 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5286 SourceLocation());
5287 }
5288 case TemplateArgument::TemplateExpansion: {
5289 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5290 Index);
5291 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5292 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5293 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5294 EllipsisLoc);
5295 }
5296 case TemplateArgument::Null:
5297 case TemplateArgument::Integral:
5298 case TemplateArgument::Declaration:
5299 case TemplateArgument::NullPtr:
5300 case TemplateArgument::Pack:
5301 // FIXME: Is this right?
5302 return TemplateArgumentLocInfo();
5303 }
5304 llvm_unreachable("unexpected template argument loc");
5305}
5306
5307TemplateArgumentLoc
5308ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5309 const RecordData &Record, unsigned &Index) {
5310 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5311
5312 if (Arg.getKind() == TemplateArgument::Expression) {
5313 if (Record[Index++]) // bool InfoHasSameExpr.
5314 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5315 }
5316 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5317 Record, Index));
5318}
5319
5320Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5321 return GetDecl(ID);
5322}
5323
5324uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5325 unsigned &Idx){
5326 if (Idx >= Record.size())
5327 return 0;
5328
5329 unsigned LocalID = Record[Idx++];
5330 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5331}
5332
5333CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5334 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005335 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005336 SavedStreamPosition SavedPosition(Cursor);
5337 Cursor.JumpToBit(Loc.Offset);
5338 ReadingKindTracker ReadingKind(Read_Decl, *this);
5339 RecordData Record;
5340 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005341 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005342 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5343 Error("Malformed AST file: missing C++ base specifiers");
5344 return 0;
5345 }
5346
5347 unsigned Idx = 0;
5348 unsigned NumBases = Record[Idx++];
5349 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5350 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5351 for (unsigned I = 0; I != NumBases; ++I)
5352 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5353 return Bases;
5354}
5355
5356serialization::DeclID
5357ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5358 if (LocalID < NUM_PREDEF_DECL_IDS)
5359 return LocalID;
5360
5361 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5362 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5363 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5364
5365 return LocalID + I->second;
5366}
5367
5368bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5369 ModuleFile &M) const {
5370 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5371 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5372 return &M == I->second;
5373}
5374
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005375ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005376 if (!D->isFromASTFile())
5377 return 0;
5378 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5379 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5380 return I->second;
5381}
5382
5383SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5384 if (ID < NUM_PREDEF_DECL_IDS)
5385 return SourceLocation();
5386
5387 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5388
5389 if (Index > DeclsLoaded.size()) {
5390 Error("declaration ID out-of-range for AST file");
5391 return SourceLocation();
5392 }
5393
5394 if (Decl *D = DeclsLoaded[Index])
5395 return D->getLocation();
5396
5397 unsigned RawLocation = 0;
5398 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5399 return ReadSourceLocation(*Rec.F, RawLocation);
5400}
5401
5402Decl *ASTReader::GetDecl(DeclID ID) {
5403 if (ID < NUM_PREDEF_DECL_IDS) {
5404 switch ((PredefinedDeclIDs)ID) {
5405 case PREDEF_DECL_NULL_ID:
5406 return 0;
5407
5408 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5409 return Context.getTranslationUnitDecl();
5410
5411 case PREDEF_DECL_OBJC_ID_ID:
5412 return Context.getObjCIdDecl();
5413
5414 case PREDEF_DECL_OBJC_SEL_ID:
5415 return Context.getObjCSelDecl();
5416
5417 case PREDEF_DECL_OBJC_CLASS_ID:
5418 return Context.getObjCClassDecl();
5419
5420 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5421 return Context.getObjCProtocolDecl();
5422
5423 case PREDEF_DECL_INT_128_ID:
5424 return Context.getInt128Decl();
5425
5426 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5427 return Context.getUInt128Decl();
5428
5429 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5430 return Context.getObjCInstanceTypeDecl();
5431
5432 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5433 return Context.getBuiltinVaListDecl();
5434 }
5435 }
5436
5437 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5438
5439 if (Index >= DeclsLoaded.size()) {
5440 assert(0 && "declaration ID out-of-range for AST file");
5441 Error("declaration ID out-of-range for AST file");
5442 return 0;
5443 }
5444
5445 if (!DeclsLoaded[Index]) {
5446 ReadDeclRecord(ID);
5447 if (DeserializationListener)
5448 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5449 }
5450
5451 return DeclsLoaded[Index];
5452}
5453
5454DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5455 DeclID GlobalID) {
5456 if (GlobalID < NUM_PREDEF_DECL_IDS)
5457 return GlobalID;
5458
5459 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5460 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5461 ModuleFile *Owner = I->second;
5462
5463 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5464 = M.GlobalToLocalDeclIDs.find(Owner);
5465 if (Pos == M.GlobalToLocalDeclIDs.end())
5466 return 0;
5467
5468 return GlobalID - Owner->BaseDeclID + Pos->second;
5469}
5470
5471serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5472 const RecordData &Record,
5473 unsigned &Idx) {
5474 if (Idx >= Record.size()) {
5475 Error("Corrupted AST file");
5476 return 0;
5477 }
5478
5479 return getGlobalDeclID(F, Record[Idx++]);
5480}
5481
5482/// \brief Resolve the offset of a statement into a statement.
5483///
5484/// This operation will read a new statement from the external
5485/// source each time it is called, and is meant to be used via a
5486/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5487Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5488 // Switch case IDs are per Decl.
5489 ClearSwitchCaseIDs();
5490
5491 // Offset here is a global offset across the entire chain.
5492 RecordLocation Loc = getLocalBitOffset(Offset);
5493 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5494 return ReadStmtFromStream(*Loc.F);
5495}
5496
5497namespace {
5498 class FindExternalLexicalDeclsVisitor {
5499 ASTReader &Reader;
5500 const DeclContext *DC;
5501 bool (*isKindWeWant)(Decl::Kind);
5502
5503 SmallVectorImpl<Decl*> &Decls;
5504 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5505
5506 public:
5507 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5508 bool (*isKindWeWant)(Decl::Kind),
5509 SmallVectorImpl<Decl*> &Decls)
5510 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5511 {
5512 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5513 PredefsVisited[I] = false;
5514 }
5515
5516 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5517 if (Preorder)
5518 return false;
5519
5520 FindExternalLexicalDeclsVisitor *This
5521 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5522
5523 ModuleFile::DeclContextInfosMap::iterator Info
5524 = M.DeclContextInfos.find(This->DC);
5525 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5526 return false;
5527
5528 // Load all of the declaration IDs
5529 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5530 *IDE = ID + Info->second.NumLexicalDecls;
5531 ID != IDE; ++ID) {
5532 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5533 continue;
5534
5535 // Don't add predefined declarations to the lexical context more
5536 // than once.
5537 if (ID->second < NUM_PREDEF_DECL_IDS) {
5538 if (This->PredefsVisited[ID->second])
5539 continue;
5540
5541 This->PredefsVisited[ID->second] = true;
5542 }
5543
5544 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5545 if (!This->DC->isDeclInLexicalTraversal(D))
5546 This->Decls.push_back(D);
5547 }
5548 }
5549
5550 return false;
5551 }
5552 };
5553}
5554
5555ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5556 bool (*isKindWeWant)(Decl::Kind),
5557 SmallVectorImpl<Decl*> &Decls) {
5558 // There might be lexical decls in multiple modules, for the TU at
5559 // least. Walk all of the modules in the order they were loaded.
5560 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5561 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5562 ++NumLexicalDeclContextsRead;
5563 return ELR_Success;
5564}
5565
5566namespace {
5567
5568class DeclIDComp {
5569 ASTReader &Reader;
5570 ModuleFile &Mod;
5571
5572public:
5573 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5574
5575 bool operator()(LocalDeclID L, LocalDeclID R) const {
5576 SourceLocation LHS = getLocation(L);
5577 SourceLocation RHS = getLocation(R);
5578 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5579 }
5580
5581 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5582 SourceLocation RHS = getLocation(R);
5583 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5584 }
5585
5586 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5587 SourceLocation LHS = getLocation(L);
5588 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5589 }
5590
5591 SourceLocation getLocation(LocalDeclID ID) const {
5592 return Reader.getSourceManager().getFileLoc(
5593 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5594 }
5595};
5596
5597}
5598
5599void ASTReader::FindFileRegionDecls(FileID File,
5600 unsigned Offset, unsigned Length,
5601 SmallVectorImpl<Decl *> &Decls) {
5602 SourceManager &SM = getSourceManager();
5603
5604 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5605 if (I == FileDeclIDs.end())
5606 return;
5607
5608 FileDeclsInfo &DInfo = I->second;
5609 if (DInfo.Decls.empty())
5610 return;
5611
5612 SourceLocation
5613 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5614 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5615
5616 DeclIDComp DIDComp(*this, *DInfo.Mod);
5617 ArrayRef<serialization::LocalDeclID>::iterator
5618 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5619 BeginLoc, DIDComp);
5620 if (BeginIt != DInfo.Decls.begin())
5621 --BeginIt;
5622
5623 // If we are pointing at a top-level decl inside an objc container, we need
5624 // to backtrack until we find it otherwise we will fail to report that the
5625 // region overlaps with an objc container.
5626 while (BeginIt != DInfo.Decls.begin() &&
5627 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5628 ->isTopLevelDeclInObjCContainer())
5629 --BeginIt;
5630
5631 ArrayRef<serialization::LocalDeclID>::iterator
5632 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5633 EndLoc, DIDComp);
5634 if (EndIt != DInfo.Decls.end())
5635 ++EndIt;
5636
5637 for (ArrayRef<serialization::LocalDeclID>::iterator
5638 DIt = BeginIt; DIt != EndIt; ++DIt)
5639 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5640}
5641
5642namespace {
5643 /// \brief ModuleFile visitor used to perform name lookup into a
5644 /// declaration context.
5645 class DeclContextNameLookupVisitor {
5646 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005647 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005648 DeclarationName Name;
5649 SmallVectorImpl<NamedDecl *> &Decls;
5650
5651 public:
5652 DeclContextNameLookupVisitor(ASTReader &Reader,
5653 SmallVectorImpl<const DeclContext *> &Contexts,
5654 DeclarationName Name,
5655 SmallVectorImpl<NamedDecl *> &Decls)
5656 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5657
5658 static bool visit(ModuleFile &M, void *UserData) {
5659 DeclContextNameLookupVisitor *This
5660 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5661
5662 // Check whether we have any visible declaration information for
5663 // this context in this module.
5664 ModuleFile::DeclContextInfosMap::iterator Info;
5665 bool FoundInfo = false;
5666 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5667 Info = M.DeclContextInfos.find(This->Contexts[I]);
5668 if (Info != M.DeclContextInfos.end() &&
5669 Info->second.NameLookupTableData) {
5670 FoundInfo = true;
5671 break;
5672 }
5673 }
5674
5675 if (!FoundInfo)
5676 return false;
5677
5678 // Look for this name within this module.
5679 ASTDeclContextNameLookupTable *LookupTable =
5680 Info->second.NameLookupTableData;
5681 ASTDeclContextNameLookupTable::iterator Pos
5682 = LookupTable->find(This->Name);
5683 if (Pos == LookupTable->end())
5684 return false;
5685
5686 bool FoundAnything = false;
5687 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5688 for (; Data.first != Data.second; ++Data.first) {
5689 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5690 if (!ND)
5691 continue;
5692
5693 if (ND->getDeclName() != This->Name) {
5694 // A name might be null because the decl's redeclarable part is
5695 // currently read before reading its name. The lookup is triggered by
5696 // building that decl (likely indirectly), and so it is later in the
5697 // sense of "already existing" and can be ignored here.
5698 continue;
5699 }
5700
5701 // Record this declaration.
5702 FoundAnything = true;
5703 This->Decls.push_back(ND);
5704 }
5705
5706 return FoundAnything;
5707 }
5708 };
5709}
5710
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005711/// \brief Retrieve the "definitive" module file for the definition of the
5712/// given declaration context, if there is one.
5713///
5714/// The "definitive" module file is the only place where we need to look to
5715/// find information about the declarations within the given declaration
5716/// context. For example, C++ and Objective-C classes, C structs/unions, and
5717/// Objective-C protocols, categories, and extensions are all defined in a
5718/// single place in the source code, so they have definitive module files
5719/// associated with them. C++ namespaces, on the other hand, can have
5720/// definitions in multiple different module files.
5721///
5722/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5723/// NDEBUG checking.
5724static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5725 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005726 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5727 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005728
5729 return 0;
5730}
5731
Richard Smith3646c682013-02-07 03:30:24 +00005732bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005733ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5734 DeclarationName Name) {
5735 assert(DC->hasExternalVisibleStorage() &&
5736 "DeclContext has no visible decls in storage");
5737 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00005738 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005739
5740 SmallVector<NamedDecl *, 64> Decls;
5741
5742 // Compute the declaration contexts we need to look into. Multiple such
5743 // declaration contexts occur when two declaration contexts from disjoint
5744 // modules get merged, e.g., when two namespaces with the same name are
5745 // independently defined in separate modules.
5746 SmallVector<const DeclContext *, 2> Contexts;
5747 Contexts.push_back(DC);
5748
5749 if (DC->isNamespace()) {
5750 MergedDeclsMap::iterator Merged
5751 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5752 if (Merged != MergedDecls.end()) {
5753 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5754 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5755 }
5756 }
5757
5758 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005759
5760 // If we can definitively determine which module file to look into,
5761 // only look there. Otherwise, look in all module files.
5762 ModuleFile *Definitive;
5763 if (Contexts.size() == 1 &&
5764 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5765 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5766 } else {
5767 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5768 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005769 ++NumVisibleDeclContextsRead;
5770 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00005771 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005772}
5773
5774namespace {
5775 /// \brief ModuleFile visitor used to retrieve all visible names in a
5776 /// declaration context.
5777 class DeclContextAllNamesVisitor {
5778 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005779 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005780 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005781 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005782
5783 public:
5784 DeclContextAllNamesVisitor(ASTReader &Reader,
5785 SmallVectorImpl<const DeclContext *> &Contexts,
5786 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005787 SmallVector<NamedDecl *, 8> > &Decls,
5788 bool VisitAll)
5789 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005790
5791 static bool visit(ModuleFile &M, void *UserData) {
5792 DeclContextAllNamesVisitor *This
5793 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5794
5795 // Check whether we have any visible declaration information for
5796 // this context in this module.
5797 ModuleFile::DeclContextInfosMap::iterator Info;
5798 bool FoundInfo = false;
5799 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5800 Info = M.DeclContextInfos.find(This->Contexts[I]);
5801 if (Info != M.DeclContextInfos.end() &&
5802 Info->second.NameLookupTableData) {
5803 FoundInfo = true;
5804 break;
5805 }
5806 }
5807
5808 if (!FoundInfo)
5809 return false;
5810
5811 ASTDeclContextNameLookupTable *LookupTable =
5812 Info->second.NameLookupTableData;
5813 bool FoundAnything = false;
5814 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005815 I = LookupTable->data_begin(), E = LookupTable->data_end();
5816 I != E;
5817 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005818 ASTDeclContextNameLookupTrait::data_type Data = *I;
5819 for (; Data.first != Data.second; ++Data.first) {
5820 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5821 *Data.first);
5822 if (!ND)
5823 continue;
5824
5825 // Record this declaration.
5826 FoundAnything = true;
5827 This->Decls[ND->getDeclName()].push_back(ND);
5828 }
5829 }
5830
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005831 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005832 }
5833 };
5834}
5835
5836void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5837 if (!DC->hasExternalVisibleStorage())
5838 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005839 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005840
5841 // Compute the declaration contexts we need to look into. Multiple such
5842 // declaration contexts occur when two declaration contexts from disjoint
5843 // modules get merged, e.g., when two namespaces with the same name are
5844 // independently defined in separate modules.
5845 SmallVector<const DeclContext *, 2> Contexts;
5846 Contexts.push_back(DC);
5847
5848 if (DC->isNamespace()) {
5849 MergedDeclsMap::iterator Merged
5850 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5851 if (Merged != MergedDecls.end()) {
5852 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5853 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5854 }
5855 }
5856
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005857 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5858 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005859 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5860 ++NumVisibleDeclContextsRead;
5861
5862 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005863 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005864 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5865 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5866 }
5867 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5868}
5869
5870/// \brief Under non-PCH compilation the consumer receives the objc methods
5871/// before receiving the implementation, and codegen depends on this.
5872/// We simulate this by deserializing and passing to consumer the methods of the
5873/// implementation before passing the deserialized implementation decl.
5874static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5875 ASTConsumer *Consumer) {
5876 assert(ImplD && Consumer);
5877
5878 for (ObjCImplDecl::method_iterator
5879 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5880 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5881
5882 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5883}
5884
5885void ASTReader::PassInterestingDeclsToConsumer() {
5886 assert(Consumer);
5887 while (!InterestingDecls.empty()) {
5888 Decl *D = InterestingDecls.front();
5889 InterestingDecls.pop_front();
5890
5891 PassInterestingDeclToConsumer(D);
5892 }
5893}
5894
5895void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5896 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5897 PassObjCImplDeclToConsumer(ImplD, Consumer);
5898 else
5899 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5900}
5901
5902void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5903 this->Consumer = Consumer;
5904
5905 if (!Consumer)
5906 return;
5907
5908 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5909 // Force deserialization of this decl, which will cause it to be queued for
5910 // passing to the consumer.
5911 GetDecl(ExternalDefinitions[I]);
5912 }
5913 ExternalDefinitions.clear();
5914
5915 PassInterestingDeclsToConsumer();
5916}
5917
5918void ASTReader::PrintStats() {
5919 std::fprintf(stderr, "*** AST File Statistics:\n");
5920
5921 unsigned NumTypesLoaded
5922 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5923 QualType());
5924 unsigned NumDeclsLoaded
5925 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5926 (Decl *)0);
5927 unsigned NumIdentifiersLoaded
5928 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5929 IdentifiersLoaded.end(),
5930 (IdentifierInfo *)0);
5931 unsigned NumMacrosLoaded
5932 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5933 MacrosLoaded.end(),
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005934 (MacroInfo *)0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005935 unsigned NumSelectorsLoaded
5936 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5937 SelectorsLoaded.end(),
5938 Selector());
5939
5940 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5941 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5942 NumSLocEntriesRead, TotalNumSLocEntries,
5943 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5944 if (!TypesLoaded.empty())
5945 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5946 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5947 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5948 if (!DeclsLoaded.empty())
5949 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5950 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5951 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5952 if (!IdentifiersLoaded.empty())
5953 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5954 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5955 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5956 if (!MacrosLoaded.empty())
5957 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5958 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5959 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5960 if (!SelectorsLoaded.empty())
5961 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5962 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5963 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5964 if (TotalNumStatements)
5965 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5966 NumStatementsRead, TotalNumStatements,
5967 ((float)NumStatementsRead/TotalNumStatements * 100));
5968 if (TotalNumMacros)
5969 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5970 NumMacrosRead, TotalNumMacros,
5971 ((float)NumMacrosRead/TotalNumMacros * 100));
5972 if (TotalLexicalDeclContexts)
5973 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5974 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5975 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5976 * 100));
5977 if (TotalVisibleDeclContexts)
5978 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5979 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5980 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5981 * 100));
5982 if (TotalNumMethodPoolEntries) {
5983 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5984 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5985 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5986 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005987 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00005988 if (NumMethodPoolLookups) {
5989 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5990 NumMethodPoolHits, NumMethodPoolLookups,
5991 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5992 }
5993 if (NumMethodPoolTableLookups) {
5994 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5995 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5996 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5997 * 100.0));
5998 }
5999
Douglas Gregore1698072013-01-25 00:38:33 +00006000 if (NumIdentifierLookupHits) {
6001 std::fprintf(stderr,
6002 " %u / %u identifier table lookups succeeded (%f%%)\n",
6003 NumIdentifierLookupHits, NumIdentifierLookups,
6004 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6005 }
6006
Douglas Gregor1a49d972013-01-25 01:03:03 +00006007 if (GlobalIndex) {
6008 std::fprintf(stderr, "\n");
6009 GlobalIndex->printStats();
6010 }
6011
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006012 std::fprintf(stderr, "\n");
6013 dump();
6014 std::fprintf(stderr, "\n");
6015}
6016
6017template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6018static void
6019dumpModuleIDMap(StringRef Name,
6020 const ContinuousRangeMap<Key, ModuleFile *,
6021 InitialCapacity> &Map) {
6022 if (Map.begin() == Map.end())
6023 return;
6024
6025 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6026 llvm::errs() << Name << ":\n";
6027 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6028 I != IEnd; ++I) {
6029 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6030 << "\n";
6031 }
6032}
6033
6034void ASTReader::dump() {
6035 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6036 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6037 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6038 dumpModuleIDMap("Global type map", GlobalTypeMap);
6039 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6040 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6041 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6042 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6043 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6044 dumpModuleIDMap("Global preprocessed entity map",
6045 GlobalPreprocessedEntityMap);
6046
6047 llvm::errs() << "\n*** PCH/Modules Loaded:";
6048 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6049 MEnd = ModuleMgr.end();
6050 M != MEnd; ++M)
6051 (*M)->dump();
6052}
6053
6054/// Return the amount of memory used by memory buffers, breaking down
6055/// by heap-backed versus mmap'ed memory.
6056void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6057 for (ModuleConstIterator I = ModuleMgr.begin(),
6058 E = ModuleMgr.end(); I != E; ++I) {
6059 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6060 size_t bytes = buf->getBufferSize();
6061 switch (buf->getBufferKind()) {
6062 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6063 sizes.malloc_bytes += bytes;
6064 break;
6065 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6066 sizes.mmap_bytes += bytes;
6067 break;
6068 }
6069 }
6070 }
6071}
6072
6073void ASTReader::InitializeSema(Sema &S) {
6074 SemaObj = &S;
6075 S.addExternalSource(this);
6076
6077 // Makes sure any declarations that were deserialized "too early"
6078 // still get added to the identifier's declaration chains.
6079 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006080 pushExternalDeclIntoScope(PreloadedDecls[I],
6081 PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006082 }
6083 PreloadedDecls.clear();
6084
6085 // Load the offsets of the declarations that Sema references.
6086 // They will be lazily deserialized when needed.
6087 if (!SemaDeclRefs.empty()) {
6088 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6089 if (!SemaObj->StdNamespace)
6090 SemaObj->StdNamespace = SemaDeclRefs[0];
6091 if (!SemaObj->StdBadAlloc)
6092 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6093 }
6094
6095 if (!FPPragmaOptions.empty()) {
6096 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6097 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6098 }
6099
6100 if (!OpenCLExtensions.empty()) {
6101 unsigned I = 0;
6102#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6103#include "clang/Basic/OpenCLExtensions.def"
6104
6105 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6106 }
6107}
6108
6109IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6110 // Note that we are loading an identifier.
6111 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006112 StringRef Name(NameStart, NameEnd - NameStart);
6113
6114 // If there is a global index, look there first to determine which modules
6115 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006116 GlobalModuleIndex::HitSet Hits;
6117 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006118 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006119 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6120 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006121 }
6122 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006123 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006124 NumIdentifierLookups,
6125 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006126 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006127 IdentifierInfo *II = Visitor.getIdentifierInfo();
6128 markIdentifierUpToDate(II);
6129 return II;
6130}
6131
6132namespace clang {
6133 /// \brief An identifier-lookup iterator that enumerates all of the
6134 /// identifiers stored within a set of AST files.
6135 class ASTIdentifierIterator : public IdentifierIterator {
6136 /// \brief The AST reader whose identifiers are being enumerated.
6137 const ASTReader &Reader;
6138
6139 /// \brief The current index into the chain of AST files stored in
6140 /// the AST reader.
6141 unsigned Index;
6142
6143 /// \brief The current position within the identifier lookup table
6144 /// of the current AST file.
6145 ASTIdentifierLookupTable::key_iterator Current;
6146
6147 /// \brief The end position within the identifier lookup table of
6148 /// the current AST file.
6149 ASTIdentifierLookupTable::key_iterator End;
6150
6151 public:
6152 explicit ASTIdentifierIterator(const ASTReader &Reader);
6153
6154 virtual StringRef Next();
6155 };
6156}
6157
6158ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6159 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6160 ASTIdentifierLookupTable *IdTable
6161 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6162 Current = IdTable->key_begin();
6163 End = IdTable->key_end();
6164}
6165
6166StringRef ASTIdentifierIterator::Next() {
6167 while (Current == End) {
6168 // If we have exhausted all of our AST files, we're done.
6169 if (Index == 0)
6170 return StringRef();
6171
6172 --Index;
6173 ASTIdentifierLookupTable *IdTable
6174 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6175 IdentifierLookupTable;
6176 Current = IdTable->key_begin();
6177 End = IdTable->key_end();
6178 }
6179
6180 // We have any identifiers remaining in the current AST file; return
6181 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006182 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006183 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006184 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006185}
6186
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +00006187IdentifierIterator *ASTReader::getIdentifiers() {
6188 if (!loadGlobalIndex())
6189 return GlobalIndex->createIdentifierIterator();
6190
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006191 return new ASTIdentifierIterator(*this);
6192}
6193
6194namespace clang { namespace serialization {
6195 class ReadMethodPoolVisitor {
6196 ASTReader &Reader;
6197 Selector Sel;
6198 unsigned PriorGeneration;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006199 unsigned InstanceBits;
6200 unsigned FactoryBits;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006201 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6202 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006203
6204 public:
6205 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6206 unsigned PriorGeneration)
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006207 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6208 InstanceBits(0), FactoryBits(0) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006209
6210 static bool visit(ModuleFile &M, void *UserData) {
6211 ReadMethodPoolVisitor *This
6212 = static_cast<ReadMethodPoolVisitor *>(UserData);
6213
6214 if (!M.SelectorLookupTable)
6215 return false;
6216
6217 // If we've already searched this module file, skip it now.
6218 if (M.Generation <= This->PriorGeneration)
6219 return true;
6220
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006221 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006222 ASTSelectorLookupTable *PoolTable
6223 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6224 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6225 if (Pos == PoolTable->end())
6226 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006227
6228 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006229 ++This->Reader.NumSelectorsRead;
6230 // FIXME: Not quite happy with the statistics here. We probably should
6231 // disable this tracking when called via LoadSelector.
6232 // Also, should entries without methods count as misses?
6233 ++This->Reader.NumMethodPoolEntriesRead;
6234 ASTSelectorLookupTrait::data_type Data = *Pos;
6235 if (This->Reader.DeserializationListener)
6236 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6237 This->Sel);
6238
6239 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6240 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006241 This->InstanceBits = Data.InstanceBits;
6242 This->FactoryBits = Data.FactoryBits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006243 return true;
6244 }
6245
6246 /// \brief Retrieve the instance methods found by this visitor.
6247 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6248 return InstanceMethods;
6249 }
6250
6251 /// \brief Retrieve the instance methods found by this visitor.
6252 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6253 return FactoryMethods;
6254 }
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006255
6256 unsigned getInstanceBits() const { return InstanceBits; }
6257 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006258 };
6259} } // end namespace clang::serialization
6260
6261/// \brief Add the given set of methods to the method list.
6262static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6263 ObjCMethodList &List) {
6264 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6265 S.addMethodToGlobalList(&List, Methods[I]);
6266 }
6267}
6268
6269void ASTReader::ReadMethodPool(Selector Sel) {
6270 // Get the selector generation and update it to the current generation.
6271 unsigned &Generation = SelectorGeneration[Sel];
6272 unsigned PriorGeneration = Generation;
6273 Generation = CurrentGeneration;
6274
6275 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006276 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006277 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6278 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6279
6280 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006281 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006282 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006283
6284 ++NumMethodPoolHits;
6285
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006286 if (!getSema())
6287 return;
6288
6289 Sema &S = *getSema();
6290 Sema::GlobalMethodPool::iterator Pos
6291 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6292
6293 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6294 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006295 Pos->second.first.setBits(Visitor.getInstanceBits());
6296 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006297}
6298
6299void ASTReader::ReadKnownNamespaces(
6300 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6301 Namespaces.clear();
6302
6303 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6304 if (NamespaceDecl *Namespace
6305 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6306 Namespaces.push_back(Namespace);
6307 }
6308}
6309
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006310void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00006311 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006312 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6313 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00006314 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006315 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00006316 Undefined.insert(std::make_pair(D, Loc));
6317 }
6318}
Nick Lewycky01a41142013-01-26 00:35:08 +00006319
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006320void ASTReader::ReadTentativeDefinitions(
6321 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6322 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6323 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6324 if (Var)
6325 TentativeDefs.push_back(Var);
6326 }
6327 TentativeDefinitions.clear();
6328}
6329
6330void ASTReader::ReadUnusedFileScopedDecls(
6331 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6332 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6333 DeclaratorDecl *D
6334 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6335 if (D)
6336 Decls.push_back(D);
6337 }
6338 UnusedFileScopedDecls.clear();
6339}
6340
6341void ASTReader::ReadDelegatingConstructors(
6342 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6343 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6344 CXXConstructorDecl *D
6345 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6346 if (D)
6347 Decls.push_back(D);
6348 }
6349 DelegatingCtorDecls.clear();
6350}
6351
6352void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6353 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6354 TypedefNameDecl *D
6355 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6356 if (D)
6357 Decls.push_back(D);
6358 }
6359 ExtVectorDecls.clear();
6360}
6361
6362void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6363 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6364 CXXRecordDecl *D
6365 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6366 if (D)
6367 Decls.push_back(D);
6368 }
6369 DynamicClasses.clear();
6370}
6371
6372void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006373ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6374 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6375 NamedDecl *D
6376 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006377 if (D)
6378 Decls.push_back(D);
6379 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006380 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006381}
6382
6383void ASTReader::ReadReferencedSelectors(
6384 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6385 if (ReferencedSelectorsData.empty())
6386 return;
6387
6388 // If there are @selector references added them to its pool. This is for
6389 // implementation of -Wselector.
6390 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6391 unsigned I = 0;
6392 while (I < DataSize) {
6393 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6394 SourceLocation SelLoc
6395 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6396 Sels.push_back(std::make_pair(Sel, SelLoc));
6397 }
6398 ReferencedSelectorsData.clear();
6399}
6400
6401void ASTReader::ReadWeakUndeclaredIdentifiers(
6402 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6403 if (WeakUndeclaredIdentifiers.empty())
6404 return;
6405
6406 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6407 IdentifierInfo *WeakId
6408 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6409 IdentifierInfo *AliasId
6410 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6411 SourceLocation Loc
6412 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6413 bool Used = WeakUndeclaredIdentifiers[I++];
6414 WeakInfo WI(AliasId, Loc);
6415 WI.setUsed(Used);
6416 WeakIDs.push_back(std::make_pair(WeakId, WI));
6417 }
6418 WeakUndeclaredIdentifiers.clear();
6419}
6420
6421void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6422 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6423 ExternalVTableUse VT;
6424 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6425 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6426 VT.DefinitionRequired = VTableUses[Idx++];
6427 VTables.push_back(VT);
6428 }
6429
6430 VTableUses.clear();
6431}
6432
6433void ASTReader::ReadPendingInstantiations(
6434 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6435 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6436 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6437 SourceLocation Loc
6438 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6439
6440 Pending.push_back(std::make_pair(D, Loc));
6441 }
6442 PendingInstantiations.clear();
6443}
6444
6445void ASTReader::LoadSelector(Selector Sel) {
6446 // It would be complicated to avoid reading the methods anyway. So don't.
6447 ReadMethodPool(Sel);
6448}
6449
6450void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6451 assert(ID && "Non-zero identifier ID required");
6452 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6453 IdentifiersLoaded[ID - 1] = II;
6454 if (DeserializationListener)
6455 DeserializationListener->IdentifierRead(ID, II);
6456}
6457
6458/// \brief Set the globally-visible declarations associated with the given
6459/// identifier.
6460///
6461/// If the AST reader is currently in a state where the given declaration IDs
6462/// cannot safely be resolved, they are queued until it is safe to resolve
6463/// them.
6464///
6465/// \param II an IdentifierInfo that refers to one or more globally-visible
6466/// declarations.
6467///
6468/// \param DeclIDs the set of declaration IDs with the name @p II that are
6469/// visible at global scope.
6470///
Douglas Gregoraa945902013-02-18 15:53:43 +00006471/// \param Decls if non-null, this vector will be populated with the set of
6472/// deserialized declarations. These declarations will not be pushed into
6473/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006474void
6475ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6476 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00006477 SmallVectorImpl<Decl *> *Decls) {
6478 if (NumCurrentElementsDeserializing && !Decls) {
6479 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006480 return;
6481 }
6482
6483 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6484 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6485 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006486 // If we're simply supposed to record the declarations, do so now.
6487 if (Decls) {
6488 Decls->push_back(D);
6489 continue;
6490 }
6491
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006492 // Introduce this declaration into the translation-unit scope
6493 // and add it to the declaration chain for this identifier, so
6494 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006495 pushExternalDeclIntoScope(D, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006496 } else {
6497 // Queue this declaration so that it will be added to the
6498 // translation unit scope and identifier's declaration chain
6499 // once a Sema object is known.
6500 PreloadedDecls.push_back(D);
6501 }
6502 }
6503}
6504
Douglas Gregor8222b892013-01-21 16:52:34 +00006505IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006506 if (ID == 0)
6507 return 0;
6508
6509 if (IdentifiersLoaded.empty()) {
6510 Error("no identifier table in AST file");
6511 return 0;
6512 }
6513
6514 ID -= 1;
6515 if (!IdentifiersLoaded[ID]) {
6516 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6517 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6518 ModuleFile *M = I->second;
6519 unsigned Index = ID - M->BaseIdentifierID;
6520 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6521
6522 // All of the strings in the AST file are preceded by a 16-bit length.
6523 // Extract that 16-bit length to avoid having to execute strlen().
6524 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6525 // unsigned integers. This is important to avoid integer overflow when
6526 // we cast them to 'unsigned'.
6527 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6528 unsigned StrLen = (((unsigned) StrLenPtr[0])
6529 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006530 IdentifiersLoaded[ID]
6531 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006532 if (DeserializationListener)
6533 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6534 }
6535
6536 return IdentifiersLoaded[ID];
6537}
6538
Douglas Gregor8222b892013-01-21 16:52:34 +00006539IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6540 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006541}
6542
6543IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6544 if (LocalID < NUM_PREDEF_IDENT_IDS)
6545 return LocalID;
6546
6547 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6548 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6549 assert(I != M.IdentifierRemap.end()
6550 && "Invalid index into identifier index remap");
6551
6552 return LocalID + I->second;
6553}
6554
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006555MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006556 if (ID == 0)
6557 return 0;
6558
6559 if (MacrosLoaded.empty()) {
6560 Error("no macro table in AST file");
6561 return 0;
6562 }
6563
6564 ID -= NUM_PREDEF_MACRO_IDS;
6565 if (!MacrosLoaded[ID]) {
6566 GlobalMacroMapType::iterator I
6567 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6568 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6569 ModuleFile *M = I->second;
6570 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006571 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6572
6573 if (DeserializationListener)
6574 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6575 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006576 }
6577
6578 return MacrosLoaded[ID];
6579}
6580
6581MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6582 if (LocalID < NUM_PREDEF_MACRO_IDS)
6583 return LocalID;
6584
6585 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6586 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6587 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6588
6589 return LocalID + I->second;
6590}
6591
6592serialization::SubmoduleID
6593ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6594 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6595 return LocalID;
6596
6597 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6598 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6599 assert(I != M.SubmoduleRemap.end()
6600 && "Invalid index into submodule index remap");
6601
6602 return LocalID + I->second;
6603}
6604
6605Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6606 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6607 assert(GlobalID == 0 && "Unhandled global submodule ID");
6608 return 0;
6609 }
6610
6611 if (GlobalID > SubmodulesLoaded.size()) {
6612 Error("submodule ID out of range in AST file");
6613 return 0;
6614 }
6615
6616 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6617}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006618
6619Module *ASTReader::getModule(unsigned ID) {
6620 return getSubmodule(ID);
6621}
6622
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006623Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6624 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6625}
6626
6627Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6628 if (ID == 0)
6629 return Selector();
6630
6631 if (ID > SelectorsLoaded.size()) {
6632 Error("selector ID out of range in AST file");
6633 return Selector();
6634 }
6635
6636 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6637 // Load this selector from the selector table.
6638 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6639 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6640 ModuleFile &M = *I->second;
6641 ASTSelectorLookupTrait Trait(*this, M);
6642 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6643 SelectorsLoaded[ID - 1] =
6644 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6645 if (DeserializationListener)
6646 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6647 }
6648
6649 return SelectorsLoaded[ID - 1];
6650}
6651
6652Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6653 return DecodeSelector(ID);
6654}
6655
6656uint32_t ASTReader::GetNumExternalSelectors() {
6657 // ID 0 (the null selector) is considered an external selector.
6658 return getTotalNumSelectors() + 1;
6659}
6660
6661serialization::SelectorID
6662ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6663 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6664 return LocalID;
6665
6666 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6667 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6668 assert(I != M.SelectorRemap.end()
6669 && "Invalid index into selector index remap");
6670
6671 return LocalID + I->second;
6672}
6673
6674DeclarationName
6675ASTReader::ReadDeclarationName(ModuleFile &F,
6676 const RecordData &Record, unsigned &Idx) {
6677 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6678 switch (Kind) {
6679 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006680 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006681
6682 case DeclarationName::ObjCZeroArgSelector:
6683 case DeclarationName::ObjCOneArgSelector:
6684 case DeclarationName::ObjCMultiArgSelector:
6685 return DeclarationName(ReadSelector(F, Record, Idx));
6686
6687 case DeclarationName::CXXConstructorName:
6688 return Context.DeclarationNames.getCXXConstructorName(
6689 Context.getCanonicalType(readType(F, Record, Idx)));
6690
6691 case DeclarationName::CXXDestructorName:
6692 return Context.DeclarationNames.getCXXDestructorName(
6693 Context.getCanonicalType(readType(F, Record, Idx)));
6694
6695 case DeclarationName::CXXConversionFunctionName:
6696 return Context.DeclarationNames.getCXXConversionFunctionName(
6697 Context.getCanonicalType(readType(F, Record, Idx)));
6698
6699 case DeclarationName::CXXOperatorName:
6700 return Context.DeclarationNames.getCXXOperatorName(
6701 (OverloadedOperatorKind)Record[Idx++]);
6702
6703 case DeclarationName::CXXLiteralOperatorName:
6704 return Context.DeclarationNames.getCXXLiteralOperatorName(
6705 GetIdentifierInfo(F, Record, Idx));
6706
6707 case DeclarationName::CXXUsingDirective:
6708 return DeclarationName::getUsingDirectiveName();
6709 }
6710
6711 llvm_unreachable("Invalid NameKind!");
6712}
6713
6714void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6715 DeclarationNameLoc &DNLoc,
6716 DeclarationName Name,
6717 const RecordData &Record, unsigned &Idx) {
6718 switch (Name.getNameKind()) {
6719 case DeclarationName::CXXConstructorName:
6720 case DeclarationName::CXXDestructorName:
6721 case DeclarationName::CXXConversionFunctionName:
6722 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6723 break;
6724
6725 case DeclarationName::CXXOperatorName:
6726 DNLoc.CXXOperatorName.BeginOpNameLoc
6727 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6728 DNLoc.CXXOperatorName.EndOpNameLoc
6729 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6730 break;
6731
6732 case DeclarationName::CXXLiteralOperatorName:
6733 DNLoc.CXXLiteralOperatorName.OpNameLoc
6734 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6735 break;
6736
6737 case DeclarationName::Identifier:
6738 case DeclarationName::ObjCZeroArgSelector:
6739 case DeclarationName::ObjCOneArgSelector:
6740 case DeclarationName::ObjCMultiArgSelector:
6741 case DeclarationName::CXXUsingDirective:
6742 break;
6743 }
6744}
6745
6746void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6747 DeclarationNameInfo &NameInfo,
6748 const RecordData &Record, unsigned &Idx) {
6749 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6750 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6751 DeclarationNameLoc DNLoc;
6752 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6753 NameInfo.setInfo(DNLoc);
6754}
6755
6756void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6757 const RecordData &Record, unsigned &Idx) {
6758 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6759 unsigned NumTPLists = Record[Idx++];
6760 Info.NumTemplParamLists = NumTPLists;
6761 if (NumTPLists) {
6762 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6763 for (unsigned i=0; i != NumTPLists; ++i)
6764 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6765 }
6766}
6767
6768TemplateName
6769ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6770 unsigned &Idx) {
6771 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6772 switch (Kind) {
6773 case TemplateName::Template:
6774 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6775
6776 case TemplateName::OverloadedTemplate: {
6777 unsigned size = Record[Idx++];
6778 UnresolvedSet<8> Decls;
6779 while (size--)
6780 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6781
6782 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6783 }
6784
6785 case TemplateName::QualifiedTemplate: {
6786 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6787 bool hasTemplKeyword = Record[Idx++];
6788 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6789 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6790 }
6791
6792 case TemplateName::DependentTemplate: {
6793 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6794 if (Record[Idx++]) // isIdentifier
6795 return Context.getDependentTemplateName(NNS,
6796 GetIdentifierInfo(F, Record,
6797 Idx));
6798 return Context.getDependentTemplateName(NNS,
6799 (OverloadedOperatorKind)Record[Idx++]);
6800 }
6801
6802 case TemplateName::SubstTemplateTemplateParm: {
6803 TemplateTemplateParmDecl *param
6804 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6805 if (!param) return TemplateName();
6806 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6807 return Context.getSubstTemplateTemplateParm(param, replacement);
6808 }
6809
6810 case TemplateName::SubstTemplateTemplateParmPack: {
6811 TemplateTemplateParmDecl *Param
6812 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6813 if (!Param)
6814 return TemplateName();
6815
6816 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6817 if (ArgPack.getKind() != TemplateArgument::Pack)
6818 return TemplateName();
6819
6820 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6821 }
6822 }
6823
6824 llvm_unreachable("Unhandled template name kind!");
6825}
6826
6827TemplateArgument
6828ASTReader::ReadTemplateArgument(ModuleFile &F,
6829 const RecordData &Record, unsigned &Idx) {
6830 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6831 switch (Kind) {
6832 case TemplateArgument::Null:
6833 return TemplateArgument();
6834 case TemplateArgument::Type:
6835 return TemplateArgument(readType(F, Record, Idx));
6836 case TemplateArgument::Declaration: {
6837 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6838 bool ForReferenceParam = Record[Idx++];
6839 return TemplateArgument(D, ForReferenceParam);
6840 }
6841 case TemplateArgument::NullPtr:
6842 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6843 case TemplateArgument::Integral: {
6844 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6845 QualType T = readType(F, Record, Idx);
6846 return TemplateArgument(Context, Value, T);
6847 }
6848 case TemplateArgument::Template:
6849 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6850 case TemplateArgument::TemplateExpansion: {
6851 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00006852 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006853 if (unsigned NumExpansions = Record[Idx++])
6854 NumTemplateExpansions = NumExpansions - 1;
6855 return TemplateArgument(Name, NumTemplateExpansions);
6856 }
6857 case TemplateArgument::Expression:
6858 return TemplateArgument(ReadExpr(F));
6859 case TemplateArgument::Pack: {
6860 unsigned NumArgs = Record[Idx++];
6861 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6862 for (unsigned I = 0; I != NumArgs; ++I)
6863 Args[I] = ReadTemplateArgument(F, Record, Idx);
6864 return TemplateArgument(Args, NumArgs);
6865 }
6866 }
6867
6868 llvm_unreachable("Unhandled template argument kind!");
6869}
6870
6871TemplateParameterList *
6872ASTReader::ReadTemplateParameterList(ModuleFile &F,
6873 const RecordData &Record, unsigned &Idx) {
6874 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6875 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6876 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6877
6878 unsigned NumParams = Record[Idx++];
6879 SmallVector<NamedDecl *, 16> Params;
6880 Params.reserve(NumParams);
6881 while (NumParams--)
6882 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6883
6884 TemplateParameterList* TemplateParams =
6885 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6886 Params.data(), Params.size(), RAngleLoc);
6887 return TemplateParams;
6888}
6889
6890void
6891ASTReader::
6892ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6893 ModuleFile &F, const RecordData &Record,
6894 unsigned &Idx) {
6895 unsigned NumTemplateArgs = Record[Idx++];
6896 TemplArgs.reserve(NumTemplateArgs);
6897 while (NumTemplateArgs--)
6898 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6899}
6900
6901/// \brief Read a UnresolvedSet structure.
6902void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6903 const RecordData &Record, unsigned &Idx) {
6904 unsigned NumDecls = Record[Idx++];
6905 Set.reserve(Context, NumDecls);
6906 while (NumDecls--) {
6907 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6908 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6909 Set.addDecl(Context, D, AS);
6910 }
6911}
6912
6913CXXBaseSpecifier
6914ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6915 const RecordData &Record, unsigned &Idx) {
6916 bool isVirtual = static_cast<bool>(Record[Idx++]);
6917 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6918 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6919 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6920 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6921 SourceRange Range = ReadSourceRange(F, Record, Idx);
6922 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6923 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6924 EllipsisLoc);
6925 Result.setInheritConstructors(inheritConstructors);
6926 return Result;
6927}
6928
6929std::pair<CXXCtorInitializer **, unsigned>
6930ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6931 unsigned &Idx) {
6932 CXXCtorInitializer **CtorInitializers = 0;
6933 unsigned NumInitializers = Record[Idx++];
6934 if (NumInitializers) {
6935 CtorInitializers
6936 = new (Context) CXXCtorInitializer*[NumInitializers];
6937 for (unsigned i=0; i != NumInitializers; ++i) {
6938 TypeSourceInfo *TInfo = 0;
6939 bool IsBaseVirtual = false;
6940 FieldDecl *Member = 0;
6941 IndirectFieldDecl *IndirectMember = 0;
6942
6943 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6944 switch (Type) {
6945 case CTOR_INITIALIZER_BASE:
6946 TInfo = GetTypeSourceInfo(F, Record, Idx);
6947 IsBaseVirtual = Record[Idx++];
6948 break;
6949
6950 case CTOR_INITIALIZER_DELEGATING:
6951 TInfo = GetTypeSourceInfo(F, Record, Idx);
6952 break;
6953
6954 case CTOR_INITIALIZER_MEMBER:
6955 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6956 break;
6957
6958 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6959 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6960 break;
6961 }
6962
6963 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6964 Expr *Init = ReadExpr(F);
6965 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6966 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6967 bool IsWritten = Record[Idx++];
6968 unsigned SourceOrderOrNumArrayIndices;
6969 SmallVector<VarDecl *, 8> Indices;
6970 if (IsWritten) {
6971 SourceOrderOrNumArrayIndices = Record[Idx++];
6972 } else {
6973 SourceOrderOrNumArrayIndices = Record[Idx++];
6974 Indices.reserve(SourceOrderOrNumArrayIndices);
6975 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6976 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6977 }
6978
6979 CXXCtorInitializer *BOMInit;
6980 if (Type == CTOR_INITIALIZER_BASE) {
6981 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6982 LParenLoc, Init, RParenLoc,
6983 MemberOrEllipsisLoc);
6984 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6985 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6986 Init, RParenLoc);
6987 } else if (IsWritten) {
6988 if (Member)
6989 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6990 LParenLoc, Init, RParenLoc);
6991 else
6992 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6993 MemberOrEllipsisLoc, LParenLoc,
6994 Init, RParenLoc);
6995 } else {
6996 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6997 LParenLoc, Init, RParenLoc,
6998 Indices.data(), Indices.size());
6999 }
7000
7001 if (IsWritten)
7002 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7003 CtorInitializers[i] = BOMInit;
7004 }
7005 }
7006
7007 return std::make_pair(CtorInitializers, NumInitializers);
7008}
7009
7010NestedNameSpecifier *
7011ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7012 const RecordData &Record, unsigned &Idx) {
7013 unsigned N = Record[Idx++];
7014 NestedNameSpecifier *NNS = 0, *Prev = 0;
7015 for (unsigned I = 0; I != N; ++I) {
7016 NestedNameSpecifier::SpecifierKind Kind
7017 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7018 switch (Kind) {
7019 case NestedNameSpecifier::Identifier: {
7020 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7021 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7022 break;
7023 }
7024
7025 case NestedNameSpecifier::Namespace: {
7026 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7027 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7028 break;
7029 }
7030
7031 case NestedNameSpecifier::NamespaceAlias: {
7032 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7033 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7034 break;
7035 }
7036
7037 case NestedNameSpecifier::TypeSpec:
7038 case NestedNameSpecifier::TypeSpecWithTemplate: {
7039 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7040 if (!T)
7041 return 0;
7042
7043 bool Template = Record[Idx++];
7044 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7045 break;
7046 }
7047
7048 case NestedNameSpecifier::Global: {
7049 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7050 // No associated value, and there can't be a prefix.
7051 break;
7052 }
7053 }
7054 Prev = NNS;
7055 }
7056 return NNS;
7057}
7058
7059NestedNameSpecifierLoc
7060ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7061 unsigned &Idx) {
7062 unsigned N = Record[Idx++];
7063 NestedNameSpecifierLocBuilder Builder;
7064 for (unsigned I = 0; I != N; ++I) {
7065 NestedNameSpecifier::SpecifierKind Kind
7066 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7067 switch (Kind) {
7068 case NestedNameSpecifier::Identifier: {
7069 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7070 SourceRange Range = ReadSourceRange(F, Record, Idx);
7071 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7072 break;
7073 }
7074
7075 case NestedNameSpecifier::Namespace: {
7076 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7077 SourceRange Range = ReadSourceRange(F, Record, Idx);
7078 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7079 break;
7080 }
7081
7082 case NestedNameSpecifier::NamespaceAlias: {
7083 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7084 SourceRange Range = ReadSourceRange(F, Record, Idx);
7085 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7086 break;
7087 }
7088
7089 case NestedNameSpecifier::TypeSpec:
7090 case NestedNameSpecifier::TypeSpecWithTemplate: {
7091 bool Template = Record[Idx++];
7092 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7093 if (!T)
7094 return NestedNameSpecifierLoc();
7095 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7096
7097 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7098 Builder.Extend(Context,
7099 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7100 T->getTypeLoc(), ColonColonLoc);
7101 break;
7102 }
7103
7104 case NestedNameSpecifier::Global: {
7105 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7106 Builder.MakeGlobal(Context, ColonColonLoc);
7107 break;
7108 }
7109 }
7110 }
7111
7112 return Builder.getWithLocInContext(Context);
7113}
7114
7115SourceRange
7116ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7117 unsigned &Idx) {
7118 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7119 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7120 return SourceRange(beg, end);
7121}
7122
7123/// \brief Read an integral value
7124llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7125 unsigned BitWidth = Record[Idx++];
7126 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7127 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7128 Idx += NumWords;
7129 return Result;
7130}
7131
7132/// \brief Read a signed integral value
7133llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7134 bool isUnsigned = Record[Idx++];
7135 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7136}
7137
7138/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007139llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7140 const llvm::fltSemantics &Sem,
7141 unsigned &Idx) {
7142 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007143}
7144
7145// \brief Read a string
7146std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7147 unsigned Len = Record[Idx++];
7148 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7149 Idx += Len;
7150 return Result;
7151}
7152
7153VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7154 unsigned &Idx) {
7155 unsigned Major = Record[Idx++];
7156 unsigned Minor = Record[Idx++];
7157 unsigned Subminor = Record[Idx++];
7158 if (Minor == 0)
7159 return VersionTuple(Major);
7160 if (Subminor == 0)
7161 return VersionTuple(Major, Minor - 1);
7162 return VersionTuple(Major, Minor - 1, Subminor - 1);
7163}
7164
7165CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7166 const RecordData &Record,
7167 unsigned &Idx) {
7168 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7169 return CXXTemporary::Create(Context, Decl);
7170}
7171
7172DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00007173 return Diag(CurrentImportLoc, DiagID);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007174}
7175
7176DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7177 return Diags.Report(Loc, DiagID);
7178}
7179
7180/// \brief Retrieve the identifier table associated with the
7181/// preprocessor.
7182IdentifierTable &ASTReader::getIdentifierTable() {
7183 return PP.getIdentifierTable();
7184}
7185
7186/// \brief Record that the given ID maps to the given switch-case
7187/// statement.
7188void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7189 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7190 "Already have a SwitchCase with this ID");
7191 (*CurrSwitchCaseStmts)[ID] = SC;
7192}
7193
7194/// \brief Retrieve the switch-case statement with the given ID.
7195SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7196 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7197 return (*CurrSwitchCaseStmts)[ID];
7198}
7199
7200void ASTReader::ClearSwitchCaseIDs() {
7201 CurrSwitchCaseStmts->clear();
7202}
7203
7204void ASTReader::ReadComments() {
7205 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007206 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007207 serialization::ModuleFile *> >::iterator
7208 I = CommentsCursors.begin(),
7209 E = CommentsCursors.end();
7210 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007211 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007212 serialization::ModuleFile &F = *I->second;
7213 SavedStreamPosition SavedPosition(Cursor);
7214
7215 RecordData Record;
7216 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007217 llvm::BitstreamEntry Entry =
7218 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7219
7220 switch (Entry.Kind) {
7221 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7222 case llvm::BitstreamEntry::Error:
7223 Error("malformed block record in AST file");
7224 return;
7225 case llvm::BitstreamEntry::EndBlock:
7226 goto NextCursor;
7227 case llvm::BitstreamEntry::Record:
7228 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007229 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007230 }
7231
7232 // Read a record.
7233 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007234 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007235 case COMMENTS_RAW_COMMENT: {
7236 unsigned Idx = 0;
7237 SourceRange SR = ReadSourceRange(F, Record, Idx);
7238 RawComment::CommentKind Kind =
7239 (RawComment::CommentKind) Record[Idx++];
7240 bool IsTrailingComment = Record[Idx++];
7241 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00007242 Comments.push_back(new (Context) RawComment(
7243 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7244 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007245 break;
7246 }
7247 }
7248 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007249 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007250 }
7251 Context.Comments.addCommentsToFront(Comments);
7252}
7253
7254void ASTReader::finishPendingActions() {
7255 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007256 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007257 // If any identifiers with corresponding top-level declarations have
7258 // been loaded, load those declarations now.
Douglas Gregoraa945902013-02-18 15:53:43 +00007259 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007260 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007261 // FIXME: std::move
7262 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7263 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00007264 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00007265
7266 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007267 }
7268
7269 // Load pending declaration chains.
7270 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7271 loadPendingDeclChain(PendingDeclChains[I]);
7272 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7273 }
7274 PendingDeclChains.clear();
7275
Douglas Gregoraa945902013-02-18 15:53:43 +00007276 // Make the most recent of the top-level declarations visible.
7277 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7278 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7279 TLD != TLDEnd; ++TLD) {
7280 IdentifierInfo *II = TLD->first;
7281 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007282 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregoraa945902013-02-18 15:53:43 +00007283 }
7284 }
7285
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007286 // Load any pending macro definitions.
7287 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007288 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7289 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7290 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7291 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00007292 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7293 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007294 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7295 if (Info.M->Kind != MK_Module)
7296 resolvePendingMacro(II, Info);
7297 }
7298 // Handle module imports.
7299 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7300 ++IDIdx) {
7301 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7302 if (Info.M->Kind == MK_Module)
7303 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007304 }
7305 }
7306 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007307
7308 // Wire up the DeclContexts for Decls that we delayed setting until
7309 // recursive loading is completed.
7310 while (!PendingDeclContextInfos.empty()) {
7311 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7312 PendingDeclContextInfos.pop_front();
7313 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7314 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7315 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7316 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007317 }
7318
7319 // If we deserialized any C++ or Objective-C class definitions, any
7320 // Objective-C protocol definitions, or any redeclarable templates, make sure
7321 // that all redeclarations point to the definitions. Note that this can only
7322 // happen now, after the redeclaration chains have been fully wired.
7323 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7324 DEnd = PendingDefinitions.end();
7325 D != DEnd; ++D) {
7326 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7327 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7328 // Make sure that the TagType points at the definition.
7329 const_cast<TagType*>(TagT)->decl = TD;
7330 }
7331
7332 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7333 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7334 REnd = RD->redecls_end();
7335 R != REnd; ++R)
7336 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7337
7338 }
7339
7340 continue;
7341 }
7342
7343 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7344 // Make sure that the ObjCInterfaceType points at the definition.
7345 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7346 ->Decl = ID;
7347
7348 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7349 REnd = ID->redecls_end();
7350 R != REnd; ++R)
7351 R->Data = ID->Data;
7352
7353 continue;
7354 }
7355
7356 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7357 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7358 REnd = PD->redecls_end();
7359 R != REnd; ++R)
7360 R->Data = PD->Data;
7361
7362 continue;
7363 }
7364
7365 RedeclarableTemplateDecl *RTD
7366 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7367 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7368 REnd = RTD->redecls_end();
7369 R != REnd; ++R)
7370 R->Common = RTD->Common;
7371 }
7372 PendingDefinitions.clear();
7373
7374 // Load the bodies of any functions or methods we've encountered. We do
7375 // this now (delayed) so that we can be sure that the declaration chains
7376 // have been fully wired up.
7377 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7378 PBEnd = PendingBodies.end();
7379 PB != PBEnd; ++PB) {
7380 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7381 // FIXME: Check for =delete/=default?
7382 // FIXME: Complain about ODR violations here?
7383 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7384 FD->setLazyBody(PB->second);
7385 continue;
7386 }
7387
7388 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7389 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7390 MD->setLazyBody(PB->second);
7391 }
7392 PendingBodies.clear();
7393}
7394
7395void ASTReader::FinishedDeserializing() {
7396 assert(NumCurrentElementsDeserializing &&
7397 "FinishedDeserializing not paired with StartedDeserializing");
7398 if (NumCurrentElementsDeserializing == 1) {
7399 // We decrease NumCurrentElementsDeserializing only after pending actions
7400 // are finished, to avoid recursively re-calling finishPendingActions().
7401 finishPendingActions();
7402 }
7403 --NumCurrentElementsDeserializing;
7404
7405 if (NumCurrentElementsDeserializing == 0 &&
7406 Consumer && !PassingDeclsToConsumer) {
7407 // Guard variable to avoid recursively redoing the process of passing
7408 // decls to consumer.
7409 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7410 true);
7411
7412 while (!InterestingDecls.empty()) {
7413 // We are not in recursive loading, so it's safe to pass the "interesting"
7414 // decls to the consumer.
7415 Decl *D = InterestingDecls.front();
7416 InterestingDecls.pop_front();
7417 PassInterestingDeclToConsumer(D);
7418 }
7419 }
7420}
7421
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007422void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7423 D = cast<NamedDecl>(D->getMostRecentDecl());
7424
7425 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7426 SemaObj->TUScope->AddDecl(D);
7427 } else if (SemaObj->TUScope) {
7428 // Adding the decl to IdResolver may have failed because it was already in
7429 // (even though it was not added in scope). If it is already in, make sure
7430 // it gets in the scope as well.
7431 if (std::find(SemaObj->IdResolver.begin(Name),
7432 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7433 SemaObj->TUScope->AddDecl(D);
7434 }
7435}
7436
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007437ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7438 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007439 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007440 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7441 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7442 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7443 Consumer(0), ModuleMgr(PP.getFileManager()),
7444 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007445 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007446 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007447 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7448 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007449 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7450 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7451 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007452 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7453 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7454 TotalNumMethodPoolEntries(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007455 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7456 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7457 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7458 PassingDeclsToConsumer(false),
7459 NumCXXBaseSpecifiersLoaded(0)
7460{
7461 SourceMgr.setExternalSLocEntrySource(this);
7462}
7463
7464ASTReader::~ASTReader() {
7465 for (DeclContextVisibleUpdatesPending::iterator
7466 I = PendingVisibleUpdates.begin(),
7467 E = PendingVisibleUpdates.end();
7468 I != E; ++I) {
7469 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7470 F = I->second.end();
7471 J != F; ++J)
7472 delete J->first;
7473 }
7474}