blob: 22caeb8656b52503c35005013e83196243d34f38 [file] [log] [blame]
Nick Lewycky995e26b2013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTReader class, which reads AST files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTReader.h"
15#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
17#include "clang/AST/ASTConsumer.h"
18#include "clang/AST/ASTContext.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/Type.h"
24#include "clang/AST/TypeLocVisitor.h"
25#include "clang/Basic/FileManager.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000026#include "clang/Basic/SourceManager.h"
27#include "clang/Basic/SourceManagerInternals.h"
28#include "clang/Basic/TargetInfo.h"
29#include "clang/Basic/TargetOptions.h"
30#include "clang/Basic/Version.h"
31#include "clang/Basic/VersionTuple.h"
32#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/HeaderSearchOptions.h"
34#include "clang/Lex/MacroInfo.h"
35#include "clang/Lex/PreprocessingRecord.h"
36#include "clang/Lex/Preprocessor.h"
37#include "clang/Lex/PreprocessorOptions.h"
38#include "clang/Sema/Scope.h"
39#include "clang/Sema/Sema.h"
40#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor1a49d972013-01-25 01:03:03 +000041#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000042#include "clang/Serialization/ModuleManager.h"
43#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000044#include "llvm/ADT/Hashing.h"
Guy Benyei7f92f2d2012-12-18 14:30:41 +000045#include "llvm/ADT/StringExtras.h"
46#include "llvm/Bitcode/BitstreamReader.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/FileSystem.h"
49#include "llvm/Support/MemoryBuffer.h"
50#include "llvm/Support/Path.h"
51#include "llvm/Support/SaveAndRestore.h"
52#include "llvm/Support/system_error.h"
53#include <algorithm>
Chris Lattnere4e4a882013-01-20 00:57:52 +000054#include <cstdio>
Guy Benyei7f92f2d2012-12-18 14:30:41 +000055#include <iterator>
56
57using namespace clang;
58using namespace clang::serialization;
59using namespace clang::serialization::reader;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +000060using llvm::BitstreamCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +000061
62//===----------------------------------------------------------------------===//
63// PCH validator implementation
64//===----------------------------------------------------------------------===//
65
66ASTReaderListener::~ASTReaderListener() {}
67
68/// \brief Compare the given set of language options against an existing set of
69/// language options.
70///
71/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
72///
73/// \returns true if the languagae options mis-match, false otherwise.
74static bool checkLanguageOptions(const LangOptions &LangOpts,
75 const LangOptions &ExistingLangOpts,
76 DiagnosticsEngine *Diags) {
77#define LANGOPT(Name, Bits, Default, Description) \
78 if (ExistingLangOpts.Name != LangOpts.Name) { \
79 if (Diags) \
80 Diags->Report(diag::err_pch_langopt_mismatch) \
81 << Description << LangOpts.Name << ExistingLangOpts.Name; \
82 return true; \
83 }
84
85#define VALUE_LANGOPT(Name, Bits, Default, Description) \
86 if (ExistingLangOpts.Name != LangOpts.Name) { \
87 if (Diags) \
88 Diags->Report(diag::err_pch_langopt_value_mismatch) \
89 << Description; \
90 return true; \
91 }
92
93#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
95 if (Diags) \
96 Diags->Report(diag::err_pch_langopt_value_mismatch) \
97 << Description; \
98 return true; \
99 }
100
101#define BENIGN_LANGOPT(Name, Bits, Default, Description)
102#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
103#include "clang/Basic/LangOptions.def"
104
105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
106 if (Diags)
107 Diags->Report(diag::err_pch_langopt_value_mismatch)
108 << "target Objective-C runtime";
109 return true;
110 }
111
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +0000112 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
113 LangOpts.CommentOpts.BlockCommandNames) {
114 if (Diags)
115 Diags->Report(diag::err_pch_langopt_value_mismatch)
116 << "block command names";
117 return true;
118 }
119
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000120 return false;
121}
122
123/// \brief Compare the given set of target options against an existing set of
124/// target options.
125///
126/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
127///
128/// \returns true if the target options mis-match, false otherwise.
129static bool checkTargetOptions(const TargetOptions &TargetOpts,
130 const TargetOptions &ExistingTargetOpts,
131 DiagnosticsEngine *Diags) {
132#define CHECK_TARGET_OPT(Field, Name) \
133 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
134 if (Diags) \
135 Diags->Report(diag::err_pch_targetopt_mismatch) \
136 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
137 return true; \
138 }
139
140 CHECK_TARGET_OPT(Triple, "target");
141 CHECK_TARGET_OPT(CPU, "target CPU");
142 CHECK_TARGET_OPT(ABI, "target ABI");
143 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
144 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
145#undef CHECK_TARGET_OPT
146
147 // Compare feature sets.
148 SmallVector<StringRef, 4> ExistingFeatures(
149 ExistingTargetOpts.FeaturesAsWritten.begin(),
150 ExistingTargetOpts.FeaturesAsWritten.end());
151 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
152 TargetOpts.FeaturesAsWritten.end());
153 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
154 std::sort(ReadFeatures.begin(), ReadFeatures.end());
155
156 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
157 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
158 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
159 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
160 ++ExistingIdx;
161 ++ReadIdx;
162 continue;
163 }
164
165 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
166 if (Diags)
167 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
168 << false << ReadFeatures[ReadIdx];
169 return true;
170 }
171
172 if (Diags)
173 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
174 << true << ExistingFeatures[ExistingIdx];
175 return true;
176 }
177
178 if (ExistingIdx < ExistingN) {
179 if (Diags)
180 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
181 << true << ExistingFeatures[ExistingIdx];
182 return true;
183 }
184
185 if (ReadIdx < ReadN) {
186 if (Diags)
187 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
188 << false << ReadFeatures[ReadIdx];
189 return true;
190 }
191
192 return false;
193}
194
195bool
196PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
197 bool Complain) {
198 const LangOptions &ExistingLangOpts = PP.getLangOpts();
199 return checkLanguageOptions(LangOpts, ExistingLangOpts,
200 Complain? &Reader.Diags : 0);
201}
202
203bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
204 bool Complain) {
205 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
206 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
207 Complain? &Reader.Diags : 0);
208}
209
210namespace {
211 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
212 MacroDefinitionsMap;
213}
214
215/// \brief Collect the macro definitions provided by the given preprocessor
216/// options.
217static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
218 MacroDefinitionsMap &Macros,
219 SmallVectorImpl<StringRef> *MacroNames = 0){
220 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
221 StringRef Macro = PPOpts.Macros[I].first;
222 bool IsUndef = PPOpts.Macros[I].second;
223
224 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
225 StringRef MacroName = MacroPair.first;
226 StringRef MacroBody = MacroPair.second;
227
228 // For an #undef'd macro, we only care about the name.
229 if (IsUndef) {
230 if (MacroNames && !Macros.count(MacroName))
231 MacroNames->push_back(MacroName);
232
233 Macros[MacroName] = std::make_pair("", true);
234 continue;
235 }
236
237 // For a #define'd macro, figure out the actual definition.
238 if (MacroName.size() == Macro.size())
239 MacroBody = "1";
240 else {
241 // Note: GCC drops anything following an end-of-line character.
242 StringRef::size_type End = MacroBody.find_first_of("\n\r");
243 MacroBody = MacroBody.substr(0, End);
244 }
245
246 if (MacroNames && !Macros.count(MacroName))
247 MacroNames->push_back(MacroName);
248 Macros[MacroName] = std::make_pair(MacroBody, false);
249 }
250}
251
252/// \brief Check the preprocessor options deserialized from the control block
253/// against the preprocessor options in an existing preprocessor.
254///
255/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
256static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
257 const PreprocessorOptions &ExistingPPOpts,
258 DiagnosticsEngine *Diags,
259 FileManager &FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000260 std::string &SuggestedPredefines,
261 const LangOptions &LangOpts) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000262 // Check macro definitions.
263 MacroDefinitionsMap ASTFileMacros;
264 collectMacroDefinitions(PPOpts, ASTFileMacros);
265 MacroDefinitionsMap ExistingMacros;
266 SmallVector<StringRef, 4> ExistingMacroNames;
267 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
268
269 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
270 // Dig out the macro definition in the existing preprocessor options.
271 StringRef MacroName = ExistingMacroNames[I];
272 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
273
274 // Check whether we know anything about this macro name or not.
275 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
276 = ASTFileMacros.find(MacroName);
277 if (Known == ASTFileMacros.end()) {
278 // FIXME: Check whether this identifier was referenced anywhere in the
279 // AST file. If so, we should reject the AST file. Unfortunately, this
280 // information isn't in the control block. What shall we do about it?
281
282 if (Existing.second) {
283 SuggestedPredefines += "#undef ";
284 SuggestedPredefines += MacroName.str();
285 SuggestedPredefines += '\n';
286 } else {
287 SuggestedPredefines += "#define ";
288 SuggestedPredefines += MacroName.str();
289 SuggestedPredefines += ' ';
290 SuggestedPredefines += Existing.first.str();
291 SuggestedPredefines += '\n';
292 }
293 continue;
294 }
295
296 // If the macro was defined in one but undef'd in the other, we have a
297 // conflict.
298 if (Existing.second != Known->second.second) {
299 if (Diags) {
300 Diags->Report(diag::err_pch_macro_def_undef)
301 << MacroName << Known->second.second;
302 }
303 return true;
304 }
305
306 // If the macro was #undef'd in both, or if the macro bodies are identical,
307 // it's fine.
308 if (Existing.second || Existing.first == Known->second.first)
309 continue;
310
311 // The macro bodies differ; complain.
312 if (Diags) {
313 Diags->Report(diag::err_pch_macro_def_conflict)
314 << MacroName << Known->second.first << Existing.first;
315 }
316 return true;
317 }
318
319 // Check whether we're using predefines.
320 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
321 if (Diags) {
322 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
323 }
324 return true;
325 }
326
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000327 // Detailed record is important since it is used for the module cache hash.
328 if (LangOpts.Modules &&
329 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
330 if (Diags) {
331 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
332 }
333 return true;
334 }
335
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000336 // Compute the #include and #include_macros lines we need.
337 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
338 StringRef File = ExistingPPOpts.Includes[I];
339 if (File == ExistingPPOpts.ImplicitPCHInclude)
340 continue;
341
342 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
343 != PPOpts.Includes.end())
344 continue;
345
346 SuggestedPredefines += "#include \"";
347 SuggestedPredefines +=
348 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
349 SuggestedPredefines += "\"\n";
350 }
351
352 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
353 StringRef File = ExistingPPOpts.MacroIncludes[I];
354 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
355 File)
356 != PPOpts.MacroIncludes.end())
357 continue;
358
359 SuggestedPredefines += "#__include_macros \"";
360 SuggestedPredefines +=
361 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
362 SuggestedPredefines += "\"\n##\n";
363 }
364
365 return false;
366}
367
368bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
369 bool Complain,
370 std::string &SuggestedPredefines) {
371 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
372
373 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
374 Complain? &Reader.Diags : 0,
375 PP.getFileManager(),
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +0000376 SuggestedPredefines,
377 PP.getLangOpts());
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000378}
379
380void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
381 unsigned ID) {
382 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
383 ++NumHeaderInfos;
384}
385
386void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
387 PP.setCounterValue(Value);
388}
389
390//===----------------------------------------------------------------------===//
391// AST reader implementation
392//===----------------------------------------------------------------------===//
393
394void
395ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
396 DeserializationListener = Listener;
397}
398
399
400
401unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
402 return serialization::ComputeHash(Sel);
403}
404
405
406std::pair<unsigned, unsigned>
407ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
408 using namespace clang::io;
409 unsigned KeyLen = ReadUnalignedLE16(d);
410 unsigned DataLen = ReadUnalignedLE16(d);
411 return std::make_pair(KeyLen, DataLen);
412}
413
414ASTSelectorLookupTrait::internal_key_type
415ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
416 using namespace clang::io;
417 SelectorTable &SelTable = Reader.getContext().Selectors;
418 unsigned N = ReadUnalignedLE16(d);
419 IdentifierInfo *FirstII
Douglas Gregor8222b892013-01-21 16:52:34 +0000420 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000421 if (N == 0)
422 return SelTable.getNullarySelector(FirstII);
423 else if (N == 1)
424 return SelTable.getUnarySelector(FirstII);
425
426 SmallVector<IdentifierInfo *, 16> Args;
427 Args.push_back(FirstII);
428 for (unsigned I = 1; I != N; ++I)
Douglas Gregor8222b892013-01-21 16:52:34 +0000429 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000430
431 return SelTable.getSelector(N, Args.data());
432}
433
434ASTSelectorLookupTrait::data_type
435ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
436 unsigned DataLen) {
437 using namespace clang::io;
438
439 data_type Result;
440
441 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +0000442 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
443 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
444 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
445 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
446 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
447 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000448
449 // Load instance methods
450 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
451 if (ObjCMethodDecl *Method
452 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
453 Result.Instance.push_back(Method);
454 }
455
456 // Load factory methods
457 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
458 if (ObjCMethodDecl *Method
459 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
460 Result.Factory.push_back(Method);
461 }
462
463 return Result;
464}
465
Douglas Gregor479633c2013-01-23 18:53:14 +0000466unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
467 return llvm::HashString(a);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000468}
469
470std::pair<unsigned, unsigned>
Douglas Gregor479633c2013-01-23 18:53:14 +0000471ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000472 using namespace clang::io;
473 unsigned DataLen = ReadUnalignedLE16(d);
474 unsigned KeyLen = ReadUnalignedLE16(d);
475 return std::make_pair(KeyLen, DataLen);
476}
477
Douglas Gregor479633c2013-01-23 18:53:14 +0000478ASTIdentifierLookupTraitBase::internal_key_type
479ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000480 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregor479633c2013-01-23 18:53:14 +0000481 return StringRef((const char*) d, n-1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000482}
483
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000484/// \brief Whether the given identifier is "interesting".
485static bool isInterestingIdentifier(IdentifierInfo &II) {
486 return II.isPoisoned() ||
487 II.isExtensionToken() ||
488 II.getObjCOrBuiltinID() ||
489 II.hasRevertedTokenIDToIdentifier() ||
490 II.hadMacroDefinition() ||
491 II.getFETokenInfo<void>();
492}
493
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000494IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
495 const unsigned char* d,
496 unsigned DataLen) {
497 using namespace clang::io;
498 unsigned RawID = ReadUnalignedLE32(d);
499 bool IsInteresting = RawID & 0x01;
500
501 // Wipe out the "is interesting" bit.
502 RawID = RawID >> 1;
503
504 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
505 if (!IsInteresting) {
506 // For uninteresting identifiers, just build the IdentifierInfo
507 // and associate it with the persistent ID.
508 IdentifierInfo *II = KnownII;
509 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000510 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000511 KnownII = II;
512 }
513 Reader.SetIdentifierInfo(ID, II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000514 if (!II->isFromAST()) {
515 bool WasInteresting = isInterestingIdentifier(*II);
516 II->setIsFromAST();
517 if (WasInteresting)
518 II->setChangedSinceDeserialization();
519 }
520 Reader.markIdentifierUpToDate(II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000521 return II;
522 }
523
524 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
525 unsigned Bits = ReadUnalignedLE16(d);
526 bool CPlusPlusOperatorKeyword = Bits & 0x01;
527 Bits >>= 1;
528 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
529 Bits >>= 1;
530 bool Poisoned = Bits & 0x01;
531 Bits >>= 1;
532 bool ExtensionToken = Bits & 0x01;
533 Bits >>= 1;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000534 bool hasSubmoduleMacros = Bits & 0x01;
535 Bits >>= 1;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000536 bool hadMacroDefinition = Bits & 0x01;
537 Bits >>= 1;
538
539 assert(Bits == 0 && "Extra bits in the identifier?");
540 DataLen -= 8;
541
542 // Build the IdentifierInfo itself and link the identifier ID with
543 // the new IdentifierInfo.
544 IdentifierInfo *II = KnownII;
545 if (!II) {
Douglas Gregor479633c2013-01-23 18:53:14 +0000546 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000547 KnownII = II;
548 }
549 Reader.markIdentifierUpToDate(II);
Douglas Gregorf4e955b2013-02-11 18:16:18 +0000550 if (!II->isFromAST()) {
551 bool WasInteresting = isInterestingIdentifier(*II);
552 II->setIsFromAST();
553 if (WasInteresting)
554 II->setChangedSinceDeserialization();
555 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000556
557 // Set or check the various bits in the IdentifierInfo structure.
558 // Token IDs are read-only.
Argyrios Kyrtzidis1ebefc72013-02-27 01:13:51 +0000559 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000560 II->RevertTokenIDToIdentifier();
561 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
562 assert(II->isExtensionToken() == ExtensionToken &&
563 "Incorrect extension token flag");
564 (void)ExtensionToken;
565 if (Poisoned)
566 II->setIsPoisoned(true);
567 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
568 "Incorrect C++ operator keyword flag");
569 (void)CPlusPlusOperatorKeyword;
570
571 // If this identifier is a macro, deserialize the macro
572 // definition.
573 if (hadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000574 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
575 DataLen -= 4;
576 SmallVector<uint32_t, 8> LocalMacroIDs;
577 if (hasSubmoduleMacros) {
578 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
579 DataLen -= 4;
580 LocalMacroIDs.push_back(LocalMacroID);
581 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +0000582 DataLen -= 4;
583 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000584
585 if (F.Kind == MK_Module) {
586 for (SmallVectorImpl<uint32_t>::iterator
587 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; ++I) {
588 MacroID MacID = Reader.getGlobalMacroID(F, *I);
589 Reader.addPendingMacroFromModule(II, &F, MacID, F.DirectImportLoc);
590 }
591 } else {
592 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
593 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000594 }
595
596 Reader.SetIdentifierInfo(ID, II);
597
598 // Read all of the declarations visible at global scope with this
599 // name.
600 if (DataLen > 0) {
601 SmallVector<uint32_t, 4> DeclIDs;
602 for (; DataLen > 0; DataLen -= 4)
603 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
604 Reader.SetGloballyVisibleDecls(II, DeclIDs);
605 }
606
607 return II;
608}
609
610unsigned
611ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
612 llvm::FoldingSetNodeID ID;
613 ID.AddInteger(Key.Kind);
614
615 switch (Key.Kind) {
616 case DeclarationName::Identifier:
617 case DeclarationName::CXXLiteralOperatorName:
618 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
619 break;
620 case DeclarationName::ObjCZeroArgSelector:
621 case DeclarationName::ObjCOneArgSelector:
622 case DeclarationName::ObjCMultiArgSelector:
623 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
624 break;
625 case DeclarationName::CXXOperatorName:
626 ID.AddInteger((OverloadedOperatorKind)Key.Data);
627 break;
628 case DeclarationName::CXXConstructorName:
629 case DeclarationName::CXXDestructorName:
630 case DeclarationName::CXXConversionFunctionName:
631 case DeclarationName::CXXUsingDirective:
632 break;
633 }
634
635 return ID.ComputeHash();
636}
637
638ASTDeclContextNameLookupTrait::internal_key_type
639ASTDeclContextNameLookupTrait::GetInternalKey(
640 const external_key_type& Name) const {
641 DeclNameKey Key;
642 Key.Kind = Name.getNameKind();
643 switch (Name.getNameKind()) {
644 case DeclarationName::Identifier:
645 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
646 break;
647 case DeclarationName::ObjCZeroArgSelector:
648 case DeclarationName::ObjCOneArgSelector:
649 case DeclarationName::ObjCMultiArgSelector:
650 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
651 break;
652 case DeclarationName::CXXOperatorName:
653 Key.Data = Name.getCXXOverloadedOperator();
654 break;
655 case DeclarationName::CXXLiteralOperatorName:
656 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
657 break;
658 case DeclarationName::CXXConstructorName:
659 case DeclarationName::CXXDestructorName:
660 case DeclarationName::CXXConversionFunctionName:
661 case DeclarationName::CXXUsingDirective:
662 Key.Data = 0;
663 break;
664 }
665
666 return Key;
667}
668
669std::pair<unsigned, unsigned>
670ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
671 using namespace clang::io;
672 unsigned KeyLen = ReadUnalignedLE16(d);
673 unsigned DataLen = ReadUnalignedLE16(d);
674 return std::make_pair(KeyLen, DataLen);
675}
676
677ASTDeclContextNameLookupTrait::internal_key_type
678ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
679 using namespace clang::io;
680
681 DeclNameKey Key;
682 Key.Kind = (DeclarationName::NameKind)*d++;
683 switch (Key.Kind) {
684 case DeclarationName::Identifier:
685 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
686 break;
687 case DeclarationName::ObjCZeroArgSelector:
688 case DeclarationName::ObjCOneArgSelector:
689 case DeclarationName::ObjCMultiArgSelector:
690 Key.Data =
691 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
692 .getAsOpaquePtr();
693 break;
694 case DeclarationName::CXXOperatorName:
695 Key.Data = *d++; // OverloadedOperatorKind
696 break;
697 case DeclarationName::CXXLiteralOperatorName:
698 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
699 break;
700 case DeclarationName::CXXConstructorName:
701 case DeclarationName::CXXDestructorName:
702 case DeclarationName::CXXConversionFunctionName:
703 case DeclarationName::CXXUsingDirective:
704 Key.Data = 0;
705 break;
706 }
707
708 return Key;
709}
710
711ASTDeclContextNameLookupTrait::data_type
712ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
713 const unsigned char* d,
714 unsigned DataLen) {
715 using namespace clang::io;
716 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidise8b61cf2013-01-11 22:29:49 +0000717 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
718 const_cast<unsigned char *>(d));
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000719 return std::make_pair(Start, Start + NumDecls);
720}
721
722bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner8f9a1eb2013-01-20 00:56:42 +0000723 BitstreamCursor &Cursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000724 const std::pair<uint64_t, uint64_t> &Offsets,
725 DeclContextInfo &Info) {
726 SavedStreamPosition SavedPosition(Cursor);
727 // First the lexical decls.
728 if (Offsets.first != 0) {
729 Cursor.JumpToBit(Offsets.first);
730
731 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000732 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000733 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000734 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000735 if (RecCode != DECL_CONTEXT_LEXICAL) {
736 Error("Expected lexical block");
737 return true;
738 }
739
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000740 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
741 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000742 }
743
744 // Now the lookup table.
745 if (Offsets.second != 0) {
746 Cursor.JumpToBit(Offsets.second);
747
748 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000749 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000750 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000751 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000752 if (RecCode != DECL_CONTEXT_VISIBLE) {
753 Error("Expected visible lookup table block");
754 return true;
755 }
756 Info.NameLookupTableData
757 = ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +0000758 (const unsigned char *)Blob.data() + Record[0],
759 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +0000760 ASTDeclContextNameLookupTrait(*this, M));
761 }
762
763 return false;
764}
765
766void ASTReader::Error(StringRef Msg) {
767 Error(diag::err_fe_pch_malformed, Msg);
768}
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
John McCallaeeacf72013-05-03 00:10:13 +00001106Token ASTReader::ReadToken(ModuleFile &F, const RecordData &Record,
1107 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++;
1286 HFI.isImport = (Flags >> 5) & 0x01;
1287 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1288 HFI.DirInfo = (Flags >> 2) & 0x03;
1289 HFI.Resolved = (Flags >> 1) & 0x01;
1290 HFI.IndexHeaderMapHeader = Flags & 0x01;
1291 HFI.NumIncludes = ReadUnalignedLE16(d);
1292 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1293 ReadUnalignedLE32(d));
1294 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1295 // The framework offset is 1 greater than the actual offset,
1296 // since 0 is used as an indicator for "no framework name".
1297 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1298 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1299 }
1300
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001301 if (d != End) {
1302 uint32_t LocalSMID = ReadUnalignedLE32(d);
1303 if (LocalSMID) {
1304 // This header is part of a module. Associate it with the module to enable
1305 // implicit module import.
1306 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1307 Module *Mod = Reader.getSubmodule(GlobalSMID);
1308 HFI.isModuleHeader = true;
1309 FileManager &FileMgr = Reader.getFileManager();
1310 ModuleMap &ModMap =
1311 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1312 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false);
1313 }
1314 }
1315
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001316 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1317 (void)End;
1318
1319 // This HeaderFileInfo was externally loaded.
1320 HFI.External = true;
1321 return HFI;
1322}
1323
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001324void ASTReader::addPendingMacroFromModule(IdentifierInfo *II,
1325 ModuleFile *M,
1326 GlobalMacroID GMacID,
1327 SourceLocation ImportLoc) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001328 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001329 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, ImportLoc));
1330}
1331
1332void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1333 ModuleFile *M,
1334 uint64_t MacroDirectivesOffset) {
1335 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1336 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001337}
1338
1339void ASTReader::ReadDefinedMacros() {
1340 // Note that we are loading defined macros.
1341 Deserializing Macros(this);
1342
1343 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1344 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001345 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001346
1347 // If there was no preprocessor block, skip this file.
1348 if (!MacroCursor.getBitStreamReader())
1349 continue;
1350
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001351 BitstreamCursor Cursor = MacroCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001352 Cursor.JumpToBit((*I)->MacroStartOffset);
1353
1354 RecordData Record;
1355 while (true) {
Chris Lattner88bde502013-01-19 21:39:22 +00001356 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1357
1358 switch (E.Kind) {
1359 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1360 case llvm::BitstreamEntry::Error:
1361 Error("malformed block record in AST file");
1362 return;
1363 case llvm::BitstreamEntry::EndBlock:
1364 goto NextCursor;
1365
1366 case llvm::BitstreamEntry::Record:
Chris Lattner88bde502013-01-19 21:39:22 +00001367 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001368 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattner88bde502013-01-19 21:39:22 +00001369 default: // Default behavior: ignore.
1370 break;
1371
1372 case PP_MACRO_OBJECT_LIKE:
1373 case PP_MACRO_FUNCTION_LIKE:
1374 getLocalIdentifier(**I, Record[0]);
1375 break;
1376
1377 case PP_TOKEN:
1378 // Ignore tokens.
1379 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001380 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001381 break;
1382 }
1383 }
Chris Lattner88bde502013-01-19 21:39:22 +00001384 NextCursor: ;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001385 }
1386}
1387
1388namespace {
1389 /// \brief Visitor class used to look up identifirs in an AST file.
1390 class IdentifierLookupVisitor {
1391 StringRef Name;
1392 unsigned PriorGeneration;
Douglas Gregore1698072013-01-25 00:38:33 +00001393 unsigned &NumIdentifierLookups;
1394 unsigned &NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001395 IdentifierInfo *Found;
Douglas Gregore1698072013-01-25 00:38:33 +00001396
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001397 public:
Douglas Gregore1698072013-01-25 00:38:33 +00001398 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1399 unsigned &NumIdentifierLookups,
1400 unsigned &NumIdentifierLookupHits)
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001401 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregore1698072013-01-25 00:38:33 +00001402 NumIdentifierLookups(NumIdentifierLookups),
1403 NumIdentifierLookupHits(NumIdentifierLookupHits),
1404 Found()
1405 {
1406 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001407
1408 static bool visit(ModuleFile &M, void *UserData) {
1409 IdentifierLookupVisitor *This
1410 = static_cast<IdentifierLookupVisitor *>(UserData);
1411
1412 // If we've already searched this module file, skip it now.
1413 if (M.Generation <= This->PriorGeneration)
1414 return true;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001415
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001416 ASTIdentifierLookupTable *IdTable
1417 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1418 if (!IdTable)
1419 return false;
1420
1421 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1422 M, This->Found);
Douglas Gregore1698072013-01-25 00:38:33 +00001423 ++This->NumIdentifierLookups;
1424 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001425 if (Pos == IdTable->end())
1426 return false;
1427
1428 // Dereferencing the iterator has the effect of building the
1429 // IdentifierInfo node and populating it with the various
1430 // declarations it needs.
Douglas Gregore1698072013-01-25 00:38:33 +00001431 ++This->NumIdentifierLookupHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001432 This->Found = *Pos;
1433 return true;
1434 }
1435
1436 // \brief Retrieve the identifier info found within the module
1437 // files.
1438 IdentifierInfo *getIdentifierInfo() const { return Found; }
1439 };
1440}
1441
1442void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1443 // Note that we are loading an identifier.
1444 Deserializing AnIdentifier(this);
1445
1446 unsigned PriorGeneration = 0;
1447 if (getContext().getLangOpts().Modules)
1448 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregor1a49d972013-01-25 01:03:03 +00001449
1450 // If there is a global index, look there first to determine which modules
1451 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001452 GlobalModuleIndex::HitSet Hits;
1453 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001454 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001455 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1456 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00001457 }
1458 }
1459
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001460 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregore1698072013-01-25 00:38:33 +00001461 NumIdentifierLookups,
1462 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00001463 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001464 markIdentifierUpToDate(&II);
1465}
1466
1467void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1468 if (!II)
1469 return;
1470
1471 II->setOutOfDate(false);
1472
1473 // Update the generation for this identifier.
1474 if (getContext().getLangOpts().Modules)
1475 IdentifierGeneration[II] = CurrentGeneration;
1476}
1477
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001478void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1479 const PendingMacroInfo &PMInfo) {
1480 assert(II);
1481
1482 if (PMInfo.M->Kind != MK_Module) {
1483 installPCHMacroDirectives(II, *PMInfo.M,
1484 PMInfo.PCHMacroData.MacroDirectivesOffset);
1485 return;
1486 }
1487
1488 // Module Macro.
1489
1490 GlobalMacroID GMacID = PMInfo.ModuleMacroData.GMacID;
1491 SourceLocation ImportLoc =
1492 SourceLocation::getFromRawEncoding(PMInfo.ModuleMacroData.ImportLoc);
1493
1494 assert(GMacID);
1495 // If this macro has already been loaded, don't do so again.
1496 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1497 return;
1498
1499 MacroInfo *MI = getMacro(GMacID);
1500 SubmoduleID SubModID = MI->getOwningModuleID();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001501 MacroDirective *MD = PP.AllocateDefMacroDirective(MI, ImportLoc,
1502 /*isImported=*/true);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001503
1504 // Determine whether this macro definition is visible.
1505 bool Hidden = false;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001506 Module *Owner = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001507 if (SubModID) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001508 if ((Owner = getSubmodule(SubModID))) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001509 if (Owner->NameVisibility == Module::Hidden) {
1510 // The owning module is not visible, and this macro definition
1511 // should not be, either.
1512 Hidden = true;
1513
1514 // Note that this macro definition was hidden because its owning
1515 // module is not yet visible.
1516 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
1517 }
1518 }
1519 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001520
1521 if (!Hidden)
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001522 installImportedMacro(II, MD, Owner);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001523}
1524
1525void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1526 ModuleFile &M, uint64_t Offset) {
1527 assert(M.Kind != MK_Module);
1528
1529 BitstreamCursor &Cursor = M.MacroCursor;
1530 SavedStreamPosition SavedPosition(Cursor);
1531 Cursor.JumpToBit(Offset);
1532
1533 llvm::BitstreamEntry Entry =
1534 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1535 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1536 Error("malformed block record in AST file");
1537 return;
1538 }
1539
1540 RecordData Record;
1541 PreprocessorRecordTypes RecType =
1542 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1543 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1544 Error("malformed block record in AST file");
1545 return;
1546 }
1547
1548 // Deserialize the macro directives history in reverse source-order.
1549 MacroDirective *Latest = 0, *Earliest = 0;
1550 unsigned Idx = 0, N = Record.size();
1551 while (Idx < N) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001552 MacroDirective *MD = 0;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001553 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001554 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1555 switch (K) {
1556 case MacroDirective::MD_Define: {
1557 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1558 MacroInfo *MI = getMacro(GMacID);
1559 bool isImported = Record[Idx++];
1560 bool isAmbiguous = Record[Idx++];
1561 DefMacroDirective *DefMD =
1562 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1563 DefMD->setAmbiguous(isAmbiguous);
1564 MD = DefMD;
1565 break;
1566 }
1567 case MacroDirective::MD_Undefine:
1568 MD = PP.AllocateUndefMacroDirective(Loc);
1569 break;
1570 case MacroDirective::MD_Visibility: {
1571 bool isPublic = Record[Idx++];
1572 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1573 break;
1574 }
1575 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001576
1577 if (!Latest)
1578 Latest = MD;
1579 if (Earliest)
1580 Earliest->setPrevious(MD);
1581 Earliest = MD;
1582 }
1583
1584 PP.setLoadedMacroDirective(II, Latest);
1585}
1586
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001587/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor7332ae42013-04-12 21:00:54 +00001588/// modules.
1589static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
1590 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001591 assert(PrevMI && NewMI);
1592 if (!NewOwner)
1593 return false;
1594 Module *PrevOwner = 0;
1595 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1596 PrevOwner = Reader.getSubmodule(PrevModID);
1597 if (!PrevOwner)
1598 return false;
1599 if (PrevOwner == NewOwner)
1600 return false;
Douglas Gregor7332ae42013-04-12 21:00:54 +00001601 return PrevOwner->IsSystem && NewOwner->IsSystem;
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001602}
1603
1604void ASTReader::installImportedMacro(IdentifierInfo *II, MacroDirective *MD,
1605 Module *Owner) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001606 assert(II && MD);
1607
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001608 DefMacroDirective *DefMD = cast<DefMacroDirective>(MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001609 MacroDirective *Prev = PP.getMacroDirective(II);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001610 if (Prev) {
1611 MacroDirective::DefInfo PrevDef = Prev->getDefinition();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001612 MacroInfo *PrevMI = PrevDef.getMacroInfo();
1613 MacroInfo *NewMI = DefMD->getInfo();
Argyrios Kyrtzidisbd25ff82013-04-03 17:39:30 +00001614 if (NewMI != PrevMI && !PrevMI->isIdenticalTo(*NewMI, PP,
1615 /*Syntactically=*/true)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001616 // Before marking the macros as ambiguous, check if this is a case where
Douglas Gregor7332ae42013-04-12 21:00:54 +00001617 // both macros are in system headers. If so, we trust that the system
1618 // did not get it wrong. This also handles cases where Clang's own
1619 // headers have a different spelling of certain system macros:
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001620 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1621 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
Douglas Gregor7332ae42013-04-12 21:00:54 +00001622 if (!areDefinedInSystemModules(PrevMI, NewMI, Owner, *this)) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00001623 PrevDef.getDirective()->setAmbiguous(true);
1624 DefMD->setAmbiguous(true);
1625 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001626 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001627 }
1628
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001629 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001630}
1631
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001632InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001633 // If this ID is bogus, just return an empty input file.
1634 if (ID == 0 || ID > F.InputFilesLoaded.size())
1635 return InputFile();
1636
1637 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001638 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001639 return F.InputFilesLoaded[ID-1];
1640
1641 // Go find this input file.
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001642 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001643 SavedStreamPosition SavedPosition(Cursor);
1644 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1645
1646 unsigned Code = Cursor.ReadCode();
1647 RecordData Record;
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001648 StringRef Blob;
1649 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001650 case INPUT_FILE: {
1651 unsigned StoredID = Record[0];
1652 assert(ID == StoredID && "Bogus stored ID or offset");
1653 (void)StoredID;
1654 off_t StoredSize = (off_t)Record[1];
1655 time_t StoredTime = (time_t)Record[2];
1656 bool Overridden = (bool)Record[3];
1657
1658 // Get the file entry for this input file.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001659 StringRef OrigFilename = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001660 std::string Filename = OrigFilename;
1661 MaybeAddSystemRootToFilename(F, Filename);
1662 const FileEntry *File
1663 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1664 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1665
1666 // If we didn't find the file, resolve it relative to the
1667 // original directory from which this AST file was created.
1668 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1669 F.OriginalDir != CurrentDir) {
1670 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1671 F.OriginalDir,
1672 CurrentDir);
1673 if (!Resolved.empty())
1674 File = FileMgr.getFile(Resolved);
1675 }
1676
1677 // For an overridden file, create a virtual file with the stored
1678 // size/timestamp.
1679 if (Overridden && File == 0) {
1680 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1681 }
1682
1683 if (File == 0) {
1684 if (Complain) {
1685 std::string ErrorStr = "could not find file '";
1686 ErrorStr += Filename;
1687 ErrorStr += "' referenced by AST file";
1688 Error(ErrorStr.c_str());
1689 }
1690 return InputFile();
1691 }
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001692
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001693 // Check if there was a request to override the contents of the file
1694 // that was part of the precompiled header. Overridding such a file
1695 // can lead to problems when lexing using the source locations from the
1696 // PCH.
1697 SourceManager &SM = getSourceManager();
1698 if (!Overridden && SM.isFileOverridden(File)) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001699 if (Complain)
1700 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001701 // After emitting the diagnostic, recover by disabling the override so
1702 // that the original file will be used.
1703 SM.disableFileContentsOverride(File);
1704 // The FileEntry is a virtual file entry with the size of the contents
1705 // that would override the original contents. Set it to the original's
1706 // size/time.
1707 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1708 StoredSize, StoredTime);
1709 }
1710
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001711 bool IsOutOfDate = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001712
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001713 // For an overridden file, there is nothing to validate.
1714 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001715#if !defined(LLVM_ON_WIN32)
1716 // In our regression testing, the Windows file system seems to
1717 // have inconsistent modification times that sometimes
1718 // erroneously trigger this error-handling path.
1719 || StoredTime != File->getModificationTime()
1720#endif
1721 )) {
Douglas Gregor677e15f2013-03-19 00:28:20 +00001722 if (Complain) {
Argyrios Kyrtzidisf8f373f2013-03-08 20:42:38 +00001723 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001724 }
1725
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001726 IsOutOfDate = true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001727 }
1728
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001729 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1730
1731 // Note that we've loaded this input file.
1732 F.InputFilesLoaded[ID-1] = IF;
1733 return IF;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001734 }
1735 }
1736
1737 return InputFile();
1738}
1739
1740const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1741 ModuleFile &M = ModuleMgr.getPrimaryModule();
1742 std::string Filename = filenameStrRef;
1743 MaybeAddSystemRootToFilename(M, Filename);
1744 const FileEntry *File = FileMgr.getFile(Filename);
1745 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1746 M.OriginalDir != CurrentDir) {
1747 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1748 M.OriginalDir,
1749 CurrentDir);
1750 if (!resolved.empty())
1751 File = FileMgr.getFile(resolved);
1752 }
1753
1754 return File;
1755}
1756
1757/// \brief If we are loading a relocatable PCH file, and the filename is
1758/// not an absolute path, add the system root to the beginning of the file
1759/// name.
1760void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1761 std::string &Filename) {
1762 // If this is not a relocatable PCH file, there's nothing to do.
1763 if (!M.RelocatablePCH)
1764 return;
1765
1766 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1767 return;
1768
1769 if (isysroot.empty()) {
1770 // If no system root was given, default to '/'
1771 Filename.insert(Filename.begin(), '/');
1772 return;
1773 }
1774
1775 unsigned Length = isysroot.size();
1776 if (isysroot[Length - 1] != '/')
1777 Filename.insert(Filename.begin(), '/');
1778
1779 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1780}
1781
1782ASTReader::ASTReadResult
1783ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001784 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001785 unsigned ClientLoadCapabilities) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001786 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001787
1788 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1789 Error("malformed block record in AST file");
1790 return Failure;
1791 }
1792
1793 // Read all of the records and blocks in the control block.
1794 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001795 while (1) {
1796 llvm::BitstreamEntry Entry = Stream.advance();
1797
1798 switch (Entry.Kind) {
1799 case llvm::BitstreamEntry::Error:
1800 Error("malformed block record in AST file");
1801 return Failure;
1802 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001803 // Validate all of the non-system input files.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001804 if (!DisableValidation) {
1805 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001806 // All user input files reside at the index range [0, Record[1]).
1807 // Record is the one from INPUT_FILE_OFFSETS.
1808 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001809 InputFile IF = getInputFile(F, I+1, Complain);
1810 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001811 return OutOfDate;
Argyrios Kyrtzidis8504b7b2013-03-01 03:26:04 +00001812 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001813 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001814 return Success;
Chris Lattner88bde502013-01-19 21:39:22 +00001815
1816 case llvm::BitstreamEntry::SubBlock:
1817 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001818 case INPUT_FILES_BLOCK_ID:
1819 F.InputFilesCursor = Stream;
1820 if (Stream.SkipBlock() || // Skip with the main cursor
1821 // Read the abbreviations
1822 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1823 Error("malformed block record in AST file");
1824 return Failure;
1825 }
1826 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00001827
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001828 default:
Chris Lattner88bde502013-01-19 21:39:22 +00001829 if (Stream.SkipBlock()) {
1830 Error("malformed block record in AST file");
1831 return Failure;
1832 }
1833 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001834 }
Chris Lattner88bde502013-01-19 21:39:22 +00001835
1836 case llvm::BitstreamEntry::Record:
1837 // The interesting case.
1838 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001839 }
1840
1841 // Read and process a record.
1842 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001843 StringRef Blob;
1844 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001845 case METADATA: {
1846 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1847 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1848 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1849 : diag::warn_pch_version_too_new);
1850 return VersionMismatch;
1851 }
1852
1853 bool hasErrors = Record[5];
1854 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1855 Diag(diag::err_pch_with_compiler_errors);
1856 return HadErrors;
1857 }
1858
1859 F.RelocatablePCH = Record[4];
1860
1861 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001862 StringRef ASTBranch = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001863 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1864 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1865 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1866 return VersionMismatch;
1867 }
1868 break;
1869 }
1870
1871 case IMPORTS: {
1872 // Load each of the imported PCH files.
1873 unsigned Idx = 0, N = Record.size();
1874 while (Idx < N) {
1875 // Read information about the AST file.
1876 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1877 // The import location will be the local one for now; we will adjust
1878 // all import locations of module imports after the global source
1879 // location info are setup.
1880 SourceLocation ImportLoc =
1881 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001882 off_t StoredSize = (off_t)Record[Idx++];
1883 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001884 unsigned Length = Record[Idx++];
1885 SmallString<128> ImportedFile(Record.begin() + Idx,
1886 Record.begin() + Idx + Length);
1887 Idx += Length;
1888
1889 // Load the AST file.
1890 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00001891 StoredSize, StoredModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001892 ClientLoadCapabilities)) {
1893 case Failure: return Failure;
1894 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorac39f132013-03-19 00:38:50 +00001895 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001896 case OutOfDate: return OutOfDate;
1897 case VersionMismatch: return VersionMismatch;
1898 case ConfigurationMismatch: return ConfigurationMismatch;
1899 case HadErrors: return HadErrors;
1900 case Success: break;
1901 }
1902 }
1903 break;
1904 }
1905
1906 case LANGUAGE_OPTIONS: {
1907 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1908 if (Listener && &F == *ModuleMgr.begin() &&
1909 ParseLanguageOptions(Record, Complain, *Listener) &&
1910 !DisableValidation)
1911 return ConfigurationMismatch;
1912 break;
1913 }
1914
1915 case TARGET_OPTIONS: {
1916 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1917 if (Listener && &F == *ModuleMgr.begin() &&
1918 ParseTargetOptions(Record, Complain, *Listener) &&
1919 !DisableValidation)
1920 return ConfigurationMismatch;
1921 break;
1922 }
1923
1924 case DIAGNOSTIC_OPTIONS: {
1925 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1926 if (Listener && &F == *ModuleMgr.begin() &&
1927 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1928 !DisableValidation)
1929 return ConfigurationMismatch;
1930 break;
1931 }
1932
1933 case FILE_SYSTEM_OPTIONS: {
1934 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1935 if (Listener && &F == *ModuleMgr.begin() &&
1936 ParseFileSystemOptions(Record, Complain, *Listener) &&
1937 !DisableValidation)
1938 return ConfigurationMismatch;
1939 break;
1940 }
1941
1942 case HEADER_SEARCH_OPTIONS: {
1943 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1944 if (Listener && &F == *ModuleMgr.begin() &&
1945 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1946 !DisableValidation)
1947 return ConfigurationMismatch;
1948 break;
1949 }
1950
1951 case PREPROCESSOR_OPTIONS: {
1952 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1953 if (Listener && &F == *ModuleMgr.begin() &&
1954 ParsePreprocessorOptions(Record, Complain, *Listener,
1955 SuggestedPredefines) &&
1956 !DisableValidation)
1957 return ConfigurationMismatch;
1958 break;
1959 }
1960
1961 case ORIGINAL_FILE:
1962 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001963 F.ActualOriginalSourceFileName = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001964 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1965 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1966 break;
1967
1968 case ORIGINAL_FILE_ID:
1969 F.OriginalSourceFileID = FileID::get(Record[0]);
1970 break;
1971
1972 case ORIGINAL_PCH_DIR:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001973 F.OriginalDir = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001974 break;
1975
1976 case INPUT_FILE_OFFSETS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00001977 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001978 F.InputFilesLoaded.resize(Record[0]);
1979 break;
1980 }
1981 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001982}
1983
1984bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00001985 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00001986
1987 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1988 Error("malformed block record in AST file");
1989 return true;
1990 }
1991
1992 // Read all of the records and blocks for the AST file.
1993 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00001994 while (1) {
1995 llvm::BitstreamEntry Entry = Stream.advance();
1996
1997 switch (Entry.Kind) {
1998 case llvm::BitstreamEntry::Error:
1999 Error("error at end of module block in AST file");
2000 return true;
2001 case llvm::BitstreamEntry::EndBlock: {
Richard Smith43828672013-04-03 22:49:41 +00002002 // Outside of C++, we do not store a lookup map for the translation unit.
2003 // Instead, mark it as needing a lookup map to be built if this module
2004 // contains any declarations lexically within it (which it always does!).
2005 // This usually has no cost, since we very rarely need the lookup map for
2006 // the translation unit outside C++.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002007 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smith43828672013-04-03 22:49:41 +00002008 if (DC->hasExternalLexicalStorage() &&
2009 !getContext().getLangOpts().CPlusPlus)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002010 DC->setMustBuildLookupTable();
Chris Lattner88bde502013-01-19 21:39:22 +00002011
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002012 return false;
2013 }
Chris Lattner88bde502013-01-19 21:39:22 +00002014 case llvm::BitstreamEntry::SubBlock:
2015 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002016 case DECLTYPES_BLOCK_ID:
2017 // We lazily load the decls block, but we want to set up the
2018 // DeclsCursor cursor to point into it. Clone our current bitcode
2019 // cursor to it, enter the block and read the abbrevs in that block.
2020 // With the main cursor, we just skip over it.
2021 F.DeclsCursor = Stream;
2022 if (Stream.SkipBlock() || // Skip with the main cursor.
2023 // Read the abbrevs.
2024 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2025 Error("malformed block record in AST file");
2026 return true;
2027 }
2028 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002029
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002030 case DECL_UPDATES_BLOCK_ID:
2031 if (Stream.SkipBlock()) {
2032 Error("malformed block record in AST file");
2033 return true;
2034 }
2035 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002036
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002037 case PREPROCESSOR_BLOCK_ID:
2038 F.MacroCursor = Stream;
2039 if (!PP.getExternalSource())
2040 PP.setExternalSource(this);
Chris Lattner88bde502013-01-19 21:39:22 +00002041
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002042 if (Stream.SkipBlock() ||
2043 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2044 Error("malformed block record in AST file");
2045 return true;
2046 }
2047 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2048 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002049
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002050 case PREPROCESSOR_DETAIL_BLOCK_ID:
2051 F.PreprocessorDetailCursor = Stream;
2052 if (Stream.SkipBlock() ||
Chris Lattner88bde502013-01-19 21:39:22 +00002053 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002054 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattner88bde502013-01-19 21:39:22 +00002055 Error("malformed preprocessor detail record in AST file");
2056 return true;
2057 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002058 F.PreprocessorDetailStartOffset
Chris Lattner88bde502013-01-19 21:39:22 +00002059 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2060
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002061 if (!PP.getPreprocessingRecord())
2062 PP.createPreprocessingRecord();
2063 if (!PP.getPreprocessingRecord()->getExternalSource())
2064 PP.getPreprocessingRecord()->SetExternalSource(*this);
2065 break;
2066
2067 case SOURCE_MANAGER_BLOCK_ID:
2068 if (ReadSourceManagerBlock(F))
2069 return true;
2070 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002071
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002072 case SUBMODULE_BLOCK_ID:
2073 if (ReadSubmoduleBlock(F))
2074 return true;
2075 break;
Chris Lattner88bde502013-01-19 21:39:22 +00002076
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002077 case COMMENTS_BLOCK_ID: {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00002078 BitstreamCursor C = Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002079 if (Stream.SkipBlock() ||
2080 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2081 Error("malformed comments block in AST file");
2082 return true;
2083 }
2084 CommentsCursors.push_back(std::make_pair(C, &F));
2085 break;
2086 }
Chris Lattner88bde502013-01-19 21:39:22 +00002087
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002088 default:
Chris Lattner88bde502013-01-19 21:39:22 +00002089 if (Stream.SkipBlock()) {
2090 Error("malformed block record in AST file");
2091 return true;
2092 }
2093 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002094 }
2095 continue;
Chris Lattner88bde502013-01-19 21:39:22 +00002096
2097 case llvm::BitstreamEntry::Record:
2098 // The interesting case.
2099 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002100 }
2101
2102 // Read and process a record.
2103 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002104 StringRef Blob;
2105 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002106 default: // Default behavior: ignore.
2107 break;
2108
2109 case TYPE_OFFSET: {
2110 if (F.LocalNumTypes != 0) {
2111 Error("duplicate TYPE_OFFSET record in AST file");
2112 return true;
2113 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002114 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002115 F.LocalNumTypes = Record[0];
2116 unsigned LocalBaseTypeIndex = Record[1];
2117 F.BaseTypeIndex = getTotalNumTypes();
2118
2119 if (F.LocalNumTypes > 0) {
2120 // Introduce the global -> local mapping for types within this module.
2121 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2122
2123 // Introduce the local -> global mapping for types within this module.
2124 F.TypeRemap.insertOrReplace(
2125 std::make_pair(LocalBaseTypeIndex,
2126 F.BaseTypeIndex - LocalBaseTypeIndex));
2127
2128 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2129 }
2130 break;
2131 }
2132
2133 case DECL_OFFSET: {
2134 if (F.LocalNumDecls != 0) {
2135 Error("duplicate DECL_OFFSET record in AST file");
2136 return true;
2137 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002138 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002139 F.LocalNumDecls = Record[0];
2140 unsigned LocalBaseDeclID = Record[1];
2141 F.BaseDeclID = getTotalNumDecls();
2142
2143 if (F.LocalNumDecls > 0) {
2144 // Introduce the global -> local mapping for declarations within this
2145 // module.
2146 GlobalDeclMap.insert(
2147 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2148
2149 // Introduce the local -> global mapping for declarations within this
2150 // module.
2151 F.DeclRemap.insertOrReplace(
2152 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2153
2154 // Introduce the global -> local mapping for declarations within this
2155 // module.
2156 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2157
2158 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2159 }
2160 break;
2161 }
2162
2163 case TU_UPDATE_LEXICAL: {
2164 DeclContext *TU = Context.getTranslationUnitDecl();
2165 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002166 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002167 Info.NumLexicalDecls
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002168 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002169 TU->setHasExternalLexicalStorage(true);
2170 break;
2171 }
2172
2173 case UPDATE_VISIBLE: {
2174 unsigned Idx = 0;
2175 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2176 ASTDeclContextNameLookupTable *Table =
2177 ASTDeclContextNameLookupTable::Create(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002178 (const unsigned char *)Blob.data() + Record[Idx++],
2179 (const unsigned char *)Blob.data(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002180 ASTDeclContextNameLookupTrait(*this, F));
2181 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2182 DeclContext *TU = Context.getTranslationUnitDecl();
2183 F.DeclContextInfos[TU].NameLookupTableData = Table;
2184 TU->setHasExternalVisibleStorage(true);
2185 } else
2186 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2187 break;
2188 }
2189
2190 case IDENTIFIER_TABLE:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002191 F.IdentifierTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002192 if (Record[0]) {
2193 F.IdentifierLookupTable
2194 = ASTIdentifierLookupTable::Create(
2195 (const unsigned char *)F.IdentifierTableData + Record[0],
2196 (const unsigned char *)F.IdentifierTableData,
2197 ASTIdentifierLookupTrait(*this, F));
2198
2199 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2200 }
2201 break;
2202
2203 case IDENTIFIER_OFFSET: {
2204 if (F.LocalNumIdentifiers != 0) {
2205 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2206 return true;
2207 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002208 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002209 F.LocalNumIdentifiers = Record[0];
2210 unsigned LocalBaseIdentifierID = Record[1];
2211 F.BaseIdentifierID = getTotalNumIdentifiers();
2212
2213 if (F.LocalNumIdentifiers > 0) {
2214 // Introduce the global -> local mapping for identifiers within this
2215 // module.
2216 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2217 &F));
2218
2219 // Introduce the local -> global mapping for identifiers within this
2220 // module.
2221 F.IdentifierRemap.insertOrReplace(
2222 std::make_pair(LocalBaseIdentifierID,
2223 F.BaseIdentifierID - LocalBaseIdentifierID));
2224
2225 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2226 + F.LocalNumIdentifiers);
2227 }
2228 break;
2229 }
2230
2231 case EXTERNAL_DEFINITIONS:
2232 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2233 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2234 break;
2235
2236 case SPECIAL_TYPES:
Douglas Gregorf5cfc892013-02-01 23:45:03 +00002237 if (SpecialTypes.empty()) {
2238 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2239 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2240 break;
2241 }
2242
2243 if (SpecialTypes.size() != Record.size()) {
2244 Error("invalid special-types record");
2245 return true;
2246 }
2247
2248 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2249 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2250 if (!SpecialTypes[I])
2251 SpecialTypes[I] = ID;
2252 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2253 // merge step?
2254 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002255 break;
2256
2257 case STATISTICS:
2258 TotalNumStatements += Record[0];
2259 TotalNumMacros += Record[1];
2260 TotalLexicalDeclContexts += Record[2];
2261 TotalVisibleDeclContexts += Record[3];
2262 break;
2263
2264 case UNUSED_FILESCOPED_DECLS:
2265 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2266 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2267 break;
2268
2269 case DELEGATING_CTORS:
2270 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2271 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2272 break;
2273
2274 case WEAK_UNDECLARED_IDENTIFIERS:
2275 if (Record.size() % 4 != 0) {
2276 Error("invalid weak identifiers record");
2277 return true;
2278 }
2279
2280 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2281 // files. This isn't the way to do it :)
2282 WeakUndeclaredIdentifiers.clear();
2283
2284 // Translate the weak, undeclared identifiers into global IDs.
2285 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2286 WeakUndeclaredIdentifiers.push_back(
2287 getGlobalIdentifierID(F, Record[I++]));
2288 WeakUndeclaredIdentifiers.push_back(
2289 getGlobalIdentifierID(F, Record[I++]));
2290 WeakUndeclaredIdentifiers.push_back(
2291 ReadSourceLocation(F, Record, I).getRawEncoding());
2292 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2293 }
2294 break;
2295
Richard Smith5ea6ef42013-01-10 23:43:47 +00002296 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002297 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith5ea6ef42013-01-10 23:43:47 +00002298 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002299 break;
2300
2301 case SELECTOR_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002302 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002303 F.LocalNumSelectors = Record[0];
2304 unsigned LocalBaseSelectorID = Record[1];
2305 F.BaseSelectorID = getTotalNumSelectors();
2306
2307 if (F.LocalNumSelectors > 0) {
2308 // Introduce the global -> local mapping for selectors within this
2309 // module.
2310 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2311
2312 // Introduce the local -> global mapping for selectors within this
2313 // module.
2314 F.SelectorRemap.insertOrReplace(
2315 std::make_pair(LocalBaseSelectorID,
2316 F.BaseSelectorID - LocalBaseSelectorID));
2317
2318 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2319 }
2320 break;
2321 }
2322
2323 case METHOD_POOL:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002324 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002325 if (Record[0])
2326 F.SelectorLookupTable
2327 = ASTSelectorLookupTable::Create(
2328 F.SelectorLookupTableData + Record[0],
2329 F.SelectorLookupTableData,
2330 ASTSelectorLookupTrait(*this, F));
2331 TotalNumMethodPoolEntries += Record[1];
2332 break;
2333
2334 case REFERENCED_SELECTOR_POOL:
2335 if (!Record.empty()) {
2336 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2337 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2338 Record[Idx++]));
2339 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2340 getRawEncoding());
2341 }
2342 }
2343 break;
2344
2345 case PP_COUNTER_VALUE:
2346 if (!Record.empty() && Listener)
2347 Listener->ReadCounter(F, Record[0]);
2348 break;
2349
2350 case FILE_SORTED_DECLS:
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002351 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002352 F.NumFileSortedDecls = Record[0];
2353 break;
2354
2355 case SOURCE_LOCATION_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002356 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002357 F.LocalNumSLocEntries = Record[0];
2358 unsigned SLocSpaceSize = Record[1];
2359 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2360 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2361 SLocSpaceSize);
2362 // Make our entry in the range map. BaseID is negative and growing, so
2363 // we invert it. Because we invert it, though, we need the other end of
2364 // the range.
2365 unsigned RangeStart =
2366 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2367 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2368 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2369
2370 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2371 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2372 GlobalSLocOffsetMap.insert(
2373 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2374 - SLocSpaceSize,&F));
2375
2376 // Initialize the remapping table.
2377 // Invalid stays invalid.
2378 F.SLocRemap.insert(std::make_pair(0U, 0));
2379 // This module. Base was 2 when being compiled.
2380 F.SLocRemap.insert(std::make_pair(2U,
2381 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2382
2383 TotalNumSLocEntries += F.LocalNumSLocEntries;
2384 break;
2385 }
2386
2387 case MODULE_OFFSET_MAP: {
2388 // Additional remapping information.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002389 const unsigned char *Data = (const unsigned char*)Blob.data();
2390 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002391
2392 // Continuous range maps we may be updating in our module.
2393 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2394 ContinuousRangeMap<uint32_t, int, 2>::Builder
2395 IdentifierRemap(F.IdentifierRemap);
2396 ContinuousRangeMap<uint32_t, int, 2>::Builder
2397 MacroRemap(F.MacroRemap);
2398 ContinuousRangeMap<uint32_t, int, 2>::Builder
2399 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2400 ContinuousRangeMap<uint32_t, int, 2>::Builder
2401 SubmoduleRemap(F.SubmoduleRemap);
2402 ContinuousRangeMap<uint32_t, int, 2>::Builder
2403 SelectorRemap(F.SelectorRemap);
2404 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2405 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2406
2407 while(Data < DataEnd) {
2408 uint16_t Len = io::ReadUnalignedLE16(Data);
2409 StringRef Name = StringRef((const char*)Data, Len);
2410 Data += Len;
2411 ModuleFile *OM = ModuleMgr.lookup(Name);
2412 if (!OM) {
2413 Error("SourceLocation remap refers to unknown module");
2414 return true;
2415 }
2416
2417 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2418 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2419 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2420 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2421 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2422 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2423 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2424 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2425
2426 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2427 SLocRemap.insert(std::make_pair(SLocOffset,
2428 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2429 IdentifierRemap.insert(
2430 std::make_pair(IdentifierIDOffset,
2431 OM->BaseIdentifierID - IdentifierIDOffset));
2432 MacroRemap.insert(std::make_pair(MacroIDOffset,
2433 OM->BaseMacroID - MacroIDOffset));
2434 PreprocessedEntityRemap.insert(
2435 std::make_pair(PreprocessedEntityIDOffset,
2436 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2437 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2438 OM->BaseSubmoduleID - SubmoduleIDOffset));
2439 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2440 OM->BaseSelectorID - SelectorIDOffset));
2441 DeclRemap.insert(std::make_pair(DeclIDOffset,
2442 OM->BaseDeclID - DeclIDOffset));
2443
2444 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2445 OM->BaseTypeIndex - TypeIndexOffset));
2446
2447 // Global -> local mappings.
2448 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2449 }
2450 break;
2451 }
2452
2453 case SOURCE_MANAGER_LINE_TABLE:
2454 if (ParseLineTable(F, Record))
2455 return true;
2456 break;
2457
2458 case SOURCE_LOCATION_PRELOADS: {
2459 // Need to transform from the local view (1-based IDs) to the global view,
2460 // which is based off F.SLocEntryBaseID.
2461 if (!F.PreloadSLocEntries.empty()) {
2462 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2463 return true;
2464 }
2465
2466 F.PreloadSLocEntries.swap(Record);
2467 break;
2468 }
2469
2470 case EXT_VECTOR_DECLS:
2471 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2472 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2473 break;
2474
2475 case VTABLE_USES:
2476 if (Record.size() % 3 != 0) {
2477 Error("Invalid VTABLE_USES record");
2478 return true;
2479 }
2480
2481 // Later tables overwrite earlier ones.
2482 // FIXME: Modules will have some trouble with this. This is clearly not
2483 // the right way to do this.
2484 VTableUses.clear();
2485
2486 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2487 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2488 VTableUses.push_back(
2489 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2490 VTableUses.push_back(Record[Idx++]);
2491 }
2492 break;
2493
2494 case DYNAMIC_CLASSES:
2495 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2496 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2497 break;
2498
2499 case PENDING_IMPLICIT_INSTANTIATIONS:
2500 if (PendingInstantiations.size() % 2 != 0) {
2501 Error("Invalid existing PendingInstantiations");
2502 return true;
2503 }
2504
2505 if (Record.size() % 2 != 0) {
2506 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2507 return true;
2508 }
2509
2510 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2511 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2512 PendingInstantiations.push_back(
2513 ReadSourceLocation(F, Record, I).getRawEncoding());
2514 }
2515 break;
2516
2517 case SEMA_DECL_REFS:
2518 // Later tables overwrite earlier ones.
2519 // FIXME: Modules will have some trouble with this.
2520 SemaDeclRefs.clear();
2521 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2522 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2523 break;
2524
2525 case PPD_ENTITIES_OFFSETS: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002526 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2527 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2528 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002529
2530 unsigned LocalBasePreprocessedEntityID = Record[0];
2531
2532 unsigned StartingID;
2533 if (!PP.getPreprocessingRecord())
2534 PP.createPreprocessingRecord();
2535 if (!PP.getPreprocessingRecord()->getExternalSource())
2536 PP.getPreprocessingRecord()->SetExternalSource(*this);
2537 StartingID
2538 = PP.getPreprocessingRecord()
2539 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2540 F.BasePreprocessedEntityID = StartingID;
2541
2542 if (F.NumPreprocessedEntities > 0) {
2543 // Introduce the global -> local mapping for preprocessed entities in
2544 // this module.
2545 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2546
2547 // Introduce the local -> global mapping for preprocessed entities in
2548 // this module.
2549 F.PreprocessedEntityRemap.insertOrReplace(
2550 std::make_pair(LocalBasePreprocessedEntityID,
2551 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2552 }
2553
2554 break;
2555 }
2556
2557 case DECL_UPDATE_OFFSETS: {
2558 if (Record.size() % 2 != 0) {
2559 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2560 return true;
2561 }
2562 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2563 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2564 .push_back(std::make_pair(&F, Record[I+1]));
2565 break;
2566 }
2567
2568 case DECL_REPLACEMENTS: {
2569 if (Record.size() % 3 != 0) {
2570 Error("invalid DECL_REPLACEMENTS block in AST file");
2571 return true;
2572 }
2573 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2574 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2575 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2576 break;
2577 }
2578
2579 case OBJC_CATEGORIES_MAP: {
2580 if (F.LocalNumObjCCategoriesInMap != 0) {
2581 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2582 return true;
2583 }
2584
2585 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002586 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002587 break;
2588 }
2589
2590 case OBJC_CATEGORIES:
2591 F.ObjCCategories.swap(Record);
2592 break;
2593
2594 case CXX_BASE_SPECIFIER_OFFSETS: {
2595 if (F.LocalNumCXXBaseSpecifiers != 0) {
2596 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2597 return true;
2598 }
2599
2600 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002601 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002602 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2603 break;
2604 }
2605
2606 case DIAG_PRAGMA_MAPPINGS:
2607 if (F.PragmaDiagMappings.empty())
2608 F.PragmaDiagMappings.swap(Record);
2609 else
2610 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2611 Record.begin(), Record.end());
2612 break;
2613
2614 case CUDA_SPECIAL_DECL_REFS:
2615 // Later tables overwrite earlier ones.
2616 // FIXME: Modules will have trouble with this.
2617 CUDASpecialDeclRefs.clear();
2618 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2619 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2620 break;
2621
2622 case HEADER_SEARCH_TABLE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002623 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002624 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002625 if (Record[0]) {
2626 F.HeaderFileInfoTable
2627 = HeaderFileInfoLookupTable::Create(
2628 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2629 (const unsigned char *)F.HeaderFileInfoTableData,
2630 HeaderFileInfoTrait(*this, F,
2631 &PP.getHeaderSearchInfo(),
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002632 Blob.data() + Record[2]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002633
2634 PP.getHeaderSearchInfo().SetExternalSource(this);
2635 if (!PP.getHeaderSearchInfo().getExternalLookup())
2636 PP.getHeaderSearchInfo().SetExternalLookup(this);
2637 }
2638 break;
2639 }
2640
2641 case FP_PRAGMA_OPTIONS:
2642 // Later tables overwrite earlier ones.
2643 FPPragmaOptions.swap(Record);
2644 break;
2645
2646 case OPENCL_EXTENSIONS:
2647 // Later tables overwrite earlier ones.
2648 OpenCLExtensions.swap(Record);
2649 break;
2650
2651 case TENTATIVE_DEFINITIONS:
2652 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2653 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2654 break;
2655
2656 case KNOWN_NAMESPACES:
2657 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2658 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2659 break;
Nick Lewycky01a41142013-01-26 00:35:08 +00002660
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002661 case UNDEFINED_BUT_USED:
2662 if (UndefinedButUsed.size() % 2 != 0) {
2663 Error("Invalid existing UndefinedButUsed");
Nick Lewycky01a41142013-01-26 00:35:08 +00002664 return true;
2665 }
2666
2667 if (Record.size() % 2 != 0) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002668 Error("invalid undefined-but-used record");
Nick Lewycky01a41142013-01-26 00:35:08 +00002669 return true;
2670 }
2671 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00002672 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2673 UndefinedButUsed.push_back(
Nick Lewycky01a41142013-01-26 00:35:08 +00002674 ReadSourceLocation(F, Record, I).getRawEncoding());
2675 }
2676 break;
2677
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002678 case IMPORTED_MODULES: {
2679 if (F.Kind != MK_Module) {
2680 // If we aren't loading a module (which has its own exports), make
2681 // all of the imported modules visible.
2682 // FIXME: Deal with macros-only imports.
2683 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2684 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2685 ImportedModules.push_back(GlobalID);
2686 }
2687 }
2688 break;
2689 }
2690
2691 case LOCAL_REDECLARATIONS: {
2692 F.RedeclarationChains.swap(Record);
2693 break;
2694 }
2695
2696 case LOCAL_REDECLARATIONS_MAP: {
2697 if (F.LocalNumRedeclarationsInMap != 0) {
2698 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2699 return true;
2700 }
2701
2702 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002703 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002704 break;
2705 }
2706
2707 case MERGED_DECLARATIONS: {
2708 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2709 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2710 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2711 for (unsigned N = Record[Idx++]; N > 0; --N)
2712 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2713 }
2714 break;
2715 }
2716
2717 case MACRO_OFFSET: {
2718 if (F.LocalNumMacros != 0) {
2719 Error("duplicate MACRO_OFFSET record in AST file");
2720 return true;
2721 }
Chris Lattnerb3ce3572013-01-20 02:38:54 +00002722 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002723 F.LocalNumMacros = Record[0];
2724 unsigned LocalBaseMacroID = Record[1];
2725 F.BaseMacroID = getTotalNumMacros();
2726
2727 if (F.LocalNumMacros > 0) {
2728 // Introduce the global -> local mapping for macros within this module.
2729 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2730
2731 // Introduce the local -> global mapping for macros within this module.
2732 F.MacroRemap.insertOrReplace(
2733 std::make_pair(LocalBaseMacroID,
2734 F.BaseMacroID - LocalBaseMacroID));
2735
2736 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2737 }
2738 break;
2739 }
2740
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002741 case MACRO_TABLE: {
2742 // FIXME: Not used yet.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002743 break;
2744 }
2745 }
2746 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002747}
2748
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002749/// \brief Move the given method to the back of the global list of methods.
2750static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2751 // Find the entry for this selector in the method pool.
2752 Sema::GlobalMethodPool::iterator Known
2753 = S.MethodPool.find(Method->getSelector());
2754 if (Known == S.MethodPool.end())
2755 return;
2756
2757 // Retrieve the appropriate method list.
2758 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2759 : Known->second.second;
2760 bool Found = false;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002761 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002762 if (!Found) {
2763 if (List->Method == Method) {
2764 Found = true;
2765 } else {
2766 // Keep searching.
2767 continue;
2768 }
2769 }
2770
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002771 if (List->getNext())
2772 List->Method = List->getNext()->Method;
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002773 else
2774 List->Method = Method;
2775 }
2776}
2777
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002778void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002779 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2780 switch (Names[I].getKind()) {
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002781 case HiddenName::Declaration: {
2782 Decl *D = Names[I].getDecl();
2783 bool wasHidden = D->Hidden;
2784 D->Hidden = false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002785
Douglas Gregor2cbd4272013-02-12 23:36:21 +00002786 if (wasHidden && SemaObj) {
2787 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2788 moveMethodToBackOfGlobalList(*SemaObj, Method);
2789 }
2790 }
2791 break;
2792 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002793 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002794 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002795 installImportedMacro(Macro.first, Macro.second, Owner);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002796 break;
2797 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002798 }
2799 }
2800}
2801
2802void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00002803 Module::NameVisibilityKind NameVisibility,
Douglas Gregor906d66a2013-03-20 21:10:35 +00002804 SourceLocation ImportLoc,
2805 bool Complain) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002806 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002807 SmallVector<Module *, 4> Stack;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002808 Stack.push_back(Mod);
2809 while (!Stack.empty()) {
2810 Mod = Stack.back();
2811 Stack.pop_back();
2812
2813 if (NameVisibility <= Mod->NameVisibility) {
2814 // This module already has this level of visibility (or greater), so
2815 // there is nothing more to do.
2816 continue;
2817 }
2818
2819 if (!Mod->isAvailable()) {
2820 // Modules that aren't available cannot be made visible.
2821 continue;
2822 }
2823
2824 // Update the module's name visibility.
2825 Mod->NameVisibility = NameVisibility;
2826
2827 // If we've already deserialized any names from this module,
2828 // mark them as visible.
2829 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2830 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00002831 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002832 HiddenNamesMap.erase(Hidden);
2833 }
2834
2835 // Push any non-explicit submodules onto the stack to be marked as
2836 // visible.
2837 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2838 SubEnd = Mod->submodule_end();
2839 Sub != SubEnd; ++Sub) {
2840 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2841 Stack.push_back(*Sub);
2842 }
2843
2844 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +00002845 SmallVector<Module *, 16> Exports;
2846 Mod->getExportedModules(Exports);
2847 for (SmallVectorImpl<Module *>::iterator
2848 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2849 Module *Exported = *I;
2850 if (Visited.insert(Exported))
2851 Stack.push_back(Exported);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002852 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00002853
2854 // Detect any conflicts.
2855 if (Complain) {
2856 assert(ImportLoc.isValid() && "Missing import location");
2857 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2858 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2859 Diag(ImportLoc, diag::warn_module_conflict)
2860 << Mod->getFullModuleName()
2861 << Mod->Conflicts[I].Other->getFullModuleName()
2862 << Mod->Conflicts[I].Message;
2863 // FIXME: Need note where the other module was imported.
2864 }
2865 }
2866 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002867 }
2868}
2869
Douglas Gregor1a49d972013-01-25 01:03:03 +00002870bool ASTReader::loadGlobalIndex() {
2871 if (GlobalIndex)
2872 return false;
2873
2874 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2875 !Context.getLangOpts().Modules)
2876 return true;
2877
2878 // Try to load the global index.
2879 TriedLoadingGlobalIndex = true;
2880 StringRef ModuleCachePath
2881 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2882 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor677e15f2013-03-19 00:28:20 +00002883 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002884 if (!Result.first)
2885 return true;
2886
2887 GlobalIndex.reset(Result.first);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002888 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregor1a49d972013-01-25 01:03:03 +00002889 return false;
2890}
2891
2892bool ASTReader::isGlobalIndexUnavailable() const {
2893 return Context.getLangOpts().Modules && UseGlobalIndex &&
2894 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2895}
2896
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002897ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2898 ModuleKind Type,
2899 SourceLocation ImportLoc,
2900 unsigned ClientLoadCapabilities) {
2901 // Bump the generation number.
2902 unsigned PreviousGeneration = CurrentGeneration++;
2903
2904 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002905 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002906 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2907 /*ImportedBy=*/0, Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00002908 0, 0,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002909 ClientLoadCapabilities)) {
2910 case Failure:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002911 case Missing:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002912 case OutOfDate:
2913 case VersionMismatch:
2914 case ConfigurationMismatch:
2915 case HadErrors:
Douglas Gregor677e15f2013-03-19 00:28:20 +00002916 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2917 Context.getLangOpts().Modules
2918 ? &PP.getHeaderSearchInfo().getModuleMap()
2919 : 0);
Douglas Gregor1a49d972013-01-25 01:03:03 +00002920
2921 // If we find that any modules are unusable, the global index is going
2922 // to be out-of-date. Just remove it.
2923 GlobalIndex.reset();
Douglas Gregor188bdcd2013-01-25 23:32:03 +00002924 ModuleMgr.setGlobalIndex(0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002925 return ReadResult;
2926
2927 case Success:
2928 break;
2929 }
2930
2931 // Here comes stuff that we only do once the entire chain is loaded.
2932
2933 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002934 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2935 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002936 M != MEnd; ++M) {
2937 ModuleFile &F = *M->Mod;
2938
2939 // Read the AST block.
2940 if (ReadASTBlock(F))
2941 return Failure;
2942
2943 // Once read, set the ModuleFile bit base offset and update the size in
2944 // bits of all files we've seen.
2945 F.GlobalBitOffset = TotalModulesSizeInBits;
2946 TotalModulesSizeInBits += F.SizeInBits;
2947 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2948
2949 // Preload SLocEntries.
2950 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2951 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2952 // Load it through the SourceManager and don't call ReadSLocEntry()
2953 // directly because the entry may have already been loaded in which case
2954 // calling ReadSLocEntry() directly would trigger an assertion in
2955 // SourceManager.
2956 SourceMgr.getLoadedSLocEntryByID(Index);
2957 }
2958 }
2959
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002960 // Setup the import locations and notify the module manager that we've
2961 // committed to these module files.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00002962 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2963 MEnd = Loaded.end();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002964 M != MEnd; ++M) {
2965 ModuleFile &F = *M->Mod;
Douglas Gregorfa69fc12013-03-22 18:50:14 +00002966
2967 ModuleMgr.moduleFileAccepted(&F);
2968
2969 // Set the import location.
Argyrios Kyrtzidis8b136d82013-02-01 16:36:14 +00002970 F.DirectImportLoc = ImportLoc;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002971 if (!M->ImportedBy)
2972 F.ImportLoc = M->ImportLoc;
2973 else
2974 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2975 M->ImportLoc.getRawEncoding());
2976 }
2977
2978 // Mark all of the identifiers in the identifier table as being out of date,
2979 // so that various accessors know to check the loaded modules when the
2980 // identifier is used.
2981 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2982 IdEnd = PP.getIdentifierTable().end();
2983 Id != IdEnd; ++Id)
2984 Id->second->setOutOfDate(true);
2985
2986 // Resolve any unresolved module exports.
Douglas Gregor906d66a2013-03-20 21:10:35 +00002987 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2988 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00002989 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2990 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregor906d66a2013-03-20 21:10:35 +00002991
2992 switch (Unresolved.Kind) {
2993 case UnresolvedModuleRef::Conflict:
2994 if (ResolvedMod) {
2995 Module::Conflict Conflict;
2996 Conflict.Other = ResolvedMod;
2997 Conflict.Message = Unresolved.String.str();
2998 Unresolved.Mod->Conflicts.push_back(Conflict);
2999 }
3000 continue;
3001
3002 case UnresolvedModuleRef::Import:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003003 if (ResolvedMod)
3004 Unresolved.Mod->Imports.push_back(ResolvedMod);
3005 continue;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003006
Douglas Gregor906d66a2013-03-20 21:10:35 +00003007 case UnresolvedModuleRef::Export:
3008 if (ResolvedMod || Unresolved.IsWildcard)
3009 Unresolved.Mod->Exports.push_back(
3010 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3011 continue;
3012 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003013 }
Douglas Gregor906d66a2013-03-20 21:10:35 +00003014 UnresolvedModuleRefs.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003015
3016 InitializeContext();
3017
3018 if (DeserializationListener)
3019 DeserializationListener->ReaderInitialized(this);
3020
3021 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3022 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3023 PrimaryModule.OriginalSourceFileID
3024 = FileID::get(PrimaryModule.SLocEntryBaseID
3025 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3026
3027 // If this AST file is a precompiled preamble, then set the
3028 // preamble file ID of the source manager to the file source file
3029 // from which the preamble was built.
3030 if (Type == MK_Preamble) {
3031 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3032 } else if (Type == MK_MainFile) {
3033 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3034 }
3035 }
3036
3037 // For any Objective-C class definitions we have already loaded, make sure
3038 // that we load any additional categories.
3039 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3040 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3041 ObjCClassesLoaded[I],
3042 PreviousGeneration);
3043 }
Douglas Gregor1a49d972013-01-25 01:03:03 +00003044
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003045 return Success;
3046}
3047
3048ASTReader::ASTReadResult
3049ASTReader::ReadASTCore(StringRef FileName,
3050 ModuleKind Type,
3051 SourceLocation ImportLoc,
3052 ModuleFile *ImportedBy,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003053 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor677e15f2013-03-19 00:28:20 +00003054 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003055 unsigned ClientLoadCapabilities) {
3056 ModuleFile *M;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003057 std::string ErrorStr;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003058 ModuleManager::AddModuleResult AddResult
3059 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3060 CurrentGeneration, ExpectedSize, ExpectedModTime,
3061 M, ErrorStr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003062
Douglas Gregor677e15f2013-03-19 00:28:20 +00003063 switch (AddResult) {
3064 case ModuleManager::AlreadyLoaded:
3065 return Success;
3066
3067 case ModuleManager::NewlyLoaded:
3068 // Load module file below.
3069 break;
3070
3071 case ModuleManager::Missing:
3072 // The module file was missing; if the client handle handle, that, return
3073 // it.
3074 if (ClientLoadCapabilities & ARR_Missing)
3075 return Missing;
3076
3077 // Otherwise, return an error.
3078 {
3079 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3080 + ErrorStr;
3081 Error(Msg);
3082 }
3083 return Failure;
3084
3085 case ModuleManager::OutOfDate:
3086 // We couldn't load the module file because it is out-of-date. If the
3087 // client can handle out-of-date, return it.
3088 if (ClientLoadCapabilities & ARR_OutOfDate)
3089 return OutOfDate;
3090
3091 // Otherwise, return an error.
3092 {
3093 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3094 + ErrorStr;
3095 Error(Msg);
3096 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003097 return Failure;
3098 }
3099
Douglas Gregor677e15f2013-03-19 00:28:20 +00003100 assert(M && "Missing module file");
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003101
3102 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3103 // module?
3104 if (FileName != "-") {
3105 CurrentDir = llvm::sys::path::parent_path(FileName);
3106 if (CurrentDir.empty()) CurrentDir = ".";
3107 }
3108
3109 ModuleFile &F = *M;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003110 BitstreamCursor &Stream = F.Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003111 Stream.init(F.StreamFile);
3112 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3113
3114 // Sniff for the signature.
3115 if (Stream.Read(8) != 'C' ||
3116 Stream.Read(8) != 'P' ||
3117 Stream.Read(8) != 'C' ||
3118 Stream.Read(8) != 'H') {
3119 Diag(diag::err_not_a_pch_file) << FileName;
3120 return Failure;
3121 }
3122
3123 // This is used for compatibility with older PCH formats.
3124 bool HaveReadControlBlock = false;
3125
Chris Lattner99a5af02013-01-20 00:00:22 +00003126 while (1) {
3127 llvm::BitstreamEntry Entry = Stream.advance();
3128
3129 switch (Entry.Kind) {
3130 case llvm::BitstreamEntry::Error:
3131 case llvm::BitstreamEntry::EndBlock:
3132 case llvm::BitstreamEntry::Record:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003133 Error("invalid record at top-level of AST file");
3134 return Failure;
Chris Lattner99a5af02013-01-20 00:00:22 +00003135
3136 case llvm::BitstreamEntry::SubBlock:
3137 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003138 }
3139
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003140 // We only know the control subblock ID.
Chris Lattner99a5af02013-01-20 00:00:22 +00003141 switch (Entry.ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003142 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3143 if (Stream.ReadBlockInfoBlock()) {
3144 Error("malformed BlockInfoBlock in AST file");
3145 return Failure;
3146 }
3147 break;
3148 case CONTROL_BLOCK_ID:
3149 HaveReadControlBlock = true;
3150 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3151 case Success:
3152 break;
3153
3154 case Failure: return Failure;
Douglas Gregor677e15f2013-03-19 00:28:20 +00003155 case Missing: return Missing;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003156 case OutOfDate: return OutOfDate;
3157 case VersionMismatch: return VersionMismatch;
3158 case ConfigurationMismatch: return ConfigurationMismatch;
3159 case HadErrors: return HadErrors;
3160 }
3161 break;
3162 case AST_BLOCK_ID:
3163 if (!HaveReadControlBlock) {
3164 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3165 Diag(diag::warn_pch_version_too_old);
3166 return VersionMismatch;
3167 }
3168
3169 // Record that we've loaded this module.
3170 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3171 return Success;
3172
3173 default:
3174 if (Stream.SkipBlock()) {
3175 Error("malformed block record in AST file");
3176 return Failure;
3177 }
3178 break;
3179 }
3180 }
3181
3182 return Success;
3183}
3184
3185void ASTReader::InitializeContext() {
3186 // If there's a listener, notify them that we "read" the translation unit.
3187 if (DeserializationListener)
3188 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3189 Context.getTranslationUnitDecl());
3190
3191 // Make sure we load the declaration update records for the translation unit,
3192 // if there are any.
3193 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3194 Context.getTranslationUnitDecl());
3195
3196 // FIXME: Find a better way to deal with collisions between these
3197 // built-in types. Right now, we just ignore the problem.
3198
3199 // Load the special types.
3200 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3201 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3202 if (!Context.CFConstantStringTypeDecl)
3203 Context.setCFConstantStringType(GetType(String));
3204 }
3205
3206 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3207 QualType FileType = GetType(File);
3208 if (FileType.isNull()) {
3209 Error("FILE type is NULL");
3210 return;
3211 }
3212
3213 if (!Context.FILEDecl) {
3214 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3215 Context.setFILEDecl(Typedef->getDecl());
3216 else {
3217 const TagType *Tag = FileType->getAs<TagType>();
3218 if (!Tag) {
3219 Error("Invalid FILE type in AST file");
3220 return;
3221 }
3222 Context.setFILEDecl(Tag->getDecl());
3223 }
3224 }
3225 }
3226
3227 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3228 QualType Jmp_bufType = GetType(Jmp_buf);
3229 if (Jmp_bufType.isNull()) {
3230 Error("jmp_buf type is NULL");
3231 return;
3232 }
3233
3234 if (!Context.jmp_bufDecl) {
3235 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3236 Context.setjmp_bufDecl(Typedef->getDecl());
3237 else {
3238 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3239 if (!Tag) {
3240 Error("Invalid jmp_buf type in AST file");
3241 return;
3242 }
3243 Context.setjmp_bufDecl(Tag->getDecl());
3244 }
3245 }
3246 }
3247
3248 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3249 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3250 if (Sigjmp_bufType.isNull()) {
3251 Error("sigjmp_buf type is NULL");
3252 return;
3253 }
3254
3255 if (!Context.sigjmp_bufDecl) {
3256 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3257 Context.setsigjmp_bufDecl(Typedef->getDecl());
3258 else {
3259 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3260 assert(Tag && "Invalid sigjmp_buf type in AST file");
3261 Context.setsigjmp_bufDecl(Tag->getDecl());
3262 }
3263 }
3264 }
3265
3266 if (unsigned ObjCIdRedef
3267 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3268 if (Context.ObjCIdRedefinitionType.isNull())
3269 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3270 }
3271
3272 if (unsigned ObjCClassRedef
3273 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3274 if (Context.ObjCClassRedefinitionType.isNull())
3275 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3276 }
3277
3278 if (unsigned ObjCSelRedef
3279 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3280 if (Context.ObjCSelRedefinitionType.isNull())
3281 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3282 }
3283
3284 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3285 QualType Ucontext_tType = GetType(Ucontext_t);
3286 if (Ucontext_tType.isNull()) {
3287 Error("ucontext_t type is NULL");
3288 return;
3289 }
3290
3291 if (!Context.ucontext_tDecl) {
3292 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3293 Context.setucontext_tDecl(Typedef->getDecl());
3294 else {
3295 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3296 assert(Tag && "Invalid ucontext_t type in AST file");
3297 Context.setucontext_tDecl(Tag->getDecl());
3298 }
3299 }
3300 }
3301 }
3302
3303 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3304
3305 // If there were any CUDA special declarations, deserialize them.
3306 if (!CUDASpecialDeclRefs.empty()) {
3307 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3308 Context.setcudaConfigureCallDecl(
3309 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3310 }
3311
3312 // Re-export any modules that were imported by a non-module AST file.
3313 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3314 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +00003315 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregor906d66a2013-03-20 21:10:35 +00003316 /*ImportLoc=*/SourceLocation(),
3317 /*Complain=*/false);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003318 }
3319 ImportedModules.clear();
3320}
3321
3322void ASTReader::finalizeForWriting() {
3323 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3324 HiddenEnd = HiddenNamesMap.end();
3325 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis52151fd2013-03-27 01:25:34 +00003326 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003327 }
3328 HiddenNamesMap.clear();
3329}
3330
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003331/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3332/// cursor into the start of the given block ID, returning false on success and
3333/// true on failure.
3334static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003335 while (1) {
3336 llvm::BitstreamEntry Entry = Cursor.advance();
3337 switch (Entry.Kind) {
3338 case llvm::BitstreamEntry::Error:
3339 case llvm::BitstreamEntry::EndBlock:
3340 return true;
3341
3342 case llvm::BitstreamEntry::Record:
3343 // Ignore top-level records.
3344 Cursor.skipRecord(Entry.ID);
3345 break;
3346
3347 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003348 if (Entry.ID == BlockID) {
3349 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003350 return true;
3351 // Found it!
3352 return false;
3353 }
3354
3355 if (Cursor.SkipBlock())
3356 return true;
3357 }
3358 }
3359}
3360
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003361/// \brief Retrieve the name of the original source file name
3362/// directly from the AST file, without actually loading the AST
3363/// file.
3364std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3365 FileManager &FileMgr,
3366 DiagnosticsEngine &Diags) {
3367 // Open the AST file.
3368 std::string ErrStr;
3369 OwningPtr<llvm::MemoryBuffer> Buffer;
3370 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3371 if (!Buffer) {
3372 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3373 return std::string();
3374 }
3375
3376 // Initialize the stream
3377 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003378 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003379 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3380 (const unsigned char *)Buffer->getBufferEnd());
3381 Stream.init(StreamFile);
3382
3383 // Sniff for the signature.
3384 if (Stream.Read(8) != 'C' ||
3385 Stream.Read(8) != 'P' ||
3386 Stream.Read(8) != 'C' ||
3387 Stream.Read(8) != 'H') {
3388 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3389 return std::string();
3390 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003391
Chris Lattner88bde502013-01-19 21:39:22 +00003392 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003393 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003394 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3395 return std::string();
Chris Lattner88bde502013-01-19 21:39:22 +00003396 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003397
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003398 // Scan for ORIGINAL_FILE inside the control block.
3399 RecordData Record;
Chris Lattner88bde502013-01-19 21:39:22 +00003400 while (1) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003401 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattner88bde502013-01-19 21:39:22 +00003402 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3403 return std::string();
3404
3405 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3406 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3407 return std::string();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003408 }
Chris Lattner88bde502013-01-19 21:39:22 +00003409
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003410 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003411 StringRef Blob;
3412 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3413 return Blob.str();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003414 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003415}
3416
3417namespace {
3418 class SimplePCHValidator : public ASTReaderListener {
3419 const LangOptions &ExistingLangOpts;
3420 const TargetOptions &ExistingTargetOpts;
3421 const PreprocessorOptions &ExistingPPOpts;
3422 FileManager &FileMgr;
3423
3424 public:
3425 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3426 const TargetOptions &ExistingTargetOpts,
3427 const PreprocessorOptions &ExistingPPOpts,
3428 FileManager &FileMgr)
3429 : ExistingLangOpts(ExistingLangOpts),
3430 ExistingTargetOpts(ExistingTargetOpts),
3431 ExistingPPOpts(ExistingPPOpts),
3432 FileMgr(FileMgr)
3433 {
3434 }
3435
3436 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3437 bool Complain) {
3438 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3439 }
3440 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3441 bool Complain) {
3442 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3443 }
3444 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3445 bool Complain,
3446 std::string &SuggestedPredefines) {
3447 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00003448 SuggestedPredefines, ExistingLangOpts);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003449 }
3450 };
3451}
3452
3453bool ASTReader::readASTFileControlBlock(StringRef Filename,
3454 FileManager &FileMgr,
3455 ASTReaderListener &Listener) {
3456 // Open the AST file.
3457 std::string ErrStr;
3458 OwningPtr<llvm::MemoryBuffer> Buffer;
3459 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3460 if (!Buffer) {
3461 return true;
3462 }
3463
3464 // Initialize the stream
3465 llvm::BitstreamReader StreamFile;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003466 BitstreamCursor Stream;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003467 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3468 (const unsigned char *)Buffer->getBufferEnd());
3469 Stream.init(StreamFile);
3470
3471 // Sniff for the signature.
3472 if (Stream.Read(8) != 'C' ||
3473 Stream.Read(8) != 'P' ||
3474 Stream.Read(8) != 'C' ||
3475 Stream.Read(8) != 'H') {
3476 return true;
3477 }
3478
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003479 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003480 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003481 return true;
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003482
3483 bool NeedsInputFiles = Listener.needsInputFileVisitation();
3484 BitstreamCursor InputFilesCursor;
3485 if (NeedsInputFiles) {
3486 InputFilesCursor = Stream;
3487 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3488 return true;
3489
3490 // Read the abbreviations
3491 while (true) {
3492 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3493 unsigned Code = InputFilesCursor.ReadCode();
3494
3495 // We expect all abbrevs to be at the start of the block.
3496 if (Code != llvm::bitc::DEFINE_ABBREV) {
3497 InputFilesCursor.JumpToBit(Offset);
3498 break;
3499 }
3500 InputFilesCursor.ReadAbbrevRecord();
3501 }
3502 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003503
3504 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003505 RecordData Record;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003506 while (1) {
3507 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3508 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3509 return false;
3510
3511 if (Entry.Kind != llvm::BitstreamEntry::Record)
3512 return true;
3513
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003514 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003515 StringRef Blob;
3516 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003517 switch ((ControlRecordTypes)RecCode) {
3518 case METADATA: {
3519 if (Record[0] != VERSION_MAJOR)
3520 return true;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003521
Douglas Gregorc544ba02013-03-27 16:47:18 +00003522 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003523 return true;
Douglas Gregorc544ba02013-03-27 16:47:18 +00003524
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003525 break;
3526 }
3527 case LANGUAGE_OPTIONS:
3528 if (ParseLanguageOptions(Record, false, Listener))
3529 return true;
3530 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003531
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003532 case TARGET_OPTIONS:
3533 if (ParseTargetOptions(Record, false, Listener))
3534 return true;
3535 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003536
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003537 case DIAGNOSTIC_OPTIONS:
3538 if (ParseDiagnosticOptions(Record, false, Listener))
3539 return true;
3540 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003541
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003542 case FILE_SYSTEM_OPTIONS:
3543 if (ParseFileSystemOptions(Record, false, Listener))
3544 return true;
3545 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003546
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003547 case HEADER_SEARCH_OPTIONS:
3548 if (ParseHeaderSearchOptions(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 PREPROCESSOR_OPTIONS: {
3553 std::string IgnoredSuggestedPredefines;
3554 if (ParsePreprocessorOptions(Record, false, Listener,
3555 IgnoredSuggestedPredefines))
3556 return true;
3557 break;
3558 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003559
Argyrios Kyrtzidisbdfdb1d2013-05-06 19:23:40 +00003560 case INPUT_FILE_OFFSETS: {
3561 if (!NeedsInputFiles)
3562 break;
3563
3564 unsigned NumInputFiles = Record[0];
3565 unsigned NumUserFiles = Record[1];
3566 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3567 for (unsigned I = 0; I != NumInputFiles; ++I) {
3568 // Go find this input file.
3569 bool isSystemFile = I >= NumUserFiles;
3570 BitstreamCursor &Cursor = InputFilesCursor;
3571 SavedStreamPosition SavedPosition(Cursor);
3572 Cursor.JumpToBit(InputFileOffs[I]);
3573
3574 unsigned Code = Cursor.ReadCode();
3575 RecordData Record;
3576 StringRef Blob;
3577 bool shouldContinue = false;
3578 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3579 case INPUT_FILE:
3580 shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3581 break;
3582 }
3583 if (!shouldContinue)
3584 break;
3585 }
3586 break;
3587 }
3588
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003589 default:
3590 // No other validation to perform.
3591 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003592 }
3593 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003594}
3595
3596
3597bool ASTReader::isAcceptableASTFile(StringRef Filename,
3598 FileManager &FileMgr,
3599 const LangOptions &LangOpts,
3600 const TargetOptions &TargetOpts,
3601 const PreprocessorOptions &PPOpts) {
3602 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3603 return !readASTFileControlBlock(Filename, FileMgr, validator);
3604}
3605
3606bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3607 // Enter the submodule block.
3608 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3609 Error("malformed submodule block record in AST file");
3610 return true;
3611 }
3612
3613 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3614 bool First = true;
3615 Module *CurrentModule = 0;
3616 RecordData Record;
3617 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003618 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3619
3620 switch (Entry.Kind) {
3621 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3622 case llvm::BitstreamEntry::Error:
3623 Error("malformed block record in AST file");
3624 return true;
3625 case llvm::BitstreamEntry::EndBlock:
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003626 return false;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003627 case llvm::BitstreamEntry::Record:
3628 // The interesting case.
3629 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003630 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00003631
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003632 // Read a record.
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003633 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003634 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003635 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003636 default: // Default behavior: ignore.
3637 break;
3638
3639 case SUBMODULE_DEFINITION: {
3640 if (First) {
3641 Error("missing submodule metadata record at beginning of block");
3642 return true;
3643 }
3644
Douglas Gregor970e4412013-03-20 03:59:18 +00003645 if (Record.size() < 8) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003646 Error("malformed module definition");
3647 return true;
3648 }
3649
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003650 StringRef Name = Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003651 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3652 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3653 bool IsFramework = Record[2];
3654 bool IsExplicit = Record[3];
3655 bool IsSystem = Record[4];
3656 bool InferSubmodules = Record[5];
3657 bool InferExplicitSubmodules = Record[6];
3658 bool InferExportWildcard = Record[7];
Douglas Gregor970e4412013-03-20 03:59:18 +00003659 bool ConfigMacrosExhaustive = Record[8];
3660
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003661 Module *ParentModule = 0;
3662 if (Parent)
3663 ParentModule = getSubmodule(Parent);
3664
3665 // Retrieve this (sub)module from the module map, creating it if
3666 // necessary.
3667 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3668 IsFramework,
3669 IsExplicit).first;
3670 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3671 if (GlobalIndex >= SubmodulesLoaded.size() ||
3672 SubmodulesLoaded[GlobalIndex]) {
3673 Error("too many submodules");
3674 return true;
3675 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003676
Douglas Gregor677e15f2013-03-19 00:28:20 +00003677 if (!ParentModule) {
3678 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3679 if (CurFile != F.File) {
3680 if (!Diags.isDiagnosticInFlight()) {
3681 Diag(diag::err_module_file_conflict)
3682 << CurrentModule->getTopLevelModuleName()
3683 << CurFile->getName()
3684 << F.File->getName();
3685 }
3686 return true;
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003687 }
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003688 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003689
3690 CurrentModule->setASTFile(F.File);
Douglas Gregor8bf778e2013-02-06 22:40:31 +00003691 }
Douglas Gregor677e15f2013-03-19 00:28:20 +00003692
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003693 CurrentModule->IsFromModuleFile = true;
3694 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3695 CurrentModule->InferSubmodules = InferSubmodules;
3696 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3697 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor970e4412013-03-20 03:59:18 +00003698 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003699 if (DeserializationListener)
3700 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3701
3702 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003703
Douglas Gregor906d66a2013-03-20 21:10:35 +00003704 // Clear out data that will be replaced by what is the module file.
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003705 CurrentModule->LinkLibraries.clear();
Douglas Gregor970e4412013-03-20 03:59:18 +00003706 CurrentModule->ConfigMacros.clear();
Douglas Gregor906d66a2013-03-20 21:10:35 +00003707 CurrentModule->UnresolvedConflicts.clear();
3708 CurrentModule->Conflicts.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003709 break;
3710 }
3711
3712 case SUBMODULE_UMBRELLA_HEADER: {
3713 if (First) {
3714 Error("missing submodule metadata record at beginning of block");
3715 return true;
3716 }
3717
3718 if (!CurrentModule)
3719 break;
3720
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003721 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003722 if (!CurrentModule->getUmbrellaHeader())
3723 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3724 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3725 Error("mismatched umbrella headers in submodule");
3726 return true;
3727 }
3728 }
3729 break;
3730 }
3731
3732 case SUBMODULE_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
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003741 // We lazily associate headers with their modules via the HeaderInfoTable.
3742 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3743 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003744 break;
3745 }
3746
3747 case SUBMODULE_EXCLUDED_HEADER: {
3748 if (First) {
3749 Error("missing submodule metadata record at beginning of block");
3750 return true;
3751 }
3752
3753 if (!CurrentModule)
3754 break;
3755
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00003756 // We lazily associate headers with their modules via the HeaderInfoTable.
3757 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3758 // of complete filenames or remove it entirely.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003759 break;
3760 }
3761
3762 case SUBMODULE_TOPHEADER: {
3763 if (First) {
3764 Error("missing submodule metadata record at beginning of block");
3765 return true;
3766 }
3767
3768 if (!CurrentModule)
3769 break;
3770
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00003771 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003772 break;
3773 }
3774
3775 case SUBMODULE_UMBRELLA_DIR: {
3776 if (First) {
3777 Error("missing submodule metadata record at beginning of block");
3778 return true;
3779 }
3780
3781 if (!CurrentModule)
3782 break;
3783
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003784 if (const DirectoryEntry *Umbrella
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003785 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003786 if (!CurrentModule->getUmbrellaDir())
3787 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3788 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3789 Error("mismatched umbrella directories in submodule");
3790 return true;
3791 }
3792 }
3793 break;
3794 }
3795
3796 case SUBMODULE_METADATA: {
3797 if (!First) {
3798 Error("submodule metadata record not at beginning of block");
3799 return true;
3800 }
3801 First = false;
3802
3803 F.BaseSubmoduleID = getTotalNumSubmodules();
3804 F.LocalNumSubmodules = Record[0];
3805 unsigned LocalBaseSubmoduleID = Record[1];
3806 if (F.LocalNumSubmodules > 0) {
3807 // Introduce the global -> local mapping for submodules within this
3808 // module.
3809 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3810
3811 // Introduce the local -> global mapping for submodules within this
3812 // module.
3813 F.SubmoduleRemap.insertOrReplace(
3814 std::make_pair(LocalBaseSubmoduleID,
3815 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3816
3817 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3818 }
3819 break;
3820 }
3821
3822 case SUBMODULE_IMPORTS: {
3823 if (First) {
3824 Error("missing submodule metadata record at beginning of block");
3825 return true;
3826 }
3827
3828 if (!CurrentModule)
3829 break;
3830
3831 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003832 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003833 Unresolved.File = &F;
3834 Unresolved.Mod = CurrentModule;
3835 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003836 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003837 Unresolved.IsWildcard = false;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003838 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003839 }
3840 break;
3841 }
3842
3843 case SUBMODULE_EXPORTS: {
3844 if (First) {
3845 Error("missing submodule metadata record at beginning of block");
3846 return true;
3847 }
3848
3849 if (!CurrentModule)
3850 break;
3851
3852 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor906d66a2013-03-20 21:10:35 +00003853 UnresolvedModuleRef Unresolved;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003854 Unresolved.File = &F;
3855 Unresolved.Mod = CurrentModule;
3856 Unresolved.ID = Record[Idx];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003857 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003858 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregor906d66a2013-03-20 21:10:35 +00003859 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003860 }
3861
3862 // Once we've loaded the set of exports, there's no reason to keep
3863 // the parsed, unresolved exports around.
3864 CurrentModule->UnresolvedExports.clear();
3865 break;
3866 }
3867 case SUBMODULE_REQUIRES: {
3868 if (First) {
3869 Error("missing submodule metadata record at beginning of block");
3870 return true;
3871 }
3872
3873 if (!CurrentModule)
3874 break;
3875
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003876 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003877 Context.getTargetInfo());
3878 break;
3879 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003880
3881 case SUBMODULE_LINK_LIBRARY:
3882 if (First) {
3883 Error("missing submodule metadata record at beginning of block");
3884 return true;
3885 }
3886
3887 if (!CurrentModule)
3888 break;
3889
3890 CurrentModule->LinkLibraries.push_back(
Chris Lattnerb3ce3572013-01-20 02:38:54 +00003891 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregorb6cbe512013-01-14 17:21:00 +00003892 break;
Douglas Gregor63a72682013-03-20 00:22:05 +00003893
3894 case SUBMODULE_CONFIG_MACRO:
3895 if (First) {
3896 Error("missing submodule metadata record at beginning of block");
3897 return true;
3898 }
3899
3900 if (!CurrentModule)
3901 break;
3902
3903 CurrentModule->ConfigMacros.push_back(Blob.str());
3904 break;
Douglas Gregor906d66a2013-03-20 21:10:35 +00003905
3906 case SUBMODULE_CONFLICT: {
3907 if (First) {
3908 Error("missing submodule metadata record at beginning of block");
3909 return true;
3910 }
3911
3912 if (!CurrentModule)
3913 break;
3914
3915 UnresolvedModuleRef Unresolved;
3916 Unresolved.File = &F;
3917 Unresolved.Mod = CurrentModule;
3918 Unresolved.ID = Record[0];
3919 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3920 Unresolved.IsWildcard = false;
3921 Unresolved.String = Blob;
3922 UnresolvedModuleRefs.push_back(Unresolved);
3923 break;
3924 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003925 }
3926 }
3927}
3928
3929/// \brief Parse the record that corresponds to a LangOptions data
3930/// structure.
3931///
3932/// This routine parses the language options from the AST file and then gives
3933/// them to the AST listener if one is set.
3934///
3935/// \returns true if the listener deems the file unacceptable, false otherwise.
3936bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3937 bool Complain,
3938 ASTReaderListener &Listener) {
3939 LangOptions LangOpts;
3940 unsigned Idx = 0;
3941#define LANGOPT(Name, Bits, Default, Description) \
3942 LangOpts.Name = Record[Idx++];
3943#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3944 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3945#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00003946#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3947#include "clang/Basic/Sanitizers.def"
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003948
3949 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3950 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3951 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3952
3953 unsigned Length = Record[Idx++];
3954 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3955 Record.begin() + Idx + Length);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003956
3957 Idx += Length;
3958
3959 // Comment options.
3960 for (unsigned N = Record[Idx++]; N; --N) {
3961 LangOpts.CommentOpts.BlockCommandNames.push_back(
3962 ReadString(Record, Idx));
3963 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00003964 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00003965
Guy Benyei7f92f2d2012-12-18 14:30:41 +00003966 return Listener.ReadLanguageOptions(LangOpts, Complain);
3967}
3968
3969bool ASTReader::ParseTargetOptions(const RecordData &Record,
3970 bool Complain,
3971 ASTReaderListener &Listener) {
3972 unsigned Idx = 0;
3973 TargetOptions TargetOpts;
3974 TargetOpts.Triple = ReadString(Record, Idx);
3975 TargetOpts.CPU = ReadString(Record, Idx);
3976 TargetOpts.ABI = ReadString(Record, Idx);
3977 TargetOpts.CXXABI = ReadString(Record, Idx);
3978 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3979 for (unsigned N = Record[Idx++]; N; --N) {
3980 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3981 }
3982 for (unsigned N = Record[Idx++]; N; --N) {
3983 TargetOpts.Features.push_back(ReadString(Record, Idx));
3984 }
3985
3986 return Listener.ReadTargetOptions(TargetOpts, Complain);
3987}
3988
3989bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3990 ASTReaderListener &Listener) {
3991 DiagnosticOptions DiagOpts;
3992 unsigned Idx = 0;
3993#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3994#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3995 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3996#include "clang/Basic/DiagnosticOptions.def"
3997
3998 for (unsigned N = Record[Idx++]; N; --N) {
3999 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4000 }
4001
4002 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4003}
4004
4005bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4006 ASTReaderListener &Listener) {
4007 FileSystemOptions FSOpts;
4008 unsigned Idx = 0;
4009 FSOpts.WorkingDir = ReadString(Record, Idx);
4010 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4011}
4012
4013bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4014 bool Complain,
4015 ASTReaderListener &Listener) {
4016 HeaderSearchOptions HSOpts;
4017 unsigned Idx = 0;
4018 HSOpts.Sysroot = ReadString(Record, Idx);
4019
4020 // Include entries.
4021 for (unsigned N = Record[Idx++]; N; --N) {
4022 std::string Path = ReadString(Record, Idx);
4023 frontend::IncludeDirGroup Group
4024 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004025 bool IsFramework = Record[Idx++];
4026 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004027 HSOpts.UserEntries.push_back(
Daniel Dunbar59fd6352013-01-30 00:34:26 +00004028 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004029 }
4030
4031 // System header prefixes.
4032 for (unsigned N = Record[Idx++]; N; --N) {
4033 std::string Prefix = ReadString(Record, Idx);
4034 bool IsSystemHeader = Record[Idx++];
4035 HSOpts.SystemHeaderPrefixes.push_back(
4036 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4037 }
4038
4039 HSOpts.ResourceDir = ReadString(Record, Idx);
4040 HSOpts.ModuleCachePath = ReadString(Record, Idx);
4041 HSOpts.DisableModuleHash = Record[Idx++];
4042 HSOpts.UseBuiltinIncludes = Record[Idx++];
4043 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4044 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4045 HSOpts.UseLibcxx = Record[Idx++];
4046
4047 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4048}
4049
4050bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4051 bool Complain,
4052 ASTReaderListener &Listener,
4053 std::string &SuggestedPredefines) {
4054 PreprocessorOptions PPOpts;
4055 unsigned Idx = 0;
4056
4057 // Macro definitions/undefs
4058 for (unsigned N = Record[Idx++]; N; --N) {
4059 std::string Macro = ReadString(Record, Idx);
4060 bool IsUndef = Record[Idx++];
4061 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4062 }
4063
4064 // Includes
4065 for (unsigned N = Record[Idx++]; N; --N) {
4066 PPOpts.Includes.push_back(ReadString(Record, Idx));
4067 }
4068
4069 // Macro Includes
4070 for (unsigned N = Record[Idx++]; N; --N) {
4071 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4072 }
4073
4074 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00004075 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004076 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4077 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4078 PPOpts.ObjCXXARCStandardLibrary =
4079 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4080 SuggestedPredefines.clear();
4081 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4082 SuggestedPredefines);
4083}
4084
4085std::pair<ModuleFile *, unsigned>
4086ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4087 GlobalPreprocessedEntityMapType::iterator
4088 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4089 assert(I != GlobalPreprocessedEntityMap.end() &&
4090 "Corrupted global preprocessed entity map");
4091 ModuleFile *M = I->second;
4092 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4093 return std::make_pair(M, LocalIndex);
4094}
4095
4096std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4097ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4098 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4099 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4100 Mod.NumPreprocessedEntities);
4101
4102 return std::make_pair(PreprocessingRecord::iterator(),
4103 PreprocessingRecord::iterator());
4104}
4105
4106std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4107ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4108 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4109 ModuleDeclIterator(this, &Mod,
4110 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4111}
4112
4113PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4114 PreprocessedEntityID PPID = Index+1;
4115 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4116 ModuleFile &M = *PPInfo.first;
4117 unsigned LocalIndex = PPInfo.second;
4118 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4119
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004120 if (!PP.getPreprocessingRecord()) {
4121 Error("no preprocessing record");
4122 return 0;
4123 }
4124
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004125 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4126 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4127
4128 llvm::BitstreamEntry Entry =
4129 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4130 if (Entry.Kind != llvm::BitstreamEntry::Record)
4131 return 0;
4132
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004133 // Read the record.
4134 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4135 ReadSourceLocation(M, PPOffs.End));
4136 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004137 StringRef Blob;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004138 RecordData Record;
4139 PreprocessorDetailRecordTypes RecType =
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004140 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4141 Entry.ID, Record, &Blob);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004142 switch (RecType) {
4143 case PPD_MACRO_EXPANSION: {
4144 bool isBuiltin = Record[0];
4145 IdentifierInfo *Name = 0;
4146 MacroDefinition *Def = 0;
4147 if (isBuiltin)
4148 Name = getLocalIdentifier(M, Record[1]);
4149 else {
4150 PreprocessedEntityID
4151 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4152 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4153 }
4154
4155 MacroExpansion *ME;
4156 if (isBuiltin)
4157 ME = new (PPRec) MacroExpansion(Name, Range);
4158 else
4159 ME = new (PPRec) MacroExpansion(Def, Range);
4160
4161 return ME;
4162 }
4163
4164 case PPD_MACRO_DEFINITION: {
4165 // Decode the identifier info and then check again; if the macro is
4166 // still defined and associated with the identifier,
4167 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4168 MacroDefinition *MD
4169 = new (PPRec) MacroDefinition(II, Range);
4170
4171 if (DeserializationListener)
4172 DeserializationListener->MacroDefinitionRead(PPID, MD);
4173
4174 return MD;
4175 }
4176
4177 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004178 const char *FullFileNameStart = Blob.data() + Record[0];
4179 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004180 const FileEntry *File = 0;
4181 if (!FullFileName.empty())
4182 File = PP.getFileManager().getFile(FullFileName);
4183
4184 // FIXME: Stable encoding
4185 InclusionDirective::InclusionKind Kind
4186 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4187 InclusionDirective *ID
4188 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004189 StringRef(Blob.data(), Record[0]),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004190 Record[1], Record[3],
4191 File,
4192 Range);
4193 return ID;
4194 }
4195 }
4196
4197 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4198}
4199
4200/// \brief \arg SLocMapI points at a chunk of a module that contains no
4201/// preprocessed entities or the entities it contains are not the ones we are
4202/// looking for. Find the next module that contains entities and return the ID
4203/// of the first entry.
4204PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4205 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4206 ++SLocMapI;
4207 for (GlobalSLocOffsetMapType::const_iterator
4208 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4209 ModuleFile &M = *SLocMapI->second;
4210 if (M.NumPreprocessedEntities)
4211 return M.BasePreprocessedEntityID;
4212 }
4213
4214 return getTotalNumPreprocessedEntities();
4215}
4216
4217namespace {
4218
4219template <unsigned PPEntityOffset::*PPLoc>
4220struct PPEntityComp {
4221 const ASTReader &Reader;
4222 ModuleFile &M;
4223
4224 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4225
4226 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4227 SourceLocation LHS = getLoc(L);
4228 SourceLocation RHS = getLoc(R);
4229 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4230 }
4231
4232 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4233 SourceLocation LHS = getLoc(L);
4234 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4235 }
4236
4237 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4238 SourceLocation RHS = getLoc(R);
4239 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4240 }
4241
4242 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4243 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4244 }
4245};
4246
4247}
4248
4249/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4250PreprocessedEntityID
4251ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4252 if (SourceMgr.isLocalSourceLocation(BLoc))
4253 return getTotalNumPreprocessedEntities();
4254
4255 GlobalSLocOffsetMapType::const_iterator
4256 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004257 BLoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004258 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4259 "Corrupted global sloc offset map");
4260
4261 if (SLocMapI->second->NumPreprocessedEntities == 0)
4262 return findNextPreprocessedEntity(SLocMapI);
4263
4264 ModuleFile &M = *SLocMapI->second;
4265 typedef const PPEntityOffset *pp_iterator;
4266 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4267 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4268
4269 size_t Count = M.NumPreprocessedEntities;
4270 size_t Half;
4271 pp_iterator First = pp_begin;
4272 pp_iterator PPI;
4273
4274 // Do a binary search manually instead of using std::lower_bound because
4275 // The end locations of entities may be unordered (when a macro expansion
4276 // is inside another macro argument), but for this case it is not important
4277 // whether we get the first macro expansion or its containing macro.
4278 while (Count > 0) {
4279 Half = Count/2;
4280 PPI = First;
4281 std::advance(PPI, Half);
4282 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4283 BLoc)){
4284 First = PPI;
4285 ++First;
4286 Count = Count - Half - 1;
4287 } else
4288 Count = Half;
4289 }
4290
4291 if (PPI == pp_end)
4292 return findNextPreprocessedEntity(SLocMapI);
4293
4294 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4295}
4296
4297/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4298PreprocessedEntityID
4299ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4300 if (SourceMgr.isLocalSourceLocation(ELoc))
4301 return getTotalNumPreprocessedEntities();
4302
4303 GlobalSLocOffsetMapType::const_iterator
4304 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00004305 ELoc.getOffset() - 1);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004306 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4307 "Corrupted global sloc offset map");
4308
4309 if (SLocMapI->second->NumPreprocessedEntities == 0)
4310 return findNextPreprocessedEntity(SLocMapI);
4311
4312 ModuleFile &M = *SLocMapI->second;
4313 typedef const PPEntityOffset *pp_iterator;
4314 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4315 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4316 pp_iterator PPI =
4317 std::upper_bound(pp_begin, pp_end, ELoc,
4318 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4319
4320 if (PPI == pp_end)
4321 return findNextPreprocessedEntity(SLocMapI);
4322
4323 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4324}
4325
4326/// \brief Returns a pair of [Begin, End) indices of preallocated
4327/// preprocessed entities that \arg Range encompasses.
4328std::pair<unsigned, unsigned>
4329 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4330 if (Range.isInvalid())
4331 return std::make_pair(0,0);
4332 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4333
4334 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4335 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4336 return std::make_pair(BeginID, EndID);
4337}
4338
4339/// \brief Optionally returns true or false if the preallocated preprocessed
4340/// entity with index \arg Index came from file \arg FID.
David Blaikiedc84cd52013-02-20 22:23:23 +00004341Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004342 FileID FID) {
4343 if (FID.isInvalid())
4344 return false;
4345
4346 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4347 ModuleFile &M = *PPInfo.first;
4348 unsigned LocalIndex = PPInfo.second;
4349 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4350
4351 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4352 if (Loc.isInvalid())
4353 return false;
4354
4355 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4356 return true;
4357 else
4358 return false;
4359}
4360
4361namespace {
4362 /// \brief Visitor used to search for information about a header file.
4363 class HeaderFileInfoVisitor {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004364 const FileEntry *FE;
4365
David Blaikiedc84cd52013-02-20 22:23:23 +00004366 Optional<HeaderFileInfo> HFI;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004367
4368 public:
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004369 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4370 : FE(FE) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004371
4372 static bool visit(ModuleFile &M, void *UserData) {
4373 HeaderFileInfoVisitor *This
4374 = static_cast<HeaderFileInfoVisitor *>(UserData);
4375
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004376 HeaderFileInfoLookupTable *Table
4377 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4378 if (!Table)
4379 return false;
4380
4381 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00004382 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004383 if (Pos == Table->end())
4384 return false;
4385
4386 This->HFI = *Pos;
4387 return true;
4388 }
4389
David Blaikiedc84cd52013-02-20 22:23:23 +00004390 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004391 };
4392}
4393
4394HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis36592b12013-03-06 18:12:44 +00004395 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004396 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
David Blaikiedc84cd52013-02-20 22:23:23 +00004397 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004398 if (Listener)
4399 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4400 return *HFI;
4401 }
4402
4403 return HeaderFileInfo();
4404}
4405
4406void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4407 // FIXME: Make it work properly with modules.
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00004408 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004409 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4410 ModuleFile &F = *(*I);
4411 unsigned Idx = 0;
4412 DiagStates.clear();
4413 assert(!Diag.DiagStates.empty());
4414 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4415 while (Idx < F.PragmaDiagMappings.size()) {
4416 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4417 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4418 if (DiagStateID != 0) {
4419 Diag.DiagStatePoints.push_back(
4420 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4421 FullSourceLoc(Loc, SourceMgr)));
4422 continue;
4423 }
4424
4425 assert(DiagStateID == 0);
4426 // A new DiagState was created here.
4427 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4428 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4429 DiagStates.push_back(NewState);
4430 Diag.DiagStatePoints.push_back(
4431 DiagnosticsEngine::DiagStatePoint(NewState,
4432 FullSourceLoc(Loc, SourceMgr)));
4433 while (1) {
4434 assert(Idx < F.PragmaDiagMappings.size() &&
4435 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4436 if (Idx >= F.PragmaDiagMappings.size()) {
4437 break; // Something is messed up but at least avoid infinite loop in
4438 // release build.
4439 }
4440 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4441 if (DiagID == (unsigned)-1) {
4442 break; // no more diag/map pairs for this location.
4443 }
4444 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4445 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4446 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4447 }
4448 }
4449 }
4450}
4451
4452/// \brief Get the correct cursor and offset for loading a type.
4453ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4454 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4455 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4456 ModuleFile *M = I->second;
4457 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4458}
4459
4460/// \brief Read and return the type with the given index..
4461///
4462/// The index is the type ID, shifted and minus the number of predefs. This
4463/// routine actually reads the record corresponding to the type at the given
4464/// location. It is a helper routine for GetType, which deals with reading type
4465/// IDs.
4466QualType ASTReader::readTypeRecord(unsigned Index) {
4467 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00004468 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004469
4470 // Keep track of where we are in the stream, then jump back there
4471 // after reading this type.
4472 SavedStreamPosition SavedPosition(DeclsCursor);
4473
4474 ReadingKindTracker ReadingKind(Read_Type, *this);
4475
4476 // Note that we are loading a type record.
4477 Deserializing AType(this);
4478
4479 unsigned Idx = 0;
4480 DeclsCursor.JumpToBit(Loc.Offset);
4481 RecordData Record;
4482 unsigned Code = DeclsCursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00004483 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004484 case TYPE_EXT_QUAL: {
4485 if (Record.size() != 2) {
4486 Error("Incorrect encoding of extended qualifier type");
4487 return QualType();
4488 }
4489 QualType Base = readType(*Loc.F, Record, Idx);
4490 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4491 return Context.getQualifiedType(Base, Quals);
4492 }
4493
4494 case TYPE_COMPLEX: {
4495 if (Record.size() != 1) {
4496 Error("Incorrect encoding of complex type");
4497 return QualType();
4498 }
4499 QualType ElemType = readType(*Loc.F, Record, Idx);
4500 return Context.getComplexType(ElemType);
4501 }
4502
4503 case TYPE_POINTER: {
4504 if (Record.size() != 1) {
4505 Error("Incorrect encoding of pointer type");
4506 return QualType();
4507 }
4508 QualType PointeeType = readType(*Loc.F, Record, Idx);
4509 return Context.getPointerType(PointeeType);
4510 }
4511
4512 case TYPE_BLOCK_POINTER: {
4513 if (Record.size() != 1) {
4514 Error("Incorrect encoding of block pointer type");
4515 return QualType();
4516 }
4517 QualType PointeeType = readType(*Loc.F, Record, Idx);
4518 return Context.getBlockPointerType(PointeeType);
4519 }
4520
4521 case TYPE_LVALUE_REFERENCE: {
4522 if (Record.size() != 2) {
4523 Error("Incorrect encoding of lvalue reference type");
4524 return QualType();
4525 }
4526 QualType PointeeType = readType(*Loc.F, Record, Idx);
4527 return Context.getLValueReferenceType(PointeeType, Record[1]);
4528 }
4529
4530 case TYPE_RVALUE_REFERENCE: {
4531 if (Record.size() != 1) {
4532 Error("Incorrect encoding of rvalue reference type");
4533 return QualType();
4534 }
4535 QualType PointeeType = readType(*Loc.F, Record, Idx);
4536 return Context.getRValueReferenceType(PointeeType);
4537 }
4538
4539 case TYPE_MEMBER_POINTER: {
4540 if (Record.size() != 2) {
4541 Error("Incorrect encoding of member pointer type");
4542 return QualType();
4543 }
4544 QualType PointeeType = readType(*Loc.F, Record, Idx);
4545 QualType ClassType = readType(*Loc.F, Record, Idx);
4546 if (PointeeType.isNull() || ClassType.isNull())
4547 return QualType();
4548
4549 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4550 }
4551
4552 case TYPE_CONSTANT_ARRAY: {
4553 QualType ElementType = readType(*Loc.F, Record, Idx);
4554 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4555 unsigned IndexTypeQuals = Record[2];
4556 unsigned Idx = 3;
4557 llvm::APInt Size = ReadAPInt(Record, Idx);
4558 return Context.getConstantArrayType(ElementType, Size,
4559 ASM, IndexTypeQuals);
4560 }
4561
4562 case TYPE_INCOMPLETE_ARRAY: {
4563 QualType ElementType = readType(*Loc.F, Record, Idx);
4564 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4565 unsigned IndexTypeQuals = Record[2];
4566 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4567 }
4568
4569 case TYPE_VARIABLE_ARRAY: {
4570 QualType ElementType = readType(*Loc.F, Record, Idx);
4571 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4572 unsigned IndexTypeQuals = Record[2];
4573 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4574 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4575 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4576 ASM, IndexTypeQuals,
4577 SourceRange(LBLoc, RBLoc));
4578 }
4579
4580 case TYPE_VECTOR: {
4581 if (Record.size() != 3) {
4582 Error("incorrect encoding of vector type in AST file");
4583 return QualType();
4584 }
4585
4586 QualType ElementType = readType(*Loc.F, Record, Idx);
4587 unsigned NumElements = Record[1];
4588 unsigned VecKind = Record[2];
4589 return Context.getVectorType(ElementType, NumElements,
4590 (VectorType::VectorKind)VecKind);
4591 }
4592
4593 case TYPE_EXT_VECTOR: {
4594 if (Record.size() != 3) {
4595 Error("incorrect encoding of extended vector type in AST file");
4596 return QualType();
4597 }
4598
4599 QualType ElementType = readType(*Loc.F, Record, Idx);
4600 unsigned NumElements = Record[1];
4601 return Context.getExtVectorType(ElementType, NumElements);
4602 }
4603
4604 case TYPE_FUNCTION_NO_PROTO: {
4605 if (Record.size() != 6) {
4606 Error("incorrect encoding of no-proto function type");
4607 return QualType();
4608 }
4609 QualType ResultType = readType(*Loc.F, Record, Idx);
4610 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4611 (CallingConv)Record[4], Record[5]);
4612 return Context.getFunctionNoProtoType(ResultType, Info);
4613 }
4614
4615 case TYPE_FUNCTION_PROTO: {
4616 QualType ResultType = readType(*Loc.F, Record, Idx);
4617
4618 FunctionProtoType::ExtProtoInfo EPI;
4619 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4620 /*hasregparm*/ Record[2],
4621 /*regparm*/ Record[3],
4622 static_cast<CallingConv>(Record[4]),
4623 /*produces*/ Record[5]);
4624
4625 unsigned Idx = 6;
4626 unsigned NumParams = Record[Idx++];
4627 SmallVector<QualType, 16> ParamTypes;
4628 for (unsigned I = 0; I != NumParams; ++I)
4629 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4630
4631 EPI.Variadic = Record[Idx++];
4632 EPI.HasTrailingReturn = Record[Idx++];
4633 EPI.TypeQuals = Record[Idx++];
4634 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4635 ExceptionSpecificationType EST =
4636 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4637 EPI.ExceptionSpecType = EST;
4638 SmallVector<QualType, 2> Exceptions;
4639 if (EST == EST_Dynamic) {
4640 EPI.NumExceptions = Record[Idx++];
4641 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4642 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4643 EPI.Exceptions = Exceptions.data();
4644 } else if (EST == EST_ComputedNoexcept) {
4645 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4646 } else if (EST == EST_Uninstantiated) {
4647 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4648 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4649 } else if (EST == EST_Unevaluated) {
4650 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4651 }
Jordan Rosebea522f2013-03-08 21:51:21 +00004652 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004653 }
4654
4655 case TYPE_UNRESOLVED_USING: {
4656 unsigned Idx = 0;
4657 return Context.getTypeDeclType(
4658 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4659 }
4660
4661 case TYPE_TYPEDEF: {
4662 if (Record.size() != 2) {
4663 Error("incorrect encoding of typedef type");
4664 return QualType();
4665 }
4666 unsigned Idx = 0;
4667 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4668 QualType Canonical = readType(*Loc.F, Record, Idx);
4669 if (!Canonical.isNull())
4670 Canonical = Context.getCanonicalType(Canonical);
4671 return Context.getTypedefType(Decl, Canonical);
4672 }
4673
4674 case TYPE_TYPEOF_EXPR:
4675 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4676
4677 case TYPE_TYPEOF: {
4678 if (Record.size() != 1) {
4679 Error("incorrect encoding of typeof(type) in AST file");
4680 return QualType();
4681 }
4682 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4683 return Context.getTypeOfType(UnderlyingType);
4684 }
4685
4686 case TYPE_DECLTYPE: {
4687 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4688 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4689 }
4690
4691 case TYPE_UNARY_TRANSFORM: {
4692 QualType BaseType = readType(*Loc.F, Record, Idx);
4693 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4694 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4695 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4696 }
4697
Richard Smitha2c36462013-04-26 16:15:35 +00004698 case TYPE_AUTO: {
4699 QualType Deduced = readType(*Loc.F, Record, Idx);
4700 bool IsDecltypeAuto = Record[Idx++];
Richard Smithdc7a4f52013-04-30 13:56:41 +00004701 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
4702 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smitha2c36462013-04-26 16:15:35 +00004703 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004704
4705 case TYPE_RECORD: {
4706 if (Record.size() != 2) {
4707 Error("incorrect encoding of record type");
4708 return QualType();
4709 }
4710 unsigned Idx = 0;
4711 bool IsDependent = Record[Idx++];
4712 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4713 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4714 QualType T = Context.getRecordType(RD);
4715 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4716 return T;
4717 }
4718
4719 case TYPE_ENUM: {
4720 if (Record.size() != 2) {
4721 Error("incorrect encoding of enum type");
4722 return QualType();
4723 }
4724 unsigned Idx = 0;
4725 bool IsDependent = Record[Idx++];
4726 QualType T
4727 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4728 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4729 return T;
4730 }
4731
4732 case TYPE_ATTRIBUTED: {
4733 if (Record.size() != 3) {
4734 Error("incorrect encoding of attributed type");
4735 return QualType();
4736 }
4737 QualType modifiedType = readType(*Loc.F, Record, Idx);
4738 QualType equivalentType = readType(*Loc.F, Record, Idx);
4739 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4740 return Context.getAttributedType(kind, modifiedType, equivalentType);
4741 }
4742
4743 case TYPE_PAREN: {
4744 if (Record.size() != 1) {
4745 Error("incorrect encoding of paren type");
4746 return QualType();
4747 }
4748 QualType InnerType = readType(*Loc.F, Record, Idx);
4749 return Context.getParenType(InnerType);
4750 }
4751
4752 case TYPE_PACK_EXPANSION: {
4753 if (Record.size() != 2) {
4754 Error("incorrect encoding of pack expansion type");
4755 return QualType();
4756 }
4757 QualType Pattern = readType(*Loc.F, Record, Idx);
4758 if (Pattern.isNull())
4759 return QualType();
David Blaikiedc84cd52013-02-20 22:23:23 +00004760 Optional<unsigned> NumExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00004761 if (Record[1])
4762 NumExpansions = Record[1] - 1;
4763 return Context.getPackExpansionType(Pattern, NumExpansions);
4764 }
4765
4766 case TYPE_ELABORATED: {
4767 unsigned Idx = 0;
4768 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4769 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4770 QualType NamedType = readType(*Loc.F, Record, Idx);
4771 return Context.getElaboratedType(Keyword, NNS, NamedType);
4772 }
4773
4774 case TYPE_OBJC_INTERFACE: {
4775 unsigned Idx = 0;
4776 ObjCInterfaceDecl *ItfD
4777 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4778 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4779 }
4780
4781 case TYPE_OBJC_OBJECT: {
4782 unsigned Idx = 0;
4783 QualType Base = readType(*Loc.F, Record, Idx);
4784 unsigned NumProtos = Record[Idx++];
4785 SmallVector<ObjCProtocolDecl*, 4> Protos;
4786 for (unsigned I = 0; I != NumProtos; ++I)
4787 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4788 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4789 }
4790
4791 case TYPE_OBJC_OBJECT_POINTER: {
4792 unsigned Idx = 0;
4793 QualType Pointee = readType(*Loc.F, Record, Idx);
4794 return Context.getObjCObjectPointerType(Pointee);
4795 }
4796
4797 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4798 unsigned Idx = 0;
4799 QualType Parm = readType(*Loc.F, Record, Idx);
4800 QualType Replacement = readType(*Loc.F, Record, Idx);
4801 return
4802 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4803 Replacement);
4804 }
4805
4806 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4807 unsigned Idx = 0;
4808 QualType Parm = readType(*Loc.F, Record, Idx);
4809 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4810 return Context.getSubstTemplateTypeParmPackType(
4811 cast<TemplateTypeParmType>(Parm),
4812 ArgPack);
4813 }
4814
4815 case TYPE_INJECTED_CLASS_NAME: {
4816 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4817 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4818 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4819 // for AST reading, too much interdependencies.
4820 return
4821 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4822 }
4823
4824 case TYPE_TEMPLATE_TYPE_PARM: {
4825 unsigned Idx = 0;
4826 unsigned Depth = Record[Idx++];
4827 unsigned Index = Record[Idx++];
4828 bool Pack = Record[Idx++];
4829 TemplateTypeParmDecl *D
4830 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4831 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4832 }
4833
4834 case TYPE_DEPENDENT_NAME: {
4835 unsigned Idx = 0;
4836 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4837 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4838 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4839 QualType Canon = readType(*Loc.F, Record, Idx);
4840 if (!Canon.isNull())
4841 Canon = Context.getCanonicalType(Canon);
4842 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4843 }
4844
4845 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4846 unsigned Idx = 0;
4847 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4848 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4849 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4850 unsigned NumArgs = Record[Idx++];
4851 SmallVector<TemplateArgument, 8> Args;
4852 Args.reserve(NumArgs);
4853 while (NumArgs--)
4854 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4855 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4856 Args.size(), Args.data());
4857 }
4858
4859 case TYPE_DEPENDENT_SIZED_ARRAY: {
4860 unsigned Idx = 0;
4861
4862 // ArrayType
4863 QualType ElementType = readType(*Loc.F, Record, Idx);
4864 ArrayType::ArraySizeModifier ASM
4865 = (ArrayType::ArraySizeModifier)Record[Idx++];
4866 unsigned IndexTypeQuals = Record[Idx++];
4867
4868 // DependentSizedArrayType
4869 Expr *NumElts = ReadExpr(*Loc.F);
4870 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4871
4872 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4873 IndexTypeQuals, Brackets);
4874 }
4875
4876 case TYPE_TEMPLATE_SPECIALIZATION: {
4877 unsigned Idx = 0;
4878 bool IsDependent = Record[Idx++];
4879 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4880 SmallVector<TemplateArgument, 8> Args;
4881 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4882 QualType Underlying = readType(*Loc.F, Record, Idx);
4883 QualType T;
4884 if (Underlying.isNull())
4885 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4886 Args.size());
4887 else
4888 T = Context.getTemplateSpecializationType(Name, Args.data(),
4889 Args.size(), Underlying);
4890 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4891 return T;
4892 }
4893
4894 case TYPE_ATOMIC: {
4895 if (Record.size() != 1) {
4896 Error("Incorrect encoding of atomic type");
4897 return QualType();
4898 }
4899 QualType ValueType = readType(*Loc.F, Record, Idx);
4900 return Context.getAtomicType(ValueType);
4901 }
4902 }
4903 llvm_unreachable("Invalid TypeCode!");
4904}
4905
4906class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4907 ASTReader &Reader;
4908 ModuleFile &F;
4909 const ASTReader::RecordData &Record;
4910 unsigned &Idx;
4911
4912 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4913 unsigned &I) {
4914 return Reader.ReadSourceLocation(F, R, I);
4915 }
4916
4917 template<typename T>
4918 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4919 return Reader.ReadDeclAs<T>(F, Record, Idx);
4920 }
4921
4922public:
4923 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4924 const ASTReader::RecordData &Record, unsigned &Idx)
4925 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4926 { }
4927
4928 // We want compile-time assurance that we've enumerated all of
4929 // these, so unfortunately we have to declare them first, then
4930 // define them out-of-line.
4931#define ABSTRACT_TYPELOC(CLASS, PARENT)
4932#define TYPELOC(CLASS, PARENT) \
4933 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4934#include "clang/AST/TypeLocNodes.def"
4935
4936 void VisitFunctionTypeLoc(FunctionTypeLoc);
4937 void VisitArrayTypeLoc(ArrayTypeLoc);
4938};
4939
4940void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4941 // nothing to do
4942}
4943void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4944 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4945 if (TL.needsExtraLocalData()) {
4946 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4947 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4948 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4949 TL.setModeAttr(Record[Idx++]);
4950 }
4951}
4952void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4953 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4954}
4955void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4956 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4957}
4958void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4959 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4960}
4961void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4962 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4963}
4964void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4965 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4966}
4967void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4968 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4969 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4970}
4971void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4972 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4973 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4974 if (Record[Idx++])
4975 TL.setSizeExpr(Reader.ReadExpr(F));
4976 else
4977 TL.setSizeExpr(0);
4978}
4979void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4980 VisitArrayTypeLoc(TL);
4981}
4982void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4983 VisitArrayTypeLoc(TL);
4984}
4985void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4986 VisitArrayTypeLoc(TL);
4987}
4988void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4989 DependentSizedArrayTypeLoc TL) {
4990 VisitArrayTypeLoc(TL);
4991}
4992void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4993 DependentSizedExtVectorTypeLoc TL) {
4994 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4995}
4996void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4997 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4998}
4999void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5000 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5001}
5002void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5003 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5004 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5005 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5006 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
5007 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
5008 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
5009 }
5010}
5011void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5012 VisitFunctionTypeLoc(TL);
5013}
5014void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5015 VisitFunctionTypeLoc(TL);
5016}
5017void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5018 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5019}
5020void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5021 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5022}
5023void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5024 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5025 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5026 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5027}
5028void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5029 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5030 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5031 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5032 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5033}
5034void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5035 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5036}
5037void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5038 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5039 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5040 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5041 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5042}
5043void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5044 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5045}
5046void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5047 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5048}
5049void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5050 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5051}
5052void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5053 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5054 if (TL.hasAttrOperand()) {
5055 SourceRange range;
5056 range.setBegin(ReadSourceLocation(Record, Idx));
5057 range.setEnd(ReadSourceLocation(Record, Idx));
5058 TL.setAttrOperandParensRange(range);
5059 }
5060 if (TL.hasAttrExprOperand()) {
5061 if (Record[Idx++])
5062 TL.setAttrExprOperand(Reader.ReadExpr(F));
5063 else
5064 TL.setAttrExprOperand(0);
5065 } else if (TL.hasAttrEnumOperand())
5066 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5067}
5068void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5069 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5070}
5071void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5072 SubstTemplateTypeParmTypeLoc TL) {
5073 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5074}
5075void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5076 SubstTemplateTypeParmPackTypeLoc TL) {
5077 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5078}
5079void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5080 TemplateSpecializationTypeLoc TL) {
5081 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5082 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5083 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5084 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5085 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5086 TL.setArgLocInfo(i,
5087 Reader.GetTemplateArgumentLocInfo(F,
5088 TL.getTypePtr()->getArg(i).getKind(),
5089 Record, Idx));
5090}
5091void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5092 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5093 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5094}
5095void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5096 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5097 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5098}
5099void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5100 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5101}
5102void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5103 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5104 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5105 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5106}
5107void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5108 DependentTemplateSpecializationTypeLoc TL) {
5109 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5110 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5111 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5112 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5113 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5114 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5115 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5116 TL.setArgLocInfo(I,
5117 Reader.GetTemplateArgumentLocInfo(F,
5118 TL.getTypePtr()->getArg(I).getKind(),
5119 Record, Idx));
5120}
5121void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5122 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5123}
5124void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5125 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5126}
5127void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5128 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5129 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5130 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5131 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5132 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5133}
5134void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5135 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5136}
5137void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5138 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5139 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5140 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5141}
5142
5143TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5144 const RecordData &Record,
5145 unsigned &Idx) {
5146 QualType InfoTy = readType(F, Record, Idx);
5147 if (InfoTy.isNull())
5148 return 0;
5149
5150 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5151 TypeLocReader TLR(*this, F, Record, Idx);
5152 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5153 TLR.Visit(TL);
5154 return TInfo;
5155}
5156
5157QualType ASTReader::GetType(TypeID ID) {
5158 unsigned FastQuals = ID & Qualifiers::FastMask;
5159 unsigned Index = ID >> Qualifiers::FastWidth;
5160
5161 if (Index < NUM_PREDEF_TYPE_IDS) {
5162 QualType T;
5163 switch ((PredefinedTypeIDs)Index) {
5164 case PREDEF_TYPE_NULL_ID: return QualType();
5165 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5166 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5167
5168 case PREDEF_TYPE_CHAR_U_ID:
5169 case PREDEF_TYPE_CHAR_S_ID:
5170 // FIXME: Check that the signedness of CharTy is correct!
5171 T = Context.CharTy;
5172 break;
5173
5174 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5175 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5176 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5177 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5178 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5179 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5180 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5181 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5182 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5183 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5184 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5185 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5186 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5187 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5188 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5189 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5190 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5191 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5192 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5193 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5194 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5195 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5196 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5197 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5198 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5199 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5200 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5201 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeib13621d2012-12-18 14:38:23 +00005202 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5203 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5204 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5205 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5206 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5207 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei21f18c42013-02-07 10:55:47 +00005208 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00005209 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005210 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5211
5212 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5213 T = Context.getAutoRRefDeductType();
5214 break;
5215
5216 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5217 T = Context.ARCUnbridgedCastTy;
5218 break;
5219
5220 case PREDEF_TYPE_VA_LIST_TAG:
5221 T = Context.getVaListTagType();
5222 break;
5223
5224 case PREDEF_TYPE_BUILTIN_FN:
5225 T = Context.BuiltinFnTy;
5226 break;
5227 }
5228
5229 assert(!T.isNull() && "Unknown predefined type");
5230 return T.withFastQualifiers(FastQuals);
5231 }
5232
5233 Index -= NUM_PREDEF_TYPE_IDS;
5234 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5235 if (TypesLoaded[Index].isNull()) {
5236 TypesLoaded[Index] = readTypeRecord(Index);
5237 if (TypesLoaded[Index].isNull())
5238 return QualType();
5239
5240 TypesLoaded[Index]->setFromAST();
5241 if (DeserializationListener)
5242 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5243 TypesLoaded[Index]);
5244 }
5245
5246 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5247}
5248
5249QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5250 return GetType(getGlobalTypeID(F, LocalID));
5251}
5252
5253serialization::TypeID
5254ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5255 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5256 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5257
5258 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5259 return LocalID;
5260
5261 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5262 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5263 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5264
5265 unsigned GlobalIndex = LocalIndex + I->second;
5266 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5267}
5268
5269TemplateArgumentLocInfo
5270ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5271 TemplateArgument::ArgKind Kind,
5272 const RecordData &Record,
5273 unsigned &Index) {
5274 switch (Kind) {
5275 case TemplateArgument::Expression:
5276 return ReadExpr(F);
5277 case TemplateArgument::Type:
5278 return GetTypeSourceInfo(F, Record, Index);
5279 case TemplateArgument::Template: {
5280 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5281 Index);
5282 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5283 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5284 SourceLocation());
5285 }
5286 case TemplateArgument::TemplateExpansion: {
5287 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5288 Index);
5289 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5290 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5291 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5292 EllipsisLoc);
5293 }
5294 case TemplateArgument::Null:
5295 case TemplateArgument::Integral:
5296 case TemplateArgument::Declaration:
5297 case TemplateArgument::NullPtr:
5298 case TemplateArgument::Pack:
5299 // FIXME: Is this right?
5300 return TemplateArgumentLocInfo();
5301 }
5302 llvm_unreachable("unexpected template argument loc");
5303}
5304
5305TemplateArgumentLoc
5306ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5307 const RecordData &Record, unsigned &Index) {
5308 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5309
5310 if (Arg.getKind() == TemplateArgument::Expression) {
5311 if (Record[Index++]) // bool InfoHasSameExpr.
5312 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5313 }
5314 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5315 Record, Index));
5316}
5317
5318Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5319 return GetDecl(ID);
5320}
5321
5322uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5323 unsigned &Idx){
5324 if (Idx >= Record.size())
5325 return 0;
5326
5327 unsigned LocalID = Record[Idx++];
5328 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5329}
5330
5331CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5332 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00005333 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005334 SavedStreamPosition SavedPosition(Cursor);
5335 Cursor.JumpToBit(Loc.Offset);
5336 ReadingKindTracker ReadingKind(Read_Decl, *this);
5337 RecordData Record;
5338 unsigned Code = Cursor.ReadCode();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00005339 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005340 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5341 Error("Malformed AST file: missing C++ base specifiers");
5342 return 0;
5343 }
5344
5345 unsigned Idx = 0;
5346 unsigned NumBases = Record[Idx++];
5347 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5348 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5349 for (unsigned I = 0; I != NumBases; ++I)
5350 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5351 return Bases;
5352}
5353
5354serialization::DeclID
5355ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5356 if (LocalID < NUM_PREDEF_DECL_IDS)
5357 return LocalID;
5358
5359 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5360 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5361 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5362
5363 return LocalID + I->second;
5364}
5365
5366bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5367 ModuleFile &M) const {
5368 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5369 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5370 return &M == I->second;
5371}
5372
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005373ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005374 if (!D->isFromASTFile())
5375 return 0;
5376 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5377 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5378 return I->second;
5379}
5380
5381SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5382 if (ID < NUM_PREDEF_DECL_IDS)
5383 return SourceLocation();
5384
5385 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5386
5387 if (Index > DeclsLoaded.size()) {
5388 Error("declaration ID out-of-range for AST file");
5389 return SourceLocation();
5390 }
5391
5392 if (Decl *D = DeclsLoaded[Index])
5393 return D->getLocation();
5394
5395 unsigned RawLocation = 0;
5396 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5397 return ReadSourceLocation(*Rec.F, RawLocation);
5398}
5399
5400Decl *ASTReader::GetDecl(DeclID ID) {
5401 if (ID < NUM_PREDEF_DECL_IDS) {
5402 switch ((PredefinedDeclIDs)ID) {
5403 case PREDEF_DECL_NULL_ID:
5404 return 0;
5405
5406 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5407 return Context.getTranslationUnitDecl();
5408
5409 case PREDEF_DECL_OBJC_ID_ID:
5410 return Context.getObjCIdDecl();
5411
5412 case PREDEF_DECL_OBJC_SEL_ID:
5413 return Context.getObjCSelDecl();
5414
5415 case PREDEF_DECL_OBJC_CLASS_ID:
5416 return Context.getObjCClassDecl();
5417
5418 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5419 return Context.getObjCProtocolDecl();
5420
5421 case PREDEF_DECL_INT_128_ID:
5422 return Context.getInt128Decl();
5423
5424 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5425 return Context.getUInt128Decl();
5426
5427 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5428 return Context.getObjCInstanceTypeDecl();
5429
5430 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5431 return Context.getBuiltinVaListDecl();
5432 }
5433 }
5434
5435 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5436
5437 if (Index >= DeclsLoaded.size()) {
5438 assert(0 && "declaration ID out-of-range for AST file");
5439 Error("declaration ID out-of-range for AST file");
5440 return 0;
5441 }
5442
5443 if (!DeclsLoaded[Index]) {
5444 ReadDeclRecord(ID);
5445 if (DeserializationListener)
5446 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5447 }
5448
5449 return DeclsLoaded[Index];
5450}
5451
5452DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5453 DeclID GlobalID) {
5454 if (GlobalID < NUM_PREDEF_DECL_IDS)
5455 return GlobalID;
5456
5457 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5458 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5459 ModuleFile *Owner = I->second;
5460
5461 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5462 = M.GlobalToLocalDeclIDs.find(Owner);
5463 if (Pos == M.GlobalToLocalDeclIDs.end())
5464 return 0;
5465
5466 return GlobalID - Owner->BaseDeclID + Pos->second;
5467}
5468
5469serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5470 const RecordData &Record,
5471 unsigned &Idx) {
5472 if (Idx >= Record.size()) {
5473 Error("Corrupted AST file");
5474 return 0;
5475 }
5476
5477 return getGlobalDeclID(F, Record[Idx++]);
5478}
5479
5480/// \brief Resolve the offset of a statement into a statement.
5481///
5482/// This operation will read a new statement from the external
5483/// source each time it is called, and is meant to be used via a
5484/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5485Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5486 // Switch case IDs are per Decl.
5487 ClearSwitchCaseIDs();
5488
5489 // Offset here is a global offset across the entire chain.
5490 RecordLocation Loc = getLocalBitOffset(Offset);
5491 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5492 return ReadStmtFromStream(*Loc.F);
5493}
5494
5495namespace {
5496 class FindExternalLexicalDeclsVisitor {
5497 ASTReader &Reader;
5498 const DeclContext *DC;
5499 bool (*isKindWeWant)(Decl::Kind);
5500
5501 SmallVectorImpl<Decl*> &Decls;
5502 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5503
5504 public:
5505 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5506 bool (*isKindWeWant)(Decl::Kind),
5507 SmallVectorImpl<Decl*> &Decls)
5508 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5509 {
5510 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5511 PredefsVisited[I] = false;
5512 }
5513
5514 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5515 if (Preorder)
5516 return false;
5517
5518 FindExternalLexicalDeclsVisitor *This
5519 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5520
5521 ModuleFile::DeclContextInfosMap::iterator Info
5522 = M.DeclContextInfos.find(This->DC);
5523 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5524 return false;
5525
5526 // Load all of the declaration IDs
5527 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5528 *IDE = ID + Info->second.NumLexicalDecls;
5529 ID != IDE; ++ID) {
5530 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5531 continue;
5532
5533 // Don't add predefined declarations to the lexical context more
5534 // than once.
5535 if (ID->second < NUM_PREDEF_DECL_IDS) {
5536 if (This->PredefsVisited[ID->second])
5537 continue;
5538
5539 This->PredefsVisited[ID->second] = true;
5540 }
5541
5542 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5543 if (!This->DC->isDeclInLexicalTraversal(D))
5544 This->Decls.push_back(D);
5545 }
5546 }
5547
5548 return false;
5549 }
5550 };
5551}
5552
5553ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5554 bool (*isKindWeWant)(Decl::Kind),
5555 SmallVectorImpl<Decl*> &Decls) {
5556 // There might be lexical decls in multiple modules, for the TU at
5557 // least. Walk all of the modules in the order they were loaded.
5558 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5559 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5560 ++NumLexicalDeclContextsRead;
5561 return ELR_Success;
5562}
5563
5564namespace {
5565
5566class DeclIDComp {
5567 ASTReader &Reader;
5568 ModuleFile &Mod;
5569
5570public:
5571 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5572
5573 bool operator()(LocalDeclID L, LocalDeclID R) const {
5574 SourceLocation LHS = getLocation(L);
5575 SourceLocation RHS = getLocation(R);
5576 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5577 }
5578
5579 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5580 SourceLocation RHS = getLocation(R);
5581 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5582 }
5583
5584 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5585 SourceLocation LHS = getLocation(L);
5586 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5587 }
5588
5589 SourceLocation getLocation(LocalDeclID ID) const {
5590 return Reader.getSourceManager().getFileLoc(
5591 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5592 }
5593};
5594
5595}
5596
5597void ASTReader::FindFileRegionDecls(FileID File,
5598 unsigned Offset, unsigned Length,
5599 SmallVectorImpl<Decl *> &Decls) {
5600 SourceManager &SM = getSourceManager();
5601
5602 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5603 if (I == FileDeclIDs.end())
5604 return;
5605
5606 FileDeclsInfo &DInfo = I->second;
5607 if (DInfo.Decls.empty())
5608 return;
5609
5610 SourceLocation
5611 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5612 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5613
5614 DeclIDComp DIDComp(*this, *DInfo.Mod);
5615 ArrayRef<serialization::LocalDeclID>::iterator
5616 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5617 BeginLoc, DIDComp);
5618 if (BeginIt != DInfo.Decls.begin())
5619 --BeginIt;
5620
5621 // If we are pointing at a top-level decl inside an objc container, we need
5622 // to backtrack until we find it otherwise we will fail to report that the
5623 // region overlaps with an objc container.
5624 while (BeginIt != DInfo.Decls.begin() &&
5625 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5626 ->isTopLevelDeclInObjCContainer())
5627 --BeginIt;
5628
5629 ArrayRef<serialization::LocalDeclID>::iterator
5630 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5631 EndLoc, DIDComp);
5632 if (EndIt != DInfo.Decls.end())
5633 ++EndIt;
5634
5635 for (ArrayRef<serialization::LocalDeclID>::iterator
5636 DIt = BeginIt; DIt != EndIt; ++DIt)
5637 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5638}
5639
5640namespace {
5641 /// \brief ModuleFile visitor used to perform name lookup into a
5642 /// declaration context.
5643 class DeclContextNameLookupVisitor {
5644 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005645 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005646 DeclarationName Name;
5647 SmallVectorImpl<NamedDecl *> &Decls;
5648
5649 public:
5650 DeclContextNameLookupVisitor(ASTReader &Reader,
5651 SmallVectorImpl<const DeclContext *> &Contexts,
5652 DeclarationName Name,
5653 SmallVectorImpl<NamedDecl *> &Decls)
5654 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5655
5656 static bool visit(ModuleFile &M, void *UserData) {
5657 DeclContextNameLookupVisitor *This
5658 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5659
5660 // Check whether we have any visible declaration information for
5661 // this context in this module.
5662 ModuleFile::DeclContextInfosMap::iterator Info;
5663 bool FoundInfo = false;
5664 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5665 Info = M.DeclContextInfos.find(This->Contexts[I]);
5666 if (Info != M.DeclContextInfos.end() &&
5667 Info->second.NameLookupTableData) {
5668 FoundInfo = true;
5669 break;
5670 }
5671 }
5672
5673 if (!FoundInfo)
5674 return false;
5675
5676 // Look for this name within this module.
5677 ASTDeclContextNameLookupTable *LookupTable =
5678 Info->second.NameLookupTableData;
5679 ASTDeclContextNameLookupTable::iterator Pos
5680 = LookupTable->find(This->Name);
5681 if (Pos == LookupTable->end())
5682 return false;
5683
5684 bool FoundAnything = false;
5685 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5686 for (; Data.first != Data.second; ++Data.first) {
5687 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5688 if (!ND)
5689 continue;
5690
5691 if (ND->getDeclName() != This->Name) {
5692 // A name might be null because the decl's redeclarable part is
5693 // currently read before reading its name. The lookup is triggered by
5694 // building that decl (likely indirectly), and so it is later in the
5695 // sense of "already existing" and can be ignored here.
5696 continue;
5697 }
5698
5699 // Record this declaration.
5700 FoundAnything = true;
5701 This->Decls.push_back(ND);
5702 }
5703
5704 return FoundAnything;
5705 }
5706 };
5707}
5708
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005709/// \brief Retrieve the "definitive" module file for the definition of the
5710/// given declaration context, if there is one.
5711///
5712/// The "definitive" module file is the only place where we need to look to
5713/// find information about the declarations within the given declaration
5714/// context. For example, C++ and Objective-C classes, C structs/unions, and
5715/// Objective-C protocols, categories, and extensions are all defined in a
5716/// single place in the source code, so they have definitive module files
5717/// associated with them. C++ namespaces, on the other hand, can have
5718/// definitions in multiple different module files.
5719///
5720/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5721/// NDEBUG checking.
5722static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5723 ASTReader &Reader) {
Douglas Gregore0d20662013-01-22 17:08:30 +00005724 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5725 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005726
5727 return 0;
5728}
5729
Richard Smith3646c682013-02-07 03:30:24 +00005730bool
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005731ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5732 DeclarationName Name) {
5733 assert(DC->hasExternalVisibleStorage() &&
5734 "DeclContext has no visible decls in storage");
5735 if (!Name)
Richard Smith3646c682013-02-07 03:30:24 +00005736 return false;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005737
5738 SmallVector<NamedDecl *, 64> Decls;
5739
5740 // Compute the declaration contexts we need to look into. Multiple such
5741 // declaration contexts occur when two declaration contexts from disjoint
5742 // modules get merged, e.g., when two namespaces with the same name are
5743 // independently defined in separate modules.
5744 SmallVector<const DeclContext *, 2> Contexts;
5745 Contexts.push_back(DC);
5746
5747 if (DC->isNamespace()) {
5748 MergedDeclsMap::iterator Merged
5749 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5750 if (Merged != MergedDecls.end()) {
5751 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5752 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5753 }
5754 }
5755
5756 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005757
5758 // If we can definitively determine which module file to look into,
5759 // only look there. Otherwise, look in all module files.
5760 ModuleFile *Definitive;
5761 if (Contexts.size() == 1 &&
5762 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5763 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5764 } else {
5765 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5766 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005767 ++NumVisibleDeclContextsRead;
5768 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith3646c682013-02-07 03:30:24 +00005769 return !Decls.empty();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005770}
5771
5772namespace {
5773 /// \brief ModuleFile visitor used to retrieve all visible names in a
5774 /// declaration context.
5775 class DeclContextAllNamesVisitor {
5776 ASTReader &Reader;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005777 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005778 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005779 bool VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005780
5781 public:
5782 DeclContextAllNamesVisitor(ASTReader &Reader,
5783 SmallVectorImpl<const DeclContext *> &Contexts,
5784 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005785 SmallVector<NamedDecl *, 8> > &Decls,
5786 bool VisitAll)
5787 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005788
5789 static bool visit(ModuleFile &M, void *UserData) {
5790 DeclContextAllNamesVisitor *This
5791 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5792
5793 // Check whether we have any visible declaration information for
5794 // this context in this module.
5795 ModuleFile::DeclContextInfosMap::iterator Info;
5796 bool FoundInfo = false;
5797 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5798 Info = M.DeclContextInfos.find(This->Contexts[I]);
5799 if (Info != M.DeclContextInfos.end() &&
5800 Info->second.NameLookupTableData) {
5801 FoundInfo = true;
5802 break;
5803 }
5804 }
5805
5806 if (!FoundInfo)
5807 return false;
5808
5809 ASTDeclContextNameLookupTable *LookupTable =
5810 Info->second.NameLookupTableData;
5811 bool FoundAnything = false;
5812 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregora6b00fc2013-01-23 22:38:11 +00005813 I = LookupTable->data_begin(), E = LookupTable->data_end();
5814 I != E;
5815 ++I) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005816 ASTDeclContextNameLookupTrait::data_type Data = *I;
5817 for (; Data.first != Data.second; ++Data.first) {
5818 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5819 *Data.first);
5820 if (!ND)
5821 continue;
5822
5823 // Record this declaration.
5824 FoundAnything = true;
5825 This->Decls[ND->getDeclName()].push_back(ND);
5826 }
5827 }
5828
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005829 return FoundAnything && !This->VisitAll;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005830 }
5831 };
5832}
5833
5834void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5835 if (!DC->hasExternalVisibleStorage())
5836 return;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005837 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005838
5839 // Compute the declaration contexts we need to look into. Multiple such
5840 // declaration contexts occur when two declaration contexts from disjoint
5841 // modules get merged, e.g., when two namespaces with the same name are
5842 // independently defined in separate modules.
5843 SmallVector<const DeclContext *, 2> Contexts;
5844 Contexts.push_back(DC);
5845
5846 if (DC->isNamespace()) {
5847 MergedDeclsMap::iterator Merged
5848 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5849 if (Merged != MergedDecls.end()) {
5850 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5851 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5852 }
5853 }
5854
Argyrios Kyrtzidisca40f302012-12-19 22:21:18 +00005855 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5856 /*VisitAll=*/DC->isFileContext());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005857 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5858 ++NumVisibleDeclContextsRead;
5859
5860 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00005861 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005862 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5863 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5864 }
5865 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5866}
5867
5868/// \brief Under non-PCH compilation the consumer receives the objc methods
5869/// before receiving the implementation, and codegen depends on this.
5870/// We simulate this by deserializing and passing to consumer the methods of the
5871/// implementation before passing the deserialized implementation decl.
5872static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5873 ASTConsumer *Consumer) {
5874 assert(ImplD && Consumer);
5875
5876 for (ObjCImplDecl::method_iterator
5877 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5878 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5879
5880 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5881}
5882
5883void ASTReader::PassInterestingDeclsToConsumer() {
5884 assert(Consumer);
5885 while (!InterestingDecls.empty()) {
5886 Decl *D = InterestingDecls.front();
5887 InterestingDecls.pop_front();
5888
5889 PassInterestingDeclToConsumer(D);
5890 }
5891}
5892
5893void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5894 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5895 PassObjCImplDeclToConsumer(ImplD, Consumer);
5896 else
5897 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5898}
5899
5900void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5901 this->Consumer = Consumer;
5902
5903 if (!Consumer)
5904 return;
5905
5906 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5907 // Force deserialization of this decl, which will cause it to be queued for
5908 // passing to the consumer.
5909 GetDecl(ExternalDefinitions[I]);
5910 }
5911 ExternalDefinitions.clear();
5912
5913 PassInterestingDeclsToConsumer();
5914}
5915
5916void ASTReader::PrintStats() {
5917 std::fprintf(stderr, "*** AST File Statistics:\n");
5918
5919 unsigned NumTypesLoaded
5920 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5921 QualType());
5922 unsigned NumDeclsLoaded
5923 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5924 (Decl *)0);
5925 unsigned NumIdentifiersLoaded
5926 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5927 IdentifiersLoaded.end(),
5928 (IdentifierInfo *)0);
5929 unsigned NumMacrosLoaded
5930 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5931 MacrosLoaded.end(),
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005932 (MacroInfo *)0);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005933 unsigned NumSelectorsLoaded
5934 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5935 SelectorsLoaded.end(),
5936 Selector());
5937
5938 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5939 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5940 NumSLocEntriesRead, TotalNumSLocEntries,
5941 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5942 if (!TypesLoaded.empty())
5943 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5944 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5945 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5946 if (!DeclsLoaded.empty())
5947 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5948 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5949 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5950 if (!IdentifiersLoaded.empty())
5951 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5952 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5953 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5954 if (!MacrosLoaded.empty())
5955 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5956 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5957 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5958 if (!SelectorsLoaded.empty())
5959 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5960 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5961 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5962 if (TotalNumStatements)
5963 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5964 NumStatementsRead, TotalNumStatements,
5965 ((float)NumStatementsRead/TotalNumStatements * 100));
5966 if (TotalNumMacros)
5967 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5968 NumMacrosRead, TotalNumMacros,
5969 ((float)NumMacrosRead/TotalNumMacros * 100));
5970 if (TotalLexicalDeclContexts)
5971 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5972 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5973 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5974 * 100));
5975 if (TotalVisibleDeclContexts)
5976 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5977 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5978 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5979 * 100));
5980 if (TotalNumMethodPoolEntries) {
5981 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5982 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5983 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5984 * 100));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00005985 }
Douglas Gregor95fb36e2013-01-28 17:54:36 +00005986 if (NumMethodPoolLookups) {
5987 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5988 NumMethodPoolHits, NumMethodPoolLookups,
5989 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5990 }
5991 if (NumMethodPoolTableLookups) {
5992 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5993 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5994 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5995 * 100.0));
5996 }
5997
Douglas Gregore1698072013-01-25 00:38:33 +00005998 if (NumIdentifierLookupHits) {
5999 std::fprintf(stderr,
6000 " %u / %u identifier table lookups succeeded (%f%%)\n",
6001 NumIdentifierLookupHits, NumIdentifierLookups,
6002 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6003 }
6004
Douglas Gregor1a49d972013-01-25 01:03:03 +00006005 if (GlobalIndex) {
6006 std::fprintf(stderr, "\n");
6007 GlobalIndex->printStats();
6008 }
6009
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006010 std::fprintf(stderr, "\n");
6011 dump();
6012 std::fprintf(stderr, "\n");
6013}
6014
6015template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6016static void
6017dumpModuleIDMap(StringRef Name,
6018 const ContinuousRangeMap<Key, ModuleFile *,
6019 InitialCapacity> &Map) {
6020 if (Map.begin() == Map.end())
6021 return;
6022
6023 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6024 llvm::errs() << Name << ":\n";
6025 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6026 I != IEnd; ++I) {
6027 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6028 << "\n";
6029 }
6030}
6031
6032void ASTReader::dump() {
6033 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6034 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6035 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6036 dumpModuleIDMap("Global type map", GlobalTypeMap);
6037 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6038 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6039 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6040 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6041 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6042 dumpModuleIDMap("Global preprocessed entity map",
6043 GlobalPreprocessedEntityMap);
6044
6045 llvm::errs() << "\n*** PCH/Modules Loaded:";
6046 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6047 MEnd = ModuleMgr.end();
6048 M != MEnd; ++M)
6049 (*M)->dump();
6050}
6051
6052/// Return the amount of memory used by memory buffers, breaking down
6053/// by heap-backed versus mmap'ed memory.
6054void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6055 for (ModuleConstIterator I = ModuleMgr.begin(),
6056 E = ModuleMgr.end(); I != E; ++I) {
6057 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6058 size_t bytes = buf->getBufferSize();
6059 switch (buf->getBufferKind()) {
6060 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6061 sizes.malloc_bytes += bytes;
6062 break;
6063 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6064 sizes.mmap_bytes += bytes;
6065 break;
6066 }
6067 }
6068 }
6069}
6070
6071void ASTReader::InitializeSema(Sema &S) {
6072 SemaObj = &S;
6073 S.addExternalSource(this);
6074
6075 // Makes sure any declarations that were deserialized "too early"
6076 // still get added to the identifier's declaration chains.
6077 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006078 pushExternalDeclIntoScope(PreloadedDecls[I],
6079 PreloadedDecls[I]->getDeclName());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006080 }
6081 PreloadedDecls.clear();
6082
6083 // Load the offsets of the declarations that Sema references.
6084 // They will be lazily deserialized when needed.
6085 if (!SemaDeclRefs.empty()) {
6086 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
6087 if (!SemaObj->StdNamespace)
6088 SemaObj->StdNamespace = SemaDeclRefs[0];
6089 if (!SemaObj->StdBadAlloc)
6090 SemaObj->StdBadAlloc = SemaDeclRefs[1];
6091 }
6092
6093 if (!FPPragmaOptions.empty()) {
6094 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6095 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6096 }
6097
6098 if (!OpenCLExtensions.empty()) {
6099 unsigned I = 0;
6100#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6101#include "clang/Basic/OpenCLExtensions.def"
6102
6103 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6104 }
6105}
6106
6107IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6108 // Note that we are loading an identifier.
6109 Deserializing AnIdentifier(this);
Douglas Gregor1a49d972013-01-25 01:03:03 +00006110 StringRef Name(NameStart, NameEnd - NameStart);
6111
6112 // If there is a global index, look there first to determine which modules
6113 // provably do not have any results for this identifier.
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006114 GlobalModuleIndex::HitSet Hits;
6115 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006116 if (!loadGlobalIndex()) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006117 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6118 HitsPtr = &Hits;
Douglas Gregor1a49d972013-01-25 01:03:03 +00006119 }
6120 }
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006121 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregore1698072013-01-25 00:38:33 +00006122 NumIdentifierLookups,
6123 NumIdentifierLookupHits);
Douglas Gregor188bdcd2013-01-25 23:32:03 +00006124 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006125 IdentifierInfo *II = Visitor.getIdentifierInfo();
6126 markIdentifierUpToDate(II);
6127 return II;
6128}
6129
6130namespace clang {
6131 /// \brief An identifier-lookup iterator that enumerates all of the
6132 /// identifiers stored within a set of AST files.
6133 class ASTIdentifierIterator : public IdentifierIterator {
6134 /// \brief The AST reader whose identifiers are being enumerated.
6135 const ASTReader &Reader;
6136
6137 /// \brief The current index into the chain of AST files stored in
6138 /// the AST reader.
6139 unsigned Index;
6140
6141 /// \brief The current position within the identifier lookup table
6142 /// of the current AST file.
6143 ASTIdentifierLookupTable::key_iterator Current;
6144
6145 /// \brief The end position within the identifier lookup table of
6146 /// the current AST file.
6147 ASTIdentifierLookupTable::key_iterator End;
6148
6149 public:
6150 explicit ASTIdentifierIterator(const ASTReader &Reader);
6151
6152 virtual StringRef Next();
6153 };
6154}
6155
6156ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6157 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6158 ASTIdentifierLookupTable *IdTable
6159 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6160 Current = IdTable->key_begin();
6161 End = IdTable->key_end();
6162}
6163
6164StringRef ASTIdentifierIterator::Next() {
6165 while (Current == End) {
6166 // If we have exhausted all of our AST files, we're done.
6167 if (Index == 0)
6168 return StringRef();
6169
6170 --Index;
6171 ASTIdentifierLookupTable *IdTable
6172 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6173 IdentifierLookupTable;
6174 Current = IdTable->key_begin();
6175 End = IdTable->key_end();
6176 }
6177
6178 // We have any identifiers remaining in the current AST file; return
6179 // the next one.
Douglas Gregor479633c2013-01-23 18:53:14 +00006180 StringRef Result = *Current;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006181 ++Current;
Douglas Gregor479633c2013-01-23 18:53:14 +00006182 return Result;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006183}
6184
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +00006185IdentifierIterator *ASTReader::getIdentifiers() {
6186 if (!loadGlobalIndex())
6187 return GlobalIndex->createIdentifierIterator();
6188
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006189 return new ASTIdentifierIterator(*this);
6190}
6191
6192namespace clang { namespace serialization {
6193 class ReadMethodPoolVisitor {
6194 ASTReader &Reader;
6195 Selector Sel;
6196 unsigned PriorGeneration;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006197 unsigned InstanceBits;
6198 unsigned FactoryBits;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00006199 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6200 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006201
6202 public:
6203 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6204 unsigned PriorGeneration)
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006205 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6206 InstanceBits(0), FactoryBits(0) { }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006207
6208 static bool visit(ModuleFile &M, void *UserData) {
6209 ReadMethodPoolVisitor *This
6210 = static_cast<ReadMethodPoolVisitor *>(UserData);
6211
6212 if (!M.SelectorLookupTable)
6213 return false;
6214
6215 // If we've already searched this module file, skip it now.
6216 if (M.Generation <= This->PriorGeneration)
6217 return true;
6218
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006219 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006220 ASTSelectorLookupTable *PoolTable
6221 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6222 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6223 if (Pos == PoolTable->end())
6224 return false;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006225
6226 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006227 ++This->Reader.NumSelectorsRead;
6228 // FIXME: Not quite happy with the statistics here. We probably should
6229 // disable this tracking when called via LoadSelector.
6230 // Also, should entries without methods count as misses?
6231 ++This->Reader.NumMethodPoolEntriesRead;
6232 ASTSelectorLookupTrait::data_type Data = *Pos;
6233 if (This->Reader.DeserializationListener)
6234 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6235 This->Sel);
6236
6237 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6238 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006239 This->InstanceBits = Data.InstanceBits;
6240 This->FactoryBits = Data.FactoryBits;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006241 return true;
6242 }
6243
6244 /// \brief Retrieve the instance methods found by this visitor.
6245 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6246 return InstanceMethods;
6247 }
6248
6249 /// \brief Retrieve the instance methods found by this visitor.
6250 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6251 return FactoryMethods;
6252 }
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006253
6254 unsigned getInstanceBits() const { return InstanceBits; }
6255 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006256 };
6257} } // end namespace clang::serialization
6258
6259/// \brief Add the given set of methods to the method list.
6260static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6261 ObjCMethodList &List) {
6262 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6263 S.addMethodToGlobalList(&List, Methods[I]);
6264 }
6265}
6266
6267void ASTReader::ReadMethodPool(Selector Sel) {
6268 // Get the selector generation and update it to the current generation.
6269 unsigned &Generation = SelectorGeneration[Sel];
6270 unsigned PriorGeneration = Generation;
6271 Generation = CurrentGeneration;
6272
6273 // Search for methods defined with this selector.
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006274 ++NumMethodPoolLookups;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006275 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6276 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6277
6278 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006279 Visitor.getFactoryMethods().empty())
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006280 return;
Douglas Gregor95fb36e2013-01-28 17:54:36 +00006281
6282 ++NumMethodPoolHits;
6283
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006284 if (!getSema())
6285 return;
6286
6287 Sema &S = *getSema();
6288 Sema::GlobalMethodPool::iterator Pos
6289 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6290
6291 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6292 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00006293 Pos->second.first.setBits(Visitor.getInstanceBits());
6294 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006295}
6296
6297void ASTReader::ReadKnownNamespaces(
6298 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6299 Namespaces.clear();
6300
6301 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6302 if (NamespaceDecl *Namespace
6303 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6304 Namespaces.push_back(Namespace);
6305 }
6306}
6307
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006308void ASTReader::ReadUndefinedButUsed(
Nick Lewycky995e26b2013-01-31 03:23:57 +00006309 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006310 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6311 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky01a41142013-01-26 00:35:08 +00006312 SourceLocation Loc =
Nick Lewyckycd0655b2013-02-01 08:13:20 +00006313 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky01a41142013-01-26 00:35:08 +00006314 Undefined.insert(std::make_pair(D, Loc));
6315 }
6316}
Nick Lewycky01a41142013-01-26 00:35:08 +00006317
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006318void ASTReader::ReadTentativeDefinitions(
6319 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6320 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6321 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6322 if (Var)
6323 TentativeDefs.push_back(Var);
6324 }
6325 TentativeDefinitions.clear();
6326}
6327
6328void ASTReader::ReadUnusedFileScopedDecls(
6329 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6330 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6331 DeclaratorDecl *D
6332 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6333 if (D)
6334 Decls.push_back(D);
6335 }
6336 UnusedFileScopedDecls.clear();
6337}
6338
6339void ASTReader::ReadDelegatingConstructors(
6340 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6341 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6342 CXXConstructorDecl *D
6343 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6344 if (D)
6345 Decls.push_back(D);
6346 }
6347 DelegatingCtorDecls.clear();
6348}
6349
6350void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6351 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6352 TypedefNameDecl *D
6353 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6354 if (D)
6355 Decls.push_back(D);
6356 }
6357 ExtVectorDecls.clear();
6358}
6359
6360void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6361 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6362 CXXRecordDecl *D
6363 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6364 if (D)
6365 Decls.push_back(D);
6366 }
6367 DynamicClasses.clear();
6368}
6369
6370void
Richard Smith5ea6ef42013-01-10 23:43:47 +00006371ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6372 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6373 NamedDecl *D
6374 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006375 if (D)
6376 Decls.push_back(D);
6377 }
Richard Smith5ea6ef42013-01-10 23:43:47 +00006378 LocallyScopedExternCDecls.clear();
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006379}
6380
6381void ASTReader::ReadReferencedSelectors(
6382 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6383 if (ReferencedSelectorsData.empty())
6384 return;
6385
6386 // If there are @selector references added them to its pool. This is for
6387 // implementation of -Wselector.
6388 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6389 unsigned I = 0;
6390 while (I < DataSize) {
6391 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6392 SourceLocation SelLoc
6393 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6394 Sels.push_back(std::make_pair(Sel, SelLoc));
6395 }
6396 ReferencedSelectorsData.clear();
6397}
6398
6399void ASTReader::ReadWeakUndeclaredIdentifiers(
6400 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6401 if (WeakUndeclaredIdentifiers.empty())
6402 return;
6403
6404 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6405 IdentifierInfo *WeakId
6406 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6407 IdentifierInfo *AliasId
6408 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6409 SourceLocation Loc
6410 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6411 bool Used = WeakUndeclaredIdentifiers[I++];
6412 WeakInfo WI(AliasId, Loc);
6413 WI.setUsed(Used);
6414 WeakIDs.push_back(std::make_pair(WeakId, WI));
6415 }
6416 WeakUndeclaredIdentifiers.clear();
6417}
6418
6419void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6420 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6421 ExternalVTableUse VT;
6422 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6423 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6424 VT.DefinitionRequired = VTableUses[Idx++];
6425 VTables.push_back(VT);
6426 }
6427
6428 VTableUses.clear();
6429}
6430
6431void ASTReader::ReadPendingInstantiations(
6432 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6433 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6434 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6435 SourceLocation Loc
6436 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6437
6438 Pending.push_back(std::make_pair(D, Loc));
6439 }
6440 PendingInstantiations.clear();
6441}
6442
6443void ASTReader::LoadSelector(Selector Sel) {
6444 // It would be complicated to avoid reading the methods anyway. So don't.
6445 ReadMethodPool(Sel);
6446}
6447
6448void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6449 assert(ID && "Non-zero identifier ID required");
6450 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6451 IdentifiersLoaded[ID - 1] = II;
6452 if (DeserializationListener)
6453 DeserializationListener->IdentifierRead(ID, II);
6454}
6455
6456/// \brief Set the globally-visible declarations associated with the given
6457/// identifier.
6458///
6459/// If the AST reader is currently in a state where the given declaration IDs
6460/// cannot safely be resolved, they are queued until it is safe to resolve
6461/// them.
6462///
6463/// \param II an IdentifierInfo that refers to one or more globally-visible
6464/// declarations.
6465///
6466/// \param DeclIDs the set of declaration IDs with the name @p II that are
6467/// visible at global scope.
6468///
Douglas Gregoraa945902013-02-18 15:53:43 +00006469/// \param Decls if non-null, this vector will be populated with the set of
6470/// deserialized declarations. These declarations will not be pushed into
6471/// scope.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006472void
6473ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6474 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregoraa945902013-02-18 15:53:43 +00006475 SmallVectorImpl<Decl *> *Decls) {
6476 if (NumCurrentElementsDeserializing && !Decls) {
6477 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006478 return;
6479 }
6480
6481 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6482 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6483 if (SemaObj) {
Douglas Gregoraa945902013-02-18 15:53:43 +00006484 // If we're simply supposed to record the declarations, do so now.
6485 if (Decls) {
6486 Decls->push_back(D);
6487 continue;
6488 }
6489
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006490 // Introduce this declaration into the translation-unit scope
6491 // and add it to the declaration chain for this identifier, so
6492 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00006493 pushExternalDeclIntoScope(D, II);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006494 } else {
6495 // Queue this declaration so that it will be added to the
6496 // translation unit scope and identifier's declaration chain
6497 // once a Sema object is known.
6498 PreloadedDecls.push_back(D);
6499 }
6500 }
6501}
6502
Douglas Gregor8222b892013-01-21 16:52:34 +00006503IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006504 if (ID == 0)
6505 return 0;
6506
6507 if (IdentifiersLoaded.empty()) {
6508 Error("no identifier table in AST file");
6509 return 0;
6510 }
6511
6512 ID -= 1;
6513 if (!IdentifiersLoaded[ID]) {
6514 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6515 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6516 ModuleFile *M = I->second;
6517 unsigned Index = ID - M->BaseIdentifierID;
6518 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6519
6520 // All of the strings in the AST file are preceded by a 16-bit length.
6521 // Extract that 16-bit length to avoid having to execute strlen().
6522 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6523 // unsigned integers. This is important to avoid integer overflow when
6524 // we cast them to 'unsigned'.
6525 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6526 unsigned StrLen = (((unsigned) StrLenPtr[0])
6527 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregor8222b892013-01-21 16:52:34 +00006528 IdentifiersLoaded[ID]
6529 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006530 if (DeserializationListener)
6531 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6532 }
6533
6534 return IdentifiersLoaded[ID];
6535}
6536
Douglas Gregor8222b892013-01-21 16:52:34 +00006537IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6538 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006539}
6540
6541IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6542 if (LocalID < NUM_PREDEF_IDENT_IDS)
6543 return LocalID;
6544
6545 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6546 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6547 assert(I != M.IdentifierRemap.end()
6548 && "Invalid index into identifier index remap");
6549
6550 return LocalID + I->second;
6551}
6552
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006553MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006554 if (ID == 0)
6555 return 0;
6556
6557 if (MacrosLoaded.empty()) {
6558 Error("no macro table in AST file");
6559 return 0;
6560 }
6561
6562 ID -= NUM_PREDEF_MACRO_IDS;
6563 if (!MacrosLoaded[ID]) {
6564 GlobalMacroMapType::iterator I
6565 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6566 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6567 ModuleFile *M = I->second;
6568 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00006569 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
6570
6571 if (DeserializationListener)
6572 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
6573 MacrosLoaded[ID]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006574 }
6575
6576 return MacrosLoaded[ID];
6577}
6578
6579MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6580 if (LocalID < NUM_PREDEF_MACRO_IDS)
6581 return LocalID;
6582
6583 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6584 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6585 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6586
6587 return LocalID + I->second;
6588}
6589
6590serialization::SubmoduleID
6591ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6592 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6593 return LocalID;
6594
6595 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6596 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6597 assert(I != M.SubmoduleRemap.end()
6598 && "Invalid index into submodule index remap");
6599
6600 return LocalID + I->second;
6601}
6602
6603Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6604 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6605 assert(GlobalID == 0 && "Unhandled global submodule ID");
6606 return 0;
6607 }
6608
6609 if (GlobalID > SubmodulesLoaded.size()) {
6610 Error("submodule ID out of range in AST file");
6611 return 0;
6612 }
6613
6614 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6615}
Douglas Gregorca2ab452013-01-12 01:29:50 +00006616
6617Module *ASTReader::getModule(unsigned ID) {
6618 return getSubmodule(ID);
6619}
6620
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006621Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6622 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6623}
6624
6625Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6626 if (ID == 0)
6627 return Selector();
6628
6629 if (ID > SelectorsLoaded.size()) {
6630 Error("selector ID out of range in AST file");
6631 return Selector();
6632 }
6633
6634 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6635 // Load this selector from the selector table.
6636 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6637 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6638 ModuleFile &M = *I->second;
6639 ASTSelectorLookupTrait Trait(*this, M);
6640 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6641 SelectorsLoaded[ID - 1] =
6642 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6643 if (DeserializationListener)
6644 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6645 }
6646
6647 return SelectorsLoaded[ID - 1];
6648}
6649
6650Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6651 return DecodeSelector(ID);
6652}
6653
6654uint32_t ASTReader::GetNumExternalSelectors() {
6655 // ID 0 (the null selector) is considered an external selector.
6656 return getTotalNumSelectors() + 1;
6657}
6658
6659serialization::SelectorID
6660ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6661 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6662 return LocalID;
6663
6664 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6665 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6666 assert(I != M.SelectorRemap.end()
6667 && "Invalid index into selector index remap");
6668
6669 return LocalID + I->second;
6670}
6671
6672DeclarationName
6673ASTReader::ReadDeclarationName(ModuleFile &F,
6674 const RecordData &Record, unsigned &Idx) {
6675 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6676 switch (Kind) {
6677 case DeclarationName::Identifier:
Douglas Gregor8222b892013-01-21 16:52:34 +00006678 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006679
6680 case DeclarationName::ObjCZeroArgSelector:
6681 case DeclarationName::ObjCOneArgSelector:
6682 case DeclarationName::ObjCMultiArgSelector:
6683 return DeclarationName(ReadSelector(F, Record, Idx));
6684
6685 case DeclarationName::CXXConstructorName:
6686 return Context.DeclarationNames.getCXXConstructorName(
6687 Context.getCanonicalType(readType(F, Record, Idx)));
6688
6689 case DeclarationName::CXXDestructorName:
6690 return Context.DeclarationNames.getCXXDestructorName(
6691 Context.getCanonicalType(readType(F, Record, Idx)));
6692
6693 case DeclarationName::CXXConversionFunctionName:
6694 return Context.DeclarationNames.getCXXConversionFunctionName(
6695 Context.getCanonicalType(readType(F, Record, Idx)));
6696
6697 case DeclarationName::CXXOperatorName:
6698 return Context.DeclarationNames.getCXXOperatorName(
6699 (OverloadedOperatorKind)Record[Idx++]);
6700
6701 case DeclarationName::CXXLiteralOperatorName:
6702 return Context.DeclarationNames.getCXXLiteralOperatorName(
6703 GetIdentifierInfo(F, Record, Idx));
6704
6705 case DeclarationName::CXXUsingDirective:
6706 return DeclarationName::getUsingDirectiveName();
6707 }
6708
6709 llvm_unreachable("Invalid NameKind!");
6710}
6711
6712void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6713 DeclarationNameLoc &DNLoc,
6714 DeclarationName Name,
6715 const RecordData &Record, unsigned &Idx) {
6716 switch (Name.getNameKind()) {
6717 case DeclarationName::CXXConstructorName:
6718 case DeclarationName::CXXDestructorName:
6719 case DeclarationName::CXXConversionFunctionName:
6720 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6721 break;
6722
6723 case DeclarationName::CXXOperatorName:
6724 DNLoc.CXXOperatorName.BeginOpNameLoc
6725 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6726 DNLoc.CXXOperatorName.EndOpNameLoc
6727 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6728 break;
6729
6730 case DeclarationName::CXXLiteralOperatorName:
6731 DNLoc.CXXLiteralOperatorName.OpNameLoc
6732 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6733 break;
6734
6735 case DeclarationName::Identifier:
6736 case DeclarationName::ObjCZeroArgSelector:
6737 case DeclarationName::ObjCOneArgSelector:
6738 case DeclarationName::ObjCMultiArgSelector:
6739 case DeclarationName::CXXUsingDirective:
6740 break;
6741 }
6742}
6743
6744void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6745 DeclarationNameInfo &NameInfo,
6746 const RecordData &Record, unsigned &Idx) {
6747 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6748 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6749 DeclarationNameLoc DNLoc;
6750 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6751 NameInfo.setInfo(DNLoc);
6752}
6753
6754void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6755 const RecordData &Record, unsigned &Idx) {
6756 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6757 unsigned NumTPLists = Record[Idx++];
6758 Info.NumTemplParamLists = NumTPLists;
6759 if (NumTPLists) {
6760 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6761 for (unsigned i=0; i != NumTPLists; ++i)
6762 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6763 }
6764}
6765
6766TemplateName
6767ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6768 unsigned &Idx) {
6769 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6770 switch (Kind) {
6771 case TemplateName::Template:
6772 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6773
6774 case TemplateName::OverloadedTemplate: {
6775 unsigned size = Record[Idx++];
6776 UnresolvedSet<8> Decls;
6777 while (size--)
6778 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6779
6780 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6781 }
6782
6783 case TemplateName::QualifiedTemplate: {
6784 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6785 bool hasTemplKeyword = Record[Idx++];
6786 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6787 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6788 }
6789
6790 case TemplateName::DependentTemplate: {
6791 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6792 if (Record[Idx++]) // isIdentifier
6793 return Context.getDependentTemplateName(NNS,
6794 GetIdentifierInfo(F, Record,
6795 Idx));
6796 return Context.getDependentTemplateName(NNS,
6797 (OverloadedOperatorKind)Record[Idx++]);
6798 }
6799
6800 case TemplateName::SubstTemplateTemplateParm: {
6801 TemplateTemplateParmDecl *param
6802 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6803 if (!param) return TemplateName();
6804 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6805 return Context.getSubstTemplateTemplateParm(param, replacement);
6806 }
6807
6808 case TemplateName::SubstTemplateTemplateParmPack: {
6809 TemplateTemplateParmDecl *Param
6810 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6811 if (!Param)
6812 return TemplateName();
6813
6814 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6815 if (ArgPack.getKind() != TemplateArgument::Pack)
6816 return TemplateName();
6817
6818 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6819 }
6820 }
6821
6822 llvm_unreachable("Unhandled template name kind!");
6823}
6824
6825TemplateArgument
6826ASTReader::ReadTemplateArgument(ModuleFile &F,
6827 const RecordData &Record, unsigned &Idx) {
6828 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6829 switch (Kind) {
6830 case TemplateArgument::Null:
6831 return TemplateArgument();
6832 case TemplateArgument::Type:
6833 return TemplateArgument(readType(F, Record, Idx));
6834 case TemplateArgument::Declaration: {
6835 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6836 bool ForReferenceParam = Record[Idx++];
6837 return TemplateArgument(D, ForReferenceParam);
6838 }
6839 case TemplateArgument::NullPtr:
6840 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6841 case TemplateArgument::Integral: {
6842 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6843 QualType T = readType(F, Record, Idx);
6844 return TemplateArgument(Context, Value, T);
6845 }
6846 case TemplateArgument::Template:
6847 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6848 case TemplateArgument::TemplateExpansion: {
6849 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikiedc84cd52013-02-20 22:23:23 +00006850 Optional<unsigned> NumTemplateExpansions;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00006851 if (unsigned NumExpansions = Record[Idx++])
6852 NumTemplateExpansions = NumExpansions - 1;
6853 return TemplateArgument(Name, NumTemplateExpansions);
6854 }
6855 case TemplateArgument::Expression:
6856 return TemplateArgument(ReadExpr(F));
6857 case TemplateArgument::Pack: {
6858 unsigned NumArgs = Record[Idx++];
6859 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6860 for (unsigned I = 0; I != NumArgs; ++I)
6861 Args[I] = ReadTemplateArgument(F, Record, Idx);
6862 return TemplateArgument(Args, NumArgs);
6863 }
6864 }
6865
6866 llvm_unreachable("Unhandled template argument kind!");
6867}
6868
6869TemplateParameterList *
6870ASTReader::ReadTemplateParameterList(ModuleFile &F,
6871 const RecordData &Record, unsigned &Idx) {
6872 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6873 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6874 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6875
6876 unsigned NumParams = Record[Idx++];
6877 SmallVector<NamedDecl *, 16> Params;
6878 Params.reserve(NumParams);
6879 while (NumParams--)
6880 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6881
6882 TemplateParameterList* TemplateParams =
6883 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6884 Params.data(), Params.size(), RAngleLoc);
6885 return TemplateParams;
6886}
6887
6888void
6889ASTReader::
6890ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6891 ModuleFile &F, const RecordData &Record,
6892 unsigned &Idx) {
6893 unsigned NumTemplateArgs = Record[Idx++];
6894 TemplArgs.reserve(NumTemplateArgs);
6895 while (NumTemplateArgs--)
6896 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6897}
6898
6899/// \brief Read a UnresolvedSet structure.
6900void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6901 const RecordData &Record, unsigned &Idx) {
6902 unsigned NumDecls = Record[Idx++];
6903 Set.reserve(Context, NumDecls);
6904 while (NumDecls--) {
6905 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6906 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6907 Set.addDecl(Context, D, AS);
6908 }
6909}
6910
6911CXXBaseSpecifier
6912ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6913 const RecordData &Record, unsigned &Idx) {
6914 bool isVirtual = static_cast<bool>(Record[Idx++]);
6915 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6916 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6917 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6918 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6919 SourceRange Range = ReadSourceRange(F, Record, Idx);
6920 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6921 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6922 EllipsisLoc);
6923 Result.setInheritConstructors(inheritConstructors);
6924 return Result;
6925}
6926
6927std::pair<CXXCtorInitializer **, unsigned>
6928ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6929 unsigned &Idx) {
6930 CXXCtorInitializer **CtorInitializers = 0;
6931 unsigned NumInitializers = Record[Idx++];
6932 if (NumInitializers) {
6933 CtorInitializers
6934 = new (Context) CXXCtorInitializer*[NumInitializers];
6935 for (unsigned i=0; i != NumInitializers; ++i) {
6936 TypeSourceInfo *TInfo = 0;
6937 bool IsBaseVirtual = false;
6938 FieldDecl *Member = 0;
6939 IndirectFieldDecl *IndirectMember = 0;
6940
6941 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6942 switch (Type) {
6943 case CTOR_INITIALIZER_BASE:
6944 TInfo = GetTypeSourceInfo(F, Record, Idx);
6945 IsBaseVirtual = Record[Idx++];
6946 break;
6947
6948 case CTOR_INITIALIZER_DELEGATING:
6949 TInfo = GetTypeSourceInfo(F, Record, Idx);
6950 break;
6951
6952 case CTOR_INITIALIZER_MEMBER:
6953 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6954 break;
6955
6956 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6957 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6958 break;
6959 }
6960
6961 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6962 Expr *Init = ReadExpr(F);
6963 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6964 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6965 bool IsWritten = Record[Idx++];
6966 unsigned SourceOrderOrNumArrayIndices;
6967 SmallVector<VarDecl *, 8> Indices;
6968 if (IsWritten) {
6969 SourceOrderOrNumArrayIndices = Record[Idx++];
6970 } else {
6971 SourceOrderOrNumArrayIndices = Record[Idx++];
6972 Indices.reserve(SourceOrderOrNumArrayIndices);
6973 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6974 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6975 }
6976
6977 CXXCtorInitializer *BOMInit;
6978 if (Type == CTOR_INITIALIZER_BASE) {
6979 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6980 LParenLoc, Init, RParenLoc,
6981 MemberOrEllipsisLoc);
6982 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6983 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6984 Init, RParenLoc);
6985 } else if (IsWritten) {
6986 if (Member)
6987 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6988 LParenLoc, Init, RParenLoc);
6989 else
6990 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6991 MemberOrEllipsisLoc, LParenLoc,
6992 Init, RParenLoc);
6993 } else {
6994 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6995 LParenLoc, Init, RParenLoc,
6996 Indices.data(), Indices.size());
6997 }
6998
6999 if (IsWritten)
7000 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7001 CtorInitializers[i] = BOMInit;
7002 }
7003 }
7004
7005 return std::make_pair(CtorInitializers, NumInitializers);
7006}
7007
7008NestedNameSpecifier *
7009ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7010 const RecordData &Record, unsigned &Idx) {
7011 unsigned N = Record[Idx++];
7012 NestedNameSpecifier *NNS = 0, *Prev = 0;
7013 for (unsigned I = 0; I != N; ++I) {
7014 NestedNameSpecifier::SpecifierKind Kind
7015 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7016 switch (Kind) {
7017 case NestedNameSpecifier::Identifier: {
7018 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7019 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7020 break;
7021 }
7022
7023 case NestedNameSpecifier::Namespace: {
7024 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7025 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7026 break;
7027 }
7028
7029 case NestedNameSpecifier::NamespaceAlias: {
7030 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7031 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7032 break;
7033 }
7034
7035 case NestedNameSpecifier::TypeSpec:
7036 case NestedNameSpecifier::TypeSpecWithTemplate: {
7037 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7038 if (!T)
7039 return 0;
7040
7041 bool Template = Record[Idx++];
7042 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7043 break;
7044 }
7045
7046 case NestedNameSpecifier::Global: {
7047 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7048 // No associated value, and there can't be a prefix.
7049 break;
7050 }
7051 }
7052 Prev = NNS;
7053 }
7054 return NNS;
7055}
7056
7057NestedNameSpecifierLoc
7058ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7059 unsigned &Idx) {
7060 unsigned N = Record[Idx++];
7061 NestedNameSpecifierLocBuilder Builder;
7062 for (unsigned I = 0; I != N; ++I) {
7063 NestedNameSpecifier::SpecifierKind Kind
7064 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7065 switch (Kind) {
7066 case NestedNameSpecifier::Identifier: {
7067 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7068 SourceRange Range = ReadSourceRange(F, Record, Idx);
7069 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7070 break;
7071 }
7072
7073 case NestedNameSpecifier::Namespace: {
7074 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7075 SourceRange Range = ReadSourceRange(F, Record, Idx);
7076 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7077 break;
7078 }
7079
7080 case NestedNameSpecifier::NamespaceAlias: {
7081 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7082 SourceRange Range = ReadSourceRange(F, Record, Idx);
7083 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7084 break;
7085 }
7086
7087 case NestedNameSpecifier::TypeSpec:
7088 case NestedNameSpecifier::TypeSpecWithTemplate: {
7089 bool Template = Record[Idx++];
7090 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7091 if (!T)
7092 return NestedNameSpecifierLoc();
7093 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7094
7095 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7096 Builder.Extend(Context,
7097 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7098 T->getTypeLoc(), ColonColonLoc);
7099 break;
7100 }
7101
7102 case NestedNameSpecifier::Global: {
7103 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7104 Builder.MakeGlobal(Context, ColonColonLoc);
7105 break;
7106 }
7107 }
7108 }
7109
7110 return Builder.getWithLocInContext(Context);
7111}
7112
7113SourceRange
7114ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7115 unsigned &Idx) {
7116 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7117 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7118 return SourceRange(beg, end);
7119}
7120
7121/// \brief Read an integral value
7122llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7123 unsigned BitWidth = Record[Idx++];
7124 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7125 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7126 Idx += NumWords;
7127 return Result;
7128}
7129
7130/// \brief Read a signed integral value
7131llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7132 bool isUnsigned = Record[Idx++];
7133 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7134}
7135
7136/// \brief Read a floating-point value
Tim Northover9ec55f22013-01-22 09:46:51 +00007137llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7138 const llvm::fltSemantics &Sem,
7139 unsigned &Idx) {
7140 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007141}
7142
7143// \brief Read a string
7144std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7145 unsigned Len = Record[Idx++];
7146 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7147 Idx += Len;
7148 return Result;
7149}
7150
7151VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7152 unsigned &Idx) {
7153 unsigned Major = Record[Idx++];
7154 unsigned Minor = Record[Idx++];
7155 unsigned Subminor = Record[Idx++];
7156 if (Minor == 0)
7157 return VersionTuple(Major);
7158 if (Subminor == 0)
7159 return VersionTuple(Major, Minor - 1);
7160 return VersionTuple(Major, Minor - 1, Subminor - 1);
7161}
7162
7163CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7164 const RecordData &Record,
7165 unsigned &Idx) {
7166 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7167 return CXXTemporary::Create(Context, Decl);
7168}
7169
7170DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7171 return Diag(SourceLocation(), DiagID);
7172}
7173
7174DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7175 return Diags.Report(Loc, DiagID);
7176}
7177
7178/// \brief Retrieve the identifier table associated with the
7179/// preprocessor.
7180IdentifierTable &ASTReader::getIdentifierTable() {
7181 return PP.getIdentifierTable();
7182}
7183
7184/// \brief Record that the given ID maps to the given switch-case
7185/// statement.
7186void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7187 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7188 "Already have a SwitchCase with this ID");
7189 (*CurrSwitchCaseStmts)[ID] = SC;
7190}
7191
7192/// \brief Retrieve the switch-case statement with the given ID.
7193SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7194 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7195 return (*CurrSwitchCaseStmts)[ID];
7196}
7197
7198void ASTReader::ClearSwitchCaseIDs() {
7199 CurrSwitchCaseStmts->clear();
7200}
7201
7202void ASTReader::ReadComments() {
7203 std::vector<RawComment *> Comments;
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007204 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007205 serialization::ModuleFile *> >::iterator
7206 I = CommentsCursors.begin(),
7207 E = CommentsCursors.end();
7208 I != E; ++I) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007209 BitstreamCursor &Cursor = I->first;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007210 serialization::ModuleFile &F = *I->second;
7211 SavedStreamPosition SavedPosition(Cursor);
7212
7213 RecordData Record;
7214 while (true) {
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007215 llvm::BitstreamEntry Entry =
7216 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7217
7218 switch (Entry.Kind) {
7219 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7220 case llvm::BitstreamEntry::Error:
7221 Error("malformed block record in AST file");
7222 return;
7223 case llvm::BitstreamEntry::EndBlock:
7224 goto NextCursor;
7225 case llvm::BitstreamEntry::Record:
7226 // The interesting case.
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007227 break;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007228 }
7229
7230 // Read a record.
7231 Record.clear();
Chris Lattnerb3ce3572013-01-20 02:38:54 +00007232 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007233 case COMMENTS_RAW_COMMENT: {
7234 unsigned Idx = 0;
7235 SourceRange SR = ReadSourceRange(F, Record, Idx);
7236 RawComment::CommentKind Kind =
7237 (RawComment::CommentKind) Record[Idx++];
7238 bool IsTrailingComment = Record[Idx++];
7239 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00007240 Comments.push_back(new (Context) RawComment(
7241 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7242 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007243 break;
7244 }
7245 }
7246 }
Chris Lattner8f9a1eb2013-01-20 00:56:42 +00007247 NextCursor:;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007248 }
7249 Context.Comments.addCommentsToFront(Comments);
7250}
7251
7252void ASTReader::finishPendingActions() {
7253 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007254 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007255 // If any identifiers with corresponding top-level declarations have
7256 // been loaded, load those declarations now.
Douglas Gregoraa945902013-02-18 15:53:43 +00007257 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007258 while (!PendingIdentifierInfos.empty()) {
Douglas Gregoraa945902013-02-18 15:53:43 +00007259 // FIXME: std::move
7260 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7261 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcc9bdcb2013-02-19 18:26:28 +00007262 PendingIdentifierInfos.pop_back();
Douglas Gregoraa945902013-02-18 15:53:43 +00007263
7264 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007265 }
7266
7267 // Load pending declaration chains.
7268 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7269 loadPendingDeclChain(PendingDeclChains[I]);
7270 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7271 }
7272 PendingDeclChains.clear();
7273
Douglas Gregoraa945902013-02-18 15:53:43 +00007274 // Make the most recent of the top-level declarations visible.
7275 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7276 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7277 TLD != TLDEnd; ++TLD) {
7278 IdentifierInfo *II = TLD->first;
7279 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007280 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregoraa945902013-02-18 15:53:43 +00007281 }
7282 }
7283
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007284 // Load any pending macro definitions.
7285 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007286 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7287 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7288 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7289 // Initialize the macro history from chained-PCHs ahead of module imports.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00007290 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7291 ++IDIdx) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00007292 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7293 if (Info.M->Kind != MK_Module)
7294 resolvePendingMacro(II, Info);
7295 }
7296 // Handle module imports.
7297 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7298 ++IDIdx) {
7299 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7300 if (Info.M->Kind == MK_Module)
7301 resolvePendingMacro(II, Info);
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007302 }
7303 }
7304 PendingMacroIDs.clear();
Argyrios Kyrtzidis7640b022013-02-16 00:48:59 +00007305
7306 // Wire up the DeclContexts for Decls that we delayed setting until
7307 // recursive loading is completed.
7308 while (!PendingDeclContextInfos.empty()) {
7309 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7310 PendingDeclContextInfos.pop_front();
7311 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7312 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7313 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7314 }
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007315 }
7316
7317 // If we deserialized any C++ or Objective-C class definitions, any
7318 // Objective-C protocol definitions, or any redeclarable templates, make sure
7319 // that all redeclarations point to the definitions. Note that this can only
7320 // happen now, after the redeclaration chains have been fully wired.
7321 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7322 DEnd = PendingDefinitions.end();
7323 D != DEnd; ++D) {
7324 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7325 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7326 // Make sure that the TagType points at the definition.
7327 const_cast<TagType*>(TagT)->decl = TD;
7328 }
7329
7330 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7331 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7332 REnd = RD->redecls_end();
7333 R != REnd; ++R)
7334 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7335
7336 }
7337
7338 continue;
7339 }
7340
7341 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7342 // Make sure that the ObjCInterfaceType points at the definition.
7343 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7344 ->Decl = ID;
7345
7346 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7347 REnd = ID->redecls_end();
7348 R != REnd; ++R)
7349 R->Data = ID->Data;
7350
7351 continue;
7352 }
7353
7354 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7355 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7356 REnd = PD->redecls_end();
7357 R != REnd; ++R)
7358 R->Data = PD->Data;
7359
7360 continue;
7361 }
7362
7363 RedeclarableTemplateDecl *RTD
7364 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7365 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7366 REnd = RTD->redecls_end();
7367 R != REnd; ++R)
7368 R->Common = RTD->Common;
7369 }
7370 PendingDefinitions.clear();
7371
7372 // Load the bodies of any functions or methods we've encountered. We do
7373 // this now (delayed) so that we can be sure that the declaration chains
7374 // have been fully wired up.
7375 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7376 PBEnd = PendingBodies.end();
7377 PB != PBEnd; ++PB) {
7378 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7379 // FIXME: Check for =delete/=default?
7380 // FIXME: Complain about ODR violations here?
7381 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7382 FD->setLazyBody(PB->second);
7383 continue;
7384 }
7385
7386 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7387 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7388 MD->setLazyBody(PB->second);
7389 }
7390 PendingBodies.clear();
7391}
7392
7393void ASTReader::FinishedDeserializing() {
7394 assert(NumCurrentElementsDeserializing &&
7395 "FinishedDeserializing not paired with StartedDeserializing");
7396 if (NumCurrentElementsDeserializing == 1) {
7397 // We decrease NumCurrentElementsDeserializing only after pending actions
7398 // are finished, to avoid recursively re-calling finishPendingActions().
7399 finishPendingActions();
7400 }
7401 --NumCurrentElementsDeserializing;
7402
7403 if (NumCurrentElementsDeserializing == 0 &&
7404 Consumer && !PassingDeclsToConsumer) {
7405 // Guard variable to avoid recursively redoing the process of passing
7406 // decls to consumer.
7407 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7408 true);
7409
7410 while (!InterestingDecls.empty()) {
7411 // We are not in recursive loading, so it's safe to pass the "interesting"
7412 // decls to the consumer.
7413 Decl *D = InterestingDecls.front();
7414 InterestingDecls.pop_front();
7415 PassInterestingDeclToConsumer(D);
7416 }
7417 }
7418}
7419
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00007420void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
7421 D = cast<NamedDecl>(D->getMostRecentDecl());
7422
7423 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7424 SemaObj->TUScope->AddDecl(D);
7425 } else if (SemaObj->TUScope) {
7426 // Adding the decl to IdResolver may have failed because it was already in
7427 // (even though it was not added in scope). If it is already in, make sure
7428 // it gets in the scope as well.
7429 if (std::find(SemaObj->IdResolver.begin(Name),
7430 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7431 SemaObj->TUScope->AddDecl(D);
7432 }
7433}
7434
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007435ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7436 StringRef isysroot, bool DisableValidation,
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007437 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007438 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7439 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7440 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7441 Consumer(0), ModuleMgr(PP.getFileManager()),
7442 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregore1698072013-01-25 00:38:33 +00007443 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorf575d6e2013-01-25 00:45:27 +00007444 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007445 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7446 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregore1698072013-01-25 00:38:33 +00007447 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7448 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7449 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregor95fb36e2013-01-28 17:54:36 +00007450 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7451 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7452 TotalNumMethodPoolEntries(0),
Guy Benyei7f92f2d2012-12-18 14:30:41 +00007453 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7454 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7455 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7456 PassingDeclsToConsumer(false),
7457 NumCXXBaseSpecifiersLoaded(0)
7458{
7459 SourceMgr.setExternalSLocEntrySource(this);
7460}
7461
7462ASTReader::~ASTReader() {
7463 for (DeclContextVisibleUpdatesPending::iterator
7464 I = PendingVisibleUpdates.begin(),
7465 E = PendingVisibleUpdates.end();
7466 I != E; ++I) {
7467 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7468 F = I->second.end();
7469 J != F; ++J)
7470 delete J->first;
7471 }
7472}