blob: 9bb9a7a8632365aea69e24fa3d82fc94d0b38230 [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;
Craig Topper8ae63872013-07-05 04:43:31 +0000213 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
214 DeclsMap;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000215}
216
217/// \brief Collect the macro definitions provided by the given preprocessor
218/// options.
219static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
220 MacroDefinitionsMap &Macros,
221 SmallVectorImpl<StringRef> *MacroNames = 0){
222 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
223 StringRef Macro = PPOpts.Macros[I].first;
224 bool IsUndef = PPOpts.Macros[I].second;
225
226 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
227 StringRef MacroName = MacroPair.first;
228 StringRef MacroBody = MacroPair.second;
229
230 // For an #undef'd macro, we only care about the name.
231 if (IsUndef) {
232 if (MacroNames && !Macros.count(MacroName))
233 MacroNames->push_back(MacroName);
234
235 Macros[MacroName] = std::make_pair("", true);
236 continue;
237 }
238
239 // For a #define'd macro, figure out the actual definition.
240 if (MacroName.size() == Macro.size())
241 MacroBody = "1";
242 else {
243 // Note: GCC drops anything following an end-of-line character.
244 StringRef::size_type End = MacroBody.find_first_of("\n\r");
245 MacroBody = MacroBody.substr(0, End);
246 }
247
248 if (MacroNames && !Macros.count(MacroName))
249 MacroNames->push_back(MacroName);
250 Macros[MacroName] = std::make_pair(MacroBody, false);
251 }
252}
253
254/// \brief Check the preprocessor options deserialized from the control block
255/// against the preprocessor options in an existing preprocessor.
256///
257/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
258static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
259 const PreprocessorOptions &ExistingPPOpts,
260 DiagnosticsEngine *Diags,
261 FileManager &FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000262 std::string &SuggestedPredefines,
263 const LangOptions &LangOpts) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000264 // Check macro definitions.
265 MacroDefinitionsMap ASTFileMacros;
266 collectMacroDefinitions(PPOpts, ASTFileMacros);
267 MacroDefinitionsMap ExistingMacros;
268 SmallVector<StringRef, 4> ExistingMacroNames;
269 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
270
271 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
272 // Dig out the macro definition in the existing preprocessor options.
273 StringRef MacroName = ExistingMacroNames[I];
274 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
275
276 // Check whether we know anything about this macro name or not.
277 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
278 = ASTFileMacros.find(MacroName);
279 if (Known == ASTFileMacros.end()) {
280 // FIXME: Check whether this identifier was referenced anywhere in the
281 // AST file. If so, we should reject the AST file. Unfortunately, this
282 // information isn't in the control block. What shall we do about it?
283
284 if (Existing.second) {
285 SuggestedPredefines += "#undef ";
286 SuggestedPredefines += MacroName.str();
287 SuggestedPredefines += '\n';
288 } else {
289 SuggestedPredefines += "#define ";
290 SuggestedPredefines += MacroName.str();
291 SuggestedPredefines += ' ';
292 SuggestedPredefines += Existing.first.str();
293 SuggestedPredefines += '\n';
294 }
295 continue;
296 }
297
298 // If the macro was defined in one but undef'd in the other, we have a
299 // conflict.
300 if (Existing.second != Known->second.second) {
301 if (Diags) {
302 Diags->Report(diag::err_pch_macro_def_undef)
303 << MacroName << Known->second.second;
304 }
305 return true;
306 }
307
308 // If the macro was #undef'd in both, or if the macro bodies are identical,
309 // it's fine.
310 if (Existing.second || Existing.first == Known->second.first)
311 continue;
312
313 // The macro bodies differ; complain.
314 if (Diags) {
315 Diags->Report(diag::err_pch_macro_def_conflict)
316 << MacroName << Known->second.first << Existing.first;
317 }
318 return true;
319 }
320
321 // Check whether we're using predefines.
322 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
323 if (Diags) {
324 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
325 }
326 return true;
327 }
328
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000329 // Detailed record is important since it is used for the module cache hash.
330 if (LangOpts.Modules &&
331 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
332 if (Diags) {
333 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
334 }
335 return true;
336 }
337
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000338 // Compute the #include and #include_macros lines we need.
339 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
340 StringRef File = ExistingPPOpts.Includes[I];
341 if (File == ExistingPPOpts.ImplicitPCHInclude)
342 continue;
343
344 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
345 != PPOpts.Includes.end())
346 continue;
347
348 SuggestedPredefines += "#include \"";
349 SuggestedPredefines +=
350 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
351 SuggestedPredefines += "\"\n";
352 }
353
354 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
355 StringRef File = ExistingPPOpts.MacroIncludes[I];
356 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
357 File)
358 != PPOpts.MacroIncludes.end())
359 continue;
360
361 SuggestedPredefines += "#__include_macros \"";
362 SuggestedPredefines +=
363 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
364 SuggestedPredefines += "\"\n##\n";
365 }
366
367 return false;
368}
369
370bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
371 bool Complain,
372 std::string &SuggestedPredefines) {
373 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
374
375 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
376 Complain? &Reader.Diags : 0,
377 PP.getFileManager(),
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000378 SuggestedPredefines,
379 PP.getLangOpts());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000380}
381
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000382void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
383 PP.setCounterValue(Value);
384}
385
386//===----------------------------------------------------------------------===//
387// AST reader implementation
388//===----------------------------------------------------------------------===//
389
390void
391ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
392 DeserializationListener = Listener;
393}
394
395
396
397unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
398 return serialization::ComputeHash(Sel);
399}
400
401
402std::pair<unsigned, unsigned>
403ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
404 using namespace clang::io;
405 unsigned KeyLen = ReadUnalignedLE16(d);
406 unsigned DataLen = ReadUnalignedLE16(d);
407 return std::make_pair(KeyLen, DataLen);
408}
409
410ASTSelectorLookupTrait::internal_key_type
411ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
412 using namespace clang::io;
413 SelectorTable &SelTable = Reader.getContext().Selectors;
414 unsigned N = ReadUnalignedLE16(d);
415 IdentifierInfo *FirstII
Douglas Gregor8222b892013-01-21 16:52:34 +0000416 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000417 if (N == 0)
418 return SelTable.getNullarySelector(FirstII);
419 else if (N == 1)
420 return SelTable.getUnarySelector(FirstII);
421
422 SmallVector<IdentifierInfo *, 16> Args;
423 Args.push_back(FirstII);
424 for (unsigned I = 1; I != N; ++I)
Douglas Gregor8222b892013-01-21 16:52:34 +0000425 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000426
427 return SelTable.getSelector(N, Args.data());
428}
429
430ASTSelectorLookupTrait::data_type
431ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
432 unsigned DataLen) {
433 using namespace clang::io;
434
435 data_type Result;
436
437 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +0000438 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
439 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
440 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
441 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
442 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
443 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000444
445 // Load instance methods
446 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
447 if (ObjCMethodDecl *Method
448 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
449 Result.Instance.push_back(Method);
450 }
451
452 // Load factory methods
453 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
454 if (ObjCMethodDecl *Method
455 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
456 Result.Factory.push_back(Method);
457 }
458
459 return Result;
460}
461
Douglas Gregor479633c2013-01-23 18:53:14 +0000462unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
463 return llvm::HashString(a);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000464}
465
466std::pair<unsigned, unsigned>
Douglas Gregor479633c2013-01-23 18:53:14 +0000467ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000468 using namespace clang::io;
469 unsigned DataLen = ReadUnalignedLE16(d);
470 unsigned KeyLen = ReadUnalignedLE16(d);
471 return std::make_pair(KeyLen, DataLen);
472}
473
Douglas Gregor479633c2013-01-23 18:53:14 +0000474ASTIdentifierLookupTraitBase::internal_key_type
475ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000476 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregor479633c2013-01-23 18:53:14 +0000477 return StringRef((const char*) d, n-1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000478}
479
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000480/// \brief Whether the given identifier is "interesting".
481static bool isInterestingIdentifier(IdentifierInfo &II) {
482 return II.isPoisoned() ||
483 II.isExtensionToken() ||
484 II.getObjCOrBuiltinID() ||
485 II.hasRevertedTokenIDToIdentifier() ||
486 II.hadMacroDefinition() ||
487 II.getFETokenInfo<void>();
488}
489
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000490IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
491 const unsigned char* d,
492 unsigned DataLen) {
493 using namespace clang::io;
494 unsigned RawID = ReadUnalignedLE32(d);
495 bool IsInteresting = RawID & 0x01;
496
497 // Wipe out the "is interesting" bit.
498 RawID = RawID >> 1;
499
500 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
501 if (!IsInteresting) {
502 // For uninteresting identifiers, just build the IdentifierInfo
503 // and associate it with the persistent ID.
504 IdentifierInfo *II = KnownII;
505 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000506 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000507 KnownII = II;
508 }
509 Reader.SetIdentifierInfo(ID, II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000510 if (!II->isFromAST()) {
511 bool WasInteresting = isInterestingIdentifier(*II);
512 II->setIsFromAST();
513 if (WasInteresting)
514 II->setChangedSinceDeserialization();
515 }
516 Reader.markIdentifierUpToDate(II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000517 return II;
518 }
519
520 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
521 unsigned Bits = ReadUnalignedLE16(d);
522 bool CPlusPlusOperatorKeyword = Bits & 0x01;
523 Bits >>= 1;
524 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
525 Bits >>= 1;
526 bool Poisoned = Bits & 0x01;
527 Bits >>= 1;
528 bool ExtensionToken = Bits & 0x01;
529 Bits >>= 1;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000530 bool hasSubmoduleMacros = Bits & 0x01;
531 Bits >>= 1;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000532 bool hadMacroDefinition = Bits & 0x01;
533 Bits >>= 1;
534
535 assert(Bits == 0 && "Extra bits in the identifier?");
536 DataLen -= 8;
537
538 // Build the IdentifierInfo itself and link the identifier ID with
539 // the new IdentifierInfo.
540 IdentifierInfo *II = KnownII;
541 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000542 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000543 KnownII = II;
544 }
545 Reader.markIdentifierUpToDate(II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000546 if (!II->isFromAST()) {
547 bool WasInteresting = isInterestingIdentifier(*II);
548 II->setIsFromAST();
549 if (WasInteresting)
550 II->setChangedSinceDeserialization();
551 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000552
553 // Set or check the various bits in the IdentifierInfo structure.
554 // Token IDs are read-only.
Argyrios Kyrtzidis1ebefc72013-02-27 01:13:51 +0000555 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000556 II->RevertTokenIDToIdentifier();
557 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
558 assert(II->isExtensionToken() == ExtensionToken &&
559 "Incorrect extension token flag");
560 (void)ExtensionToken;
561 if (Poisoned)
562 II->setIsPoisoned(true);
563 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
564 "Incorrect C++ operator keyword flag");
565 (void)CPlusPlusOperatorKeyword;
566
567 // If this identifier is a macro, deserialize the macro
568 // definition.
569 if (hadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000570 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
571 DataLen -= 4;
572 SmallVector<uint32_t, 8> LocalMacroIDs;
573 if (hasSubmoduleMacros) {
574 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
575 DataLen -= 4;
576 LocalMacroIDs.push_back(LocalMacroID);
577 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000578 DataLen -= 4;
579 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000580
581 if (F.Kind == MK_Module) {
582 for (SmallVectorImpl<uint32_t>::iterator
583 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
584 MacroID MacID = Reader.getGlobalMacroID(F, *I);
585 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
586 }
587 } else {
588 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
589 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000590 }
591
592 Reader.SetIdentifierInfo(ID, II);
593
594 // Read all of the declarations visible at global scope with this
595 // name.
596 if (DataLen > 0) {
597 SmallVector<uint32_t, 4> DeclIDs;
598 for (; DataLen > 0; DataLen -= 4)
599 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
600 Reader.SetGloballyVisibleDecls(II, DeclIDs);
601 }
602
603 return II;
604}
605
606unsigned
607ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
608 llvm::FoldingSetNodeID ID;
609 ID.AddInteger(Key.Kind);
610
611 switch (Key.Kind) {
612 case DeclarationName::Identifier:
613 case DeclarationName::CXXLiteralOperatorName:
614 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
615 break;
616 case DeclarationName::ObjCZeroArgSelector:
617 case DeclarationName::ObjCOneArgSelector:
618 case DeclarationName::ObjCMultiArgSelector:
619 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
620 break;
621 case DeclarationName::CXXOperatorName:
622 ID.AddInteger((OverloadedOperatorKind)Key.Data);
623 break;
624 case DeclarationName::CXXConstructorName:
625 case DeclarationName::CXXDestructorName:
626 case DeclarationName::CXXConversionFunctionName:
627 case DeclarationName::CXXUsingDirective:
628 break;
629 }
630
631 return ID.ComputeHash();
632}
633
634ASTDeclContextNameLookupTrait::internal_key_type
635ASTDeclContextNameLookupTrait::GetInternalKey(
636 const external_key_type& Name) const {
637 DeclNameKey Key;
638 Key.Kind = Name.getNameKind();
639 switch (Name.getNameKind()) {
640 case DeclarationName::Identifier:
641 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
642 break;
643 case DeclarationName::ObjCZeroArgSelector:
644 case DeclarationName::ObjCOneArgSelector:
645 case DeclarationName::ObjCMultiArgSelector:
646 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
647 break;
648 case DeclarationName::CXXOperatorName:
649 Key.Data = Name.getCXXOverloadedOperator();
650 break;
651 case DeclarationName::CXXLiteralOperatorName:
652 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
653 break;
654 case DeclarationName::CXXConstructorName:
655 case DeclarationName::CXXDestructorName:
656 case DeclarationName::CXXConversionFunctionName:
657 case DeclarationName::CXXUsingDirective:
658 Key.Data = 0;
659 break;
660 }
661
662 return Key;
663}
664
665std::pair<unsigned, unsigned>
666ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
667 using namespace clang::io;
668 unsigned KeyLen = ReadUnalignedLE16(d);
669 unsigned DataLen = ReadUnalignedLE16(d);
670 return std::make_pair(KeyLen, DataLen);
671}
672
673ASTDeclContextNameLookupTrait::internal_key_type
674ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
675 using namespace clang::io;
676
677 DeclNameKey Key;
678 Key.Kind = (DeclarationName::NameKind)*d++;
679 switch (Key.Kind) {
680 case DeclarationName::Identifier:
681 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
682 break;
683 case DeclarationName::ObjCZeroArgSelector:
684 case DeclarationName::ObjCOneArgSelector:
685 case DeclarationName::ObjCMultiArgSelector:
686 Key.Data =
687 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
688 .getAsOpaquePtr();
689 break;
690 case DeclarationName::CXXOperatorName:
691 Key.Data = *d++; // OverloadedOperatorKind
692 break;
693 case DeclarationName::CXXLiteralOperatorName:
694 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
695 break;
696 case DeclarationName::CXXConstructorName:
697 case DeclarationName::CXXDestructorName:
698 case DeclarationName::CXXConversionFunctionName:
699 case DeclarationName::CXXUsingDirective:
700 Key.Data = 0;
701 break;
702 }
703
704 return Key;
705}
706
707ASTDeclContextNameLookupTrait::data_type
708ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
709 const unsigned char* d,
710 unsigned DataLen) {
711 using namespace clang::io;
712 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidise8b61cf2013-01-11 22:29:49 +0000713 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
714 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000715 return std::make_pair(Start, Start + NumDecls);
716}
717
718bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000719 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000720 const std::pair<uint64_t, uint64_t> &Offsets,
721 DeclContextInfo &Info) {
722 SavedStreamPosition SavedPosition(Cursor);
723 // First the lexical decls.
724 if (Offsets.first != 0) {
725 Cursor.JumpToBit(Offsets.first);
726
727 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000728 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000729 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000730 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000731 if (RecCode != DECL_CONTEXT_LEXICAL) {
732 Error("Expected lexical block");
733 return true;
734 }
735
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000736 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
737 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000738 }
739
740 // Now the lookup table.
741 if (Offsets.second != 0) {
742 Cursor.JumpToBit(Offsets.second);
743
744 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000745 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000746 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000747 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000748 if (RecCode != DECL_CONTEXT_VISIBLE) {
749 Error("Expected visible lookup table block");
750 return true;
751 }
752 Info.NameLookupTableData
753 = ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000754 (const unsigned char *)Blob.data() + Record[0],
755 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000756 ASTDeclContextNameLookupTrait(*this, M));
757 }
758
759 return false;
760}
761
762void ASTReader::Error(StringRef Msg) {
763 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregorc1478612013-05-10 22:15:13 +0000764 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
765 Diag(diag::note_module_cache_path)
766 << PP.getHeaderSearchInfo().getModuleCachePath();
767 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000768}
769
770void ASTReader::Error(unsigned DiagID,
771 StringRef Arg1, StringRef Arg2) {
772 if (Diags.isDiagnosticInFlight())
773 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
774 else
775 Diag(DiagID) << Arg1 << Arg2;
776}
777
778//===----------------------------------------------------------------------===//
779// Source Manager Deserialization
780//===----------------------------------------------------------------------===//
781
782/// \brief Read the line table in the source manager block.
783/// \returns true if there was an error.
784bool ASTReader::ParseLineTable(ModuleFile &F,
785 SmallVectorImpl<uint64_t> &Record) {
786 unsigned Idx = 0;
787 LineTableInfo &LineTable = SourceMgr.getLineTable();
788
789 // Parse the file names
790 std::map<int, int> FileIDs;
791 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
792 // Extract the file name
793 unsigned FilenameLen = Record[Idx++];
794 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
795 Idx += FilenameLen;
796 MaybeAddSystemRootToFilename(F, Filename);
797 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
798 }
799
800 // Parse the line entries
801 std::vector<LineEntry> Entries;
802 while (Idx < Record.size()) {
803 int FID = Record[Idx++];
804 assert(FID >= 0 && "Serialized line entries for non-local file.");
805 // Remap FileID from 1-based old view.
806 FID += F.SLocEntryBaseID - 1;
807
808 // Extract the line entries
809 unsigned NumEntries = Record[Idx++];
810 assert(NumEntries && "Numentries is 00000");
811 Entries.clear();
812 Entries.reserve(NumEntries);
813 for (unsigned I = 0; I != NumEntries; ++I) {
814 unsigned FileOffset = Record[Idx++];
815 unsigned LineNo = Record[Idx++];
816 int FilenameID = FileIDs[Record[Idx++]];
817 SrcMgr::CharacteristicKind FileKind
818 = (SrcMgr::CharacteristicKind)Record[Idx++];
819 unsigned IncludeOffset = Record[Idx++];
820 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
821 FileKind, IncludeOffset));
822 }
823 LineTable.AddEntry(FileID::get(FID), Entries);
824 }
825
826 return false;
827}
828
829/// \brief Read a source manager block
830bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
831 using namespace SrcMgr;
832
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000833 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000834
835 // Set the source-location entry cursor to the current position in
836 // the stream. This cursor will be used to read the contents of the
837 // source manager block initially, and then lazily read
838 // source-location entries as needed.
839 SLocEntryCursor = F.Stream;
840
841 // The stream itself is going to skip over the source manager block.
842 if (F.Stream.SkipBlock()) {
843 Error("malformed block record in AST file");
844 return true;
845 }
846
847 // Enter the source manager block.
848 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
849 Error("malformed source manager block record in AST file");
850 return true;
851 }
852
853 RecordData Record;
854 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +0000855 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
856
857 switch (E.Kind) {
858 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
859 case llvm::BitstreamEntry::Error:
860 Error("malformed block record in AST file");
861 return true;
862 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000863 return false;
Chris Lattner88bde502013-01-19 21:39:22 +0000864 case llvm::BitstreamEntry::Record:
865 // The interesting case.
866 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000867 }
Chris Lattner88bde502013-01-19 21:39:22 +0000868
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000869 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000870 Record.clear();
Chris Lattner125eb3e2013-01-21 18:28:26 +0000871 StringRef Blob;
872 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000873 default: // Default behavior: ignore.
874 break;
875
876 case SM_SLOC_FILE_ENTRY:
877 case SM_SLOC_BUFFER_ENTRY:
878 case SM_SLOC_EXPANSION_ENTRY:
879 // Once we hit one of the source location entries, we're done.
880 return false;
881 }
882 }
883}
884
885/// \brief If a header file is not found at the path that we expect it to be
886/// and the PCH file was moved from its original location, try to resolve the
887/// file by assuming that header+PCH were moved together and the header is in
888/// the same place relative to the PCH.
889static std::string
890resolveFileRelativeToOriginalDir(const std::string &Filename,
891 const std::string &OriginalDir,
892 const std::string &CurrDir) {
893 assert(OriginalDir != CurrDir &&
894 "No point trying to resolve the file if the PCH dir didn't change");
895 using namespace llvm::sys;
896 SmallString<128> filePath(Filename);
897 fs::make_absolute(filePath);
898 assert(path::is_absolute(OriginalDir));
899 SmallString<128> currPCHPath(CurrDir);
900
901 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
902 fileDirE = path::end(path::parent_path(filePath));
903 path::const_iterator origDirI = path::begin(OriginalDir),
904 origDirE = path::end(OriginalDir);
905 // Skip the common path components from filePath and OriginalDir.
906 while (fileDirI != fileDirE && origDirI != origDirE &&
907 *fileDirI == *origDirI) {
908 ++fileDirI;
909 ++origDirI;
910 }
911 for (; origDirI != origDirE; ++origDirI)
912 path::append(currPCHPath, "..");
913 path::append(currPCHPath, fileDirI, fileDirE);
914 path::append(currPCHPath, path::filename(Filename));
915 return currPCHPath.str();
916}
917
918bool ASTReader::ReadSLocEntry(int ID) {
919 if (ID == 0)
920 return false;
921
922 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
923 Error("source location entry ID out-of-range for AST file");
924 return true;
925 }
926
927 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
928 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000929 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000930 unsigned BaseOffset = F->SLocEntryBaseOffset;
931
932 ++NumSLocEntriesRead;
Chris Lattner88bde502013-01-19 21:39:22 +0000933 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
934 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000935 Error("incorrectly-formatted source location entry in AST file");
936 return true;
937 }
Chris Lattner88bde502013-01-19 21:39:22 +0000938
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000939 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000940 StringRef Blob;
941 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000942 default:
943 Error("incorrectly-formatted source location entry in AST file");
944 return true;
945
946 case SM_SLOC_FILE_ENTRY: {
947 // We will detect whether a file changed and return 'Failure' for it, but
948 // we will also try to fail gracefully by setting up the SLocEntry.
949 unsigned InputID = Record[4];
950 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +0000951 const FileEntry *File = IF.getFile();
952 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000953
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +0000954 // Note that we only check if a File was returned. If it was out-of-date
955 // we have complained but we will continue creating a FileID to recover
956 // gracefully.
957 if (!File)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000958 return true;
959
960 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
961 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
962 // This is the module's main file.
963 IncludeLoc = getImportLocation(F);
964 }
965 SrcMgr::CharacteristicKind
966 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
967 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
968 ID, BaseOffset + Record[0]);
969 SrcMgr::FileInfo &FileInfo =
970 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
971 FileInfo.NumCreatedFIDs = Record[5];
972 if (Record[3])
973 FileInfo.setHasLineDirectives();
974
975 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
976 unsigned NumFileDecls = Record[7];
977 if (NumFileDecls) {
978 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
979 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
980 NumFileDecls));
981 }
982
983 const SrcMgr::ContentCache *ContentCache
984 = SourceMgr.getOrCreateContentCache(File,
985 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
986 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
987 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
988 unsigned Code = SLocEntryCursor.ReadCode();
989 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000990 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000991
992 if (RecCode != SM_SLOC_BUFFER_BLOB) {
993 Error("AST record has invalid code");
994 return true;
995 }
996
997 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000998 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000999 SourceMgr.overrideFileContents(File, Buffer);
1000 }
1001
1002 break;
1003 }
1004
1005 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001006 const char *Name = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001007 unsigned Offset = Record[0];
1008 SrcMgr::CharacteristicKind
1009 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1010 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1011 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1012 IncludeLoc = getImportLocation(F);
1013 }
1014 unsigned Code = SLocEntryCursor.ReadCode();
1015 Record.clear();
1016 unsigned RecCode
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001017 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001018
1019 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1020 Error("AST record has invalid code");
1021 return true;
1022 }
1023
1024 llvm::MemoryBuffer *Buffer
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001025 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001026 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1027 BaseOffset + Offset, IncludeLoc);
1028 break;
1029 }
1030
1031 case SM_SLOC_EXPANSION_ENTRY: {
1032 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1033 SourceMgr.createExpansionLoc(SpellingLoc,
1034 ReadSourceLocation(*F, Record[2]),
1035 ReadSourceLocation(*F, Record[3]),
1036 Record[4],
1037 ID,
1038 BaseOffset + Record[0]);
1039 break;
1040 }
1041 }
1042
1043 return false;
1044}
1045
1046std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1047 if (ID == 0)
1048 return std::make_pair(SourceLocation(), "");
1049
1050 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1051 Error("source location entry ID out-of-range for AST file");
1052 return std::make_pair(SourceLocation(), "");
1053 }
1054
1055 // Find which module file this entry lands in.
1056 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1057 if (M->Kind != MK_Module)
1058 return std::make_pair(SourceLocation(), "");
1059
1060 // FIXME: Can we map this down to a particular submodule? That would be
1061 // ideal.
1062 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1063}
1064
1065/// \brief Find the location where the module F is imported.
1066SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1067 if (F->ImportLoc.isValid())
1068 return F->ImportLoc;
1069
1070 // Otherwise we have a PCH. It's considered to be "imported" at the first
1071 // location of its includer.
1072 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1073 // Main file is the importer. We assume that it is the first entry in the
1074 // entry table. We can't ask the manager, because at the time of PCH loading
1075 // the main file entry doesn't exist yet.
1076 // The very first entry is the invalid instantiation loc, which takes up
1077 // offsets 0 and 1.
1078 return SourceLocation::getFromRawEncoding(2U);
1079 }
1080 //return F->Loaders[0]->FirstLoc;
1081 return F->ImportedBy[0]->FirstLoc;
1082}
1083
1084/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1085/// specified cursor. Read the abbreviations that are at the top of the block
1086/// and then leave the cursor pointing into the block.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001087bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001088 if (Cursor.EnterSubBlock(BlockID)) {
1089 Error("malformed block record in AST file");
1090 return Failure;
1091 }
1092
1093 while (true) {
1094 uint64_t Offset = Cursor.GetCurrentBitNo();
1095 unsigned Code = Cursor.ReadCode();
1096
1097 // We expect all abbrevs to be at the start of the block.
1098 if (Code != llvm::bitc::DEFINE_ABBREV) {
1099 Cursor.JumpToBit(Offset);
1100 return false;
1101 }
1102 Cursor.ReadAbbrevRecord();
1103 }
1104}
1105
Richard Smithac32d902013-08-07 21:41:30 +00001106Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallaeeacf72013-05-03 00:10:13 +00001107 unsigned &Idx) {
1108 Token Tok;
1109 Tok.startToken();
1110 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1111 Tok.setLength(Record[Idx++]);
1112 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1113 Tok.setIdentifierInfo(II);
1114 Tok.setKind((tok::TokenKind)Record[Idx++]);
1115 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1116 return Tok;
1117}
1118
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001119MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001120 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001121
1122 // Keep track of where we are in the stream, then jump back there
1123 // after reading this macro.
1124 SavedStreamPosition SavedPosition(Stream);
1125
1126 Stream.JumpToBit(Offset);
1127 RecordData Record;
1128 SmallVector<IdentifierInfo*, 16> MacroArgs;
1129 MacroInfo *Macro = 0;
1130
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001131 while (true) {
Chris Lattner99a5af02013-01-20 00:00:22 +00001132 // Advance to the next record, but if we get to the end of the block, don't
1133 // pop it (removing all the abbreviations from the cursor) since we want to
1134 // be able to reseek within the block and read entries.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001135 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattner99a5af02013-01-20 00:00:22 +00001136 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1137
1138 switch (Entry.Kind) {
1139 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1140 case llvm::BitstreamEntry::Error:
1141 Error("malformed block record in AST file");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001142 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001143 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001144 return Macro;
Chris Lattner99a5af02013-01-20 00:00:22 +00001145 case llvm::BitstreamEntry::Record:
1146 // The interesting case.
1147 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001148 }
1149
1150 // Read a record.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001151 Record.clear();
1152 PreprocessorRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001153 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001154 switch (RecType) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001155 case PP_MACRO_DIRECTIVE_HISTORY:
1156 return Macro;
1157
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001158 case PP_MACRO_OBJECT_LIKE:
1159 case PP_MACRO_FUNCTION_LIKE: {
1160 // If we already have a macro, that means that we've hit the end
1161 // of the definition of the macro we were looking for. We're
1162 // done.
1163 if (Macro)
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001164 return Macro;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001165
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001166 unsigned NextIndex = 1; // Skip identifier ID.
1167 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001168 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001169 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001170 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001171 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001172
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001173 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1174 // Decode function-like macro info.
1175 bool isC99VarArgs = Record[NextIndex++];
1176 bool isGNUVarArgs = Record[NextIndex++];
1177 bool hasCommaPasting = Record[NextIndex++];
1178 MacroArgs.clear();
1179 unsigned NumArgs = Record[NextIndex++];
1180 for (unsigned i = 0; i != NumArgs; ++i)
1181 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1182
1183 // Install function-like macro info.
1184 MI->setIsFunctionLike();
1185 if (isC99VarArgs) MI->setIsC99Varargs();
1186 if (isGNUVarArgs) MI->setIsGNUVarargs();
1187 if (hasCommaPasting) MI->setHasCommaPasting();
1188 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1189 PP.getPreprocessorAllocator());
1190 }
1191
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001192 // Remember that we saw this macro last so that we add the tokens that
1193 // form its body to it.
1194 Macro = MI;
1195
1196 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1197 Record[NextIndex]) {
1198 // We have a macro definition. Register the association
1199 PreprocessedEntityID
1200 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1201 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis0b849d32013-02-22 18:35:59 +00001202 PreprocessingRecord::PPEntityID
1203 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1204 MacroDefinition *PPDef =
1205 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1206 if (PPDef)
1207 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001208 }
1209
1210 ++NumMacrosRead;
1211 break;
1212 }
1213
1214 case PP_TOKEN: {
1215 // If we see a TOKEN before a PP_MACRO_*, then the file is
1216 // erroneous, just pretend we didn't see this.
1217 if (Macro == 0) break;
1218
John McCallaeeacf72013-05-03 00:10:13 +00001219 unsigned Idx = 0;
1220 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001221 Macro->AddTokenToBody(Tok);
1222 break;
1223 }
1224 }
1225 }
1226}
1227
1228PreprocessedEntityID
1229ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1230 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1231 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1232 assert(I != M.PreprocessedEntityRemap.end()
1233 && "Invalid index into preprocessed entity index remap");
1234
1235 return LocalID + I->second;
1236}
1237
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001238unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1239 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001240}
1241
1242HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001243HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1244 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1245 FE->getName() };
1246 return ikey;
1247}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001248
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001249bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1250 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001251 return false;
1252
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001253 if (strcmp(a.Filename, b.Filename) == 0)
1254 return true;
1255
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001256 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001257 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001258 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1259 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis1c1508b2013-03-04 20:33:40 +00001260 return (FEA && FEA == FEB);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001261}
1262
1263std::pair<unsigned, unsigned>
1264HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1265 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1266 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001267 return std::make_pair(KeyLen, DataLen);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001268}
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001269
1270HeaderFileInfoTrait::internal_key_type
1271HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1272 internal_key_type ikey;
1273 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1274 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1275 ikey.Filename = (const char *)d;
1276 return ikey;
1277}
1278
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001279HeaderFileInfoTrait::data_type
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001280HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001281 unsigned DataLen) {
1282 const unsigned char *End = d + DataLen;
1283 using namespace clang::io;
1284 HeaderFileInfo HFI;
1285 unsigned Flags = *d++;
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001286 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1287 ((Flags >> 6) & 0x03);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001288 HFI.isImport = (Flags >> 5) & 0x01;
1289 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1290 HFI.DirInfo = (Flags >> 2) & 0x03;
1291 HFI.Resolved = (Flags >> 1) & 0x01;
1292 HFI.IndexHeaderMapHeader = Flags & 0x01;
1293 HFI.NumIncludes = ReadUnalignedLE16(d);
1294 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1295 ReadUnalignedLE32(d));
1296 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1297 // The framework offset is 1 greater than the actual offset,
1298 // since 0 is used as an indicator for "no framework name".
1299 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1300 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1301 }
1302
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001303 if (d != End) {
1304 uint32_t LocalSMID = ReadUnalignedLE32(d);
1305 if (LocalSMID) {
1306 // This header is part of a module. Associate it with the module to enable
1307 // implicit module import.
1308 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1309 Module *Mod = Reader.getSubmodule(GlobalSMID);
1310 HFI.isModuleHeader = true;
1311 FileManager &FileMgr = Reader.getFileManager();
1312 ModuleMap &ModMap =
1313 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001314 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001315 }
1316 }
1317
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001318 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1319 (void)End;
1320
1321 // This HeaderFileInfo was externally loaded.
1322 HFI.External = true;
1323 return HFI;
1324}
1325
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001326void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1327 ModuleFile *M,
1328 GlobalMacroID GMacID,
1329 SourceLocation ImportLoc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001330 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001331 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1332}
1333
1334void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1335 ModuleFile *M,
1336 uint64_t MacroDirectivesOffset) {
1337 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1338 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001339}
1340
1341void ASTReader::ReadDefinedMacros() {
1342 // Note that we are loading defined macros.
1343 Deserializing Macros(this);
1344
1345 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1346 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001347 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001348
1349 // If there was no preprocessor block, skip this file.
1350 if (!MacroCursor.getBitStreamReader())
1351 continue;
1352
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001353 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001354 Cursor.JumpToBit((*I)->MacroStartOffset);
1355
1356 RecordData Record;
1357 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001358 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1359
1360 switch (E.Kind) {
1361 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1362 case llvm::BitstreamEntry::Error:
1363 Error("malformed block record in AST file");
1364 return;
1365 case llvm::BitstreamEntry::EndBlock:
1366 goto NextCursor;
1367
1368 case llvm::BitstreamEntry::Record:
Chris Lattner88bde502013-01-19 21:39:22 +00001369 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001370 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001371 default: // Default behavior: ignore.
1372 break;
1373
1374 case PP_MACRO_OBJECT_LIKE:
1375 case PP_MACRO_FUNCTION_LIKE:
1376 getLocalIdentifier(**I, Record[0]);
1377 break;
1378
1379 case PP_TOKEN:
1380 // Ignore tokens.
1381 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001382 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001383 break;
1384 }
1385 }
Chris Lattner88bde502013-01-19 21:39:22 +00001386 NextCursor: ;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001387 }
1388}
1389
1390namespace {
1391 /// \brief Visitor class used to look up identifirs in an AST file.
1392 class IdentifierLookupVisitor {
1393 StringRef Name;
1394 unsigned PriorGeneration;
Douglas Gregore1698072013-01-25 00:38:33 +00001395 unsigned &NumIdentifierLookups;
1396 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001397 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001398
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001399 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001400 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1401 unsigned &NumIdentifierLookups,
1402 unsigned &NumIdentifierLookupHits)
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001403 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregore1698072013-01-25 00:38:33 +00001404 NumIdentifierLookups(NumIdentifierLookups),
1405 NumIdentifierLookupHits(NumIdentifierLookupHits),
1406 Found()
1407 {
1408 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001409
1410 static bool visit(ModuleFile &M, void *UserData) {
1411 IdentifierLookupVisitor *This
1412 = static_cast<IdentifierLookupVisitor *>(UserData);
1413
1414 // If we've already searched this module file, skip it now.
1415 if (M.Generation <= This->PriorGeneration)
1416 return true;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001417
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001418 ASTIdentifierLookupTable *IdTable
1419 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1420 if (!IdTable)
1421 return false;
1422
1423 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1424 M, This->Found);
Douglas Gregore1698072013-01-25 00:38:33 +00001425 ++This->NumIdentifierLookups;
1426 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001427 if (Pos == IdTable->end())
1428 return false;
1429
1430 // Dereferencing the iterator has the effect of building the
1431 // IdentifierInfo node and populating it with the various
1432 // declarations it needs.
Douglas Gregore1698072013-01-25 00:38:33 +00001433 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001434 This->Found = *Pos;
1435 return true;
1436 }
1437
1438 // \brief Retrieve the identifier info found within the module
1439 // files.
1440 IdentifierInfo *getIdentifierInfo() const { return Found; }
1441 };
1442}
1443
1444void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1445 // Note that we are loading an identifier.
1446 Deserializing AnIdentifier(this);
1447
1448 unsigned PriorGeneration = 0;
1449 if (getContext().getLangOpts().Modules)
1450 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregor1a49d972013-01-25 01:03:03 +00001451
1452 // If there is a global index, look there first to determine which modules
1453 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001454 GlobalModuleIndex::HitSet Hits;
1455 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001456 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001457 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1458 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001459 }
1460 }
1461
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001462 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregore1698072013-01-25 00:38:33 +00001463 NumIdentifierLookups,
1464 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001465 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001466 markIdentifierUpToDate(&II);
1467}
1468
1469void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1470 if (!II)
1471 return;
1472
1473 II->setOutOfDate(false);
1474
1475 // Update the generation for this identifier.
1476 if (getContext().getLangOpts().Modules)
1477 IdentifierGeneration[II] = CurrentGeneration;
1478}
1479
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001480void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1481 const PendingMacroInfo &PMInfo) {
1482 assert(II);
1483
1484 if (PMInfo.M->Kind != MK_Module) {
1485 installPCHMacroDirectives(II, *PMInfo.M,
1486 PMInfo.PCHMacroData.MacroDirectivesOffset);
1487 return;
1488 }
1489
1490 // Module Macro.
1491
1492 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1493 SourceLocation ImportLoc =
1494 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1495
1496 assert(GMacID);
1497 // If this macro has already been loaded, don't do so again.
1498 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1499 return;
1500
1501 MacroInfo *MI = getMacro(GMacID);
1502 SubmoduleID SubModID = MI->getOwningModuleID();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001503 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1504 /*isImported=*/true);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001505
1506 // Determine whether this macro definition is visible.
1507 bool Hidden = false;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001508 Module *Owner = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001509 if (SubModID) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001510 if ((Owner = getSubmodule(SubModID))) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001511 if (Owner->NameVisibility == Module::Hidden) {
1512 // The owning module is not visible, and this macro definition
1513 // should not be, either.
1514 Hidden = true;
1515
1516 // Note that this macro definition was hidden because its owning
1517 // module is not yet visible.
1518 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1519 }
1520 }
1521 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001522
1523 if (!Hidden)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001524 installImportedMacro(II, MD, Owner);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001525}
1526
1527void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1528 ModuleFile &M, uint64_t Offset) {
1529 assert(M.Kind != MK_Module);
1530
1531 BitstreamCursor &Cursor = M.MacroCursor;
1532 SavedStreamPosition SavedPosition(Cursor);
1533 Cursor.JumpToBit(Offset);
1534
1535 llvm::BitstreamEntry Entry =
1536 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1537 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1538 Error("malformed block record in AST file");
1539 return;
1540 }
1541
1542 RecordData Record;
1543 PreprocessorRecordTypes RecType =
1544 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1545 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1546 Error("malformed block record in AST file");
1547 return;
1548 }
1549
1550 // Deserialize the macro directives history in reverse source-order.
1551 MacroDirective *Latest = 0, *Earliest = 0;
1552 unsigned Idx = 0, N = Record.size();
1553 while (Idx < N) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001554 MacroDirective *MD = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001555 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001556 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1557 switch (K) {
1558 case MacroDirective::MD_Define: {
1559 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1560 MacroInfo *MI = getMacro(GMacID);
1561 bool isImported = Record[Idx++];
1562 bool isAmbiguous = Record[Idx++];
1563 DefMacroDirective *DefMD =
1564 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1565 DefMD->setAmbiguous(isAmbiguous);
1566 MD = DefMD;
1567 break;
1568 }
1569 case MacroDirective::MD_Undefine:
1570 MD = PP.AllocateUndefMacroDirective(Loc);
1571 break;
1572 case MacroDirective::MD_Visibility: {
1573 bool isPublic = Record[Idx++];
1574 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1575 break;
1576 }
1577 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001578
1579 if (!Latest)
1580 Latest = MD;
1581 if (Earliest)
1582 Earliest->setPrevious(MD);
1583 Earliest = MD;
1584 }
1585
1586 PP.setLoadedMacroDirective(II, Latest);
1587}
1588
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001589/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor7332ae42013-04-12 21:00:54 +00001590/// modules.
1591static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001592 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001593 assert(PrevMI && NewMI);
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001594 Module *PrevOwner = 0;
1595 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1596 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001597 SourceManager &SrcMgr = Reader.getSourceManager();
1598 bool PrevInSystem
1599 = PrevOwner? PrevOwner->IsSystem
1600 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1601 bool NewInSystem
1602 = NewOwner? NewOwner->IsSystem
1603 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1604 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001605 return false;
Douglas Gregorf9dbae72013-06-07 22:56:11 +00001606 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001607}
1608
1609void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1610 Module *Owner) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001611 assert(II && MD);
1612
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001613 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001614 MacroDirective *Prev = PP.getMacroDirective(II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001615 if (Prev) {
1616 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001617 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1618 MacroInfo *NewMI = DefMD->getInfo();
Argyrios Kyrtzidisbd25ff82013-04-03 17:39:30 +00001619 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1620 /*Syntactically=*/true)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001621 // Before marking the macros as ambiguous, check if this is a case where
Douglas Gregor7332ae42013-04-12 21:00:54 +00001622 // both macros are in system headers. If so, we trust that the system
1623 // did not get it wrong. This also handles cases where Clang's own
1624 // headers have a different spelling of certain system macros:
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001625 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1626 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
Douglas Gregor7332ae42013-04-12 21:00:54 +00001627 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001628 PrevDef.getDirective()->setAmbiguous(true);
1629 DefMD->setAmbiguous(true);
1630 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001631 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001632 }
1633
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001634 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001635}
1636
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001637InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001638 // If this ID is bogus, just return an empty input file.
1639 if (ID == 0 || ID > F.InputFilesLoaded.size())
1640 return InputFile();
1641
1642 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001643 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001644 return F.InputFilesLoaded[ID-1];
1645
1646 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001647 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001648 SavedStreamPosition SavedPosition(Cursor);
1649 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1650
1651 unsigned Code = Cursor.ReadCode();
1652 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001653 StringRef Blob;
1654 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001655 case INPUT_FILE: {
1656 unsigned StoredID = Record[0];
1657 assert(ID == StoredID && "Bogus stored ID or offset");
1658 (void)StoredID;
1659 off_t StoredSize = (off_t)Record[1];
1660 time_t StoredTime = (time_t)Record[2];
1661 bool Overridden = (bool)Record[3];
1662
1663 // Get the file entry for this input file.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001664 StringRef OrigFilename = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001665 std::string Filename = OrigFilename;
1666 MaybeAddSystemRootToFilename(F, Filename);
1667 const FileEntry *File
1668 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1669 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1670
1671 // If we didn't find the file, resolve it relative to the
1672 // original directory from which this AST file was created.
1673 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1674 F.OriginalDir != CurrentDir) {
1675 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1676 F.OriginalDir,
1677 CurrentDir);
1678 if (!Resolved.empty())
1679 File = FileMgr.getFile(Resolved);
1680 }
1681
1682 // For an overridden file, create a virtual file with the stored
1683 // size/timestamp.
1684 if (Overridden && File == 0) {
1685 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1686 }
1687
1688 if (File == 0) {
1689 if (Complain) {
1690 std::string ErrorStr = "could not find file '";
1691 ErrorStr += Filename;
1692 ErrorStr += "' referenced by AST file";
1693 Error(ErrorStr.c_str());
1694 }
1695 return InputFile();
1696 }
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001697
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001698 // Check if there was a request to override the contents of the file
1699 // that was part of the precompiled header. Overridding such a file
1700 // can lead to problems when lexing using the source locations from the
1701 // PCH.
1702 SourceManager &SM = getSourceManager();
1703 if (!Overridden && SM.isFileOverridden(File)) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001704 if (Complain)
1705 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001706 // After emitting the diagnostic, recover by disabling the override so
1707 // that the original file will be used.
1708 SM.disableFileContentsOverride(File);
1709 // The FileEntry is a virtual file entry with the size of the contents
1710 // that would override the original contents. Set it to the original's
1711 // size/time.
1712 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1713 StoredSize, StoredTime);
1714 }
1715
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001716 bool IsOutOfDate = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001717
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001718 // For an overridden file, there is nothing to validate.
1719 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001720#if !defined(LLVM_ON_WIN32)
1721 // In our regression testing, the Windows file system seems to
1722 // have inconsistent modification times that sometimes
1723 // erroneously trigger this error-handling path.
1724 || StoredTime != File->getModificationTime()
1725#endif
1726 )) {
Douglas Gregor677e15f2013-03-19 00:28:20 +00001727 if (Complain) {
Argyrios Kyrtzidisf8f373f2013-03-08 20:42:38 +00001728 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregorc1478612013-05-10 22:15:13 +00001729 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
1730 Diag(diag::note_module_cache_path)
1731 << PP.getHeaderSearchInfo().getModuleCachePath();
1732 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00001733 }
1734
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001735 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001736 }
1737
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001738 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1739
1740 // Note that we've loaded this input file.
1741 F.InputFilesLoaded[ID-1] = IF;
1742 return IF;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001743 }
1744 }
1745
1746 return InputFile();
1747}
1748
1749const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1750 ModuleFile &M = ModuleMgr.getPrimaryModule();
1751 std::string Filename = filenameStrRef;
1752 MaybeAddSystemRootToFilename(M, Filename);
1753 const FileEntry *File = FileMgr.getFile(Filename);
1754 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1755 M.OriginalDir != CurrentDir) {
1756 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1757 M.OriginalDir,
1758 CurrentDir);
1759 if (!resolved.empty())
1760 File = FileMgr.getFile(resolved);
1761 }
1762
1763 return File;
1764}
1765
1766/// \brief If we are loading a relocatable PCH file, and the filename is
1767/// not an absolute path, add the system root to the beginning of the file
1768/// name.
1769void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1770 std::string &Filename) {
1771 // If this is not a relocatable PCH file, there's nothing to do.
1772 if (!M.RelocatablePCH)
1773 return;
1774
1775 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1776 return;
1777
1778 if (isysroot.empty()) {
1779 // If no system root was given, default to '/'
1780 Filename.insert(Filename.begin(), '/');
1781 return;
1782 }
1783
1784 unsigned Length = isysroot.size();
1785 if (isysroot[Length - 1] != '/')
1786 Filename.insert(Filename.begin(), '/');
1787
1788 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1789}
1790
1791ASTReader::ASTReadResult
1792ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001793 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001794 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001795 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001796
1797 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1798 Error("malformed block record in AST file");
1799 return Failure;
1800 }
1801
1802 // Read all of the records and blocks in the control block.
1803 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001804 while (1) {
1805 llvm::BitstreamEntry Entry = Stream.advance();
1806
1807 switch (Entry.Kind) {
1808 case llvm::BitstreamEntry::Error:
1809 Error("malformed block record in AST file");
1810 return Failure;
1811 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001812 // Validate all of the non-system input files.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001813 if (!DisableValidation) {
1814 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001815 // All user input files reside at the index range [0, Record[1]).
1816 // Record is the one from INPUT_FILE_OFFSETS.
1817 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001818 InputFile IF = getInputFile(F, I+1, Complain);
1819 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001820 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001821 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001822 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001823 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001824
1825 case llvm::BitstreamEntry::SubBlock:
1826 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001827 case INPUT_FILES_BLOCK_ID:
1828 F.InputFilesCursor = Stream;
1829 if (Stream.SkipBlock() || // Skip with the main cursor
1830 // Read the abbreviations
1831 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1832 Error("malformed block record in AST file");
1833 return Failure;
1834 }
1835 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001836
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001837 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001838 if (Stream.SkipBlock()) {
1839 Error("malformed block record in AST file");
1840 return Failure;
1841 }
1842 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001843 }
Chris Lattner88bde502013-01-19 21:39:22 +00001844
1845 case llvm::BitstreamEntry::Record:
1846 // The interesting case.
1847 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001848 }
1849
1850 // Read and process a record.
1851 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001852 StringRef Blob;
1853 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001854 case METADATA: {
1855 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1856 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1857 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1858 : diag::warn_pch_version_too_new);
1859 return VersionMismatch;
1860 }
1861
1862 bool hasErrors = Record[5];
1863 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1864 Diag(diag::err_pch_with_compiler_errors);
1865 return HadErrors;
1866 }
1867
1868 F.RelocatablePCH = Record[4];
1869
1870 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001871 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001872 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1873 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1874 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1875 return VersionMismatch;
1876 }
1877 break;
1878 }
1879
1880 case IMPORTS: {
1881 // Load each of the imported PCH files.
1882 unsigned Idx = 0, N = Record.size();
1883 while (Idx < N) {
1884 // Read information about the AST file.
1885 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1886 // The import location will be the local one for now; we will adjust
1887 // all import locations of module imports after the global source
1888 // location info are setup.
1889 SourceLocation ImportLoc =
1890 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001891 off_t StoredSize = (off_t)Record[Idx++];
1892 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001893 unsigned Length = Record[Idx++];
1894 SmallString<128> ImportedFile(Record.begin() + Idx,
1895 Record.begin() + Idx + Length);
1896 Idx += Length;
1897
1898 // Load the AST file.
1899 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00001900 StoredSize, StoredModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001901 ClientLoadCapabilities)) {
1902 case Failure: return Failure;
1903 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorac39f132013-03-19 00:38:50 +00001904 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001905 case OutOfDate: return OutOfDate;
1906 case VersionMismatch: return VersionMismatch;
1907 case ConfigurationMismatch: return ConfigurationMismatch;
1908 case HadErrors: return HadErrors;
1909 case Success: break;
1910 }
1911 }
1912 break;
1913 }
1914
1915 case LANGUAGE_OPTIONS: {
1916 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1917 if (Listener && &F == *ModuleMgr.begin() &&
1918 ParseLanguageOptions(Record, Complain, *Listener) &&
1919 !DisableValidation)
1920 return ConfigurationMismatch;
1921 break;
1922 }
1923
1924 case TARGET_OPTIONS: {
1925 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1926 if (Listener && &F == *ModuleMgr.begin() &&
1927 ParseTargetOptions(Record, Complain, *Listener) &&
1928 !DisableValidation)
1929 return ConfigurationMismatch;
1930 break;
1931 }
1932
1933 case DIAGNOSTIC_OPTIONS: {
1934 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1935 if (Listener && &F == *ModuleMgr.begin() &&
1936 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1937 !DisableValidation)
1938 return ConfigurationMismatch;
1939 break;
1940 }
1941
1942 case FILE_SYSTEM_OPTIONS: {
1943 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1944 if (Listener && &F == *ModuleMgr.begin() &&
1945 ParseFileSystemOptions(Record, Complain, *Listener) &&
1946 !DisableValidation)
1947 return ConfigurationMismatch;
1948 break;
1949 }
1950
1951 case HEADER_SEARCH_OPTIONS: {
1952 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1953 if (Listener && &F == *ModuleMgr.begin() &&
1954 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1955 !DisableValidation)
1956 return ConfigurationMismatch;
1957 break;
1958 }
1959
1960 case PREPROCESSOR_OPTIONS: {
1961 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1962 if (Listener && &F == *ModuleMgr.begin() &&
1963 ParsePreprocessorOptions(Record, Complain, *Listener,
1964 SuggestedPredefines) &&
1965 !DisableValidation)
1966 return ConfigurationMismatch;
1967 break;
1968 }
1969
1970 case ORIGINAL_FILE:
1971 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001972 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001973 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1974 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1975 break;
1976
1977 case ORIGINAL_FILE_ID:
1978 F.OriginalSourceFileID = FileID::get(Record[0]);
1979 break;
1980
1981 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001982 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001983 break;
1984
1985 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001986 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001987 F.InputFilesLoaded.resize(Record[0]);
1988 break;
1989 }
1990 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001991}
1992
1993bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001994 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001995
1996 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1997 Error("malformed block record in AST file");
1998 return true;
1999 }
2000
2001 // Read all of the records and blocks for the AST file.
2002 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00002003 while (1) {
2004 llvm::BitstreamEntry Entry = Stream.advance();
2005
2006 switch (Entry.Kind) {
2007 case llvm::BitstreamEntry::Error:
2008 Error("error at end of module block in AST file");
2009 return true;
2010 case llvm::BitstreamEntry::EndBlock: {
Richard Smith43828672013-04-03 22:49:41 +00002011 // Outside of C++, we do not store a lookup map for the translation unit.
2012 // Instead, mark it as needing a lookup map to be built if this module
2013 // contains any declarations lexically within it (which it always does!).
2014 // This usually has no cost, since we very rarely need the lookup map for
2015 // the translation unit outside C++.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002016 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smith43828672013-04-03 22:49:41 +00002017 if (DC->hasExternalLexicalStorage() &&
2018 !getContext().getLangOpts().CPlusPlus)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002019 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00002020
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002021 return false;
2022 }
Chris Lattner88bde502013-01-19 21:39:22 +00002023 case llvm::BitstreamEntry::SubBlock:
2024 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002025 case DECLTYPES_BLOCK_ID:
2026 // We lazily load the decls block, but we want to set up the
2027 // DeclsCursor cursor to point into it. Clone our current bitcode
2028 // cursor to it, enter the block and read the abbrevs in that block.
2029 // With the main cursor, we just skip over it.
2030 F.DeclsCursor = Stream;
2031 if (Stream.SkipBlock() || // Skip with the main cursor.
2032 // Read the abbrevs.
2033 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
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 DECL_UPDATES_BLOCK_ID:
2040 if (Stream.SkipBlock()) {
2041 Error("malformed block record in AST file");
2042 return true;
2043 }
2044 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002045
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002046 case PREPROCESSOR_BLOCK_ID:
2047 F.MacroCursor = Stream;
2048 if (!PP.getExternalSource())
2049 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00002050
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002051 if (Stream.SkipBlock() ||
2052 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2053 Error("malformed block record in AST file");
2054 return true;
2055 }
2056 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2057 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002058
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002059 case PREPROCESSOR_DETAIL_BLOCK_ID:
2060 F.PreprocessorDetailCursor = Stream;
2061 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00002062 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002063 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00002064 Error("malformed preprocessor detail record in AST file");
2065 return true;
2066 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002067 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002068 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2069
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002070 if (!PP.getPreprocessingRecord())
2071 PP.createPreprocessingRecord();
2072 if (!PP.getPreprocessingRecord()->getExternalSource())
2073 PP.getPreprocessingRecord()->SetExternalSource(*this);
2074 break;
2075
2076 case SOURCE_MANAGER_BLOCK_ID:
2077 if (ReadSourceManagerBlock(F))
2078 return true;
2079 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002080
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002081 case SUBMODULE_BLOCK_ID:
2082 if (ReadSubmoduleBlock(F))
2083 return true;
2084 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002085
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002086 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002087 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002088 if (Stream.SkipBlock() ||
2089 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2090 Error("malformed comments block in AST file");
2091 return true;
2092 }
2093 CommentsCursors.push_back(std::make_pair(C, &F));
2094 break;
2095 }
Chris Lattner88bde502013-01-19 21:39:22 +00002096
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002097 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002098 if (Stream.SkipBlock()) {
2099 Error("malformed block record in AST file");
2100 return true;
2101 }
2102 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002103 }
2104 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002105
2106 case llvm::BitstreamEntry::Record:
2107 // The interesting case.
2108 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002109 }
2110
2111 // Read and process a record.
2112 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002113 StringRef Blob;
2114 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002115 default: // Default behavior: ignore.
2116 break;
2117
2118 case TYPE_OFFSET: {
2119 if (F.LocalNumTypes != 0) {
2120 Error("duplicate TYPE_OFFSET record in AST file");
2121 return true;
2122 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002123 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002124 F.LocalNumTypes = Record[0];
2125 unsigned LocalBaseTypeIndex = Record[1];
2126 F.BaseTypeIndex = getTotalNumTypes();
2127
2128 if (F.LocalNumTypes > 0) {
2129 // Introduce the global -> local mapping for types within this module.
2130 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2131
2132 // Introduce the local -> global mapping for types within this module.
2133 F.TypeRemap.insertOrReplace(
2134 std::make_pair(LocalBaseTypeIndex,
2135 F.BaseTypeIndex - LocalBaseTypeIndex));
2136
2137 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2138 }
2139 break;
2140 }
2141
2142 case DECL_OFFSET: {
2143 if (F.LocalNumDecls != 0) {
2144 Error("duplicate DECL_OFFSET record in AST file");
2145 return true;
2146 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002147 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002148 F.LocalNumDecls = Record[0];
2149 unsigned LocalBaseDeclID = Record[1];
2150 F.BaseDeclID = getTotalNumDecls();
2151
2152 if (F.LocalNumDecls > 0) {
2153 // Introduce the global -> local mapping for declarations within this
2154 // module.
2155 GlobalDeclMap.insert(
2156 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2157
2158 // Introduce the local -> global mapping for declarations within this
2159 // module.
2160 F.DeclRemap.insertOrReplace(
2161 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2162
2163 // Introduce the global -> local mapping for declarations within this
2164 // module.
2165 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2166
2167 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2168 }
2169 break;
2170 }
2171
2172 case TU_UPDATE_LEXICAL: {
2173 DeclContext *TU = Context.getTranslationUnitDecl();
2174 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002175 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002176 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002177 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002178 TU->setHasExternalLexicalStorage(true);
2179 break;
2180 }
2181
2182 case UPDATE_VISIBLE: {
2183 unsigned Idx = 0;
2184 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2185 ASTDeclContextNameLookupTable *Table =
2186 ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002187 (const unsigned char *)Blob.data() + Record[Idx++],
2188 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002189 ASTDeclContextNameLookupTrait(*this, F));
2190 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2191 DeclContext *TU = Context.getTranslationUnitDecl();
2192 F.DeclContextInfos[TU].NameLookupTableData = Table;
2193 TU->setHasExternalVisibleStorage(true);
2194 } else
2195 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2196 break;
2197 }
2198
2199 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002200 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002201 if (Record[0]) {
2202 F.IdentifierLookupTable
2203 = ASTIdentifierLookupTable::Create(
2204 (const unsigned char *)F.IdentifierTableData + Record[0],
2205 (const unsigned char *)F.IdentifierTableData,
2206 ASTIdentifierLookupTrait(*this, F));
2207
2208 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2209 }
2210 break;
2211
2212 case IDENTIFIER_OFFSET: {
2213 if (F.LocalNumIdentifiers != 0) {
2214 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2215 return true;
2216 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002217 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002218 F.LocalNumIdentifiers = Record[0];
2219 unsigned LocalBaseIdentifierID = Record[1];
2220 F.BaseIdentifierID = getTotalNumIdentifiers();
2221
2222 if (F.LocalNumIdentifiers > 0) {
2223 // Introduce the global -> local mapping for identifiers within this
2224 // module.
2225 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2226 &F));
2227
2228 // Introduce the local -> global mapping for identifiers within this
2229 // module.
2230 F.IdentifierRemap.insertOrReplace(
2231 std::make_pair(LocalBaseIdentifierID,
2232 F.BaseIdentifierID - LocalBaseIdentifierID));
2233
2234 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2235 + F.LocalNumIdentifiers);
2236 }
2237 break;
2238 }
2239
2240 case EXTERNAL_DEFINITIONS:
2241 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2242 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2243 break;
2244
2245 case SPECIAL_TYPES:
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002246 if (SpecialTypes.empty()) {
2247 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2248 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2249 break;
2250 }
2251
2252 if (SpecialTypes.size() != Record.size()) {
2253 Error("invalid special-types record");
2254 return true;
2255 }
2256
2257 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2258 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2259 if (!SpecialTypes[I])
2260 SpecialTypes[I] = ID;
2261 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2262 // merge step?
2263 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002264 break;
2265
2266 case STATISTICS:
2267 TotalNumStatements += Record[0];
2268 TotalNumMacros += Record[1];
2269 TotalLexicalDeclContexts += Record[2];
2270 TotalVisibleDeclContexts += Record[3];
2271 break;
2272
2273 case UNUSED_FILESCOPED_DECLS:
2274 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2275 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2276 break;
2277
2278 case DELEGATING_CTORS:
2279 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2280 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2281 break;
2282
2283 case WEAK_UNDECLARED_IDENTIFIERS:
2284 if (Record.size() % 4 != 0) {
2285 Error("invalid weak identifiers record");
2286 return true;
2287 }
2288
2289 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2290 // files. This isn't the way to do it :)
2291 WeakUndeclaredIdentifiers.clear();
2292
2293 // Translate the weak, undeclared identifiers into global IDs.
2294 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2295 WeakUndeclaredIdentifiers.push_back(
2296 getGlobalIdentifierID(F, Record[I++]));
2297 WeakUndeclaredIdentifiers.push_back(
2298 getGlobalIdentifierID(F, Record[I++]));
2299 WeakUndeclaredIdentifiers.push_back(
2300 ReadSourceLocation(F, Record, I).getRawEncoding());
2301 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2302 }
2303 break;
2304
Richard Smith5ea6ef42013-01-10 23:43:47 +00002305 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002306 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002307 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002308 break;
2309
2310 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002311 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002312 F.LocalNumSelectors = Record[0];
2313 unsigned LocalBaseSelectorID = Record[1];
2314 F.BaseSelectorID = getTotalNumSelectors();
2315
2316 if (F.LocalNumSelectors > 0) {
2317 // Introduce the global -> local mapping for selectors within this
2318 // module.
2319 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2320
2321 // Introduce the local -> global mapping for selectors within this
2322 // module.
2323 F.SelectorRemap.insertOrReplace(
2324 std::make_pair(LocalBaseSelectorID,
2325 F.BaseSelectorID - LocalBaseSelectorID));
2326
2327 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2328 }
2329 break;
2330 }
2331
2332 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002333 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002334 if (Record[0])
2335 F.SelectorLookupTable
2336 = ASTSelectorLookupTable::Create(
2337 F.SelectorLookupTableData + Record[0],
2338 F.SelectorLookupTableData,
2339 ASTSelectorLookupTrait(*this, F));
2340 TotalNumMethodPoolEntries += Record[1];
2341 break;
2342
2343 case REFERENCED_SELECTOR_POOL:
2344 if (!Record.empty()) {
2345 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2346 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2347 Record[Idx++]));
2348 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2349 getRawEncoding());
2350 }
2351 }
2352 break;
2353
2354 case PP_COUNTER_VALUE:
2355 if (!Record.empty() && Listener)
2356 Listener->ReadCounter(F, Record[0]);
2357 break;
2358
2359 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002360 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002361 F.NumFileSortedDecls = Record[0];
2362 break;
2363
2364 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002365 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002366 F.LocalNumSLocEntries = Record[0];
2367 unsigned SLocSpaceSize = Record[1];
2368 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2369 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2370 SLocSpaceSize);
2371 // Make our entry in the range map. BaseID is negative and growing, so
2372 // we invert it. Because we invert it, though, we need the other end of
2373 // the range.
2374 unsigned RangeStart =
2375 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2376 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2377 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2378
2379 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2380 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2381 GlobalSLocOffsetMap.insert(
2382 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2383 - SLocSpaceSize,&F));
2384
2385 // Initialize the remapping table.
2386 // Invalid stays invalid.
2387 F.SLocRemap.insert(std::make_pair(0U, 0));
2388 // This module. Base was 2 when being compiled.
2389 F.SLocRemap.insert(std::make_pair(2U,
2390 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2391
2392 TotalNumSLocEntries += F.LocalNumSLocEntries;
2393 break;
2394 }
2395
2396 case MODULE_OFFSET_MAP: {
2397 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002398 const unsigned char *Data = (const unsigned char*)Blob.data();
2399 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002400
2401 // Continuous range maps we may be updating in our module.
2402 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2403 ContinuousRangeMap<uint32_t, int, 2>::Builder
2404 IdentifierRemap(F.IdentifierRemap);
2405 ContinuousRangeMap<uint32_t, int, 2>::Builder
2406 MacroRemap(F.MacroRemap);
2407 ContinuousRangeMap<uint32_t, int, 2>::Builder
2408 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2409 ContinuousRangeMap<uint32_t, int, 2>::Builder
2410 SubmoduleRemap(F.SubmoduleRemap);
2411 ContinuousRangeMap<uint32_t, int, 2>::Builder
2412 SelectorRemap(F.SelectorRemap);
2413 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2414 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2415
2416 while(Data < DataEnd) {
2417 uint16_t Len = io::ReadUnalignedLE16(Data);
2418 StringRef Name = StringRef((const char*)Data, Len);
2419 Data += Len;
2420 ModuleFile *OM = ModuleMgr.lookup(Name);
2421 if (!OM) {
2422 Error("SourceLocation remap refers to unknown module");
2423 return true;
2424 }
2425
2426 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2427 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2428 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2429 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2430 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2431 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2432 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2433 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2434
2435 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2436 SLocRemap.insert(std::make_pair(SLocOffset,
2437 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2438 IdentifierRemap.insert(
2439 std::make_pair(IdentifierIDOffset,
2440 OM->BaseIdentifierID - IdentifierIDOffset));
2441 MacroRemap.insert(std::make_pair(MacroIDOffset,
2442 OM->BaseMacroID - MacroIDOffset));
2443 PreprocessedEntityRemap.insert(
2444 std::make_pair(PreprocessedEntityIDOffset,
2445 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2446 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2447 OM->BaseSubmoduleID - SubmoduleIDOffset));
2448 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2449 OM->BaseSelectorID - SelectorIDOffset));
2450 DeclRemap.insert(std::make_pair(DeclIDOffset,
2451 OM->BaseDeclID - DeclIDOffset));
2452
2453 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2454 OM->BaseTypeIndex - TypeIndexOffset));
2455
2456 // Global -> local mappings.
2457 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2458 }
2459 break;
2460 }
2461
2462 case SOURCE_MANAGER_LINE_TABLE:
2463 if (ParseLineTable(F, Record))
2464 return true;
2465 break;
2466
2467 case SOURCE_LOCATION_PRELOADS: {
2468 // Need to transform from the local view (1-based IDs) to the global view,
2469 // which is based off F.SLocEntryBaseID.
2470 if (!F.PreloadSLocEntries.empty()) {
2471 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2472 return true;
2473 }
2474
2475 F.PreloadSLocEntries.swap(Record);
2476 break;
2477 }
2478
2479 case EXT_VECTOR_DECLS:
2480 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2481 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2482 break;
2483
2484 case VTABLE_USES:
2485 if (Record.size() % 3 != 0) {
2486 Error("Invalid VTABLE_USES record");
2487 return true;
2488 }
2489
2490 // Later tables overwrite earlier ones.
2491 // FIXME: Modules will have some trouble with this. This is clearly not
2492 // the right way to do this.
2493 VTableUses.clear();
2494
2495 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2496 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2497 VTableUses.push_back(
2498 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2499 VTableUses.push_back(Record[Idx++]);
2500 }
2501 break;
2502
2503 case DYNAMIC_CLASSES:
2504 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2505 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2506 break;
2507
2508 case PENDING_IMPLICIT_INSTANTIATIONS:
2509 if (PendingInstantiations.size() % 2 != 0) {
2510 Error("Invalid existing PendingInstantiations");
2511 return true;
2512 }
2513
2514 if (Record.size() % 2 != 0) {
2515 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2516 return true;
2517 }
2518
2519 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2520 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2521 PendingInstantiations.push_back(
2522 ReadSourceLocation(F, Record, I).getRawEncoding());
2523 }
2524 break;
2525
2526 case SEMA_DECL_REFS:
2527 // Later tables overwrite earlier ones.
2528 // FIXME: Modules will have some trouble with this.
2529 SemaDeclRefs.clear();
2530 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2531 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2532 break;
2533
2534 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002535 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2536 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2537 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002538
2539 unsigned LocalBasePreprocessedEntityID = Record[0];
2540
2541 unsigned StartingID;
2542 if (!PP.getPreprocessingRecord())
2543 PP.createPreprocessingRecord();
2544 if (!PP.getPreprocessingRecord()->getExternalSource())
2545 PP.getPreprocessingRecord()->SetExternalSource(*this);
2546 StartingID
2547 = PP.getPreprocessingRecord()
2548 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2549 F.BasePreprocessedEntityID = StartingID;
2550
2551 if (F.NumPreprocessedEntities > 0) {
2552 // Introduce the global -> local mapping for preprocessed entities in
2553 // this module.
2554 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2555
2556 // Introduce the local -> global mapping for preprocessed entities in
2557 // this module.
2558 F.PreprocessedEntityRemap.insertOrReplace(
2559 std::make_pair(LocalBasePreprocessedEntityID,
2560 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2561 }
2562
2563 break;
2564 }
2565
2566 case DECL_UPDATE_OFFSETS: {
2567 if (Record.size() % 2 != 0) {
2568 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2569 return true;
2570 }
2571 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2572 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2573 .push_back(std::make_pair(&F, Record[I+1]));
2574 break;
2575 }
2576
2577 case DECL_REPLACEMENTS: {
2578 if (Record.size() % 3 != 0) {
2579 Error("invalid DECL_REPLACEMENTS block in AST file");
2580 return true;
2581 }
2582 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2583 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2584 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2585 break;
2586 }
2587
2588 case OBJC_CATEGORIES_MAP: {
2589 if (F.LocalNumObjCCategoriesInMap != 0) {
2590 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2591 return true;
2592 }
2593
2594 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002595 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002596 break;
2597 }
2598
2599 case OBJC_CATEGORIES:
2600 F.ObjCCategories.swap(Record);
2601 break;
2602
2603 case CXX_BASE_SPECIFIER_OFFSETS: {
2604 if (F.LocalNumCXXBaseSpecifiers != 0) {
2605 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2606 return true;
2607 }
2608
2609 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002610 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002611 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2612 break;
2613 }
2614
2615 case DIAG_PRAGMA_MAPPINGS:
2616 if (F.PragmaDiagMappings.empty())
2617 F.PragmaDiagMappings.swap(Record);
2618 else
2619 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2620 Record.begin(), Record.end());
2621 break;
2622
2623 case CUDA_SPECIAL_DECL_REFS:
2624 // Later tables overwrite earlier ones.
2625 // FIXME: Modules will have trouble with this.
2626 CUDASpecialDeclRefs.clear();
2627 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2628 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2629 break;
2630
2631 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002632 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002633 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002634 if (Record[0]) {
2635 F.HeaderFileInfoTable
2636 = HeaderFileInfoLookupTable::Create(
2637 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2638 (const unsigned char *)F.HeaderFileInfoTableData,
2639 HeaderFileInfoTrait(*this, F,
2640 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002641 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002642
2643 PP.getHeaderSearchInfo().SetExternalSource(this);
2644 if (!PP.getHeaderSearchInfo().getExternalLookup())
2645 PP.getHeaderSearchInfo().SetExternalLookup(this);
2646 }
2647 break;
2648 }
2649
2650 case FP_PRAGMA_OPTIONS:
2651 // Later tables overwrite earlier ones.
2652 FPPragmaOptions.swap(Record);
2653 break;
2654
2655 case OPENCL_EXTENSIONS:
2656 // Later tables overwrite earlier ones.
2657 OpenCLExtensions.swap(Record);
2658 break;
2659
2660 case TENTATIVE_DEFINITIONS:
2661 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2662 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2663 break;
2664
2665 case KNOWN_NAMESPACES:
2666 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2667 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2668 break;
Nick Lewycky01a41142013-01-26 00:35:08 +00002669
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002670 case UNDEFINED_BUT_USED:
2671 if (UndefinedButUsed.size() % 2 != 0) {
2672 Error("Invalid existing UndefinedButUsed");
Nick Lewycky01a41142013-01-26 00:35:08 +00002673 return true;
2674 }
2675
2676 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002677 Error("invalid undefined-but-used record");
Nick Lewycky01a41142013-01-26 00:35:08 +00002678 return true;
2679 }
2680 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002681 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2682 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00002683 ReadSourceLocation(F, Record, I).getRawEncoding());
2684 }
2685 break;
2686
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002687 case IMPORTED_MODULES: {
2688 if (F.Kind != MK_Module) {
2689 // If we aren't loading a module (which has its own exports), make
2690 // all of the imported modules visible.
2691 // FIXME: Deal with macros-only imports.
2692 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2693 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2694 ImportedModules.push_back(GlobalID);
2695 }
2696 }
2697 break;
2698 }
2699
2700 case LOCAL_REDECLARATIONS: {
2701 F.RedeclarationChains.swap(Record);
2702 break;
2703 }
2704
2705 case LOCAL_REDECLARATIONS_MAP: {
2706 if (F.LocalNumRedeclarationsInMap != 0) {
2707 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2708 return true;
2709 }
2710
2711 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002712 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002713 break;
2714 }
2715
2716 case MERGED_DECLARATIONS: {
2717 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2718 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2719 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2720 for (unsigned N = Record[Idx++]; N > 0; --N)
2721 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2722 }
2723 break;
2724 }
2725
2726 case MACRO_OFFSET: {
2727 if (F.LocalNumMacros != 0) {
2728 Error("duplicate MACRO_OFFSET record in AST file");
2729 return true;
2730 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002731 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002732 F.LocalNumMacros = Record[0];
2733 unsigned LocalBaseMacroID = Record[1];
2734 F.BaseMacroID = getTotalNumMacros();
2735
2736 if (F.LocalNumMacros > 0) {
2737 // Introduce the global -> local mapping for macros within this module.
2738 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2739
2740 // Introduce the local -> global mapping for macros within this module.
2741 F.MacroRemap.insertOrReplace(
2742 std::make_pair(LocalBaseMacroID,
2743 F.BaseMacroID - LocalBaseMacroID));
2744
2745 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2746 }
2747 break;
2748 }
2749
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002750 case MACRO_TABLE: {
2751 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002752 break;
2753 }
Richard Smithac32d902013-08-07 21:41:30 +00002754
2755 case LATE_PARSED_TEMPLATE: {
2756 LateParsedTemplates.append(Record.begin(), Record.end());
2757 break;
2758 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002759 }
2760 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002761}
2762
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002763/// \brief Move the given method to the back of the global list of methods.
2764static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2765 // Find the entry for this selector in the method pool.
2766 Sema::GlobalMethodPool::iterator Known
2767 = S.MethodPool.find(Method->getSelector());
2768 if (Known == S.MethodPool.end())
2769 return;
2770
2771 // Retrieve the appropriate method list.
2772 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2773 : Known->second.second;
2774 bool Found = false;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002775 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002776 if (!Found) {
2777 if (List->Method == Method) {
2778 Found = true;
2779 } else {
2780 // Keep searching.
2781 continue;
2782 }
2783 }
2784
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002785 if (List->getNext())
2786 List->Method = List->getNext()->Method;
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002787 else
2788 List->Method = Method;
2789 }
2790}
2791
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002792void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002793 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2794 switch (Names[I].getKind()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002795 case HiddenName::Declaration: {
2796 Decl *D = Names[I].getDecl();
2797 bool wasHidden = D->Hidden;
2798 D->Hidden = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002799
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002800 if (wasHidden && SemaObj) {
2801 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2802 moveMethodToBackOfGlobalList(*SemaObj, Method);
2803 }
2804 }
2805 break;
2806 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002807 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002808 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002809 installImportedMacro(Macro.first, Macro.second, Owner);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002810 break;
2811 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002812 }
2813 }
2814}
2815
2816void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00002817 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00002818 SourceLocation ImportLoc,
2819 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002820 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002821 SmallVector<Module *, 4> Stack;
Robert Wilhelm344472e2013-08-23 16:11:15 +00002822 Stack.push_back(Mod);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002823 while (!Stack.empty()) {
Robert Wilhelm344472e2013-08-23 16:11:15 +00002824 Mod = Stack.pop_back_val();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002825
2826 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm344472e2013-08-23 16:11:15 +00002827 // This module already has this level of visibility (or greater), so
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002828 // there is nothing more to do.
2829 continue;
2830 }
2831
2832 if (!Mod->isAvailable()) {
2833 // Modules that aren't available cannot be made visible.
2834 continue;
2835 }
2836
2837 // Update the module's name visibility.
2838 Mod->NameVisibility = NameVisibility;
2839
2840 // If we've already deserialized any names from this module,
2841 // mark them as visible.
2842 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2843 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002844 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002845 HiddenNamesMap.erase(Hidden);
2846 }
2847
2848 // Push any non-explicit submodules onto the stack to be marked as
2849 // visible.
2850 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2851 SubEnd = Mod->submodule_end();
2852 Sub != SubEnd; ++Sub) {
2853 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2854 Stack.push_back(*Sub);
2855 }
2856
2857 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00002858 SmallVector<Module *, 16> Exports;
2859 Mod->getExportedModules(Exports);
2860 for (SmallVectorImpl<Module *>::iterator
2861 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2862 Module *Exported = *I;
2863 if (Visited.insert(Exported))
2864 Stack.push_back(Exported);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002865 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002866
2867 // Detect any conflicts.
2868 if (Complain) {
2869 assert(ImportLoc.isValid() && "Missing import location");
2870 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2871 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2872 Diag(ImportLoc, diag::warn_module_conflict)
2873 << Mod->getFullModuleName()
2874 << Mod->Conflicts[I].Other->getFullModuleName()
2875 << Mod->Conflicts[I].Message;
2876 // FIXME: Need note where the other module was imported.
2877 }
2878 }
2879 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002880 }
2881}
2882
Douglas Gregor1a49d972013-01-25 01:03:03 +00002883bool ASTReader::loadGlobalIndex() {
2884 if (GlobalIndex)
2885 return false;
2886
2887 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2888 !Context.getLangOpts().Modules)
2889 return true;
2890
2891 // Try to load the global index.
2892 TriedLoadingGlobalIndex = true;
2893 StringRef ModuleCachePath
2894 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2895 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor677e15f2013-03-19 00:28:20 +00002896 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002897 if (!Result.first)
2898 return true;
2899
2900 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002901 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-01-25 01:03:03 +00002902 return false;
2903}
2904
2905bool ASTReader::isGlobalIndexUnavailable() const {
2906 return Context.getLangOpts().Modules && UseGlobalIndex &&
2907 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2908}
2909
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002910ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2911 ModuleKind Type,
2912 SourceLocation ImportLoc,
2913 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00002914 llvm::SaveAndRestore<SourceLocation>
2915 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
2916
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002917 // Bump the generation number.
2918 unsigned PreviousGeneration = CurrentGeneration++;
2919
2920 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002921 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002922 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2923 /*ImportedBy=*/0, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002924 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002925 ClientLoadCapabilities)) {
2926 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002927 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002928 case OutOfDate:
2929 case VersionMismatch:
2930 case ConfigurationMismatch:
2931 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002932 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2933 Context.getLangOpts().Modules
2934 ? &PP.getHeaderSearchInfo().getModuleMap()
2935 : 0);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002936
2937 // If we find that any modules are unusable, the global index is going
2938 // to be out-of-date. Just remove it.
2939 GlobalIndex.reset();
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002940 ModuleMgr.setGlobalIndex(0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002941 return ReadResult;
2942
2943 case Success:
2944 break;
2945 }
2946
2947 // Here comes stuff that we only do once the entire chain is loaded.
2948
2949 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002950 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2951 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002952 M != MEnd; ++M) {
2953 ModuleFile &F = *M->Mod;
2954
2955 // Read the AST block.
2956 if (ReadASTBlock(F))
2957 return Failure;
2958
2959 // Once read, set the ModuleFile bit base offset and update the size in
2960 // bits of all files we've seen.
2961 F.GlobalBitOffset = TotalModulesSizeInBits;
2962 TotalModulesSizeInBits += F.SizeInBits;
2963 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2964
2965 // Preload SLocEntries.
2966 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2967 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2968 // Load it through the SourceManager and don't call ReadSLocEntry()
2969 // directly because the entry may have already been loaded in which case
2970 // calling ReadSLocEntry() directly would trigger an assertion in
2971 // SourceManager.
2972 SourceMgr.getLoadedSLocEntryByID(Index);
2973 }
2974 }
2975
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002976 // Setup the import locations and notify the module manager that we've
2977 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002978 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2979 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002980 M != MEnd; ++M) {
2981 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002982
2983 ModuleMgr.moduleFileAccepted(&F);
2984
2985 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00002986 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002987 if (!M->ImportedBy)
2988 F.ImportLoc = M->ImportLoc;
2989 else
2990 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2991 M->ImportLoc.getRawEncoding());
2992 }
2993
2994 // Mark all of the identifiers in the identifier table as being out of date,
2995 // so that various accessors know to check the loaded modules when the
2996 // identifier is used.
2997 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2998 IdEnd = PP.getIdentifierTable().end();
2999 Id != IdEnd; ++Id)
3000 Id->second->setOutOfDate(true);
3001
3002 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00003003 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3004 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003005 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3006 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00003007
3008 switch (Unresolved.Kind) {
3009 case UnresolvedModuleRef::Conflict:
3010 if (ResolvedMod) {
3011 Module::Conflict Conflict;
3012 Conflict.Other = ResolvedMod;
3013 Conflict.Message = Unresolved.String.str();
3014 Unresolved.Mod->Conflicts.push_back(Conflict);
3015 }
3016 continue;
3017
3018 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003019 if (ResolvedMod)
3020 Unresolved.Mod->Imports.push_back(ResolvedMod);
3021 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003022
Douglas Gregor906d66a2013-03-20 21:10:35 +00003023 case UnresolvedModuleRef::Export:
3024 if (ResolvedMod || Unresolved.IsWildcard)
3025 Unresolved.Mod->Exports.push_back(
3026 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3027 continue;
3028 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003029 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003030 UnresolvedModuleRefs.clear();
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00003031
3032 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3033 // Might be unnecessary as use declarations are only used to build the
3034 // module itself.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003035
3036 InitializeContext();
3037
3038 if (DeserializationListener)
3039 DeserializationListener->ReaderInitialized(this);
3040
3041 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3042 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3043 PrimaryModule.OriginalSourceFileID
3044 = FileID::get(PrimaryModule.SLocEntryBaseID
3045 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3046
3047 // If this AST file is a precompiled preamble, then set the
3048 // preamble file ID of the source manager to the file source file
3049 // from which the preamble was built.
3050 if (Type == MK_Preamble) {
3051 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3052 } else if (Type == MK_MainFile) {
3053 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3054 }
3055 }
3056
3057 // For any Objective-C class definitions we have already loaded, make sure
3058 // that we load any additional categories.
3059 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3060 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3061 ObjCClassesLoaded[I],
3062 PreviousGeneration);
3063 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00003064
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003065 return Success;
3066}
3067
3068ASTReader::ASTReadResult
3069ASTReader::ReadASTCore(StringRef FileName,
3070 ModuleKind Type,
3071 SourceLocation ImportLoc,
3072 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003073 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003074 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003075 unsigned ClientLoadCapabilities) {
3076 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003077 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003078 ModuleManager::AddModuleResult AddResult
3079 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3080 CurrentGeneration, ExpectedSize, ExpectedModTime,
3081 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003082
Douglas Gregor677e15f2013-03-19 00:28:20 +00003083 switch (AddResult) {
3084 case ModuleManager::AlreadyLoaded:
3085 return Success;
3086
3087 case ModuleManager::NewlyLoaded:
3088 // Load module file below.
3089 break;
3090
3091 case ModuleManager::Missing:
3092 // The module file was missing; if the client handle handle, that, return
3093 // it.
3094 if (ClientLoadCapabilities & ARR_Missing)
3095 return Missing;
3096
3097 // Otherwise, return an error.
3098 {
3099 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3100 + ErrorStr;
3101 Error(Msg);
3102 }
3103 return Failure;
3104
3105 case ModuleManager::OutOfDate:
3106 // We couldn't load the module file because it is out-of-date. If the
3107 // client can handle out-of-date, return it.
3108 if (ClientLoadCapabilities & ARR_OutOfDate)
3109 return OutOfDate;
3110
3111 // Otherwise, return an error.
3112 {
3113 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3114 + ErrorStr;
3115 Error(Msg);
3116 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003117 return Failure;
3118 }
3119
Douglas Gregor677e15f2013-03-19 00:28:20 +00003120 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003121
3122 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3123 // module?
3124 if (FileName != "-") {
3125 CurrentDir = llvm::sys::path::parent_path(FileName);
3126 if (CurrentDir.empty()) CurrentDir = ".";
3127 }
3128
3129 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003130 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003131 Stream.init(F.StreamFile);
3132 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3133
3134 // Sniff for the signature.
3135 if (Stream.Read(8) != 'C' ||
3136 Stream.Read(8) != 'P' ||
3137 Stream.Read(8) != 'C' ||
3138 Stream.Read(8) != 'H') {
3139 Diag(diag::err_not_a_pch_file) << FileName;
3140 return Failure;
3141 }
3142
3143 // This is used for compatibility with older PCH formats.
3144 bool HaveReadControlBlock = false;
3145
Chris Lattner99a5af02013-01-20 00:00:22 +00003146 while (1) {
3147 llvm::BitstreamEntry Entry = Stream.advance();
3148
3149 switch (Entry.Kind) {
3150 case llvm::BitstreamEntry::Error:
3151 case llvm::BitstreamEntry::EndBlock:
3152 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003153 Error("invalid record at top-level of AST file");
3154 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003155
3156 case llvm::BitstreamEntry::SubBlock:
3157 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003158 }
3159
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003160 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003161 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003162 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3163 if (Stream.ReadBlockInfoBlock()) {
3164 Error("malformed BlockInfoBlock in AST file");
3165 return Failure;
3166 }
3167 break;
3168 case CONTROL_BLOCK_ID:
3169 HaveReadControlBlock = true;
3170 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3171 case Success:
3172 break;
3173
3174 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003175 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003176 case OutOfDate: return OutOfDate;
3177 case VersionMismatch: return VersionMismatch;
3178 case ConfigurationMismatch: return ConfigurationMismatch;
3179 case HadErrors: return HadErrors;
3180 }
3181 break;
3182 case AST_BLOCK_ID:
3183 if (!HaveReadControlBlock) {
3184 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3185 Diag(diag::warn_pch_version_too_old);
3186 return VersionMismatch;
3187 }
3188
3189 // Record that we've loaded this module.
3190 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3191 return Success;
3192
3193 default:
3194 if (Stream.SkipBlock()) {
3195 Error("malformed block record in AST file");
3196 return Failure;
3197 }
3198 break;
3199 }
3200 }
3201
3202 return Success;
3203}
3204
3205void ASTReader::InitializeContext() {
3206 // If there's a listener, notify them that we "read" the translation unit.
3207 if (DeserializationListener)
3208 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3209 Context.getTranslationUnitDecl());
3210
3211 // Make sure we load the declaration update records for the translation unit,
3212 // if there are any.
3213 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3214 Context.getTranslationUnitDecl());
3215
3216 // FIXME: Find a better way to deal with collisions between these
3217 // built-in types. Right now, we just ignore the problem.
3218
3219 // Load the special types.
3220 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3221 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3222 if (!Context.CFConstantStringTypeDecl)
3223 Context.setCFConstantStringType(GetType(String));
3224 }
3225
3226 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3227 QualType FileType = GetType(File);
3228 if (FileType.isNull()) {
3229 Error("FILE type is NULL");
3230 return;
3231 }
3232
3233 if (!Context.FILEDecl) {
3234 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3235 Context.setFILEDecl(Typedef->getDecl());
3236 else {
3237 const TagType *Tag = FileType->getAs<TagType>();
3238 if (!Tag) {
3239 Error("Invalid FILE type in AST file");
3240 return;
3241 }
3242 Context.setFILEDecl(Tag->getDecl());
3243 }
3244 }
3245 }
3246
3247 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3248 QualType Jmp_bufType = GetType(Jmp_buf);
3249 if (Jmp_bufType.isNull()) {
3250 Error("jmp_buf type is NULL");
3251 return;
3252 }
3253
3254 if (!Context.jmp_bufDecl) {
3255 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3256 Context.setjmp_bufDecl(Typedef->getDecl());
3257 else {
3258 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3259 if (!Tag) {
3260 Error("Invalid jmp_buf type in AST file");
3261 return;
3262 }
3263 Context.setjmp_bufDecl(Tag->getDecl());
3264 }
3265 }
3266 }
3267
3268 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3269 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3270 if (Sigjmp_bufType.isNull()) {
3271 Error("sigjmp_buf type is NULL");
3272 return;
3273 }
3274
3275 if (!Context.sigjmp_bufDecl) {
3276 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3277 Context.setsigjmp_bufDecl(Typedef->getDecl());
3278 else {
3279 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3280 assert(Tag && "Invalid sigjmp_buf type in AST file");
3281 Context.setsigjmp_bufDecl(Tag->getDecl());
3282 }
3283 }
3284 }
3285
3286 if (unsigned ObjCIdRedef
3287 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3288 if (Context.ObjCIdRedefinitionType.isNull())
3289 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3290 }
3291
3292 if (unsigned ObjCClassRedef
3293 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3294 if (Context.ObjCClassRedefinitionType.isNull())
3295 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3296 }
3297
3298 if (unsigned ObjCSelRedef
3299 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3300 if (Context.ObjCSelRedefinitionType.isNull())
3301 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3302 }
3303
3304 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3305 QualType Ucontext_tType = GetType(Ucontext_t);
3306 if (Ucontext_tType.isNull()) {
3307 Error("ucontext_t type is NULL");
3308 return;
3309 }
3310
3311 if (!Context.ucontext_tDecl) {
3312 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3313 Context.setucontext_tDecl(Typedef->getDecl());
3314 else {
3315 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3316 assert(Tag && "Invalid ucontext_t type in AST file");
3317 Context.setucontext_tDecl(Tag->getDecl());
3318 }
3319 }
3320 }
3321 }
3322
3323 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3324
3325 // If there were any CUDA special declarations, deserialize them.
3326 if (!CUDASpecialDeclRefs.empty()) {
3327 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3328 Context.setcudaConfigureCallDecl(
3329 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3330 }
3331
3332 // Re-export any modules that were imported by a non-module AST file.
3333 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3334 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003335 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003336 /*ImportLoc=*/SourceLocation(),
3337 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003338 }
3339 ImportedModules.clear();
3340}
3341
3342void ASTReader::finalizeForWriting() {
3343 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3344 HiddenEnd = HiddenNamesMap.end();
3345 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003346 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003347 }
3348 HiddenNamesMap.clear();
3349}
3350
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003351/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3352/// cursor into the start of the given block ID, returning false on success and
3353/// true on failure.
3354static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003355 while (1) {
3356 llvm::BitstreamEntry Entry = Cursor.advance();
3357 switch (Entry.Kind) {
3358 case llvm::BitstreamEntry::Error:
3359 case llvm::BitstreamEntry::EndBlock:
3360 return true;
3361
3362 case llvm::BitstreamEntry::Record:
3363 // Ignore top-level records.
3364 Cursor.skipRecord(Entry.ID);
3365 break;
3366
3367 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003368 if (Entry.ID == BlockID) {
3369 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003370 return true;
3371 // Found it!
3372 return false;
3373 }
3374
3375 if (Cursor.SkipBlock())
3376 return true;
3377 }
3378 }
3379}
3380
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003381/// \brief Retrieve the name of the original source file name
3382/// directly from the AST file, without actually loading the AST
3383/// file.
3384std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3385 FileManager &FileMgr,
3386 DiagnosticsEngine &Diags) {
3387 // Open the AST file.
3388 std::string ErrStr;
3389 OwningPtr<llvm::MemoryBuffer> Buffer;
3390 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3391 if (!Buffer) {
3392 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3393 return std::string();
3394 }
3395
3396 // Initialize the stream
3397 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003398 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003399 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3400 (const unsigned char *)Buffer->getBufferEnd());
3401 Stream.init(StreamFile);
3402
3403 // Sniff for the signature.
3404 if (Stream.Read(8) != 'C' ||
3405 Stream.Read(8) != 'P' ||
3406 Stream.Read(8) != 'C' ||
3407 Stream.Read(8) != 'H') {
3408 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3409 return std::string();
3410 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003411
Chris Lattner88bde502013-01-19 21:39:22 +00003412 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003413 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003414 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3415 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003416 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003417
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003418 // Scan for ORIGINAL_FILE inside the control block.
3419 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003420 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003421 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003422 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3423 return std::string();
3424
3425 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3426 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3427 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003428 }
Chris Lattner88bde502013-01-19 21:39:22 +00003429
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003430 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003431 StringRef Blob;
3432 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3433 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003434 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003435}
3436
3437namespace {
3438 class SimplePCHValidator : public ASTReaderListener {
3439 const LangOptions &ExistingLangOpts;
3440 const TargetOptions &ExistingTargetOpts;
3441 const PreprocessorOptions &ExistingPPOpts;
3442 FileManager &FileMgr;
3443
3444 public:
3445 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3446 const TargetOptions &ExistingTargetOpts,
3447 const PreprocessorOptions &ExistingPPOpts,
3448 FileManager &FileMgr)
3449 : ExistingLangOpts(ExistingLangOpts),
3450 ExistingTargetOpts(ExistingTargetOpts),
3451 ExistingPPOpts(ExistingPPOpts),
3452 FileMgr(FileMgr)
3453 {
3454 }
3455
3456 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3457 bool Complain) {
3458 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3459 }
3460 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3461 bool Complain) {
3462 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3463 }
3464 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3465 bool Complain,
3466 std::string &SuggestedPredefines) {
3467 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00003468 SuggestedPredefines, ExistingLangOpts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003469 }
3470 };
3471}
3472
3473bool ASTReader::readASTFileControlBlock(StringRef Filename,
3474 FileManager &FileMgr,
3475 ASTReaderListener &Listener) {
3476 // Open the AST file.
3477 std::string ErrStr;
3478 OwningPtr<llvm::MemoryBuffer> Buffer;
3479 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3480 if (!Buffer) {
3481 return true;
3482 }
3483
3484 // Initialize the stream
3485 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003486 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003487 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3488 (const unsigned char *)Buffer->getBufferEnd());
3489 Stream.init(StreamFile);
3490
3491 // Sniff for the signature.
3492 if (Stream.Read(8) != 'C' ||
3493 Stream.Read(8) != 'P' ||
3494 Stream.Read(8) != 'C' ||
3495 Stream.Read(8) != 'H') {
3496 return true;
3497 }
3498
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003499 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003500 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003501 return true;
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003502
3503 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3504 BitstreamCursor InputFilesCursor;
3505 if (NeedsInputFiles) {
3506 InputFilesCursor = Stream;
3507 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3508 return true;
3509
3510 // Read the abbreviations
3511 while (true) {
3512 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3513 unsigned Code = InputFilesCursor.ReadCode();
3514
3515 // We expect all abbrevs to be at the start of the block.
3516 if (Code != llvm::bitc::DEFINE_ABBREV) {
3517 InputFilesCursor.JumpToBit(Offset);
3518 break;
3519 }
3520 InputFilesCursor.ReadAbbrevRecord();
3521 }
3522 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003523
3524 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003525 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003526 while (1) {
3527 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3528 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3529 return false;
3530
3531 if (Entry.Kind != llvm::BitstreamEntry::Record)
3532 return true;
3533
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003534 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003535 StringRef Blob;
3536 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003537 switch ((ControlRecordTypes)RecCode) {
3538 case METADATA: {
3539 if (Record[0] != VERSION_MAJOR)
3540 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003541
Douglas Gregorc544ba02013-03-27 16:47:18 +00003542 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003543 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00003544
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003545 break;
3546 }
3547 case LANGUAGE_OPTIONS:
3548 if (ParseLanguageOptions(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 TARGET_OPTIONS:
3553 if (ParseTargetOptions(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 DIAGNOSTIC_OPTIONS:
3558 if (ParseDiagnosticOptions(Record, false, Listener))
3559 return true;
3560 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003561
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003562 case FILE_SYSTEM_OPTIONS:
3563 if (ParseFileSystemOptions(Record, false, Listener))
3564 return true;
3565 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003566
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003567 case HEADER_SEARCH_OPTIONS:
3568 if (ParseHeaderSearchOptions(Record, false, Listener))
3569 return true;
3570 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003571
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003572 case PREPROCESSOR_OPTIONS: {
3573 std::string IgnoredSuggestedPredefines;
3574 if (ParsePreprocessorOptions(Record, false, Listener,
3575 IgnoredSuggestedPredefines))
3576 return true;
3577 break;
3578 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003579
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003580 case INPUT_FILE_OFFSETS: {
3581 if (!NeedsInputFiles)
3582 break;
3583
3584 unsigned NumInputFiles = Record[0];
3585 unsigned NumUserFiles = Record[1];
3586 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3587 for (unsigned I = 0; I != NumInputFiles; ++I) {
3588 // Go find this input file.
3589 bool isSystemFile = I >= NumUserFiles;
3590 BitstreamCursor &Cursor = InputFilesCursor;
3591 SavedStreamPosition SavedPosition(Cursor);
3592 Cursor.JumpToBit(InputFileOffs[I]);
3593
3594 unsigned Code = Cursor.ReadCode();
3595 RecordData Record;
3596 StringRef Blob;
3597 bool shouldContinue = false;
3598 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3599 case INPUT_FILE:
3600 shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3601 break;
3602 }
3603 if (!shouldContinue)
3604 break;
3605 }
3606 break;
3607 }
3608
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003609 default:
3610 // No other validation to perform.
3611 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003612 }
3613 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003614}
3615
3616
3617bool ASTReader::isAcceptableASTFile(StringRef Filename,
3618 FileManager &FileMgr,
3619 const LangOptions &LangOpts,
3620 const TargetOptions &TargetOpts,
3621 const PreprocessorOptions &PPOpts) {
3622 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3623 return !readASTFileControlBlock(Filename, FileMgr, validator);
3624}
3625
3626bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3627 // Enter the submodule block.
3628 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3629 Error("malformed submodule block record in AST file");
3630 return true;
3631 }
3632
3633 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3634 bool First = true;
3635 Module *CurrentModule = 0;
3636 RecordData Record;
3637 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003638 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3639
3640 switch (Entry.Kind) {
3641 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3642 case llvm::BitstreamEntry::Error:
3643 Error("malformed block record in AST file");
3644 return true;
3645 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003646 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003647 case llvm::BitstreamEntry::Record:
3648 // The interesting case.
3649 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003650 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003651
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003652 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003653 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003654 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003655 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003656 default: // Default behavior: ignore.
3657 break;
3658
3659 case SUBMODULE_DEFINITION: {
3660 if (First) {
3661 Error("missing submodule metadata record at beginning of block");
3662 return true;
3663 }
3664
Douglas Gregor970e4412013-03-20 03:59:18 +00003665 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003666 Error("malformed module definition");
3667 return true;
3668 }
3669
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003670 StringRef Name = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003671 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3672 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3673 bool IsFramework = Record[2];
3674 bool IsExplicit = Record[3];
3675 bool IsSystem = Record[4];
3676 bool InferSubmodules = Record[5];
3677 bool InferExplicitSubmodules = Record[6];
3678 bool InferExportWildcard = Record[7];
Douglas Gregor970e4412013-03-20 03:59:18 +00003679 bool ConfigMacrosExhaustive = Record[8];
3680
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003681 Module *ParentModule = 0;
3682 if (Parent)
3683 ParentModule = getSubmodule(Parent);
3684
3685 // Retrieve this (sub)module from the module map, creating it if
3686 // necessary.
3687 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3688 IsFramework,
3689 IsExplicit).first;
3690 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3691 if (GlobalIndex >= SubmodulesLoaded.size() ||
3692 SubmodulesLoaded[GlobalIndex]) {
3693 Error("too many submodules");
3694 return true;
3695 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003696
Douglas Gregor677e15f2013-03-19 00:28:20 +00003697 if (!ParentModule) {
3698 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3699 if (CurFile != F.File) {
3700 if (!Diags.isDiagnosticInFlight()) {
3701 Diag(diag::err_module_file_conflict)
3702 << CurrentModule->getTopLevelModuleName()
3703 << CurFile->getName()
3704 << F.File->getName();
3705 }
3706 return true;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003707 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003708 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003709
3710 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003711 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003712
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003713 CurrentModule->IsFromModuleFile = true;
3714 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3715 CurrentModule->InferSubmodules = InferSubmodules;
3716 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3717 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00003718 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003719 if (DeserializationListener)
3720 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3721
3722 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003723
Douglas Gregor906d66a2013-03-20 21:10:35 +00003724 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003725 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00003726 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00003727 CurrentModule->UnresolvedConflicts.clear();
3728 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003729 break;
3730 }
3731
3732 case SUBMODULE_UMBRELLA_HEADER: {
3733 if (First) {
3734 Error("missing submodule metadata record at beginning of block");
3735 return true;
3736 }
3737
3738 if (!CurrentModule)
3739 break;
3740
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003741 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003742 if (!CurrentModule->getUmbrellaHeader())
3743 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3744 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3745 Error("mismatched umbrella headers in submodule");
3746 return true;
3747 }
3748 }
3749 break;
3750 }
3751
3752 case SUBMODULE_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_EXCLUDED_HEADER: {
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 Kyrtzidis55ea75b2013-03-13 21:13:51 +00003776 // We lazily associate headers with their modules via the HeaderInfoTable.
3777 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3778 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003779 break;
3780 }
3781
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00003782 case SUBMODULE_PRIVATE_HEADER: {
3783 if (First) {
3784 Error("missing submodule metadata record at beginning of block");
3785 return true;
3786 }
3787
3788 if (!CurrentModule)
3789 break;
3790
3791 // We lazily associate headers with their modules via the HeaderInfoTable.
3792 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3793 // of complete filenames or remove it entirely.
3794 break;
3795 }
3796
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003797 case SUBMODULE_TOPHEADER: {
3798 if (First) {
3799 Error("missing submodule metadata record at beginning of block");
3800 return true;
3801 }
3802
3803 if (!CurrentModule)
3804 break;
3805
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00003806 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003807 break;
3808 }
3809
3810 case SUBMODULE_UMBRELLA_DIR: {
3811 if (First) {
3812 Error("missing submodule metadata record at beginning of block");
3813 return true;
3814 }
3815
3816 if (!CurrentModule)
3817 break;
3818
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003819 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003820 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003821 if (!CurrentModule->getUmbrellaDir())
3822 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3823 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3824 Error("mismatched umbrella directories in submodule");
3825 return true;
3826 }
3827 }
3828 break;
3829 }
3830
3831 case SUBMODULE_METADATA: {
3832 if (!First) {
3833 Error("submodule metadata record not at beginning of block");
3834 return true;
3835 }
3836 First = false;
3837
3838 F.BaseSubmoduleID = getTotalNumSubmodules();
3839 F.LocalNumSubmodules = Record[0];
3840 unsigned LocalBaseSubmoduleID = Record[1];
3841 if (F.LocalNumSubmodules > 0) {
3842 // Introduce the global -> local mapping for submodules within this
3843 // module.
3844 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3845
3846 // Introduce the local -> global mapping for submodules within this
3847 // module.
3848 F.SubmoduleRemap.insertOrReplace(
3849 std::make_pair(LocalBaseSubmoduleID,
3850 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3851
3852 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3853 }
3854 break;
3855 }
3856
3857 case SUBMODULE_IMPORTS: {
3858 if (First) {
3859 Error("missing submodule metadata record at beginning of block");
3860 return true;
3861 }
3862
3863 if (!CurrentModule)
3864 break;
3865
3866 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003867 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003868 Unresolved.File = &F;
3869 Unresolved.Mod = CurrentModule;
3870 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003871 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003872 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003873 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003874 }
3875 break;
3876 }
3877
3878 case SUBMODULE_EXPORTS: {
3879 if (First) {
3880 Error("missing submodule metadata record at beginning of block");
3881 return true;
3882 }
3883
3884 if (!CurrentModule)
3885 break;
3886
3887 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003888 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003889 Unresolved.File = &F;
3890 Unresolved.Mod = CurrentModule;
3891 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003892 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003893 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003894 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003895 }
3896
3897 // Once we've loaded the set of exports, there's no reason to keep
3898 // the parsed, unresolved exports around.
3899 CurrentModule->UnresolvedExports.clear();
3900 break;
3901 }
3902 case SUBMODULE_REQUIRES: {
3903 if (First) {
3904 Error("missing submodule metadata record at beginning of block");
3905 return true;
3906 }
3907
3908 if (!CurrentModule)
3909 break;
3910
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003911 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003912 Context.getTargetInfo());
3913 break;
3914 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003915
3916 case SUBMODULE_LINK_LIBRARY:
3917 if (First) {
3918 Error("missing submodule metadata record at beginning of block");
3919 return true;
3920 }
3921
3922 if (!CurrentModule)
3923 break;
3924
3925 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003926 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003927 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00003928
3929 case SUBMODULE_CONFIG_MACRO:
3930 if (First) {
3931 Error("missing submodule metadata record at beginning of block");
3932 return true;
3933 }
3934
3935 if (!CurrentModule)
3936 break;
3937
3938 CurrentModule->ConfigMacros.push_back(Blob.str());
3939 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003940
3941 case SUBMODULE_CONFLICT: {
3942 if (First) {
3943 Error("missing submodule metadata record at beginning of block");
3944 return true;
3945 }
3946
3947 if (!CurrentModule)
3948 break;
3949
3950 UnresolvedModuleRef Unresolved;
3951 Unresolved.File = &F;
3952 Unresolved.Mod = CurrentModule;
3953 Unresolved.ID = Record[0];
3954 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3955 Unresolved.IsWildcard = false;
3956 Unresolved.String = Blob;
3957 UnresolvedModuleRefs.push_back(Unresolved);
3958 break;
3959 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003960 }
3961 }
3962}
3963
3964/// \brief Parse the record that corresponds to a LangOptions data
3965/// structure.
3966///
3967/// This routine parses the language options from the AST file and then gives
3968/// them to the AST listener if one is set.
3969///
3970/// \returns true if the listener deems the file unacceptable, false otherwise.
3971bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3972 bool Complain,
3973 ASTReaderListener &Listener) {
3974 LangOptions LangOpts;
3975 unsigned Idx = 0;
3976#define LANGOPT(Name, Bits, Default, Description) \
3977 LangOpts.Name = Record[Idx++];
3978#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3979 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3980#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003981#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3982#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003983
3984 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3985 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3986 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3987
3988 unsigned Length = Record[Idx++];
3989 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3990 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003991
3992 Idx += Length;
3993
3994 // Comment options.
3995 for (unsigned N = Record[Idx++]; N; --N) {
3996 LangOpts.CommentOpts.BlockCommandNames.push_back(
3997 ReadString(Record, Idx));
3998 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00003999 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00004000
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004001 return Listener.ReadLanguageOptions(LangOpts, Complain);
4002}
4003
4004bool ASTReader::ParseTargetOptions(const RecordData &Record,
4005 bool Complain,
4006 ASTReaderListener &Listener) {
4007 unsigned Idx = 0;
4008 TargetOptions TargetOpts;
4009 TargetOpts.Triple = ReadString(Record, Idx);
4010 TargetOpts.CPU = ReadString(Record, Idx);
4011 TargetOpts.ABI = ReadString(Record, Idx);
4012 TargetOpts.CXXABI = ReadString(Record, Idx);
4013 TargetOpts.LinkerVersion = ReadString(Record, Idx);
4014 for (unsigned N = Record[Idx++]; N; --N) {
4015 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4016 }
4017 for (unsigned N = Record[Idx++]; N; --N) {
4018 TargetOpts.Features.push_back(ReadString(Record, Idx));
4019 }
4020
4021 return Listener.ReadTargetOptions(TargetOpts, Complain);
4022}
4023
4024bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4025 ASTReaderListener &Listener) {
4026 DiagnosticOptions DiagOpts;
4027 unsigned Idx = 0;
4028#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
4029#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4030 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4031#include "clang/Basic/DiagnosticOptions.def"
4032
4033 for (unsigned N = Record[Idx++]; N; --N) {
4034 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4035 }
4036
4037 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4038}
4039
4040bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4041 ASTReaderListener &Listener) {
4042 FileSystemOptions FSOpts;
4043 unsigned Idx = 0;
4044 FSOpts.WorkingDir = ReadString(Record, Idx);
4045 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4046}
4047
4048bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4049 bool Complain,
4050 ASTReaderListener &Listener) {
4051 HeaderSearchOptions HSOpts;
4052 unsigned Idx = 0;
4053 HSOpts.Sysroot = ReadString(Record, Idx);
4054
4055 // Include entries.
4056 for (unsigned N = Record[Idx++]; N; --N) {
4057 std::string Path = ReadString(Record, Idx);
4058 frontend::IncludeDirGroup Group
4059 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004060 bool IsFramework = Record[Idx++];
4061 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004062 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00004063 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004064 }
4065
4066 // System header prefixes.
4067 for (unsigned N = Record[Idx++]; N; --N) {
4068 std::string Prefix = ReadString(Record, Idx);
4069 bool IsSystemHeader = Record[Idx++];
4070 HSOpts.SystemHeaderPrefixes.push_back(
4071 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4072 }
4073
4074 HSOpts.ResourceDir = ReadString(Record, Idx);
4075 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4076 HSOpts.DisableModuleHash = Record[Idx++];
4077 HSOpts.UseBuiltinIncludes = Record[Idx++];
4078 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4079 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4080 HSOpts.UseLibcxx = Record[Idx++];
4081
4082 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4083}
4084
4085bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4086 bool Complain,
4087 ASTReaderListener &Listener,
4088 std::string &SuggestedPredefines) {
4089 PreprocessorOptions PPOpts;
4090 unsigned Idx = 0;
4091
4092 // Macro definitions/undefs
4093 for (unsigned N = Record[Idx++]; N; --N) {
4094 std::string Macro = ReadString(Record, Idx);
4095 bool IsUndef = Record[Idx++];
4096 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4097 }
4098
4099 // Includes
4100 for (unsigned N = Record[Idx++]; N; --N) {
4101 PPOpts.Includes.push_back(ReadString(Record, Idx));
4102 }
4103
4104 // Macro Includes
4105 for (unsigned N = Record[Idx++]; N; --N) {
4106 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4107 }
4108
4109 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004110 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004111 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4112 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4113 PPOpts.ObjCXXARCStandardLibrary =
4114 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4115 SuggestedPredefines.clear();
4116 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4117 SuggestedPredefines);
4118}
4119
4120std::pair<ModuleFile *, unsigned>
4121ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4122 GlobalPreprocessedEntityMapType::iterator
4123 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4124 assert(I != GlobalPreprocessedEntityMap.end() &&
4125 "Corrupted global preprocessed entity map");
4126 ModuleFile *M = I->second;
4127 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4128 return std::make_pair(M, LocalIndex);
4129}
4130
4131std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4132ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4133 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4134 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4135 Mod.NumPreprocessedEntities);
4136
4137 return std::make_pair(PreprocessingRecord::iterator(),
4138 PreprocessingRecord::iterator());
4139}
4140
4141std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4142ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4143 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4144 ModuleDeclIterator(this, &Mod,
4145 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4146}
4147
4148PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4149 PreprocessedEntityID PPID = Index+1;
4150 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4151 ModuleFile &M = *PPInfo.first;
4152 unsigned LocalIndex = PPInfo.second;
4153 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4154
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004155 if (!PP.getPreprocessingRecord()) {
4156 Error("no preprocessing record");
4157 return 0;
4158 }
4159
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004160 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4161 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4162
4163 llvm::BitstreamEntry Entry =
4164 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4165 if (Entry.Kind != llvm::BitstreamEntry::Record)
4166 return 0;
4167
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004168 // Read the record.
4169 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4170 ReadSourceLocation(M, PPOffs.End));
4171 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004172 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004173 RecordData Record;
4174 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004175 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4176 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004177 switch (RecType) {
4178 case PPD_MACRO_EXPANSION: {
4179 bool isBuiltin = Record[0];
4180 IdentifierInfo *Name = 0;
4181 MacroDefinition *Def = 0;
4182 if (isBuiltin)
4183 Name = getLocalIdentifier(M, Record[1]);
4184 else {
4185 PreprocessedEntityID
4186 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4187 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4188 }
4189
4190 MacroExpansion *ME;
4191 if (isBuiltin)
4192 ME = new (PPRec) MacroExpansion(Name, Range);
4193 else
4194 ME = new (PPRec) MacroExpansion(Def, Range);
4195
4196 return ME;
4197 }
4198
4199 case PPD_MACRO_DEFINITION: {
4200 // Decode the identifier info and then check again; if the macro is
4201 // still defined and associated with the identifier,
4202 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4203 MacroDefinition *MD
4204 = new (PPRec) MacroDefinition(II, Range);
4205
4206 if (DeserializationListener)
4207 DeserializationListener->MacroDefinitionRead(PPID, MD);
4208
4209 return MD;
4210 }
4211
4212 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004213 const char *FullFileNameStart = Blob.data() + Record[0];
4214 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004215 const FileEntry *File = 0;
4216 if (!FullFileName.empty())
4217 File = PP.getFileManager().getFile(FullFileName);
4218
4219 // FIXME: Stable encoding
4220 InclusionDirective::InclusionKind Kind
4221 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4222 InclusionDirective *ID
4223 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004224 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004225 Record[1], Record[3],
4226 File,
4227 Range);
4228 return ID;
4229 }
4230 }
4231
4232 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4233}
4234
4235/// \brief \arg SLocMapI points at a chunk of a module that contains no
4236/// preprocessed entities or the entities it contains are not the ones we are
4237/// looking for. Find the next module that contains entities and return the ID
4238/// of the first entry.
4239PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4240 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4241 ++SLocMapI;
4242 for (GlobalSLocOffsetMapType::const_iterator
4243 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4244 ModuleFile &M = *SLocMapI->second;
4245 if (M.NumPreprocessedEntities)
4246 return M.BasePreprocessedEntityID;
4247 }
4248
4249 return getTotalNumPreprocessedEntities();
4250}
4251
4252namespace {
4253
4254template <unsigned PPEntityOffset::*PPLoc>
4255struct PPEntityComp {
4256 const ASTReader &Reader;
4257 ModuleFile &M;
4258
4259 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4260
4261 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4262 SourceLocation LHS = getLoc(L);
4263 SourceLocation RHS = getLoc(R);
4264 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4265 }
4266
4267 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4268 SourceLocation LHS = getLoc(L);
4269 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4270 }
4271
4272 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4273 SourceLocation RHS = getLoc(R);
4274 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4275 }
4276
4277 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4278 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4279 }
4280};
4281
4282}
4283
4284/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4285PreprocessedEntityID
4286ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4287 if (SourceMgr.isLocalSourceLocation(BLoc))
4288 return getTotalNumPreprocessedEntities();
4289
4290 GlobalSLocOffsetMapType::const_iterator
4291 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004292 BLoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004293 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4294 "Corrupted global sloc offset map");
4295
4296 if (SLocMapI->second->NumPreprocessedEntities == 0)
4297 return findNextPreprocessedEntity(SLocMapI);
4298
4299 ModuleFile &M = *SLocMapI->second;
4300 typedef const PPEntityOffset *pp_iterator;
4301 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4302 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4303
4304 size_t Count = M.NumPreprocessedEntities;
4305 size_t Half;
4306 pp_iterator First = pp_begin;
4307 pp_iterator PPI;
4308
4309 // Do a binary search manually instead of using std::lower_bound because
4310 // The end locations of entities may be unordered (when a macro expansion
4311 // is inside another macro argument), but for this case it is not important
4312 // whether we get the first macro expansion or its containing macro.
4313 while (Count > 0) {
4314 Half = Count/2;
4315 PPI = First;
4316 std::advance(PPI, Half);
4317 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4318 BLoc)){
4319 First = PPI;
4320 ++First;
4321 Count = Count - Half - 1;
4322 } else
4323 Count = Half;
4324 }
4325
4326 if (PPI == pp_end)
4327 return findNextPreprocessedEntity(SLocMapI);
4328
4329 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4330}
4331
4332/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4333PreprocessedEntityID
4334ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4335 if (SourceMgr.isLocalSourceLocation(ELoc))
4336 return getTotalNumPreprocessedEntities();
4337
4338 GlobalSLocOffsetMapType::const_iterator
4339 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004340 ELoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004341 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4342 "Corrupted global sloc offset map");
4343
4344 if (SLocMapI->second->NumPreprocessedEntities == 0)
4345 return findNextPreprocessedEntity(SLocMapI);
4346
4347 ModuleFile &M = *SLocMapI->second;
4348 typedef const PPEntityOffset *pp_iterator;
4349 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4350 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4351 pp_iterator PPI =
4352 std::upper_bound(pp_begin, pp_end, ELoc,
4353 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4354
4355 if (PPI == pp_end)
4356 return findNextPreprocessedEntity(SLocMapI);
4357
4358 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4359}
4360
4361/// \brief Returns a pair of [Begin, End) indices of preallocated
4362/// preprocessed entities that \arg Range encompasses.
4363std::pair<unsigned, unsigned>
4364 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4365 if (Range.isInvalid())
4366 return std::make_pair(0,0);
4367 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4368
4369 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4370 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4371 return std::make_pair(BeginID, EndID);
4372}
4373
4374/// \brief Optionally returns true or false if the preallocated preprocessed
4375/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00004376Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004377 FileID FID) {
4378 if (FID.isInvalid())
4379 return false;
4380
4381 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4382 ModuleFile &M = *PPInfo.first;
4383 unsigned LocalIndex = PPInfo.second;
4384 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4385
4386 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4387 if (Loc.isInvalid())
4388 return false;
4389
4390 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4391 return true;
4392 else
4393 return false;
4394}
4395
4396namespace {
4397 /// \brief Visitor used to search for information about a header file.
4398 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004399 const FileEntry *FE;
4400
David Blaikiedc84cd52013-02-20 22:23:23 +00004401 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004402
4403 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004404 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4405 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004406
4407 static bool visit(ModuleFile &M, void *UserData) {
4408 HeaderFileInfoVisitor *This
4409 = static_cast<HeaderFileInfoVisitor *>(UserData);
4410
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004411 HeaderFileInfoLookupTable *Table
4412 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4413 if (!Table)
4414 return false;
4415
4416 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00004417 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004418 if (Pos == Table->end())
4419 return false;
4420
4421 This->HFI = *Pos;
4422 return true;
4423 }
4424
David Blaikiedc84cd52013-02-20 22:23:23 +00004425 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004426 };
4427}
4428
4429HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004430 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004431 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidisf9ba8512013-05-08 23:46:55 +00004432 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004433 return *HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004434
4435 return HeaderFileInfo();
4436}
4437
4438void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4439 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004440 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004441 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4442 ModuleFile &F = *(*I);
4443 unsigned Idx = 0;
4444 DiagStates.clear();
4445 assert(!Diag.DiagStates.empty());
4446 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4447 while (Idx < F.PragmaDiagMappings.size()) {
4448 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4449 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4450 if (DiagStateID != 0) {
4451 Diag.DiagStatePoints.push_back(
4452 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4453 FullSourceLoc(Loc, SourceMgr)));
4454 continue;
4455 }
4456
4457 assert(DiagStateID == 0);
4458 // A new DiagState was created here.
4459 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4460 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4461 DiagStates.push_back(NewState);
4462 Diag.DiagStatePoints.push_back(
4463 DiagnosticsEngine::DiagStatePoint(NewState,
4464 FullSourceLoc(Loc, SourceMgr)));
4465 while (1) {
4466 assert(Idx < F.PragmaDiagMappings.size() &&
4467 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4468 if (Idx >= F.PragmaDiagMappings.size()) {
4469 break; // Something is messed up but at least avoid infinite loop in
4470 // release build.
4471 }
4472 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4473 if (DiagID == (unsigned)-1) {
4474 break; // no more diag/map pairs for this location.
4475 }
4476 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4477 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4478 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4479 }
4480 }
4481 }
4482}
4483
4484/// \brief Get the correct cursor and offset for loading a type.
4485ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4486 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4487 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4488 ModuleFile *M = I->second;
4489 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4490}
4491
4492/// \brief Read and return the type with the given index..
4493///
4494/// The index is the type ID, shifted and minus the number of predefs. This
4495/// routine actually reads the record corresponding to the type at the given
4496/// location. It is a helper routine for GetType, which deals with reading type
4497/// IDs.
4498QualType ASTReader::readTypeRecord(unsigned Index) {
4499 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004500 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004501
4502 // Keep track of where we are in the stream, then jump back there
4503 // after reading this type.
4504 SavedStreamPosition SavedPosition(DeclsCursor);
4505
4506 ReadingKindTracker ReadingKind(Read_Type, *this);
4507
4508 // Note that we are loading a type record.
4509 Deserializing AType(this);
4510
4511 unsigned Idx = 0;
4512 DeclsCursor.JumpToBit(Loc.Offset);
4513 RecordData Record;
4514 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004515 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004516 case TYPE_EXT_QUAL: {
4517 if (Record.size() != 2) {
4518 Error("Incorrect encoding of extended qualifier type");
4519 return QualType();
4520 }
4521 QualType Base = readType(*Loc.F, Record, Idx);
4522 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4523 return Context.getQualifiedType(Base, Quals);
4524 }
4525
4526 case TYPE_COMPLEX: {
4527 if (Record.size() != 1) {
4528 Error("Incorrect encoding of complex type");
4529 return QualType();
4530 }
4531 QualType ElemType = readType(*Loc.F, Record, Idx);
4532 return Context.getComplexType(ElemType);
4533 }
4534
4535 case TYPE_POINTER: {
4536 if (Record.size() != 1) {
4537 Error("Incorrect encoding of pointer type");
4538 return QualType();
4539 }
4540 QualType PointeeType = readType(*Loc.F, Record, Idx);
4541 return Context.getPointerType(PointeeType);
4542 }
4543
Reid Kleckner12df2462013-06-24 17:51:48 +00004544 case TYPE_DECAYED: {
4545 if (Record.size() != 1) {
4546 Error("Incorrect encoding of decayed type");
4547 return QualType();
4548 }
4549 QualType OriginalType = readType(*Loc.F, Record, Idx);
4550 QualType DT = Context.getAdjustedParameterType(OriginalType);
4551 if (!isa<DecayedType>(DT))
4552 Error("Decayed type does not decay");
4553 return DT;
4554 }
4555
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004556 case TYPE_BLOCK_POINTER: {
4557 if (Record.size() != 1) {
4558 Error("Incorrect encoding of block pointer type");
4559 return QualType();
4560 }
4561 QualType PointeeType = readType(*Loc.F, Record, Idx);
4562 return Context.getBlockPointerType(PointeeType);
4563 }
4564
4565 case TYPE_LVALUE_REFERENCE: {
4566 if (Record.size() != 2) {
4567 Error("Incorrect encoding of lvalue reference type");
4568 return QualType();
4569 }
4570 QualType PointeeType = readType(*Loc.F, Record, Idx);
4571 return Context.getLValueReferenceType(PointeeType, Record[1]);
4572 }
4573
4574 case TYPE_RVALUE_REFERENCE: {
4575 if (Record.size() != 1) {
4576 Error("Incorrect encoding of rvalue reference type");
4577 return QualType();
4578 }
4579 QualType PointeeType = readType(*Loc.F, Record, Idx);
4580 return Context.getRValueReferenceType(PointeeType);
4581 }
4582
4583 case TYPE_MEMBER_POINTER: {
4584 if (Record.size() != 2) {
4585 Error("Incorrect encoding of member pointer type");
4586 return QualType();
4587 }
4588 QualType PointeeType = readType(*Loc.F, Record, Idx);
4589 QualType ClassType = readType(*Loc.F, Record, Idx);
4590 if (PointeeType.isNull() || ClassType.isNull())
4591 return QualType();
4592
4593 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4594 }
4595
4596 case TYPE_CONSTANT_ARRAY: {
4597 QualType ElementType = readType(*Loc.F, Record, Idx);
4598 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4599 unsigned IndexTypeQuals = Record[2];
4600 unsigned Idx = 3;
4601 llvm::APInt Size = ReadAPInt(Record, Idx);
4602 return Context.getConstantArrayType(ElementType, Size,
4603 ASM, IndexTypeQuals);
4604 }
4605
4606 case TYPE_INCOMPLETE_ARRAY: {
4607 QualType ElementType = readType(*Loc.F, Record, Idx);
4608 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4609 unsigned IndexTypeQuals = Record[2];
4610 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4611 }
4612
4613 case TYPE_VARIABLE_ARRAY: {
4614 QualType ElementType = readType(*Loc.F, Record, Idx);
4615 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4616 unsigned IndexTypeQuals = Record[2];
4617 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4618 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4619 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4620 ASM, IndexTypeQuals,
4621 SourceRange(LBLoc, RBLoc));
4622 }
4623
4624 case TYPE_VECTOR: {
4625 if (Record.size() != 3) {
4626 Error("incorrect encoding of vector type in AST file");
4627 return QualType();
4628 }
4629
4630 QualType ElementType = readType(*Loc.F, Record, Idx);
4631 unsigned NumElements = Record[1];
4632 unsigned VecKind = Record[2];
4633 return Context.getVectorType(ElementType, NumElements,
4634 (VectorType::VectorKind)VecKind);
4635 }
4636
4637 case TYPE_EXT_VECTOR: {
4638 if (Record.size() != 3) {
4639 Error("incorrect encoding of extended vector type in AST file");
4640 return QualType();
4641 }
4642
4643 QualType ElementType = readType(*Loc.F, Record, Idx);
4644 unsigned NumElements = Record[1];
4645 return Context.getExtVectorType(ElementType, NumElements);
4646 }
4647
4648 case TYPE_FUNCTION_NO_PROTO: {
4649 if (Record.size() != 6) {
4650 Error("incorrect encoding of no-proto function type");
4651 return QualType();
4652 }
4653 QualType ResultType = readType(*Loc.F, Record, Idx);
4654 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4655 (CallingConv)Record[4], Record[5]);
4656 return Context.getFunctionNoProtoType(ResultType, Info);
4657 }
4658
4659 case TYPE_FUNCTION_PROTO: {
4660 QualType ResultType = readType(*Loc.F, Record, Idx);
4661
4662 FunctionProtoType::ExtProtoInfo EPI;
4663 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4664 /*hasregparm*/ Record[2],
4665 /*regparm*/ Record[3],
4666 static_cast<CallingConv>(Record[4]),
4667 /*produces*/ Record[5]);
4668
4669 unsigned Idx = 6;
4670 unsigned NumParams = Record[Idx++];
4671 SmallVector<QualType, 16> ParamTypes;
4672 for (unsigned I = 0; I != NumParams; ++I)
4673 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4674
4675 EPI.Variadic = Record[Idx++];
4676 EPI.HasTrailingReturn = Record[Idx++];
4677 EPI.TypeQuals = Record[Idx++];
4678 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4679 ExceptionSpecificationType EST =
4680 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4681 EPI.ExceptionSpecType = EST;
4682 SmallVector<QualType, 2> Exceptions;
4683 if (EST == EST_Dynamic) {
4684 EPI.NumExceptions = Record[Idx++];
4685 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4686 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4687 EPI.Exceptions = Exceptions.data();
4688 } else if (EST == EST_ComputedNoexcept) {
4689 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4690 } else if (EST == EST_Uninstantiated) {
4691 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4692 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4693 } else if (EST == EST_Unevaluated) {
4694 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4695 }
Jordan Rosebea522f2013-03-08 21:51:21 +00004696 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004697 }
4698
4699 case TYPE_UNRESOLVED_USING: {
4700 unsigned Idx = 0;
4701 return Context.getTypeDeclType(
4702 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4703 }
4704
4705 case TYPE_TYPEDEF: {
4706 if (Record.size() != 2) {
4707 Error("incorrect encoding of typedef type");
4708 return QualType();
4709 }
4710 unsigned Idx = 0;
4711 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4712 QualType Canonical = readType(*Loc.F, Record, Idx);
4713 if (!Canonical.isNull())
4714 Canonical = Context.getCanonicalType(Canonical);
4715 return Context.getTypedefType(Decl, Canonical);
4716 }
4717
4718 case TYPE_TYPEOF_EXPR:
4719 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4720
4721 case TYPE_TYPEOF: {
4722 if (Record.size() != 1) {
4723 Error("incorrect encoding of typeof(type) in AST file");
4724 return QualType();
4725 }
4726 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4727 return Context.getTypeOfType(UnderlyingType);
4728 }
4729
4730 case TYPE_DECLTYPE: {
4731 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4732 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4733 }
4734
4735 case TYPE_UNARY_TRANSFORM: {
4736 QualType BaseType = readType(*Loc.F, Record, Idx);
4737 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4738 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4739 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4740 }
4741
Richard Smitha2c36462013-04-26 16:15:35 +00004742 case TYPE_AUTO: {
4743 QualType Deduced = readType(*Loc.F, Record, Idx);
4744 bool IsDecltypeAuto = Record[Idx++];
Richard Smithdc7a4f52013-04-30 13:56:41 +00004745 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek152b4e42013-08-22 12:12:24 +00004746 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smitha2c36462013-04-26 16:15:35 +00004747 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004748
4749 case TYPE_RECORD: {
4750 if (Record.size() != 2) {
4751 Error("incorrect encoding of record type");
4752 return QualType();
4753 }
4754 unsigned Idx = 0;
4755 bool IsDependent = Record[Idx++];
4756 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4757 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4758 QualType T = Context.getRecordType(RD);
4759 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4760 return T;
4761 }
4762
4763 case TYPE_ENUM: {
4764 if (Record.size() != 2) {
4765 Error("incorrect encoding of enum type");
4766 return QualType();
4767 }
4768 unsigned Idx = 0;
4769 bool IsDependent = Record[Idx++];
4770 QualType T
4771 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4772 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4773 return T;
4774 }
4775
4776 case TYPE_ATTRIBUTED: {
4777 if (Record.size() != 3) {
4778 Error("incorrect encoding of attributed type");
4779 return QualType();
4780 }
4781 QualType modifiedType = readType(*Loc.F, Record, Idx);
4782 QualType equivalentType = readType(*Loc.F, Record, Idx);
4783 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4784 return Context.getAttributedType(kind, modifiedType, equivalentType);
4785 }
4786
4787 case TYPE_PAREN: {
4788 if (Record.size() != 1) {
4789 Error("incorrect encoding of paren type");
4790 return QualType();
4791 }
4792 QualType InnerType = readType(*Loc.F, Record, Idx);
4793 return Context.getParenType(InnerType);
4794 }
4795
4796 case TYPE_PACK_EXPANSION: {
4797 if (Record.size() != 2) {
4798 Error("incorrect encoding of pack expansion type");
4799 return QualType();
4800 }
4801 QualType Pattern = readType(*Loc.F, Record, Idx);
4802 if (Pattern.isNull())
4803 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00004804 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004805 if (Record[1])
4806 NumExpansions = Record[1] - 1;
4807 return Context.getPackExpansionType(Pattern, NumExpansions);
4808 }
4809
4810 case TYPE_ELABORATED: {
4811 unsigned Idx = 0;
4812 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4813 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4814 QualType NamedType = readType(*Loc.F, Record, Idx);
4815 return Context.getElaboratedType(Keyword, NNS, NamedType);
4816 }
4817
4818 case TYPE_OBJC_INTERFACE: {
4819 unsigned Idx = 0;
4820 ObjCInterfaceDecl *ItfD
4821 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4822 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4823 }
4824
4825 case TYPE_OBJC_OBJECT: {
4826 unsigned Idx = 0;
4827 QualType Base = readType(*Loc.F, Record, Idx);
4828 unsigned NumProtos = Record[Idx++];
4829 SmallVector<ObjCProtocolDecl*, 4> Protos;
4830 for (unsigned I = 0; I != NumProtos; ++I)
4831 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4832 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4833 }
4834
4835 case TYPE_OBJC_OBJECT_POINTER: {
4836 unsigned Idx = 0;
4837 QualType Pointee = readType(*Loc.F, Record, Idx);
4838 return Context.getObjCObjectPointerType(Pointee);
4839 }
4840
4841 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4842 unsigned Idx = 0;
4843 QualType Parm = readType(*Loc.F, Record, Idx);
4844 QualType Replacement = readType(*Loc.F, Record, Idx);
4845 return
4846 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4847 Replacement);
4848 }
4849
4850 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4851 unsigned Idx = 0;
4852 QualType Parm = readType(*Loc.F, Record, Idx);
4853 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4854 return Context.getSubstTemplateTypeParmPackType(
4855 cast<TemplateTypeParmType>(Parm),
4856 ArgPack);
4857 }
4858
4859 case TYPE_INJECTED_CLASS_NAME: {
4860 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4861 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4862 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4863 // for AST reading, too much interdependencies.
4864 return
4865 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4866 }
4867
4868 case TYPE_TEMPLATE_TYPE_PARM: {
4869 unsigned Idx = 0;
4870 unsigned Depth = Record[Idx++];
4871 unsigned Index = Record[Idx++];
4872 bool Pack = Record[Idx++];
4873 TemplateTypeParmDecl *D
4874 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4875 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4876 }
4877
4878 case TYPE_DEPENDENT_NAME: {
4879 unsigned Idx = 0;
4880 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4881 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4882 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4883 QualType Canon = readType(*Loc.F, Record, Idx);
4884 if (!Canon.isNull())
4885 Canon = Context.getCanonicalType(Canon);
4886 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4887 }
4888
4889 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4890 unsigned Idx = 0;
4891 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4892 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4893 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4894 unsigned NumArgs = Record[Idx++];
4895 SmallVector<TemplateArgument, 8> Args;
4896 Args.reserve(NumArgs);
4897 while (NumArgs--)
4898 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4899 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4900 Args.size(), Args.data());
4901 }
4902
4903 case TYPE_DEPENDENT_SIZED_ARRAY: {
4904 unsigned Idx = 0;
4905
4906 // ArrayType
4907 QualType ElementType = readType(*Loc.F, Record, Idx);
4908 ArrayType::ArraySizeModifier ASM
4909 = (ArrayType::ArraySizeModifier)Record[Idx++];
4910 unsigned IndexTypeQuals = Record[Idx++];
4911
4912 // DependentSizedArrayType
4913 Expr *NumElts = ReadExpr(*Loc.F);
4914 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4915
4916 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4917 IndexTypeQuals, Brackets);
4918 }
4919
4920 case TYPE_TEMPLATE_SPECIALIZATION: {
4921 unsigned Idx = 0;
4922 bool IsDependent = Record[Idx++];
4923 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4924 SmallVector<TemplateArgument, 8> Args;
4925 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4926 QualType Underlying = readType(*Loc.F, Record, Idx);
4927 QualType T;
4928 if (Underlying.isNull())
4929 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4930 Args.size());
4931 else
4932 T = Context.getTemplateSpecializationType(Name, Args.data(),
4933 Args.size(), Underlying);
4934 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4935 return T;
4936 }
4937
4938 case TYPE_ATOMIC: {
4939 if (Record.size() != 1) {
4940 Error("Incorrect encoding of atomic type");
4941 return QualType();
4942 }
4943 QualType ValueType = readType(*Loc.F, Record, Idx);
4944 return Context.getAtomicType(ValueType);
4945 }
4946 }
4947 llvm_unreachable("Invalid TypeCode!");
4948}
4949
4950class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4951 ASTReader &Reader;
4952 ModuleFile &F;
4953 const ASTReader::RecordData &Record;
4954 unsigned &Idx;
4955
4956 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4957 unsigned &I) {
4958 return Reader.ReadSourceLocation(F, R, I);
4959 }
4960
4961 template<typename T>
4962 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4963 return Reader.ReadDeclAs<T>(F, Record, Idx);
4964 }
4965
4966public:
4967 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4968 const ASTReader::RecordData &Record, unsigned &Idx)
4969 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4970 { }
4971
4972 // We want compile-time assurance that we've enumerated all of
4973 // these, so unfortunately we have to declare them first, then
4974 // define them out-of-line.
4975#define ABSTRACT_TYPELOC(CLASS, PARENT)
4976#define TYPELOC(CLASS, PARENT) \
4977 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4978#include "clang/AST/TypeLocNodes.def"
4979
4980 void VisitFunctionTypeLoc(FunctionTypeLoc);
4981 void VisitArrayTypeLoc(ArrayTypeLoc);
4982};
4983
4984void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4985 // nothing to do
4986}
4987void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4988 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4989 if (TL.needsExtraLocalData()) {
4990 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4991 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4992 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4993 TL.setModeAttr(Record[Idx++]);
4994 }
4995}
4996void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4997 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4998}
4999void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5000 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5001}
Reid Kleckner12df2462013-06-24 17:51:48 +00005002void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5003 // nothing to do
5004}
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005005void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5006 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5007}
5008void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5009 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5010}
5011void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5012 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5013}
5014void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5015 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5016 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5017}
5018void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5019 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5020 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5021 if (Record[Idx++])
5022 TL.setSizeExpr(Reader.ReadExpr(F));
5023 else
5024 TL.setSizeExpr(0);
5025}
5026void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5027 VisitArrayTypeLoc(TL);
5028}
5029void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5030 VisitArrayTypeLoc(TL);
5031}
5032void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5033 VisitArrayTypeLoc(TL);
5034}
5035void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5036 DependentSizedArrayTypeLoc TL) {
5037 VisitArrayTypeLoc(TL);
5038}
5039void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5040 DependentSizedExtVectorTypeLoc TL) {
5041 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5042}
5043void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5044 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5045}
5046void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5047 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5048}
5049void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5050 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5051 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5052 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5053 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5054 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
5055 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5056 }
5057}
5058void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5059 VisitFunctionTypeLoc(TL);
5060}
5061void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5062 VisitFunctionTypeLoc(TL);
5063}
5064void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5065 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5066}
5067void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5068 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5069}
5070void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5071 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5072 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5073 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5074}
5075void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5076 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5077 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5078 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5079 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5080}
5081void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5082 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5083}
5084void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5085 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5086 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5087 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5088 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5089}
5090void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5091 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5092}
5093void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5094 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5095}
5096void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5097 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5098}
5099void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5100 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5101 if (TL.hasAttrOperand()) {
5102 SourceRange range;
5103 range.setBegin(ReadSourceLocation(Record, Idx));
5104 range.setEnd(ReadSourceLocation(Record, Idx));
5105 TL.setAttrOperandParensRange(range);
5106 }
5107 if (TL.hasAttrExprOperand()) {
5108 if (Record[Idx++])
5109 TL.setAttrExprOperand(Reader.ReadExpr(F));
5110 else
5111 TL.setAttrExprOperand(0);
5112 } else if (TL.hasAttrEnumOperand())
5113 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5114}
5115void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5116 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5117}
5118void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5119 SubstTemplateTypeParmTypeLoc TL) {
5120 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5121}
5122void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5123 SubstTemplateTypeParmPackTypeLoc TL) {
5124 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5125}
5126void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5127 TemplateSpecializationTypeLoc TL) {
5128 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5129 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5130 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5131 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5132 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5133 TL.setArgLocInfo(i,
5134 Reader.GetTemplateArgumentLocInfo(F,
5135 TL.getTypePtr()->getArg(i).getKind(),
5136 Record, Idx));
5137}
5138void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5139 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5140 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5141}
5142void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5143 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5144 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5145}
5146void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5147 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5148}
5149void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5150 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5151 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5152 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5153}
5154void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5155 DependentTemplateSpecializationTypeLoc TL) {
5156 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5157 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5158 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5159 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5160 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5161 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5162 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5163 TL.setArgLocInfo(I,
5164 Reader.GetTemplateArgumentLocInfo(F,
5165 TL.getTypePtr()->getArg(I).getKind(),
5166 Record, Idx));
5167}
5168void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5169 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5170}
5171void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5172 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5173}
5174void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5175 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5176 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5177 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5178 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5179 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5180}
5181void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5182 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5183}
5184void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5185 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5186 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5187 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5188}
5189
5190TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5191 const RecordData &Record,
5192 unsigned &Idx) {
5193 QualType InfoTy = readType(F, Record, Idx);
5194 if (InfoTy.isNull())
5195 return 0;
5196
5197 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5198 TypeLocReader TLR(*this, F, Record, Idx);
5199 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5200 TLR.Visit(TL);
5201 return TInfo;
5202}
5203
5204QualType ASTReader::GetType(TypeID ID) {
5205 unsigned FastQuals = ID & Qualifiers::FastMask;
5206 unsigned Index = ID >> Qualifiers::FastWidth;
5207
5208 if (Index < NUM_PREDEF_TYPE_IDS) {
5209 QualType T;
5210 switch ((PredefinedTypeIDs)Index) {
5211 case PREDEF_TYPE_NULL_ID: return QualType();
5212 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5213 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5214
5215 case PREDEF_TYPE_CHAR_U_ID:
5216 case PREDEF_TYPE_CHAR_S_ID:
5217 // FIXME: Check that the signedness of CharTy is correct!
5218 T = Context.CharTy;
5219 break;
5220
5221 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5222 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5223 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5224 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5225 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5226 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5227 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5228 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5229 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5230 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5231 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5232 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5233 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5234 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5235 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5236 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5237 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5238 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5239 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5240 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5241 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5242 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5243 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5244 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5245 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5246 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5247 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5248 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005249 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5250 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5251 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5252 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5253 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5254 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005255 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005256 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005257 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5258
5259 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5260 T = Context.getAutoRRefDeductType();
5261 break;
5262
5263 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5264 T = Context.ARCUnbridgedCastTy;
5265 break;
5266
5267 case PREDEF_TYPE_VA_LIST_TAG:
5268 T = Context.getVaListTagType();
5269 break;
5270
5271 case PREDEF_TYPE_BUILTIN_FN:
5272 T = Context.BuiltinFnTy;
5273 break;
5274 }
5275
5276 assert(!T.isNull() && "Unknown predefined type");
5277 return T.withFastQualifiers(FastQuals);
5278 }
5279
5280 Index -= NUM_PREDEF_TYPE_IDS;
5281 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5282 if (TypesLoaded[Index].isNull()) {
5283 TypesLoaded[Index] = readTypeRecord(Index);
5284 if (TypesLoaded[Index].isNull())
5285 return QualType();
5286
5287 TypesLoaded[Index]->setFromAST();
5288 if (DeserializationListener)
5289 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5290 TypesLoaded[Index]);
5291 }
5292
5293 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5294}
5295
5296QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5297 return GetType(getGlobalTypeID(F, LocalID));
5298}
5299
5300serialization::TypeID
5301ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5302 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5303 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5304
5305 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5306 return LocalID;
5307
5308 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5309 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5310 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5311
5312 unsigned GlobalIndex = LocalIndex + I->second;
5313 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5314}
5315
5316TemplateArgumentLocInfo
5317ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5318 TemplateArgument::ArgKind Kind,
5319 const RecordData &Record,
5320 unsigned &Index) {
5321 switch (Kind) {
5322 case TemplateArgument::Expression:
5323 return ReadExpr(F);
5324 case TemplateArgument::Type:
5325 return GetTypeSourceInfo(F, Record, Index);
5326 case TemplateArgument::Template: {
5327 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5328 Index);
5329 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5330 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5331 SourceLocation());
5332 }
5333 case TemplateArgument::TemplateExpansion: {
5334 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5335 Index);
5336 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5337 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5338 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5339 EllipsisLoc);
5340 }
5341 case TemplateArgument::Null:
5342 case TemplateArgument::Integral:
5343 case TemplateArgument::Declaration:
5344 case TemplateArgument::NullPtr:
5345 case TemplateArgument::Pack:
5346 // FIXME: Is this right?
5347 return TemplateArgumentLocInfo();
5348 }
5349 llvm_unreachable("unexpected template argument loc");
5350}
5351
5352TemplateArgumentLoc
5353ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5354 const RecordData &Record, unsigned &Index) {
5355 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5356
5357 if (Arg.getKind() == TemplateArgument::Expression) {
5358 if (Record[Index++]) // bool InfoHasSameExpr.
5359 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5360 }
5361 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5362 Record, Index));
5363}
5364
Enea Zaffanellac1cef082013-08-10 07:24:53 +00005365const ASTTemplateArgumentListInfo*
5366ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5367 const RecordData &Record,
5368 unsigned &Index) {
5369 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5370 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5371 unsigned NumArgsAsWritten = Record[Index++];
5372 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5373 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5374 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5375 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5376}
5377
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005378Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5379 return GetDecl(ID);
5380}
5381
5382uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5383 unsigned &Idx){
5384 if (Idx >= Record.size())
5385 return 0;
5386
5387 unsigned LocalID = Record[Idx++];
5388 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5389}
5390
5391CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5392 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005393 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005394 SavedStreamPosition SavedPosition(Cursor);
5395 Cursor.JumpToBit(Loc.Offset);
5396 ReadingKindTracker ReadingKind(Read_Decl, *this);
5397 RecordData Record;
5398 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005399 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005400 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5401 Error("Malformed AST file: missing C++ base specifiers");
5402 return 0;
5403 }
5404
5405 unsigned Idx = 0;
5406 unsigned NumBases = Record[Idx++];
5407 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5408 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5409 for (unsigned I = 0; I != NumBases; ++I)
5410 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5411 return Bases;
5412}
5413
5414serialization::DeclID
5415ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5416 if (LocalID < NUM_PREDEF_DECL_IDS)
5417 return LocalID;
5418
5419 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5420 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5421 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5422
5423 return LocalID + I->second;
5424}
5425
5426bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5427 ModuleFile &M) const {
5428 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5429 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5430 return &M == I->second;
5431}
5432
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005433ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005434 if (!D->isFromASTFile())
5435 return 0;
5436 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5437 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5438 return I->second;
5439}
5440
5441SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5442 if (ID < NUM_PREDEF_DECL_IDS)
5443 return SourceLocation();
5444
5445 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5446
5447 if (Index > DeclsLoaded.size()) {
5448 Error("declaration ID out-of-range for AST file");
5449 return SourceLocation();
5450 }
5451
5452 if (Decl *D = DeclsLoaded[Index])
5453 return D->getLocation();
5454
5455 unsigned RawLocation = 0;
5456 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5457 return ReadSourceLocation(*Rec.F, RawLocation);
5458}
5459
5460Decl *ASTReader::GetDecl(DeclID ID) {
5461 if (ID < NUM_PREDEF_DECL_IDS) {
5462 switch ((PredefinedDeclIDs)ID) {
5463 case PREDEF_DECL_NULL_ID:
5464 return 0;
5465
5466 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5467 return Context.getTranslationUnitDecl();
5468
5469 case PREDEF_DECL_OBJC_ID_ID:
5470 return Context.getObjCIdDecl();
5471
5472 case PREDEF_DECL_OBJC_SEL_ID:
5473 return Context.getObjCSelDecl();
5474
5475 case PREDEF_DECL_OBJC_CLASS_ID:
5476 return Context.getObjCClassDecl();
5477
5478 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5479 return Context.getObjCProtocolDecl();
5480
5481 case PREDEF_DECL_INT_128_ID:
5482 return Context.getInt128Decl();
5483
5484 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5485 return Context.getUInt128Decl();
5486
5487 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5488 return Context.getObjCInstanceTypeDecl();
5489
5490 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5491 return Context.getBuiltinVaListDecl();
5492 }
5493 }
5494
5495 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5496
5497 if (Index >= DeclsLoaded.size()) {
5498 assert(0 && "declaration ID out-of-range for AST file");
5499 Error("declaration ID out-of-range for AST file");
5500 return 0;
5501 }
5502
5503 if (!DeclsLoaded[Index]) {
5504 ReadDeclRecord(ID);
5505 if (DeserializationListener)
5506 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5507 }
5508
5509 return DeclsLoaded[Index];
5510}
5511
5512DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5513 DeclID GlobalID) {
5514 if (GlobalID < NUM_PREDEF_DECL_IDS)
5515 return GlobalID;
5516
5517 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5518 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5519 ModuleFile *Owner = I->second;
5520
5521 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5522 = M.GlobalToLocalDeclIDs.find(Owner);
5523 if (Pos == M.GlobalToLocalDeclIDs.end())
5524 return 0;
5525
5526 return GlobalID - Owner->BaseDeclID + Pos->second;
5527}
5528
5529serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5530 const RecordData &Record,
5531 unsigned &Idx) {
5532 if (Idx >= Record.size()) {
5533 Error("Corrupted AST file");
5534 return 0;
5535 }
5536
5537 return getGlobalDeclID(F, Record[Idx++]);
5538}
5539
5540/// \brief Resolve the offset of a statement into a statement.
5541///
5542/// This operation will read a new statement from the external
5543/// source each time it is called, and is meant to be used via a
5544/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5545Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5546 // Switch case IDs are per Decl.
5547 ClearSwitchCaseIDs();
5548
5549 // Offset here is a global offset across the entire chain.
5550 RecordLocation Loc = getLocalBitOffset(Offset);
5551 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5552 return ReadStmtFromStream(*Loc.F);
5553}
5554
5555namespace {
5556 class FindExternalLexicalDeclsVisitor {
5557 ASTReader &Reader;
5558 const DeclContext *DC;
5559 bool (*isKindWeWant)(Decl::Kind);
5560
5561 SmallVectorImpl<Decl*> &Decls;
5562 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5563
5564 public:
5565 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5566 bool (*isKindWeWant)(Decl::Kind),
5567 SmallVectorImpl<Decl*> &Decls)
5568 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5569 {
5570 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5571 PredefsVisited[I] = false;
5572 }
5573
5574 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5575 if (Preorder)
5576 return false;
5577
5578 FindExternalLexicalDeclsVisitor *This
5579 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5580
5581 ModuleFile::DeclContextInfosMap::iterator Info
5582 = M.DeclContextInfos.find(This->DC);
5583 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5584 return false;
5585
5586 // Load all of the declaration IDs
5587 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5588 *IDE = ID + Info->second.NumLexicalDecls;
5589 ID != IDE; ++ID) {
5590 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5591 continue;
5592
5593 // Don't add predefined declarations to the lexical context more
5594 // than once.
5595 if (ID->second < NUM_PREDEF_DECL_IDS) {
5596 if (This->PredefsVisited[ID->second])
5597 continue;
5598
5599 This->PredefsVisited[ID->second] = true;
5600 }
5601
5602 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5603 if (!This->DC->isDeclInLexicalTraversal(D))
5604 This->Decls.push_back(D);
5605 }
5606 }
5607
5608 return false;
5609 }
5610 };
5611}
5612
5613ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5614 bool (*isKindWeWant)(Decl::Kind),
5615 SmallVectorImpl<Decl*> &Decls) {
5616 // There might be lexical decls in multiple modules, for the TU at
5617 // least. Walk all of the modules in the order they were loaded.
5618 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5619 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5620 ++NumLexicalDeclContextsRead;
5621 return ELR_Success;
5622}
5623
5624namespace {
5625
5626class DeclIDComp {
5627 ASTReader &Reader;
5628 ModuleFile &Mod;
5629
5630public:
5631 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5632
5633 bool operator()(LocalDeclID L, LocalDeclID R) const {
5634 SourceLocation LHS = getLocation(L);
5635 SourceLocation RHS = getLocation(R);
5636 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5637 }
5638
5639 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5640 SourceLocation RHS = getLocation(R);
5641 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5642 }
5643
5644 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5645 SourceLocation LHS = getLocation(L);
5646 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5647 }
5648
5649 SourceLocation getLocation(LocalDeclID ID) const {
5650 return Reader.getSourceManager().getFileLoc(
5651 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5652 }
5653};
5654
5655}
5656
5657void ASTReader::FindFileRegionDecls(FileID File,
5658 unsigned Offset, unsigned Length,
5659 SmallVectorImpl<Decl *> &Decls) {
5660 SourceManager &SM = getSourceManager();
5661
5662 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5663 if (I == FileDeclIDs.end())
5664 return;
5665
5666 FileDeclsInfo &DInfo = I->second;
5667 if (DInfo.Decls.empty())
5668 return;
5669
5670 SourceLocation
5671 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5672 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5673
5674 DeclIDComp DIDComp(*this, *DInfo.Mod);
5675 ArrayRef<serialization::LocalDeclID>::iterator
5676 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5677 BeginLoc, DIDComp);
5678 if (BeginIt != DInfo.Decls.begin())
5679 --BeginIt;
5680
5681 // If we are pointing at a top-level decl inside an objc container, we need
5682 // to backtrack until we find it otherwise we will fail to report that the
5683 // region overlaps with an objc container.
5684 while (BeginIt != DInfo.Decls.begin() &&
5685 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5686 ->isTopLevelDeclInObjCContainer())
5687 --BeginIt;
5688
5689 ArrayRef<serialization::LocalDeclID>::iterator
5690 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5691 EndLoc, DIDComp);
5692 if (EndIt != DInfo.Decls.end())
5693 ++EndIt;
5694
5695 for (ArrayRef<serialization::LocalDeclID>::iterator
5696 DIt = BeginIt; DIt != EndIt; ++DIt)
5697 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5698}
5699
5700namespace {
5701 /// \brief ModuleFile visitor used to perform name lookup into a
5702 /// declaration context.
5703 class DeclContextNameLookupVisitor {
5704 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005705 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005706 DeclarationName Name;
5707 SmallVectorImpl<NamedDecl *> &Decls;
5708
5709 public:
5710 DeclContextNameLookupVisitor(ASTReader &Reader,
5711 SmallVectorImpl<const DeclContext *> &Contexts,
5712 DeclarationName Name,
5713 SmallVectorImpl<NamedDecl *> &Decls)
5714 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5715
5716 static bool visit(ModuleFile &M, void *UserData) {
5717 DeclContextNameLookupVisitor *This
5718 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5719
5720 // Check whether we have any visible declaration information for
5721 // this context in this module.
5722 ModuleFile::DeclContextInfosMap::iterator Info;
5723 bool FoundInfo = false;
5724 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5725 Info = M.DeclContextInfos.find(This->Contexts[I]);
5726 if (Info != M.DeclContextInfos.end() &&
5727 Info->second.NameLookupTableData) {
5728 FoundInfo = true;
5729 break;
5730 }
5731 }
5732
5733 if (!FoundInfo)
5734 return false;
5735
5736 // Look for this name within this module.
5737 ASTDeclContextNameLookupTable *LookupTable =
5738 Info->second.NameLookupTableData;
5739 ASTDeclContextNameLookupTable::iterator Pos
5740 = LookupTable->find(This->Name);
5741 if (Pos == LookupTable->end())
5742 return false;
5743
5744 bool FoundAnything = false;
5745 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5746 for (; Data.first != Data.second; ++Data.first) {
5747 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5748 if (!ND)
5749 continue;
5750
5751 if (ND->getDeclName() != This->Name) {
5752 // A name might be null because the decl's redeclarable part is
5753 // currently read before reading its name. The lookup is triggered by
5754 // building that decl (likely indirectly), and so it is later in the
5755 // sense of "already existing" and can be ignored here.
5756 continue;
5757 }
5758
5759 // Record this declaration.
5760 FoundAnything = true;
5761 This->Decls.push_back(ND);
5762 }
5763
5764 return FoundAnything;
5765 }
5766 };
5767}
5768
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005769/// \brief Retrieve the "definitive" module file for the definition of the
5770/// given declaration context, if there is one.
5771///
5772/// The "definitive" module file is the only place where we need to look to
5773/// find information about the declarations within the given declaration
5774/// context. For example, C++ and Objective-C classes, C structs/unions, and
5775/// Objective-C protocols, categories, and extensions are all defined in a
5776/// single place in the source code, so they have definitive module files
5777/// associated with them. C++ namespaces, on the other hand, can have
5778/// definitions in multiple different module files.
5779///
5780/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5781/// NDEBUG checking.
5782static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5783 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005784 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5785 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005786
5787 return 0;
5788}
5789
Richard Smith3646c682013-02-07 03:30:24 +00005790bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005791ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5792 DeclarationName Name) {
5793 assert(DC->hasExternalVisibleStorage() &&
5794 "DeclContext has no visible decls in storage");
5795 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00005796 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005797
5798 SmallVector<NamedDecl *, 64> Decls;
5799
5800 // Compute the declaration contexts we need to look into. Multiple such
5801 // declaration contexts occur when two declaration contexts from disjoint
5802 // modules get merged, e.g., when two namespaces with the same name are
5803 // independently defined in separate modules.
5804 SmallVector<const DeclContext *, 2> Contexts;
5805 Contexts.push_back(DC);
5806
5807 if (DC->isNamespace()) {
5808 MergedDeclsMap::iterator Merged
5809 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5810 if (Merged != MergedDecls.end()) {
5811 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5812 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5813 }
5814 }
5815
5816 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005817
5818 // If we can definitively determine which module file to look into,
5819 // only look there. Otherwise, look in all module files.
5820 ModuleFile *Definitive;
5821 if (Contexts.size() == 1 &&
5822 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5823 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5824 } else {
5825 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5826 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005827 ++NumVisibleDeclContextsRead;
5828 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00005829 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005830}
5831
5832namespace {
5833 /// \brief ModuleFile visitor used to retrieve all visible names in a
5834 /// declaration context.
5835 class DeclContextAllNamesVisitor {
5836 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005837 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper8ae63872013-07-05 04:43:31 +00005838 DeclsMap &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005839 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005840
5841 public:
5842 DeclContextAllNamesVisitor(ASTReader &Reader,
5843 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper8ae63872013-07-05 04:43:31 +00005844 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005845 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005846
5847 static bool visit(ModuleFile &M, void *UserData) {
5848 DeclContextAllNamesVisitor *This
5849 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5850
5851 // Check whether we have any visible declaration information for
5852 // this context in this module.
5853 ModuleFile::DeclContextInfosMap::iterator Info;
5854 bool FoundInfo = false;
5855 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5856 Info = M.DeclContextInfos.find(This->Contexts[I]);
5857 if (Info != M.DeclContextInfos.end() &&
5858 Info->second.NameLookupTableData) {
5859 FoundInfo = true;
5860 break;
5861 }
5862 }
5863
5864 if (!FoundInfo)
5865 return false;
5866
5867 ASTDeclContextNameLookupTable *LookupTable =
5868 Info->second.NameLookupTableData;
5869 bool FoundAnything = false;
5870 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005871 I = LookupTable->data_begin(), E = LookupTable->data_end();
5872 I != E;
5873 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005874 ASTDeclContextNameLookupTrait::data_type Data = *I;
5875 for (; Data.first != Data.second; ++Data.first) {
5876 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5877 *Data.first);
5878 if (!ND)
5879 continue;
5880
5881 // Record this declaration.
5882 FoundAnything = true;
5883 This->Decls[ND->getDeclName()].push_back(ND);
5884 }
5885 }
5886
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005887 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005888 }
5889 };
5890}
5891
5892void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5893 if (!DC->hasExternalVisibleStorage())
5894 return;
Craig Topperee0a4792013-07-05 04:33:53 +00005895 DeclsMap Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005896
5897 // Compute the declaration contexts we need to look into. Multiple such
5898 // declaration contexts occur when two declaration contexts from disjoint
5899 // modules get merged, e.g., when two namespaces with the same name are
5900 // independently defined in separate modules.
5901 SmallVector<const DeclContext *, 2> Contexts;
5902 Contexts.push_back(DC);
5903
5904 if (DC->isNamespace()) {
5905 MergedDeclsMap::iterator Merged
5906 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5907 if (Merged != MergedDecls.end()) {
5908 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5909 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5910 }
5911 }
5912
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005913 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5914 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005915 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5916 ++NumVisibleDeclContextsRead;
5917
Craig Topperee0a4792013-07-05 04:33:53 +00005918 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005919 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5920 }
5921 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5922}
5923
5924/// \brief Under non-PCH compilation the consumer receives the objc methods
5925/// before receiving the implementation, and codegen depends on this.
5926/// We simulate this by deserializing and passing to consumer the methods of the
5927/// implementation before passing the deserialized implementation decl.
5928static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5929 ASTConsumer *Consumer) {
5930 assert(ImplD && Consumer);
5931
5932 for (ObjCImplDecl::method_iterator
5933 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5934 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5935
5936 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5937}
5938
5939void ASTReader::PassInterestingDeclsToConsumer() {
5940 assert(Consumer);
5941 while (!InterestingDecls.empty()) {
5942 Decl *D = InterestingDecls.front();
5943 InterestingDecls.pop_front();
5944
5945 PassInterestingDeclToConsumer(D);
5946 }
5947}
5948
5949void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5950 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5951 PassObjCImplDeclToConsumer(ImplD, Consumer);
5952 else
5953 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5954}
5955
5956void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5957 this->Consumer = Consumer;
5958
5959 if (!Consumer)
5960 return;
5961
5962 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5963 // Force deserialization of this decl, which will cause it to be queued for
5964 // passing to the consumer.
5965 GetDecl(ExternalDefinitions[I]);
5966 }
5967 ExternalDefinitions.clear();
5968
5969 PassInterestingDeclsToConsumer();
5970}
5971
5972void ASTReader::PrintStats() {
5973 std::fprintf(stderr, "*** AST File Statistics:\n");
5974
5975 unsigned NumTypesLoaded
5976 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5977 QualType());
5978 unsigned NumDeclsLoaded
5979 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5980 (Decl *)0);
5981 unsigned NumIdentifiersLoaded
5982 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5983 IdentifiersLoaded.end(),
5984 (IdentifierInfo *)0);
5985 unsigned NumMacrosLoaded
5986 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5987 MacrosLoaded.end(),
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005988 (MacroInfo *)0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005989 unsigned NumSelectorsLoaded
5990 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5991 SelectorsLoaded.end(),
5992 Selector());
5993
5994 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5995 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5996 NumSLocEntriesRead, TotalNumSLocEntries,
5997 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5998 if (!TypesLoaded.empty())
5999 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6000 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6001 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6002 if (!DeclsLoaded.empty())
6003 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6004 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6005 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6006 if (!IdentifiersLoaded.empty())
6007 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6008 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6009 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6010 if (!MacrosLoaded.empty())
6011 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6012 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6013 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6014 if (!SelectorsLoaded.empty())
6015 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6016 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6017 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6018 if (TotalNumStatements)
6019 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6020 NumStatementsRead, TotalNumStatements,
6021 ((float)NumStatementsRead/TotalNumStatements * 100));
6022 if (TotalNumMacros)
6023 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6024 NumMacrosRead, TotalNumMacros,
6025 ((float)NumMacrosRead/TotalNumMacros * 100));
6026 if (TotalLexicalDeclContexts)
6027 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6028 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6029 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6030 * 100));
6031 if (TotalVisibleDeclContexts)
6032 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6033 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6034 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6035 * 100));
6036 if (TotalNumMethodPoolEntries) {
6037 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6038 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6039 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6040 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006041 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006042 if (NumMethodPoolLookups) {
6043 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6044 NumMethodPoolHits, NumMethodPoolLookups,
6045 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6046 }
6047 if (NumMethodPoolTableLookups) {
6048 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6049 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6050 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6051 * 100.0));
6052 }
6053
Douglas Gregore1698072013-01-25 00:38:33 +00006054 if (NumIdentifierLookupHits) {
6055 std::fprintf(stderr,
6056 " %u / %u identifier table lookups succeeded (%f%%)\n",
6057 NumIdentifierLookupHits, NumIdentifierLookups,
6058 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6059 }
6060
Douglas Gregor1a49d972013-01-25 01:03:03 +00006061 if (GlobalIndex) {
6062 std::fprintf(stderr, "\n");
6063 GlobalIndex->printStats();
6064 }
6065
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006066 std::fprintf(stderr, "\n");
6067 dump();
6068 std::fprintf(stderr, "\n");
6069}
6070
6071template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6072static void
6073dumpModuleIDMap(StringRef Name,
6074 const ContinuousRangeMap<Key, ModuleFile *,
6075 InitialCapacity> &Map) {
6076 if (Map.begin() == Map.end())
6077 return;
6078
6079 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6080 llvm::errs() << Name << ":\n";
6081 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6082 I != IEnd; ++I) {
6083 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6084 << "\n";
6085 }
6086}
6087
6088void ASTReader::dump() {
6089 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6090 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6091 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6092 dumpModuleIDMap("Global type map", GlobalTypeMap);
6093 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6094 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6095 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6096 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6097 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6098 dumpModuleIDMap("Global preprocessed entity map",
6099 GlobalPreprocessedEntityMap);
6100
6101 llvm::errs() << "\n*** PCH/Modules Loaded:";
6102 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6103 MEnd = ModuleMgr.end();
6104 M != MEnd; ++M)
6105 (*M)->dump();
6106}
6107
6108/// Return the amount of memory used by memory buffers, breaking down
6109/// by heap-backed versus mmap'ed memory.
6110void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6111 for (ModuleConstIterator I = ModuleMgr.begin(),
6112 E = ModuleMgr.end(); I != E; ++I) {
6113 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6114 size_t bytes = buf->getBufferSize();
6115 switch (buf->getBufferKind()) {
6116 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6117 sizes.malloc_bytes += bytes;
6118 break;
6119 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6120 sizes.mmap_bytes += bytes;
6121 break;
6122 }
6123 }
6124 }
6125}
6126
6127void ASTReader::InitializeSema(Sema &S) {
6128 SemaObj = &S;
6129 S.addExternalSource(this);
6130
6131 // Makes sure any declarations that were deserialized "too early"
6132 // still get added to the identifier's declaration chains.
6133 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006134 pushExternalDeclIntoScope(PreloadedDecls[I],
6135 PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006136 }
6137 PreloadedDecls.clear();
6138
6139 // Load the offsets of the declarations that Sema references.
6140 // They will be lazily deserialized when needed.
6141 if (!SemaDeclRefs.empty()) {
6142 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6143 if (!SemaObj->StdNamespace)
6144 SemaObj->StdNamespace = SemaDeclRefs[0];
6145 if (!SemaObj->StdBadAlloc)
6146 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6147 }
6148
6149 if (!FPPragmaOptions.empty()) {
6150 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6151 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6152 }
6153
6154 if (!OpenCLExtensions.empty()) {
6155 unsigned I = 0;
6156#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6157#include "clang/Basic/OpenCLExtensions.def"
6158
6159 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6160 }
6161}
6162
6163IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6164 // Note that we are loading an identifier.
6165 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006166 StringRef Name(NameStart, NameEnd - NameStart);
6167
6168 // If there is a global index, look there first to determine which modules
6169 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006170 GlobalModuleIndex::HitSet Hits;
6171 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006172 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006173 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6174 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006175 }
6176 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006177 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006178 NumIdentifierLookups,
6179 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006180 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006181 IdentifierInfo *II = Visitor.getIdentifierInfo();
6182 markIdentifierUpToDate(II);
6183 return II;
6184}
6185
6186namespace clang {
6187 /// \brief An identifier-lookup iterator that enumerates all of the
6188 /// identifiers stored within a set of AST files.
6189 class ASTIdentifierIterator : public IdentifierIterator {
6190 /// \brief The AST reader whose identifiers are being enumerated.
6191 const ASTReader &Reader;
6192
6193 /// \brief The current index into the chain of AST files stored in
6194 /// the AST reader.
6195 unsigned Index;
6196
6197 /// \brief The current position within the identifier lookup table
6198 /// of the current AST file.
6199 ASTIdentifierLookupTable::key_iterator Current;
6200
6201 /// \brief The end position within the identifier lookup table of
6202 /// the current AST file.
6203 ASTIdentifierLookupTable::key_iterator End;
6204
6205 public:
6206 explicit ASTIdentifierIterator(const ASTReader &Reader);
6207
6208 virtual StringRef Next();
6209 };
6210}
6211
6212ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6213 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6214 ASTIdentifierLookupTable *IdTable
6215 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6216 Current = IdTable->key_begin();
6217 End = IdTable->key_end();
6218}
6219
6220StringRef ASTIdentifierIterator::Next() {
6221 while (Current == End) {
6222 // If we have exhausted all of our AST files, we're done.
6223 if (Index == 0)
6224 return StringRef();
6225
6226 --Index;
6227 ASTIdentifierLookupTable *IdTable
6228 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6229 IdentifierLookupTable;
6230 Current = IdTable->key_begin();
6231 End = IdTable->key_end();
6232 }
6233
6234 // We have any identifiers remaining in the current AST file; return
6235 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006236 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006237 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006238 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006239}
6240
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +00006241IdentifierIterator *ASTReader::getIdentifiers() {
6242 if (!loadGlobalIndex())
6243 return GlobalIndex->createIdentifierIterator();
6244
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006245 return new ASTIdentifierIterator(*this);
6246}
6247
6248namespace clang { namespace serialization {
6249 class ReadMethodPoolVisitor {
6250 ASTReader &Reader;
6251 Selector Sel;
6252 unsigned PriorGeneration;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006253 unsigned InstanceBits;
6254 unsigned FactoryBits;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006255 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6256 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006257
6258 public:
6259 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6260 unsigned PriorGeneration)
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006261 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6262 InstanceBits(0), FactoryBits(0) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006263
6264 static bool visit(ModuleFile &M, void *UserData) {
6265 ReadMethodPoolVisitor *This
6266 = static_cast<ReadMethodPoolVisitor *>(UserData);
6267
6268 if (!M.SelectorLookupTable)
6269 return false;
6270
6271 // If we've already searched this module file, skip it now.
6272 if (M.Generation <= This->PriorGeneration)
6273 return true;
6274
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006275 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006276 ASTSelectorLookupTable *PoolTable
6277 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6278 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6279 if (Pos == PoolTable->end())
6280 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006281
6282 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006283 ++This->Reader.NumSelectorsRead;
6284 // FIXME: Not quite happy with the statistics here. We probably should
6285 // disable this tracking when called via LoadSelector.
6286 // Also, should entries without methods count as misses?
6287 ++This->Reader.NumMethodPoolEntriesRead;
6288 ASTSelectorLookupTrait::data_type Data = *Pos;
6289 if (This->Reader.DeserializationListener)
6290 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6291 This->Sel);
6292
6293 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6294 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006295 This->InstanceBits = Data.InstanceBits;
6296 This->FactoryBits = Data.FactoryBits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006297 return true;
6298 }
6299
6300 /// \brief Retrieve the instance methods found by this visitor.
6301 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6302 return InstanceMethods;
6303 }
6304
6305 /// \brief Retrieve the instance methods found by this visitor.
6306 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6307 return FactoryMethods;
6308 }
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006309
6310 unsigned getInstanceBits() const { return InstanceBits; }
6311 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006312 };
6313} } // end namespace clang::serialization
6314
6315/// \brief Add the given set of methods to the method list.
6316static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6317 ObjCMethodList &List) {
6318 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6319 S.addMethodToGlobalList(&List, Methods[I]);
6320 }
6321}
6322
6323void ASTReader::ReadMethodPool(Selector Sel) {
6324 // Get the selector generation and update it to the current generation.
6325 unsigned &Generation = SelectorGeneration[Sel];
6326 unsigned PriorGeneration = Generation;
6327 Generation = CurrentGeneration;
6328
6329 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006330 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006331 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6332 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6333
6334 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006335 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006336 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006337
6338 ++NumMethodPoolHits;
6339
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006340 if (!getSema())
6341 return;
6342
6343 Sema &S = *getSema();
6344 Sema::GlobalMethodPool::iterator Pos
6345 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6346
6347 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6348 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006349 Pos->second.first.setBits(Visitor.getInstanceBits());
6350 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006351}
6352
6353void ASTReader::ReadKnownNamespaces(
6354 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6355 Namespaces.clear();
6356
6357 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6358 if (NamespaceDecl *Namespace
6359 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6360 Namespaces.push_back(Namespace);
6361 }
6362}
6363
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006364void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00006365 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006366 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6367 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00006368 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006369 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00006370 Undefined.insert(std::make_pair(D, Loc));
6371 }
6372}
Nick Lewycky01a41142013-01-26 00:35:08 +00006373
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006374void ASTReader::ReadTentativeDefinitions(
6375 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6376 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6377 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6378 if (Var)
6379 TentativeDefs.push_back(Var);
6380 }
6381 TentativeDefinitions.clear();
6382}
6383
6384void ASTReader::ReadUnusedFileScopedDecls(
6385 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6386 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6387 DeclaratorDecl *D
6388 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6389 if (D)
6390 Decls.push_back(D);
6391 }
6392 UnusedFileScopedDecls.clear();
6393}
6394
6395void ASTReader::ReadDelegatingConstructors(
6396 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6397 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6398 CXXConstructorDecl *D
6399 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6400 if (D)
6401 Decls.push_back(D);
6402 }
6403 DelegatingCtorDecls.clear();
6404}
6405
6406void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6407 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6408 TypedefNameDecl *D
6409 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6410 if (D)
6411 Decls.push_back(D);
6412 }
6413 ExtVectorDecls.clear();
6414}
6415
6416void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6417 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6418 CXXRecordDecl *D
6419 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6420 if (D)
6421 Decls.push_back(D);
6422 }
6423 DynamicClasses.clear();
6424}
6425
6426void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006427ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6428 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6429 NamedDecl *D
6430 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006431 if (D)
6432 Decls.push_back(D);
6433 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006434 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006435}
6436
6437void ASTReader::ReadReferencedSelectors(
6438 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6439 if (ReferencedSelectorsData.empty())
6440 return;
6441
6442 // If there are @selector references added them to its pool. This is for
6443 // implementation of -Wselector.
6444 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6445 unsigned I = 0;
6446 while (I < DataSize) {
6447 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6448 SourceLocation SelLoc
6449 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6450 Sels.push_back(std::make_pair(Sel, SelLoc));
6451 }
6452 ReferencedSelectorsData.clear();
6453}
6454
6455void ASTReader::ReadWeakUndeclaredIdentifiers(
6456 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6457 if (WeakUndeclaredIdentifiers.empty())
6458 return;
6459
6460 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6461 IdentifierInfo *WeakId
6462 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6463 IdentifierInfo *AliasId
6464 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6465 SourceLocation Loc
6466 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6467 bool Used = WeakUndeclaredIdentifiers[I++];
6468 WeakInfo WI(AliasId, Loc);
6469 WI.setUsed(Used);
6470 WeakIDs.push_back(std::make_pair(WeakId, WI));
6471 }
6472 WeakUndeclaredIdentifiers.clear();
6473}
6474
6475void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6476 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6477 ExternalVTableUse VT;
6478 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6479 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6480 VT.DefinitionRequired = VTableUses[Idx++];
6481 VTables.push_back(VT);
6482 }
6483
6484 VTableUses.clear();
6485}
6486
6487void ASTReader::ReadPendingInstantiations(
6488 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6489 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6490 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6491 SourceLocation Loc
6492 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6493
6494 Pending.push_back(std::make_pair(D, Loc));
6495 }
6496 PendingInstantiations.clear();
6497}
6498
Richard Smithac32d902013-08-07 21:41:30 +00006499void ASTReader::ReadLateParsedTemplates(
6500 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
6501 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
6502 /* In loop */) {
6503 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
6504
6505 LateParsedTemplate *LT = new LateParsedTemplate;
6506 LT->D = GetDecl(LateParsedTemplates[Idx++]);
6507
6508 ModuleFile *F = getOwningModuleFile(LT->D);
6509 assert(F && "No module");
6510
6511 unsigned TokN = LateParsedTemplates[Idx++];
6512 LT->Toks.reserve(TokN);
6513 for (unsigned T = 0; T < TokN; ++T)
6514 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
6515
6516 LPTMap[FD] = LT;
6517 }
6518
6519 LateParsedTemplates.clear();
6520}
6521
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006522void ASTReader::LoadSelector(Selector Sel) {
6523 // It would be complicated to avoid reading the methods anyway. So don't.
6524 ReadMethodPool(Sel);
6525}
6526
6527void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6528 assert(ID && "Non-zero identifier ID required");
6529 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6530 IdentifiersLoaded[ID - 1] = II;
6531 if (DeserializationListener)
6532 DeserializationListener->IdentifierRead(ID, II);
6533}
6534
6535/// \brief Set the globally-visible declarations associated with the given
6536/// identifier.
6537///
6538/// If the AST reader is currently in a state where the given declaration IDs
6539/// cannot safely be resolved, they are queued until it is safe to resolve
6540/// them.
6541///
6542/// \param II an IdentifierInfo that refers to one or more globally-visible
6543/// declarations.
6544///
6545/// \param DeclIDs the set of declaration IDs with the name @p II that are
6546/// visible at global scope.
6547///
Douglas Gregoraa945902013-02-18 15:53:43 +00006548/// \param Decls if non-null, this vector will be populated with the set of
6549/// deserialized declarations. These declarations will not be pushed into
6550/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006551void
6552ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6553 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00006554 SmallVectorImpl<Decl *> *Decls) {
6555 if (NumCurrentElementsDeserializing && !Decls) {
6556 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006557 return;
6558 }
6559
6560 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6561 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6562 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006563 // If we're simply supposed to record the declarations, do so now.
6564 if (Decls) {
6565 Decls->push_back(D);
6566 continue;
6567 }
6568
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006569 // Introduce this declaration into the translation-unit scope
6570 // and add it to the declaration chain for this identifier, so
6571 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006572 pushExternalDeclIntoScope(D, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006573 } else {
6574 // Queue this declaration so that it will be added to the
6575 // translation unit scope and identifier's declaration chain
6576 // once a Sema object is known.
6577 PreloadedDecls.push_back(D);
6578 }
6579 }
6580}
6581
Douglas Gregor8222b892013-01-21 16:52:34 +00006582IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006583 if (ID == 0)
6584 return 0;
6585
6586 if (IdentifiersLoaded.empty()) {
6587 Error("no identifier table in AST file");
6588 return 0;
6589 }
6590
6591 ID -= 1;
6592 if (!IdentifiersLoaded[ID]) {
6593 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6594 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6595 ModuleFile *M = I->second;
6596 unsigned Index = ID - M->BaseIdentifierID;
6597 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6598
6599 // All of the strings in the AST file are preceded by a 16-bit length.
6600 // Extract that 16-bit length to avoid having to execute strlen().
6601 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6602 // unsigned integers. This is important to avoid integer overflow when
6603 // we cast them to 'unsigned'.
6604 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6605 unsigned StrLen = (((unsigned) StrLenPtr[0])
6606 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006607 IdentifiersLoaded[ID]
6608 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006609 if (DeserializationListener)
6610 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6611 }
6612
6613 return IdentifiersLoaded[ID];
6614}
6615
Douglas Gregor8222b892013-01-21 16:52:34 +00006616IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6617 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006618}
6619
6620IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6621 if (LocalID < NUM_PREDEF_IDENT_IDS)
6622 return LocalID;
6623
6624 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6625 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6626 assert(I != M.IdentifierRemap.end()
6627 && "Invalid index into identifier index remap");
6628
6629 return LocalID + I->second;
6630}
6631
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006632MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006633 if (ID == 0)
6634 return 0;
6635
6636 if (MacrosLoaded.empty()) {
6637 Error("no macro table in AST file");
6638 return 0;
6639 }
6640
6641 ID -= NUM_PREDEF_MACRO_IDS;
6642 if (!MacrosLoaded[ID]) {
6643 GlobalMacroMapType::iterator I
6644 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6645 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6646 ModuleFile *M = I->second;
6647 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006648 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6649
6650 if (DeserializationListener)
6651 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6652 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006653 }
6654
6655 return MacrosLoaded[ID];
6656}
6657
6658MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6659 if (LocalID < NUM_PREDEF_MACRO_IDS)
6660 return LocalID;
6661
6662 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6663 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6664 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6665
6666 return LocalID + I->second;
6667}
6668
6669serialization::SubmoduleID
6670ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6671 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6672 return LocalID;
6673
6674 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6675 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6676 assert(I != M.SubmoduleRemap.end()
6677 && "Invalid index into submodule index remap");
6678
6679 return LocalID + I->second;
6680}
6681
6682Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6683 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6684 assert(GlobalID == 0 && "Unhandled global submodule ID");
6685 return 0;
6686 }
6687
6688 if (GlobalID > SubmodulesLoaded.size()) {
6689 Error("submodule ID out of range in AST file");
6690 return 0;
6691 }
6692
6693 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6694}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006695
6696Module *ASTReader::getModule(unsigned ID) {
6697 return getSubmodule(ID);
6698}
6699
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006700Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6701 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6702}
6703
6704Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6705 if (ID == 0)
6706 return Selector();
6707
6708 if (ID > SelectorsLoaded.size()) {
6709 Error("selector ID out of range in AST file");
6710 return Selector();
6711 }
6712
6713 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6714 // Load this selector from the selector table.
6715 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6716 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6717 ModuleFile &M = *I->second;
6718 ASTSelectorLookupTrait Trait(*this, M);
6719 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6720 SelectorsLoaded[ID - 1] =
6721 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6722 if (DeserializationListener)
6723 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6724 }
6725
6726 return SelectorsLoaded[ID - 1];
6727}
6728
6729Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6730 return DecodeSelector(ID);
6731}
6732
6733uint32_t ASTReader::GetNumExternalSelectors() {
6734 // ID 0 (the null selector) is considered an external selector.
6735 return getTotalNumSelectors() + 1;
6736}
6737
6738serialization::SelectorID
6739ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6740 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6741 return LocalID;
6742
6743 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6744 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6745 assert(I != M.SelectorRemap.end()
6746 && "Invalid index into selector index remap");
6747
6748 return LocalID + I->second;
6749}
6750
6751DeclarationName
6752ASTReader::ReadDeclarationName(ModuleFile &F,
6753 const RecordData &Record, unsigned &Idx) {
6754 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6755 switch (Kind) {
6756 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006757 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006758
6759 case DeclarationName::ObjCZeroArgSelector:
6760 case DeclarationName::ObjCOneArgSelector:
6761 case DeclarationName::ObjCMultiArgSelector:
6762 return DeclarationName(ReadSelector(F, Record, Idx));
6763
6764 case DeclarationName::CXXConstructorName:
6765 return Context.DeclarationNames.getCXXConstructorName(
6766 Context.getCanonicalType(readType(F, Record, Idx)));
6767
6768 case DeclarationName::CXXDestructorName:
6769 return Context.DeclarationNames.getCXXDestructorName(
6770 Context.getCanonicalType(readType(F, Record, Idx)));
6771
6772 case DeclarationName::CXXConversionFunctionName:
6773 return Context.DeclarationNames.getCXXConversionFunctionName(
6774 Context.getCanonicalType(readType(F, Record, Idx)));
6775
6776 case DeclarationName::CXXOperatorName:
6777 return Context.DeclarationNames.getCXXOperatorName(
6778 (OverloadedOperatorKind)Record[Idx++]);
6779
6780 case DeclarationName::CXXLiteralOperatorName:
6781 return Context.DeclarationNames.getCXXLiteralOperatorName(
6782 GetIdentifierInfo(F, Record, Idx));
6783
6784 case DeclarationName::CXXUsingDirective:
6785 return DeclarationName::getUsingDirectiveName();
6786 }
6787
6788 llvm_unreachable("Invalid NameKind!");
6789}
6790
6791void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6792 DeclarationNameLoc &DNLoc,
6793 DeclarationName Name,
6794 const RecordData &Record, unsigned &Idx) {
6795 switch (Name.getNameKind()) {
6796 case DeclarationName::CXXConstructorName:
6797 case DeclarationName::CXXDestructorName:
6798 case DeclarationName::CXXConversionFunctionName:
6799 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6800 break;
6801
6802 case DeclarationName::CXXOperatorName:
6803 DNLoc.CXXOperatorName.BeginOpNameLoc
6804 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6805 DNLoc.CXXOperatorName.EndOpNameLoc
6806 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6807 break;
6808
6809 case DeclarationName::CXXLiteralOperatorName:
6810 DNLoc.CXXLiteralOperatorName.OpNameLoc
6811 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6812 break;
6813
6814 case DeclarationName::Identifier:
6815 case DeclarationName::ObjCZeroArgSelector:
6816 case DeclarationName::ObjCOneArgSelector:
6817 case DeclarationName::ObjCMultiArgSelector:
6818 case DeclarationName::CXXUsingDirective:
6819 break;
6820 }
6821}
6822
6823void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6824 DeclarationNameInfo &NameInfo,
6825 const RecordData &Record, unsigned &Idx) {
6826 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6827 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6828 DeclarationNameLoc DNLoc;
6829 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6830 NameInfo.setInfo(DNLoc);
6831}
6832
6833void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6834 const RecordData &Record, unsigned &Idx) {
6835 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6836 unsigned NumTPLists = Record[Idx++];
6837 Info.NumTemplParamLists = NumTPLists;
6838 if (NumTPLists) {
6839 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6840 for (unsigned i=0; i != NumTPLists; ++i)
6841 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6842 }
6843}
6844
6845TemplateName
6846ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6847 unsigned &Idx) {
6848 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6849 switch (Kind) {
6850 case TemplateName::Template:
6851 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6852
6853 case TemplateName::OverloadedTemplate: {
6854 unsigned size = Record[Idx++];
6855 UnresolvedSet<8> Decls;
6856 while (size--)
6857 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6858
6859 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6860 }
6861
6862 case TemplateName::QualifiedTemplate: {
6863 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6864 bool hasTemplKeyword = Record[Idx++];
6865 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6866 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6867 }
6868
6869 case TemplateName::DependentTemplate: {
6870 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6871 if (Record[Idx++]) // isIdentifier
6872 return Context.getDependentTemplateName(NNS,
6873 GetIdentifierInfo(F, Record,
6874 Idx));
6875 return Context.getDependentTemplateName(NNS,
6876 (OverloadedOperatorKind)Record[Idx++]);
6877 }
6878
6879 case TemplateName::SubstTemplateTemplateParm: {
6880 TemplateTemplateParmDecl *param
6881 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6882 if (!param) return TemplateName();
6883 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6884 return Context.getSubstTemplateTemplateParm(param, replacement);
6885 }
6886
6887 case TemplateName::SubstTemplateTemplateParmPack: {
6888 TemplateTemplateParmDecl *Param
6889 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6890 if (!Param)
6891 return TemplateName();
6892
6893 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6894 if (ArgPack.getKind() != TemplateArgument::Pack)
6895 return TemplateName();
6896
6897 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6898 }
6899 }
6900
6901 llvm_unreachable("Unhandled template name kind!");
6902}
6903
6904TemplateArgument
6905ASTReader::ReadTemplateArgument(ModuleFile &F,
6906 const RecordData &Record, unsigned &Idx) {
6907 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6908 switch (Kind) {
6909 case TemplateArgument::Null:
6910 return TemplateArgument();
6911 case TemplateArgument::Type:
6912 return TemplateArgument(readType(F, Record, Idx));
6913 case TemplateArgument::Declaration: {
6914 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6915 bool ForReferenceParam = Record[Idx++];
6916 return TemplateArgument(D, ForReferenceParam);
6917 }
6918 case TemplateArgument::NullPtr:
6919 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6920 case TemplateArgument::Integral: {
6921 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6922 QualType T = readType(F, Record, Idx);
6923 return TemplateArgument(Context, Value, T);
6924 }
6925 case TemplateArgument::Template:
6926 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6927 case TemplateArgument::TemplateExpansion: {
6928 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00006929 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006930 if (unsigned NumExpansions = Record[Idx++])
6931 NumTemplateExpansions = NumExpansions - 1;
6932 return TemplateArgument(Name, NumTemplateExpansions);
6933 }
6934 case TemplateArgument::Expression:
6935 return TemplateArgument(ReadExpr(F));
6936 case TemplateArgument::Pack: {
6937 unsigned NumArgs = Record[Idx++];
6938 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6939 for (unsigned I = 0; I != NumArgs; ++I)
6940 Args[I] = ReadTemplateArgument(F, Record, Idx);
6941 return TemplateArgument(Args, NumArgs);
6942 }
6943 }
6944
6945 llvm_unreachable("Unhandled template argument kind!");
6946}
6947
6948TemplateParameterList *
6949ASTReader::ReadTemplateParameterList(ModuleFile &F,
6950 const RecordData &Record, unsigned &Idx) {
6951 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6952 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6953 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6954
6955 unsigned NumParams = Record[Idx++];
6956 SmallVector<NamedDecl *, 16> Params;
6957 Params.reserve(NumParams);
6958 while (NumParams--)
6959 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6960
6961 TemplateParameterList* TemplateParams =
6962 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6963 Params.data(), Params.size(), RAngleLoc);
6964 return TemplateParams;
6965}
6966
6967void
6968ASTReader::
Craig Topper6b9240e2013-07-05 19:34:19 +00006969ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006970 ModuleFile &F, const RecordData &Record,
6971 unsigned &Idx) {
6972 unsigned NumTemplateArgs = Record[Idx++];
6973 TemplArgs.reserve(NumTemplateArgs);
6974 while (NumTemplateArgs--)
6975 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6976}
6977
6978/// \brief Read a UnresolvedSet structure.
Richard Smithc2d77572013-08-30 04:46:40 +00006979void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006980 const RecordData &Record, unsigned &Idx) {
6981 unsigned NumDecls = Record[Idx++];
6982 Set.reserve(Context, NumDecls);
6983 while (NumDecls--) {
Richard Smithc2d77572013-08-30 04:46:40 +00006984 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006985 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smithc2d77572013-08-30 04:46:40 +00006986 Set.addLazyDecl(Context, ID, AS);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006987 }
6988}
6989
6990CXXBaseSpecifier
6991ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6992 const RecordData &Record, unsigned &Idx) {
6993 bool isVirtual = static_cast<bool>(Record[Idx++]);
6994 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6995 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6996 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6997 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6998 SourceRange Range = ReadSourceRange(F, Record, Idx);
6999 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7000 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7001 EllipsisLoc);
7002 Result.setInheritConstructors(inheritConstructors);
7003 return Result;
7004}
7005
7006std::pair<CXXCtorInitializer **, unsigned>
7007ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7008 unsigned &Idx) {
7009 CXXCtorInitializer **CtorInitializers = 0;
7010 unsigned NumInitializers = Record[Idx++];
7011 if (NumInitializers) {
7012 CtorInitializers
7013 = new (Context) CXXCtorInitializer*[NumInitializers];
7014 for (unsigned i=0; i != NumInitializers; ++i) {
7015 TypeSourceInfo *TInfo = 0;
7016 bool IsBaseVirtual = false;
7017 FieldDecl *Member = 0;
7018 IndirectFieldDecl *IndirectMember = 0;
7019
7020 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7021 switch (Type) {
7022 case CTOR_INITIALIZER_BASE:
7023 TInfo = GetTypeSourceInfo(F, Record, Idx);
7024 IsBaseVirtual = Record[Idx++];
7025 break;
7026
7027 case CTOR_INITIALIZER_DELEGATING:
7028 TInfo = GetTypeSourceInfo(F, Record, Idx);
7029 break;
7030
7031 case CTOR_INITIALIZER_MEMBER:
7032 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7033 break;
7034
7035 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7036 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7037 break;
7038 }
7039
7040 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7041 Expr *Init = ReadExpr(F);
7042 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7043 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7044 bool IsWritten = Record[Idx++];
7045 unsigned SourceOrderOrNumArrayIndices;
7046 SmallVector<VarDecl *, 8> Indices;
7047 if (IsWritten) {
7048 SourceOrderOrNumArrayIndices = Record[Idx++];
7049 } else {
7050 SourceOrderOrNumArrayIndices = Record[Idx++];
7051 Indices.reserve(SourceOrderOrNumArrayIndices);
7052 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7053 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7054 }
7055
7056 CXXCtorInitializer *BOMInit;
7057 if (Type == CTOR_INITIALIZER_BASE) {
7058 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7059 LParenLoc, Init, RParenLoc,
7060 MemberOrEllipsisLoc);
7061 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7062 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7063 Init, RParenLoc);
7064 } else if (IsWritten) {
7065 if (Member)
7066 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7067 LParenLoc, Init, RParenLoc);
7068 else
7069 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7070 MemberOrEllipsisLoc, LParenLoc,
7071 Init, RParenLoc);
7072 } else {
Argyrios Kyrtzidisf8f480f2013-05-30 23:59:46 +00007073 if (IndirectMember) {
7074 assert(Indices.empty() && "Indirect field improperly initialized");
7075 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7076 MemberOrEllipsisLoc, LParenLoc,
7077 Init, RParenLoc);
7078 } else {
7079 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7080 LParenLoc, Init, RParenLoc,
7081 Indices.data(), Indices.size());
7082 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007083 }
7084
7085 if (IsWritten)
7086 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7087 CtorInitializers[i] = BOMInit;
7088 }
7089 }
7090
7091 return std::make_pair(CtorInitializers, NumInitializers);
7092}
7093
7094NestedNameSpecifier *
7095ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7096 const RecordData &Record, unsigned &Idx) {
7097 unsigned N = Record[Idx++];
7098 NestedNameSpecifier *NNS = 0, *Prev = 0;
7099 for (unsigned I = 0; I != N; ++I) {
7100 NestedNameSpecifier::SpecifierKind Kind
7101 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7102 switch (Kind) {
7103 case NestedNameSpecifier::Identifier: {
7104 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7105 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7106 break;
7107 }
7108
7109 case NestedNameSpecifier::Namespace: {
7110 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7111 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7112 break;
7113 }
7114
7115 case NestedNameSpecifier::NamespaceAlias: {
7116 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7117 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7118 break;
7119 }
7120
7121 case NestedNameSpecifier::TypeSpec:
7122 case NestedNameSpecifier::TypeSpecWithTemplate: {
7123 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7124 if (!T)
7125 return 0;
7126
7127 bool Template = Record[Idx++];
7128 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7129 break;
7130 }
7131
7132 case NestedNameSpecifier::Global: {
7133 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7134 // No associated value, and there can't be a prefix.
7135 break;
7136 }
7137 }
7138 Prev = NNS;
7139 }
7140 return NNS;
7141}
7142
7143NestedNameSpecifierLoc
7144ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7145 unsigned &Idx) {
7146 unsigned N = Record[Idx++];
7147 NestedNameSpecifierLocBuilder Builder;
7148 for (unsigned I = 0; I != N; ++I) {
7149 NestedNameSpecifier::SpecifierKind Kind
7150 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7151 switch (Kind) {
7152 case NestedNameSpecifier::Identifier: {
7153 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7154 SourceRange Range = ReadSourceRange(F, Record, Idx);
7155 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7156 break;
7157 }
7158
7159 case NestedNameSpecifier::Namespace: {
7160 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7161 SourceRange Range = ReadSourceRange(F, Record, Idx);
7162 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7163 break;
7164 }
7165
7166 case NestedNameSpecifier::NamespaceAlias: {
7167 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7168 SourceRange Range = ReadSourceRange(F, Record, Idx);
7169 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7170 break;
7171 }
7172
7173 case NestedNameSpecifier::TypeSpec:
7174 case NestedNameSpecifier::TypeSpecWithTemplate: {
7175 bool Template = Record[Idx++];
7176 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7177 if (!T)
7178 return NestedNameSpecifierLoc();
7179 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7180
7181 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7182 Builder.Extend(Context,
7183 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7184 T->getTypeLoc(), ColonColonLoc);
7185 break;
7186 }
7187
7188 case NestedNameSpecifier::Global: {
7189 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7190 Builder.MakeGlobal(Context, ColonColonLoc);
7191 break;
7192 }
7193 }
7194 }
7195
7196 return Builder.getWithLocInContext(Context);
7197}
7198
7199SourceRange
7200ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7201 unsigned &Idx) {
7202 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7203 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7204 return SourceRange(beg, end);
7205}
7206
7207/// \brief Read an integral value
7208llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7209 unsigned BitWidth = Record[Idx++];
7210 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7211 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7212 Idx += NumWords;
7213 return Result;
7214}
7215
7216/// \brief Read a signed integral value
7217llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7218 bool isUnsigned = Record[Idx++];
7219 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7220}
7221
7222/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007223llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7224 const llvm::fltSemantics &Sem,
7225 unsigned &Idx) {
7226 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007227}
7228
7229// \brief Read a string
7230std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7231 unsigned Len = Record[Idx++];
7232 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7233 Idx += Len;
7234 return Result;
7235}
7236
7237VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7238 unsigned &Idx) {
7239 unsigned Major = Record[Idx++];
7240 unsigned Minor = Record[Idx++];
7241 unsigned Subminor = Record[Idx++];
7242 if (Minor == 0)
7243 return VersionTuple(Major);
7244 if (Subminor == 0)
7245 return VersionTuple(Major, Minor - 1);
7246 return VersionTuple(Major, Minor - 1, Subminor - 1);
7247}
7248
7249CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7250 const RecordData &Record,
7251 unsigned &Idx) {
7252 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7253 return CXXTemporary::Create(Context, Decl);
7254}
7255
7256DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidis3b7deda2013-05-24 05:44:08 +00007257 return Diag(CurrentImportLoc, DiagID);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007258}
7259
7260DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7261 return Diags.Report(Loc, DiagID);
7262}
7263
7264/// \brief Retrieve the identifier table associated with the
7265/// preprocessor.
7266IdentifierTable &ASTReader::getIdentifierTable() {
7267 return PP.getIdentifierTable();
7268}
7269
7270/// \brief Record that the given ID maps to the given switch-case
7271/// statement.
7272void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7273 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7274 "Already have a SwitchCase with this ID");
7275 (*CurrSwitchCaseStmts)[ID] = SC;
7276}
7277
7278/// \brief Retrieve the switch-case statement with the given ID.
7279SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7280 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7281 return (*CurrSwitchCaseStmts)[ID];
7282}
7283
7284void ASTReader::ClearSwitchCaseIDs() {
7285 CurrSwitchCaseStmts->clear();
7286}
7287
7288void ASTReader::ReadComments() {
7289 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007290 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007291 serialization::ModuleFile *> >::iterator
7292 I = CommentsCursors.begin(),
7293 E = CommentsCursors.end();
7294 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007295 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007296 serialization::ModuleFile &F = *I->second;
7297 SavedStreamPosition SavedPosition(Cursor);
7298
7299 RecordData Record;
7300 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007301 llvm::BitstreamEntry Entry =
7302 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7303
7304 switch (Entry.Kind) {
7305 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7306 case llvm::BitstreamEntry::Error:
7307 Error("malformed block record in AST file");
7308 return;
7309 case llvm::BitstreamEntry::EndBlock:
7310 goto NextCursor;
7311 case llvm::BitstreamEntry::Record:
7312 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007313 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007314 }
7315
7316 // Read a record.
7317 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007318 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007319 case COMMENTS_RAW_COMMENT: {
7320 unsigned Idx = 0;
7321 SourceRange SR = ReadSourceRange(F, Record, Idx);
7322 RawComment::CommentKind Kind =
7323 (RawComment::CommentKind) Record[Idx++];
7324 bool IsTrailingComment = Record[Idx++];
7325 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00007326 Comments.push_back(new (Context) RawComment(
7327 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7328 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007329 break;
7330 }
7331 }
7332 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007333 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007334 }
7335 Context.Comments.addCommentsToFront(Comments);
7336}
7337
7338void ASTReader::finishPendingActions() {
7339 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007340 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007341 // If any identifiers with corresponding top-level declarations have
7342 // been loaded, load those declarations now.
Craig Topperee0a4792013-07-05 04:33:53 +00007343 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
7344 TopLevelDeclsMap;
7345 TopLevelDeclsMap TopLevelDecls;
7346
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007347 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007348 // FIXME: std::move
7349 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7350 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00007351 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00007352
7353 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007354 }
7355
7356 // Load pending declaration chains.
7357 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7358 loadPendingDeclChain(PendingDeclChains[I]);
7359 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7360 }
7361 PendingDeclChains.clear();
7362
Douglas Gregoraa945902013-02-18 15:53:43 +00007363 // Make the most recent of the top-level declarations visible.
Craig Topperee0a4792013-07-05 04:33:53 +00007364 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
7365 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007366 IdentifierInfo *II = TLD->first;
7367 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007368 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregoraa945902013-02-18 15:53:43 +00007369 }
7370 }
7371
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007372 // Load any pending macro definitions.
7373 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007374 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7375 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7376 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7377 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00007378 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7379 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007380 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7381 if (Info.M->Kind != MK_Module)
7382 resolvePendingMacro(II, Info);
7383 }
7384 // Handle module imports.
7385 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7386 ++IDIdx) {
7387 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7388 if (Info.M->Kind == MK_Module)
7389 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007390 }
7391 }
7392 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007393
7394 // Wire up the DeclContexts for Decls that we delayed setting until
7395 // recursive loading is completed.
7396 while (!PendingDeclContextInfos.empty()) {
7397 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7398 PendingDeclContextInfos.pop_front();
7399 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7400 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7401 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7402 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007403 }
7404
7405 // If we deserialized any C++ or Objective-C class definitions, any
7406 // Objective-C protocol definitions, or any redeclarable templates, make sure
7407 // that all redeclarations point to the definitions. Note that this can only
7408 // happen now, after the redeclaration chains have been fully wired.
7409 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7410 DEnd = PendingDefinitions.end();
7411 D != DEnd; ++D) {
7412 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7413 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7414 // Make sure that the TagType points at the definition.
7415 const_cast<TagType*>(TagT)->decl = TD;
7416 }
7417
7418 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7419 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7420 REnd = RD->redecls_end();
7421 R != REnd; ++R)
7422 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7423
7424 }
7425
7426 continue;
7427 }
7428
7429 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7430 // Make sure that the ObjCInterfaceType points at the definition.
7431 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7432 ->Decl = ID;
7433
7434 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7435 REnd = ID->redecls_end();
7436 R != REnd; ++R)
7437 R->Data = ID->Data;
7438
7439 continue;
7440 }
7441
7442 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7443 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7444 REnd = PD->redecls_end();
7445 R != REnd; ++R)
7446 R->Data = PD->Data;
7447
7448 continue;
7449 }
7450
7451 RedeclarableTemplateDecl *RTD
7452 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7453 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7454 REnd = RTD->redecls_end();
7455 R != REnd; ++R)
7456 R->Common = RTD->Common;
7457 }
7458 PendingDefinitions.clear();
7459
7460 // Load the bodies of any functions or methods we've encountered. We do
7461 // this now (delayed) so that we can be sure that the declaration chains
7462 // have been fully wired up.
7463 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7464 PBEnd = PendingBodies.end();
7465 PB != PBEnd; ++PB) {
7466 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7467 // FIXME: Check for =delete/=default?
7468 // FIXME: Complain about ODR violations here?
7469 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7470 FD->setLazyBody(PB->second);
7471 continue;
7472 }
7473
7474 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7475 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7476 MD->setLazyBody(PB->second);
7477 }
7478 PendingBodies.clear();
7479}
7480
7481void ASTReader::FinishedDeserializing() {
7482 assert(NumCurrentElementsDeserializing &&
7483 "FinishedDeserializing not paired with StartedDeserializing");
7484 if (NumCurrentElementsDeserializing == 1) {
7485 // We decrease NumCurrentElementsDeserializing only after pending actions
7486 // are finished, to avoid recursively re-calling finishPendingActions().
7487 finishPendingActions();
7488 }
7489 --NumCurrentElementsDeserializing;
7490
7491 if (NumCurrentElementsDeserializing == 0 &&
7492 Consumer && !PassingDeclsToConsumer) {
7493 // Guard variable to avoid recursively redoing the process of passing
7494 // decls to consumer.
7495 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7496 true);
7497
7498 while (!InterestingDecls.empty()) {
7499 // We are not in recursive loading, so it's safe to pass the "interesting"
7500 // decls to the consumer.
7501 Decl *D = InterestingDecls.front();
7502 InterestingDecls.pop_front();
7503 PassInterestingDeclToConsumer(D);
7504 }
7505 }
7506}
7507
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007508void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7509 D = cast<NamedDecl>(D->getMostRecentDecl());
7510
7511 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7512 SemaObj->TUScope->AddDecl(D);
7513 } else if (SemaObj->TUScope) {
7514 // Adding the decl to IdResolver may have failed because it was already in
7515 // (even though it was not added in scope). If it is already in, make sure
7516 // it gets in the scope as well.
7517 if (std::find(SemaObj->IdResolver.begin(Name),
7518 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7519 SemaObj->TUScope->AddDecl(D);
7520 }
7521}
7522
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007523ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7524 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007525 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007526 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7527 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7528 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7529 Consumer(0), ModuleMgr(PP.getFileManager()),
7530 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007531 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007532 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007533 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7534 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007535 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7536 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7537 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007538 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7539 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7540 TotalNumMethodPoolEntries(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007541 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7542 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7543 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7544 PassingDeclsToConsumer(false),
Richard Smithafb90df2013-07-31 00:26:46 +00007545 NumCXXBaseSpecifiersLoaded(0), ReadingKind(Read_None)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007546{
7547 SourceMgr.setExternalSLocEntrySource(this);
7548}
7549
7550ASTReader::~ASTReader() {
7551 for (DeclContextVisibleUpdatesPending::iterator
7552 I = PendingVisibleUpdates.begin(),
7553 E = PendingVisibleUpdates.end();
7554 I != E; ++I) {
7555 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7556 F = I->second.end();
7557 J != F; ++J)
7558 delete J->first;
7559 }
7560}