blob: 0f674530bc6706c256b6584a7ecec41d592f9abb [file] [log] [blame]
Nick Lewyckyf0f56162013-01-31 03:23:57 +00001//===--- ASTReader.cpp - AST File Reader ----------------------------------===//
Guy Benyei11169dd2012-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 Benyei11169dd2012-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 Gregore060e572013-01-25 01:03:03 +000041#include "clang/Serialization/GlobalModuleIndex.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000042#include "clang/Serialization/ModuleManager.h"
43#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000044#include "llvm/ADT/Hashing.h"
Guy Benyei11169dd2012-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 Lattner91f373e2013-01-20 00:57:52 +000054#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000055#include <iterator>
56
57using namespace clang;
58using namespace clang::serialization;
59using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000060using llvm::BitstreamCursor;
Guy Benyei11169dd2012-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 Gribenkoacf2e782013-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 Benyei11169dd2012-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,
260 std::string &SuggestedPredefines) {
261 // Check macro definitions.
262 MacroDefinitionsMap ASTFileMacros;
263 collectMacroDefinitions(PPOpts, ASTFileMacros);
264 MacroDefinitionsMap ExistingMacros;
265 SmallVector<StringRef, 4> ExistingMacroNames;
266 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
267
268 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
269 // Dig out the macro definition in the existing preprocessor options.
270 StringRef MacroName = ExistingMacroNames[I];
271 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
272
273 // Check whether we know anything about this macro name or not.
274 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
275 = ASTFileMacros.find(MacroName);
276 if (Known == ASTFileMacros.end()) {
277 // FIXME: Check whether this identifier was referenced anywhere in the
278 // AST file. If so, we should reject the AST file. Unfortunately, this
279 // information isn't in the control block. What shall we do about it?
280
281 if (Existing.second) {
282 SuggestedPredefines += "#undef ";
283 SuggestedPredefines += MacroName.str();
284 SuggestedPredefines += '\n';
285 } else {
286 SuggestedPredefines += "#define ";
287 SuggestedPredefines += MacroName.str();
288 SuggestedPredefines += ' ';
289 SuggestedPredefines += Existing.first.str();
290 SuggestedPredefines += '\n';
291 }
292 continue;
293 }
294
295 // If the macro was defined in one but undef'd in the other, we have a
296 // conflict.
297 if (Existing.second != Known->second.second) {
298 if (Diags) {
299 Diags->Report(diag::err_pch_macro_def_undef)
300 << MacroName << Known->second.second;
301 }
302 return true;
303 }
304
305 // If the macro was #undef'd in both, or if the macro bodies are identical,
306 // it's fine.
307 if (Existing.second || Existing.first == Known->second.first)
308 continue;
309
310 // The macro bodies differ; complain.
311 if (Diags) {
312 Diags->Report(diag::err_pch_macro_def_conflict)
313 << MacroName << Known->second.first << Existing.first;
314 }
315 return true;
316 }
317
318 // Check whether we're using predefines.
319 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
320 if (Diags) {
321 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
322 }
323 return true;
324 }
325
326 // Compute the #include and #include_macros lines we need.
327 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
328 StringRef File = ExistingPPOpts.Includes[I];
329 if (File == ExistingPPOpts.ImplicitPCHInclude)
330 continue;
331
332 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
333 != PPOpts.Includes.end())
334 continue;
335
336 SuggestedPredefines += "#include \"";
337 SuggestedPredefines +=
338 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
339 SuggestedPredefines += "\"\n";
340 }
341
342 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
343 StringRef File = ExistingPPOpts.MacroIncludes[I];
344 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
345 File)
346 != PPOpts.MacroIncludes.end())
347 continue;
348
349 SuggestedPredefines += "#__include_macros \"";
350 SuggestedPredefines +=
351 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
352 SuggestedPredefines += "\"\n##\n";
353 }
354
355 return false;
356}
357
358bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
359 bool Complain,
360 std::string &SuggestedPredefines) {
361 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
362
363 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
364 Complain? &Reader.Diags : 0,
365 PP.getFileManager(),
366 SuggestedPredefines);
367}
368
369void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
370 unsigned ID) {
371 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
372 ++NumHeaderInfos;
373}
374
375void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
376 PP.setCounterValue(Value);
377}
378
379//===----------------------------------------------------------------------===//
380// AST reader implementation
381//===----------------------------------------------------------------------===//
382
383void
384ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
385 DeserializationListener = Listener;
386}
387
388
389
390unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
391 return serialization::ComputeHash(Sel);
392}
393
394
395std::pair<unsigned, unsigned>
396ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
397 using namespace clang::io;
398 unsigned KeyLen = ReadUnalignedLE16(d);
399 unsigned DataLen = ReadUnalignedLE16(d);
400 return std::make_pair(KeyLen, DataLen);
401}
402
403ASTSelectorLookupTrait::internal_key_type
404ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
405 using namespace clang::io;
406 SelectorTable &SelTable = Reader.getContext().Selectors;
407 unsigned N = ReadUnalignedLE16(d);
408 IdentifierInfo *FirstII
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000409 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000410 if (N == 0)
411 return SelTable.getNullarySelector(FirstII);
412 else if (N == 1)
413 return SelTable.getUnarySelector(FirstII);
414
415 SmallVector<IdentifierInfo *, 16> Args;
416 Args.push_back(FirstII);
417 for (unsigned I = 1; I != N; ++I)
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000418 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000419
420 return SelTable.getSelector(N, Args.data());
421}
422
423ASTSelectorLookupTrait::data_type
424ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
425 unsigned DataLen) {
426 using namespace clang::io;
427
428 data_type Result;
429
430 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
431 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
432 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
433
434 // Load instance methods
435 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
436 if (ObjCMethodDecl *Method
437 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
438 Result.Instance.push_back(Method);
439 }
440
441 // Load factory methods
442 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
443 if (ObjCMethodDecl *Method
444 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
445 Result.Factory.push_back(Method);
446 }
447
448 return Result;
449}
450
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000451unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
452 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000453}
454
455std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000456ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000457 using namespace clang::io;
458 unsigned DataLen = ReadUnalignedLE16(d);
459 unsigned KeyLen = ReadUnalignedLE16(d);
460 return std::make_pair(KeyLen, DataLen);
461}
462
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000463ASTIdentifierLookupTraitBase::internal_key_type
464ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000465 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000466 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000467}
468
Douglas Gregordcf25082013-02-11 18:16:18 +0000469/// \brief Whether the given identifier is "interesting".
470static bool isInterestingIdentifier(IdentifierInfo &II) {
471 return II.isPoisoned() ||
472 II.isExtensionToken() ||
473 II.getObjCOrBuiltinID() ||
474 II.hasRevertedTokenIDToIdentifier() ||
475 II.hadMacroDefinition() ||
476 II.getFETokenInfo<void>();
477}
478
Guy Benyei11169dd2012-12-18 14:30:41 +0000479IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
480 const unsigned char* d,
481 unsigned DataLen) {
482 using namespace clang::io;
483 unsigned RawID = ReadUnalignedLE32(d);
484 bool IsInteresting = RawID & 0x01;
485
486 // Wipe out the "is interesting" bit.
487 RawID = RawID >> 1;
488
489 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
490 if (!IsInteresting) {
491 // For uninteresting identifiers, just build the IdentifierInfo
492 // and associate it with the persistent ID.
493 IdentifierInfo *II = KnownII;
494 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000495 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000496 KnownII = II;
497 }
498 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000499 if (!II->isFromAST()) {
500 bool WasInteresting = isInterestingIdentifier(*II);
501 II->setIsFromAST();
502 if (WasInteresting)
503 II->setChangedSinceDeserialization();
504 }
505 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000506 return II;
507 }
508
509 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
510 unsigned Bits = ReadUnalignedLE16(d);
511 bool CPlusPlusOperatorKeyword = Bits & 0x01;
512 Bits >>= 1;
513 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
514 Bits >>= 1;
515 bool Poisoned = Bits & 0x01;
516 Bits >>= 1;
517 bool ExtensionToken = Bits & 0x01;
518 Bits >>= 1;
519 bool hadMacroDefinition = Bits & 0x01;
520 Bits >>= 1;
521
522 assert(Bits == 0 && "Extra bits in the identifier?");
523 DataLen -= 8;
524
525 // Build the IdentifierInfo itself and link the identifier ID with
526 // the new IdentifierInfo.
527 IdentifierInfo *II = KnownII;
528 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000529 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000530 KnownII = II;
531 }
532 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000533 if (!II->isFromAST()) {
534 bool WasInteresting = isInterestingIdentifier(*II);
535 II->setIsFromAST();
536 if (WasInteresting)
537 II->setChangedSinceDeserialization();
538 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000539
540 // Set or check the various bits in the IdentifierInfo structure.
541 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000542 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000543 II->RevertTokenIDToIdentifier();
544 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
545 assert(II->isExtensionToken() == ExtensionToken &&
546 "Incorrect extension token flag");
547 (void)ExtensionToken;
548 if (Poisoned)
549 II->setIsPoisoned(true);
550 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
551 "Incorrect C++ operator keyword flag");
552 (void)CPlusPlusOperatorKeyword;
553
554 // If this identifier is a macro, deserialize the macro
555 // definition.
556 if (hadMacroDefinition) {
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000557 SmallVector<MacroID, 4> MacroIDs;
558 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
559 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
560 DataLen -= 4;
561 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000562 DataLen -= 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000563 Reader.setIdentifierIsMacro(II, MacroIDs);
Guy Benyei11169dd2012-12-18 14:30:41 +0000564 }
565
566 Reader.SetIdentifierInfo(ID, II);
567
568 // Read all of the declarations visible at global scope with this
569 // name.
570 if (DataLen > 0) {
571 SmallVector<uint32_t, 4> DeclIDs;
572 for (; DataLen > 0; DataLen -= 4)
573 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
574 Reader.SetGloballyVisibleDecls(II, DeclIDs);
575 }
576
577 return II;
578}
579
580unsigned
581ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
582 llvm::FoldingSetNodeID ID;
583 ID.AddInteger(Key.Kind);
584
585 switch (Key.Kind) {
586 case DeclarationName::Identifier:
587 case DeclarationName::CXXLiteralOperatorName:
588 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
589 break;
590 case DeclarationName::ObjCZeroArgSelector:
591 case DeclarationName::ObjCOneArgSelector:
592 case DeclarationName::ObjCMultiArgSelector:
593 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
594 break;
595 case DeclarationName::CXXOperatorName:
596 ID.AddInteger((OverloadedOperatorKind)Key.Data);
597 break;
598 case DeclarationName::CXXConstructorName:
599 case DeclarationName::CXXDestructorName:
600 case DeclarationName::CXXConversionFunctionName:
601 case DeclarationName::CXXUsingDirective:
602 break;
603 }
604
605 return ID.ComputeHash();
606}
607
608ASTDeclContextNameLookupTrait::internal_key_type
609ASTDeclContextNameLookupTrait::GetInternalKey(
610 const external_key_type& Name) const {
611 DeclNameKey Key;
612 Key.Kind = Name.getNameKind();
613 switch (Name.getNameKind()) {
614 case DeclarationName::Identifier:
615 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
616 break;
617 case DeclarationName::ObjCZeroArgSelector:
618 case DeclarationName::ObjCOneArgSelector:
619 case DeclarationName::ObjCMultiArgSelector:
620 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
621 break;
622 case DeclarationName::CXXOperatorName:
623 Key.Data = Name.getCXXOverloadedOperator();
624 break;
625 case DeclarationName::CXXLiteralOperatorName:
626 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
627 break;
628 case DeclarationName::CXXConstructorName:
629 case DeclarationName::CXXDestructorName:
630 case DeclarationName::CXXConversionFunctionName:
631 case DeclarationName::CXXUsingDirective:
632 Key.Data = 0;
633 break;
634 }
635
636 return Key;
637}
638
639std::pair<unsigned, unsigned>
640ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
641 using namespace clang::io;
642 unsigned KeyLen = ReadUnalignedLE16(d);
643 unsigned DataLen = ReadUnalignedLE16(d);
644 return std::make_pair(KeyLen, DataLen);
645}
646
647ASTDeclContextNameLookupTrait::internal_key_type
648ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
649 using namespace clang::io;
650
651 DeclNameKey Key;
652 Key.Kind = (DeclarationName::NameKind)*d++;
653 switch (Key.Kind) {
654 case DeclarationName::Identifier:
655 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
656 break;
657 case DeclarationName::ObjCZeroArgSelector:
658 case DeclarationName::ObjCOneArgSelector:
659 case DeclarationName::ObjCMultiArgSelector:
660 Key.Data =
661 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
662 .getAsOpaquePtr();
663 break;
664 case DeclarationName::CXXOperatorName:
665 Key.Data = *d++; // OverloadedOperatorKind
666 break;
667 case DeclarationName::CXXLiteralOperatorName:
668 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
669 break;
670 case DeclarationName::CXXConstructorName:
671 case DeclarationName::CXXDestructorName:
672 case DeclarationName::CXXConversionFunctionName:
673 case DeclarationName::CXXUsingDirective:
674 Key.Data = 0;
675 break;
676 }
677
678 return Key;
679}
680
681ASTDeclContextNameLookupTrait::data_type
682ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
683 const unsigned char* d,
684 unsigned DataLen) {
685 using namespace clang::io;
686 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000687 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
688 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000689 return std::make_pair(Start, Start + NumDecls);
690}
691
692bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000693 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000694 const std::pair<uint64_t, uint64_t> &Offsets,
695 DeclContextInfo &Info) {
696 SavedStreamPosition SavedPosition(Cursor);
697 // First the lexical decls.
698 if (Offsets.first != 0) {
699 Cursor.JumpToBit(Offsets.first);
700
701 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000702 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000703 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000704 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000705 if (RecCode != DECL_CONTEXT_LEXICAL) {
706 Error("Expected lexical block");
707 return true;
708 }
709
Chris Lattner0e6c9402013-01-20 02:38:54 +0000710 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
711 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000712 }
713
714 // Now the lookup table.
715 if (Offsets.second != 0) {
716 Cursor.JumpToBit(Offsets.second);
717
718 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000719 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000720 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000721 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000722 if (RecCode != DECL_CONTEXT_VISIBLE) {
723 Error("Expected visible lookup table block");
724 return true;
725 }
726 Info.NameLookupTableData
727 = ASTDeclContextNameLookupTable::Create(
Chris Lattner0e6c9402013-01-20 02:38:54 +0000728 (const unsigned char *)Blob.data() + Record[0],
729 (const unsigned char *)Blob.data(),
Guy Benyei11169dd2012-12-18 14:30:41 +0000730 ASTDeclContextNameLookupTrait(*this, M));
731 }
732
733 return false;
734}
735
736void ASTReader::Error(StringRef Msg) {
737 Error(diag::err_fe_pch_malformed, Msg);
738}
739
740void ASTReader::Error(unsigned DiagID,
741 StringRef Arg1, StringRef Arg2) {
742 if (Diags.isDiagnosticInFlight())
743 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
744 else
745 Diag(DiagID) << Arg1 << Arg2;
746}
747
748//===----------------------------------------------------------------------===//
749// Source Manager Deserialization
750//===----------------------------------------------------------------------===//
751
752/// \brief Read the line table in the source manager block.
753/// \returns true if there was an error.
754bool ASTReader::ParseLineTable(ModuleFile &F,
755 SmallVectorImpl<uint64_t> &Record) {
756 unsigned Idx = 0;
757 LineTableInfo &LineTable = SourceMgr.getLineTable();
758
759 // Parse the file names
760 std::map<int, int> FileIDs;
761 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
762 // Extract the file name
763 unsigned FilenameLen = Record[Idx++];
764 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
765 Idx += FilenameLen;
766 MaybeAddSystemRootToFilename(F, Filename);
767 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
768 }
769
770 // Parse the line entries
771 std::vector<LineEntry> Entries;
772 while (Idx < Record.size()) {
773 int FID = Record[Idx++];
774 assert(FID >= 0 && "Serialized line entries for non-local file.");
775 // Remap FileID from 1-based old view.
776 FID += F.SLocEntryBaseID - 1;
777
778 // Extract the line entries
779 unsigned NumEntries = Record[Idx++];
780 assert(NumEntries && "Numentries is 00000");
781 Entries.clear();
782 Entries.reserve(NumEntries);
783 for (unsigned I = 0; I != NumEntries; ++I) {
784 unsigned FileOffset = Record[Idx++];
785 unsigned LineNo = Record[Idx++];
786 int FilenameID = FileIDs[Record[Idx++]];
787 SrcMgr::CharacteristicKind FileKind
788 = (SrcMgr::CharacteristicKind)Record[Idx++];
789 unsigned IncludeOffset = Record[Idx++];
790 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
791 FileKind, IncludeOffset));
792 }
793 LineTable.AddEntry(FileID::get(FID), Entries);
794 }
795
796 return false;
797}
798
799/// \brief Read a source manager block
800bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
801 using namespace SrcMgr;
802
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000803 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000804
805 // Set the source-location entry cursor to the current position in
806 // the stream. This cursor will be used to read the contents of the
807 // source manager block initially, and then lazily read
808 // source-location entries as needed.
809 SLocEntryCursor = F.Stream;
810
811 // The stream itself is going to skip over the source manager block.
812 if (F.Stream.SkipBlock()) {
813 Error("malformed block record in AST file");
814 return true;
815 }
816
817 // Enter the source manager block.
818 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
819 Error("malformed source manager block record in AST file");
820 return true;
821 }
822
823 RecordData Record;
824 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +0000825 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
826
827 switch (E.Kind) {
828 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
829 case llvm::BitstreamEntry::Error:
830 Error("malformed block record in AST file");
831 return true;
832 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +0000833 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +0000834 case llvm::BitstreamEntry::Record:
835 // The interesting case.
836 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000837 }
Chris Lattnere7b154b2013-01-19 21:39:22 +0000838
Guy Benyei11169dd2012-12-18 14:30:41 +0000839 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +0000840 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +0000841 StringRef Blob;
842 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000843 default: // Default behavior: ignore.
844 break;
845
846 case SM_SLOC_FILE_ENTRY:
847 case SM_SLOC_BUFFER_ENTRY:
848 case SM_SLOC_EXPANSION_ENTRY:
849 // Once we hit one of the source location entries, we're done.
850 return false;
851 }
852 }
853}
854
855/// \brief If a header file is not found at the path that we expect it to be
856/// and the PCH file was moved from its original location, try to resolve the
857/// file by assuming that header+PCH were moved together and the header is in
858/// the same place relative to the PCH.
859static std::string
860resolveFileRelativeToOriginalDir(const std::string &Filename,
861 const std::string &OriginalDir,
862 const std::string &CurrDir) {
863 assert(OriginalDir != CurrDir &&
864 "No point trying to resolve the file if the PCH dir didn't change");
865 using namespace llvm::sys;
866 SmallString<128> filePath(Filename);
867 fs::make_absolute(filePath);
868 assert(path::is_absolute(OriginalDir));
869 SmallString<128> currPCHPath(CurrDir);
870
871 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
872 fileDirE = path::end(path::parent_path(filePath));
873 path::const_iterator origDirI = path::begin(OriginalDir),
874 origDirE = path::end(OriginalDir);
875 // Skip the common path components from filePath and OriginalDir.
876 while (fileDirI != fileDirE && origDirI != origDirE &&
877 *fileDirI == *origDirI) {
878 ++fileDirI;
879 ++origDirI;
880 }
881 for (; origDirI != origDirE; ++origDirI)
882 path::append(currPCHPath, "..");
883 path::append(currPCHPath, fileDirI, fileDirE);
884 path::append(currPCHPath, path::filename(Filename));
885 return currPCHPath.str();
886}
887
888bool ASTReader::ReadSLocEntry(int ID) {
889 if (ID == 0)
890 return false;
891
892 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
893 Error("source location entry ID out-of-range for AST file");
894 return true;
895 }
896
897 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
898 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000899 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000900 unsigned BaseOffset = F->SLocEntryBaseOffset;
901
902 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +0000903 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
904 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000905 Error("incorrectly-formatted source location entry in AST file");
906 return true;
907 }
Chris Lattnere7b154b2013-01-19 21:39:22 +0000908
Guy Benyei11169dd2012-12-18 14:30:41 +0000909 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000910 StringRef Blob;
911 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000912 default:
913 Error("incorrectly-formatted source location entry in AST file");
914 return true;
915
916 case SM_SLOC_FILE_ENTRY: {
917 // We will detect whether a file changed and return 'Failure' for it, but
918 // we will also try to fail gracefully by setting up the SLocEntry.
919 unsigned InputID = Record[4];
920 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +0000921 const FileEntry *File = IF.getFile();
922 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +0000923
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +0000924 // Note that we only check if a File was returned. If it was out-of-date
925 // we have complained but we will continue creating a FileID to recover
926 // gracefully.
927 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +0000928 return true;
929
930 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
931 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
932 // This is the module's main file.
933 IncludeLoc = getImportLocation(F);
934 }
935 SrcMgr::CharacteristicKind
936 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
937 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
938 ID, BaseOffset + Record[0]);
939 SrcMgr::FileInfo &FileInfo =
940 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
941 FileInfo.NumCreatedFIDs = Record[5];
942 if (Record[3])
943 FileInfo.setHasLineDirectives();
944
945 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
946 unsigned NumFileDecls = Record[7];
947 if (NumFileDecls) {
948 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
949 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
950 NumFileDecls));
951 }
952
953 const SrcMgr::ContentCache *ContentCache
954 = SourceMgr.getOrCreateContentCache(File,
955 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
956 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
957 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
958 unsigned Code = SLocEntryCursor.ReadCode();
959 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000960 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000961
962 if (RecCode != SM_SLOC_BUFFER_BLOB) {
963 Error("AST record has invalid code");
964 return true;
965 }
966
967 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +0000968 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +0000969 SourceMgr.overrideFileContents(File, Buffer);
970 }
971
972 break;
973 }
974
975 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +0000976 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +0000977 unsigned Offset = Record[0];
978 SrcMgr::CharacteristicKind
979 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
980 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
981 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
982 IncludeLoc = getImportLocation(F);
983 }
984 unsigned Code = SLocEntryCursor.ReadCode();
985 Record.clear();
986 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +0000987 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000988
989 if (RecCode != SM_SLOC_BUFFER_BLOB) {
990 Error("AST record has invalid code");
991 return true;
992 }
993
994 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +0000995 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei11169dd2012-12-18 14:30:41 +0000996 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
997 BaseOffset + Offset, IncludeLoc);
998 break;
999 }
1000
1001 case SM_SLOC_EXPANSION_ENTRY: {
1002 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1003 SourceMgr.createExpansionLoc(SpellingLoc,
1004 ReadSourceLocation(*F, Record[2]),
1005 ReadSourceLocation(*F, Record[3]),
1006 Record[4],
1007 ID,
1008 BaseOffset + Record[0]);
1009 break;
1010 }
1011 }
1012
1013 return false;
1014}
1015
1016std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1017 if (ID == 0)
1018 return std::make_pair(SourceLocation(), "");
1019
1020 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1021 Error("source location entry ID out-of-range for AST file");
1022 return std::make_pair(SourceLocation(), "");
1023 }
1024
1025 // Find which module file this entry lands in.
1026 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1027 if (M->Kind != MK_Module)
1028 return std::make_pair(SourceLocation(), "");
1029
1030 // FIXME: Can we map this down to a particular submodule? That would be
1031 // ideal.
1032 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1033}
1034
1035/// \brief Find the location where the module F is imported.
1036SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1037 if (F->ImportLoc.isValid())
1038 return F->ImportLoc;
1039
1040 // Otherwise we have a PCH. It's considered to be "imported" at the first
1041 // location of its includer.
1042 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1043 // Main file is the importer. We assume that it is the first entry in the
1044 // entry table. We can't ask the manager, because at the time of PCH loading
1045 // the main file entry doesn't exist yet.
1046 // The very first entry is the invalid instantiation loc, which takes up
1047 // offsets 0 and 1.
1048 return SourceLocation::getFromRawEncoding(2U);
1049 }
1050 //return F->Loaders[0]->FirstLoc;
1051 return F->ImportedBy[0]->FirstLoc;
1052}
1053
1054/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1055/// specified cursor. Read the abbreviations that are at the top of the block
1056/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001057bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001058 if (Cursor.EnterSubBlock(BlockID)) {
1059 Error("malformed block record in AST file");
1060 return Failure;
1061 }
1062
1063 while (true) {
1064 uint64_t Offset = Cursor.GetCurrentBitNo();
1065 unsigned Code = Cursor.ReadCode();
1066
1067 // We expect all abbrevs to be at the start of the block.
1068 if (Code != llvm::bitc::DEFINE_ABBREV) {
1069 Cursor.JumpToBit(Offset);
1070 return false;
1071 }
1072 Cursor.ReadAbbrevRecord();
1073 }
1074}
1075
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00001076void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001077 MacroDirective *Hint) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001078 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001079
1080 // Keep track of where we are in the stream, then jump back there
1081 // after reading this macro.
1082 SavedStreamPosition SavedPosition(Stream);
1083
1084 Stream.JumpToBit(Offset);
1085 RecordData Record;
1086 SmallVector<IdentifierInfo*, 16> MacroArgs;
1087 MacroInfo *Macro = 0;
1088
Douglas Gregor6a7cb9f2013-01-18 04:34:14 +00001089 // RAII object to add the loaded macro information once we're done
1090 // adding tokens.
1091 struct AddLoadedMacroInfoRAII {
1092 Preprocessor &PP;
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001093 MacroDirective *Hint;
1094 MacroDirective *MD;
Douglas Gregor6a7cb9f2013-01-18 04:34:14 +00001095 IdentifierInfo *II;
1096
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001097 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroDirective *Hint)
1098 : PP(PP), Hint(Hint), MD(), II() { }
Douglas Gregor6a7cb9f2013-01-18 04:34:14 +00001099 ~AddLoadedMacroInfoRAII( ) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001100 if (MD) {
Douglas Gregor6a7cb9f2013-01-18 04:34:14 +00001101 // Finally, install the macro.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001102 PP.addLoadedMacroInfo(II, MD, Hint);
Douglas Gregor6a7cb9f2013-01-18 04:34:14 +00001103 }
1104 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00001105 } AddLoadedMacroInfo(PP, Hint);
Douglas Gregor6a7cb9f2013-01-18 04:34:14 +00001106
Guy Benyei11169dd2012-12-18 14:30:41 +00001107 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001108 // Advance to the next record, but if we get to the end of the block, don't
1109 // pop it (removing all the abbreviations from the cursor) since we want to
1110 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001111 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001112 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1113
1114 switch (Entry.Kind) {
1115 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1116 case llvm::BitstreamEntry::Error:
1117 Error("malformed block record in AST file");
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 return;
Chris Lattnerefa77172013-01-20 00:00:22 +00001119 case llvm::BitstreamEntry::EndBlock:
1120 return;
1121 case llvm::BitstreamEntry::Record:
1122 // The interesting case.
1123 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001124 }
1125
1126 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001127 Record.clear();
1128 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001129 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001130 switch (RecType) {
1131 case PP_MACRO_OBJECT_LIKE:
1132 case PP_MACRO_FUNCTION_LIKE: {
1133 // If we already have a macro, that means that we've hit the end
1134 // of the definition of the macro we were looking for. We're
1135 // done.
1136 if (Macro)
1137 return;
1138
1139 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
1140 if (II == 0) {
1141 Error("macro must have a name in AST file");
1142 return;
1143 }
1144
1145 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1146
1147 // If this macro has already been loaded, don't do so again.
1148 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1149 return;
1150
1151 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1152 unsigned NextIndex = 3;
1153 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1154 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001155 // FIXME: Location should be import location in case of module.
1156 MacroDirective *MD = PP.AllocateMacroDirective(MI, Loc,
1157 /*isImported=*/true);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001158 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001159
1160 // Record this macro.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001161 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MD;
Guy Benyei11169dd2012-12-18 14:30:41 +00001162
1163 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1164 if (UndefLoc.isValid())
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001165 MD->setUndefLoc(UndefLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001166
1167 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001168
1169 bool IsPublic = Record[NextIndex++];
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001170 MD->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001171
1172 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1173 // Decode function-like macro info.
1174 bool isC99VarArgs = Record[NextIndex++];
1175 bool isGNUVarArgs = Record[NextIndex++];
1176 bool hasCommaPasting = Record[NextIndex++];
1177 MacroArgs.clear();
1178 unsigned NumArgs = Record[NextIndex++];
1179 for (unsigned i = 0; i != NumArgs; ++i)
1180 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1181
1182 // Install function-like macro info.
1183 MI->setIsFunctionLike();
1184 if (isC99VarArgs) MI->setIsC99Varargs();
1185 if (isGNUVarArgs) MI->setIsGNUVarargs();
1186 if (hasCommaPasting) MI->setHasCommaPasting();
1187 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1188 PP.getPreprocessorAllocator());
1189 }
1190
1191 if (DeserializationListener)
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001192 DeserializationListener->MacroRead(GlobalID, MD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001193
1194 // If an update record marked this as undefined, do so now.
1195 // FIXME: Only if the submodule this update came from is visible?
1196 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1197 if (Update != MacroUpdates.end()) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001198 if (MD->getUndefLoc().isInvalid()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001199 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1200 bool Hidden = false;
1201 if (unsigned SubmoduleID = Update->second[I].first) {
1202 if (Module *Owner = getSubmodule(SubmoduleID)) {
1203 if (Owner->NameVisibility == Module::Hidden) {
1204 // Note that this #undef is hidden.
1205 Hidden = true;
1206
1207 // Record this hiding for later.
1208 HiddenNamesMap[Owner].push_back(
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001209 HiddenName(II, MD, Update->second[I].second.UndefLoc));
Guy Benyei11169dd2012-12-18 14:30:41 +00001210 }
1211 }
1212 }
1213
1214 if (!Hidden) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001215 MD->setUndefLoc(Update->second[I].second.UndefLoc);
Guy Benyei11169dd2012-12-18 14:30:41 +00001216 if (PPMutationListener *Listener = PP.getPPMutationListener())
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001217 Listener->UndefinedMacro(MD);
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 break;
1219 }
1220 }
1221 }
1222 MacroUpdates.erase(Update);
1223 }
1224
1225 // Determine whether this macro definition is visible.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001226 bool Hidden = !MD->isPublic();
Guy Benyei11169dd2012-12-18 14:30:41 +00001227 if (!Hidden && GlobalSubmoduleID) {
1228 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1229 if (Owner->NameVisibility == Module::Hidden) {
1230 // The owning module is not visible, and this macro definition
1231 // should not be, either.
1232 Hidden = true;
1233
1234 // Note that this macro definition was hidden because its owning
1235 // module is not yet visible.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001236 HiddenNamesMap[Owner].push_back(HiddenName(II, MD));
Guy Benyei11169dd2012-12-18 14:30:41 +00001237 }
1238 }
1239 }
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001240 MD->setHidden(Hidden);
Guy Benyei11169dd2012-12-18 14:30:41 +00001241
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00001242 // Make sure we install the macro once we're done.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001243 AddLoadedMacroInfo.MD = MD;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00001244 AddLoadedMacroInfo.II = II;
Douglas Gregor6a7cb9f2013-01-18 04:34:14 +00001245
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 // Remember that we saw this macro last so that we add the tokens that
1247 // form its body to it.
1248 Macro = MI;
1249
1250 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1251 Record[NextIndex]) {
1252 // We have a macro definition. Register the association
1253 PreprocessedEntityID
1254 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1255 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001256 PreprocessingRecord::PPEntityID
1257 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1258 MacroDefinition *PPDef =
1259 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1260 if (PPDef)
1261 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001262 }
1263
1264 ++NumMacrosRead;
1265 break;
1266 }
1267
1268 case PP_TOKEN: {
1269 // If we see a TOKEN before a PP_MACRO_*, then the file is
1270 // erroneous, just pretend we didn't see this.
1271 if (Macro == 0) break;
1272
1273 Token Tok;
1274 Tok.startToken();
1275 Tok.setLocation(ReadSourceLocation(F, Record[0]));
1276 Tok.setLength(Record[1]);
1277 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
1278 Tok.setIdentifierInfo(II);
1279 Tok.setKind((tok::TokenKind)Record[3]);
1280 Tok.setFlag((Token::TokenFlags)Record[4]);
1281 Macro->AddTokenToBody(Tok);
1282 break;
1283 }
1284 }
1285 }
1286}
1287
1288PreprocessedEntityID
1289ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1290 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1291 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1292 assert(I != M.PreprocessedEntityRemap.end()
1293 && "Invalid index into preprocessed entity index remap");
1294
1295 return LocalID + I->second;
1296}
1297
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001298unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1299 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001300}
1301
1302HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001303HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1304 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1305 FE->getName() };
1306 return ikey;
1307}
Guy Benyei11169dd2012-12-18 14:30:41 +00001308
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001309bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1310 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001311 return false;
1312
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001313 if (strcmp(a.Filename, b.Filename) == 0)
1314 return true;
1315
Guy Benyei11169dd2012-12-18 14:30:41 +00001316 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001317 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001318 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1319 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001320 return (FEA && FEA == FEB);
Guy Benyei11169dd2012-12-18 14:30:41 +00001321}
1322
1323std::pair<unsigned, unsigned>
1324HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1325 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1326 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001327 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001328}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001329
1330HeaderFileInfoTrait::internal_key_type
1331HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1332 internal_key_type ikey;
1333 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1334 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1335 ikey.Filename = (const char *)d;
1336 return ikey;
1337}
1338
Guy Benyei11169dd2012-12-18 14:30:41 +00001339HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001340HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001341 unsigned DataLen) {
1342 const unsigned char *End = d + DataLen;
1343 using namespace clang::io;
1344 HeaderFileInfo HFI;
1345 unsigned Flags = *d++;
1346 HFI.isImport = (Flags >> 5) & 0x01;
1347 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1348 HFI.DirInfo = (Flags >> 2) & 0x03;
1349 HFI.Resolved = (Flags >> 1) & 0x01;
1350 HFI.IndexHeaderMapHeader = Flags & 0x01;
1351 HFI.NumIncludes = ReadUnalignedLE16(d);
1352 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1353 ReadUnalignedLE32(d));
1354 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1355 // The framework offset is 1 greater than the actual offset,
1356 // since 0 is used as an indicator for "no framework name".
1357 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1358 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1359 }
1360
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001361 if (d != End) {
1362 uint32_t LocalSMID = ReadUnalignedLE32(d);
1363 if (LocalSMID) {
1364 // This header is part of a module. Associate it with the module to enable
1365 // implicit module import.
1366 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1367 Module *Mod = Reader.getSubmodule(GlobalSMID);
1368 HFI.isModuleHeader = true;
1369 FileManager &FileMgr = Reader.getFileManager();
1370 ModuleMap &ModMap =
1371 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1372 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), /*Excluded=*/false);
1373 }
1374 }
1375
Guy Benyei11169dd2012-12-18 14:30:41 +00001376 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1377 (void)End;
1378
1379 // This HeaderFileInfo was externally loaded.
1380 HFI.External = true;
1381 return HFI;
1382}
1383
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00001384void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
Guy Benyei11169dd2012-12-18 14:30:41 +00001385 II->setHadMacroDefinition(true);
1386 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00001387 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00001388}
1389
1390void ASTReader::ReadDefinedMacros() {
1391 // Note that we are loading defined macros.
1392 Deserializing Macros(this);
1393
1394 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1395 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001396 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001397
1398 // If there was no preprocessor block, skip this file.
1399 if (!MacroCursor.getBitStreamReader())
1400 continue;
1401
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001402 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001403 Cursor.JumpToBit((*I)->MacroStartOffset);
1404
1405 RecordData Record;
1406 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001407 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1408
1409 switch (E.Kind) {
1410 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1411 case llvm::BitstreamEntry::Error:
1412 Error("malformed block record in AST file");
1413 return;
1414 case llvm::BitstreamEntry::EndBlock:
1415 goto NextCursor;
1416
1417 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001418 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001419 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001420 default: // Default behavior: ignore.
1421 break;
1422
1423 case PP_MACRO_OBJECT_LIKE:
1424 case PP_MACRO_FUNCTION_LIKE:
1425 getLocalIdentifier(**I, Record[0]);
1426 break;
1427
1428 case PP_TOKEN:
1429 // Ignore tokens.
1430 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001431 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001432 break;
1433 }
1434 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001435 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001436 }
1437}
1438
1439namespace {
1440 /// \brief Visitor class used to look up identifirs in an AST file.
1441 class IdentifierLookupVisitor {
1442 StringRef Name;
1443 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001444 unsigned &NumIdentifierLookups;
1445 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001446 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001447
Guy Benyei11169dd2012-12-18 14:30:41 +00001448 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001449 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1450 unsigned &NumIdentifierLookups,
1451 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001452 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001453 NumIdentifierLookups(NumIdentifierLookups),
1454 NumIdentifierLookupHits(NumIdentifierLookupHits),
1455 Found()
1456 {
1457 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001458
1459 static bool visit(ModuleFile &M, void *UserData) {
1460 IdentifierLookupVisitor *This
1461 = static_cast<IdentifierLookupVisitor *>(UserData);
1462
1463 // If we've already searched this module file, skip it now.
1464 if (M.Generation <= This->PriorGeneration)
1465 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001466
Guy Benyei11169dd2012-12-18 14:30:41 +00001467 ASTIdentifierLookupTable *IdTable
1468 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1469 if (!IdTable)
1470 return false;
1471
1472 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1473 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001474 ++This->NumIdentifierLookups;
1475 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001476 if (Pos == IdTable->end())
1477 return false;
1478
1479 // Dereferencing the iterator has the effect of building the
1480 // IdentifierInfo node and populating it with the various
1481 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001482 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001483 This->Found = *Pos;
1484 return true;
1485 }
1486
1487 // \brief Retrieve the identifier info found within the module
1488 // files.
1489 IdentifierInfo *getIdentifierInfo() const { return Found; }
1490 };
1491}
1492
1493void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1494 // Note that we are loading an identifier.
1495 Deserializing AnIdentifier(this);
1496
1497 unsigned PriorGeneration = 0;
1498 if (getContext().getLangOpts().Modules)
1499 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001500
1501 // If there is a global index, look there first to determine which modules
1502 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001503 GlobalModuleIndex::HitSet Hits;
1504 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00001505 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001506 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1507 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001508 }
1509 }
1510
Douglas Gregor7211ac12013-01-25 23:32:03 +00001511 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001512 NumIdentifierLookups,
1513 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001514 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001515 markIdentifierUpToDate(&II);
1516}
1517
1518void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1519 if (!II)
1520 return;
1521
1522 II->setOutOfDate(false);
1523
1524 // Update the generation for this identifier.
1525 if (getContext().getLangOpts().Modules)
1526 IdentifierGeneration[II] = CurrentGeneration;
1527}
1528
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001529InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001530 // If this ID is bogus, just return an empty input file.
1531 if (ID == 0 || ID > F.InputFilesLoaded.size())
1532 return InputFile();
1533
1534 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001535 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001536 return F.InputFilesLoaded[ID-1];
1537
1538 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001539 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001540 SavedStreamPosition SavedPosition(Cursor);
1541 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1542
1543 unsigned Code = Cursor.ReadCode();
1544 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001545 StringRef Blob;
1546 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001547 case INPUT_FILE: {
1548 unsigned StoredID = Record[0];
1549 assert(ID == StoredID && "Bogus stored ID or offset");
1550 (void)StoredID;
1551 off_t StoredSize = (off_t)Record[1];
1552 time_t StoredTime = (time_t)Record[2];
1553 bool Overridden = (bool)Record[3];
1554
1555 // Get the file entry for this input file.
Chris Lattner0e6c9402013-01-20 02:38:54 +00001556 StringRef OrigFilename = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001557 std::string Filename = OrigFilename;
1558 MaybeAddSystemRootToFilename(F, Filename);
1559 const FileEntry *File
1560 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1561 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1562
1563 // If we didn't find the file, resolve it relative to the
1564 // original directory from which this AST file was created.
1565 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1566 F.OriginalDir != CurrentDir) {
1567 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1568 F.OriginalDir,
1569 CurrentDir);
1570 if (!Resolved.empty())
1571 File = FileMgr.getFile(Resolved);
1572 }
1573
1574 // For an overridden file, create a virtual file with the stored
1575 // size/timestamp.
1576 if (Overridden && File == 0) {
1577 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1578 }
1579
1580 if (File == 0) {
1581 if (Complain) {
1582 std::string ErrorStr = "could not find file '";
1583 ErrorStr += Filename;
1584 ErrorStr += "' referenced by AST file";
1585 Error(ErrorStr.c_str());
1586 }
1587 return InputFile();
1588 }
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001589
Guy Benyei11169dd2012-12-18 14:30:41 +00001590 // Check if there was a request to override the contents of the file
1591 // that was part of the precompiled header. Overridding such a file
1592 // can lead to problems when lexing using the source locations from the
1593 // PCH.
1594 SourceManager &SM = getSourceManager();
1595 if (!Overridden && SM.isFileOverridden(File)) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001596 if (Complain)
1597 Error(diag::err_fe_pch_file_overridden, Filename);
Guy Benyei11169dd2012-12-18 14:30:41 +00001598 // After emitting the diagnostic, recover by disabling the override so
1599 // that the original file will be used.
1600 SM.disableFileContentsOverride(File);
1601 // The FileEntry is a virtual file entry with the size of the contents
1602 // that would override the original contents. Set it to the original's
1603 // size/time.
1604 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1605 StoredSize, StoredTime);
1606 }
1607
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001608 bool IsOutOfDate = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00001609
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001610 // For an overridden file, there is nothing to validate.
1611 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei11169dd2012-12-18 14:30:41 +00001612#if !defined(LLVM_ON_WIN32)
1613 // In our regression testing, the Windows file system seems to
1614 // have inconsistent modification times that sometimes
1615 // erroneously trigger this error-handling path.
1616 || StoredTime != File->getModificationTime()
1617#endif
1618 )) {
Douglas Gregor7029ce12013-03-19 00:28:20 +00001619 if (Complain) {
Argyrios Kyrtzidisc4720392013-03-08 20:42:38 +00001620 Error(diag::err_fe_pch_file_modified, Filename, F.FileName);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001621 }
1622
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001623 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00001624 }
1625
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001626 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
1627
1628 // Note that we've loaded this input file.
1629 F.InputFilesLoaded[ID-1] = IF;
1630 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00001631 }
1632 }
1633
1634 return InputFile();
1635}
1636
1637const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
1638 ModuleFile &M = ModuleMgr.getPrimaryModule();
1639 std::string Filename = filenameStrRef;
1640 MaybeAddSystemRootToFilename(M, Filename);
1641 const FileEntry *File = FileMgr.getFile(Filename);
1642 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1643 M.OriginalDir != CurrentDir) {
1644 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1645 M.OriginalDir,
1646 CurrentDir);
1647 if (!resolved.empty())
1648 File = FileMgr.getFile(resolved);
1649 }
1650
1651 return File;
1652}
1653
1654/// \brief If we are loading a relocatable PCH file, and the filename is
1655/// not an absolute path, add the system root to the beginning of the file
1656/// name.
1657void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1658 std::string &Filename) {
1659 // If this is not a relocatable PCH file, there's nothing to do.
1660 if (!M.RelocatablePCH)
1661 return;
1662
1663 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
1664 return;
1665
1666 if (isysroot.empty()) {
1667 // If no system root was given, default to '/'
1668 Filename.insert(Filename.begin(), '/');
1669 return;
1670 }
1671
1672 unsigned Length = isysroot.size();
1673 if (isysroot[Length - 1] != '/')
1674 Filename.insert(Filename.begin(), '/');
1675
1676 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
1677}
1678
1679ASTReader::ASTReadResult
1680ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001681 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei11169dd2012-12-18 14:30:41 +00001682 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001683 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00001684
1685 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1686 Error("malformed block record in AST file");
1687 return Failure;
1688 }
1689
1690 // Read all of the records and blocks in the control block.
1691 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001692 while (1) {
1693 llvm::BitstreamEntry Entry = Stream.advance();
1694
1695 switch (Entry.Kind) {
1696 case llvm::BitstreamEntry::Error:
1697 Error("malformed block record in AST file");
1698 return Failure;
1699 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001700 // Validate all of the non-system input files.
Guy Benyei11169dd2012-12-18 14:30:41 +00001701 if (!DisableValidation) {
1702 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001703 // All user input files reside at the index range [0, Record[1]).
1704 // Record is the one from INPUT_FILE_OFFSETS.
1705 for (unsigned I = 0, N = Record[1]; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001706 InputFile IF = getInputFile(F, I+1, Complain);
1707 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00001708 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001709 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001710 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001711 return Success;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001712
1713 case llvm::BitstreamEntry::SubBlock:
1714 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001715 case INPUT_FILES_BLOCK_ID:
1716 F.InputFilesCursor = Stream;
1717 if (Stream.SkipBlock() || // Skip with the main cursor
1718 // Read the abbreviations
1719 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1720 Error("malformed block record in AST file");
1721 return Failure;
1722 }
1723 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001724
Guy Benyei11169dd2012-12-18 14:30:41 +00001725 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001726 if (Stream.SkipBlock()) {
1727 Error("malformed block record in AST file");
1728 return Failure;
1729 }
1730 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00001731 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001732
1733 case llvm::BitstreamEntry::Record:
1734 // The interesting case.
1735 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001736 }
1737
1738 // Read and process a record.
1739 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001740 StringRef Blob;
1741 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001742 case METADATA: {
1743 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1744 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1745 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1746 : diag::warn_pch_version_too_new);
1747 return VersionMismatch;
1748 }
1749
1750 bool hasErrors = Record[5];
1751 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1752 Diag(diag::err_pch_with_compiler_errors);
1753 return HadErrors;
1754 }
1755
1756 F.RelocatablePCH = Record[4];
1757
1758 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001759 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001760 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
1761 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1762 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
1763 return VersionMismatch;
1764 }
1765 break;
1766 }
1767
1768 case IMPORTS: {
1769 // Load each of the imported PCH files.
1770 unsigned Idx = 0, N = Record.size();
1771 while (Idx < N) {
1772 // Read information about the AST file.
1773 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1774 // The import location will be the local one for now; we will adjust
1775 // all import locations of module imports after the global source
1776 // location info are setup.
1777 SourceLocation ImportLoc =
1778 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001779 off_t StoredSize = (off_t)Record[Idx++];
1780 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00001781 unsigned Length = Record[Idx++];
1782 SmallString<128> ImportedFile(Record.begin() + Idx,
1783 Record.begin() + Idx + Length);
1784 Idx += Length;
1785
1786 // Load the AST file.
1787 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00001788 StoredSize, StoredModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00001789 ClientLoadCapabilities)) {
1790 case Failure: return Failure;
1791 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00001792 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00001793 case OutOfDate: return OutOfDate;
1794 case VersionMismatch: return VersionMismatch;
1795 case ConfigurationMismatch: return ConfigurationMismatch;
1796 case HadErrors: return HadErrors;
1797 case Success: break;
1798 }
1799 }
1800 break;
1801 }
1802
1803 case LANGUAGE_OPTIONS: {
1804 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1805 if (Listener && &F == *ModuleMgr.begin() &&
1806 ParseLanguageOptions(Record, Complain, *Listener) &&
1807 !DisableValidation)
1808 return ConfigurationMismatch;
1809 break;
1810 }
1811
1812 case TARGET_OPTIONS: {
1813 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1814 if (Listener && &F == *ModuleMgr.begin() &&
1815 ParseTargetOptions(Record, Complain, *Listener) &&
1816 !DisableValidation)
1817 return ConfigurationMismatch;
1818 break;
1819 }
1820
1821 case DIAGNOSTIC_OPTIONS: {
1822 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1823 if (Listener && &F == *ModuleMgr.begin() &&
1824 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1825 !DisableValidation)
1826 return ConfigurationMismatch;
1827 break;
1828 }
1829
1830 case FILE_SYSTEM_OPTIONS: {
1831 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1832 if (Listener && &F == *ModuleMgr.begin() &&
1833 ParseFileSystemOptions(Record, Complain, *Listener) &&
1834 !DisableValidation)
1835 return ConfigurationMismatch;
1836 break;
1837 }
1838
1839 case HEADER_SEARCH_OPTIONS: {
1840 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1841 if (Listener && &F == *ModuleMgr.begin() &&
1842 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1843 !DisableValidation)
1844 return ConfigurationMismatch;
1845 break;
1846 }
1847
1848 case PREPROCESSOR_OPTIONS: {
1849 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1850 if (Listener && &F == *ModuleMgr.begin() &&
1851 ParsePreprocessorOptions(Record, Complain, *Listener,
1852 SuggestedPredefines) &&
1853 !DisableValidation)
1854 return ConfigurationMismatch;
1855 break;
1856 }
1857
1858 case ORIGINAL_FILE:
1859 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00001860 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001861 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1862 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
1863 break;
1864
1865 case ORIGINAL_FILE_ID:
1866 F.OriginalSourceFileID = FileID::get(Record[0]);
1867 break;
1868
1869 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00001870 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00001871 break;
1872
1873 case INPUT_FILE_OFFSETS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00001874 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001875 F.InputFilesLoaded.resize(Record[0]);
1876 break;
1877 }
1878 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001879}
1880
1881bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001882 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00001883
1884 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
1885 Error("malformed block record in AST file");
1886 return true;
1887 }
1888
1889 // Read all of the records and blocks for the AST file.
1890 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001891 while (1) {
1892 llvm::BitstreamEntry Entry = Stream.advance();
1893
1894 switch (Entry.Kind) {
1895 case llvm::BitstreamEntry::Error:
1896 Error("error at end of module block in AST file");
1897 return true;
1898 case llvm::BitstreamEntry::EndBlock: {
Guy Benyei11169dd2012-12-18 14:30:41 +00001899 DeclContext *DC = Context.getTranslationUnitDecl();
1900 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1901 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00001902
Guy Benyei11169dd2012-12-18 14:30:41 +00001903 return false;
1904 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001905 case llvm::BitstreamEntry::SubBlock:
1906 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001907 case DECLTYPES_BLOCK_ID:
1908 // We lazily load the decls block, but we want to set up the
1909 // DeclsCursor cursor to point into it. Clone our current bitcode
1910 // cursor to it, enter the block and read the abbrevs in that block.
1911 // With the main cursor, we just skip over it.
1912 F.DeclsCursor = Stream;
1913 if (Stream.SkipBlock() || // Skip with the main cursor.
1914 // Read the abbrevs.
1915 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
1916 Error("malformed block record in AST file");
1917 return true;
1918 }
1919 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001920
Guy Benyei11169dd2012-12-18 14:30:41 +00001921 case DECL_UPDATES_BLOCK_ID:
1922 if (Stream.SkipBlock()) {
1923 Error("malformed block record in AST file");
1924 return true;
1925 }
1926 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001927
Guy Benyei11169dd2012-12-18 14:30:41 +00001928 case PREPROCESSOR_BLOCK_ID:
1929 F.MacroCursor = Stream;
1930 if (!PP.getExternalSource())
1931 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00001932
Guy Benyei11169dd2012-12-18 14:30:41 +00001933 if (Stream.SkipBlock() ||
1934 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
1935 Error("malformed block record in AST file");
1936 return true;
1937 }
1938 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
1939 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001940
Guy Benyei11169dd2012-12-18 14:30:41 +00001941 case PREPROCESSOR_DETAIL_BLOCK_ID:
1942 F.PreprocessorDetailCursor = Stream;
1943 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00001944 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00001945 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001946 Error("malformed preprocessor detail record in AST file");
1947 return true;
1948 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001949 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00001950 = F.PreprocessorDetailCursor.GetCurrentBitNo();
1951
Guy Benyei11169dd2012-12-18 14:30:41 +00001952 if (!PP.getPreprocessingRecord())
1953 PP.createPreprocessingRecord();
1954 if (!PP.getPreprocessingRecord()->getExternalSource())
1955 PP.getPreprocessingRecord()->SetExternalSource(*this);
1956 break;
1957
1958 case SOURCE_MANAGER_BLOCK_ID:
1959 if (ReadSourceManagerBlock(F))
1960 return true;
1961 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001962
Guy Benyei11169dd2012-12-18 14:30:41 +00001963 case SUBMODULE_BLOCK_ID:
1964 if (ReadSubmoduleBlock(F))
1965 return true;
1966 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001967
Guy Benyei11169dd2012-12-18 14:30:41 +00001968 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001969 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00001970 if (Stream.SkipBlock() ||
1971 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1972 Error("malformed comments block in AST file");
1973 return true;
1974 }
1975 CommentsCursors.push_back(std::make_pair(C, &F));
1976 break;
1977 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001978
Guy Benyei11169dd2012-12-18 14:30:41 +00001979 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001980 if (Stream.SkipBlock()) {
1981 Error("malformed block record in AST file");
1982 return true;
1983 }
1984 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001985 }
1986 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001987
1988 case llvm::BitstreamEntry::Record:
1989 // The interesting case.
1990 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001991 }
1992
1993 // Read and process a record.
1994 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001995 StringRef Blob;
1996 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001997 default: // Default behavior: ignore.
1998 break;
1999
2000 case TYPE_OFFSET: {
2001 if (F.LocalNumTypes != 0) {
2002 Error("duplicate TYPE_OFFSET record in AST file");
2003 return true;
2004 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002005 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002006 F.LocalNumTypes = Record[0];
2007 unsigned LocalBaseTypeIndex = Record[1];
2008 F.BaseTypeIndex = getTotalNumTypes();
2009
2010 if (F.LocalNumTypes > 0) {
2011 // Introduce the global -> local mapping for types within this module.
2012 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2013
2014 // Introduce the local -> global mapping for types within this module.
2015 F.TypeRemap.insertOrReplace(
2016 std::make_pair(LocalBaseTypeIndex,
2017 F.BaseTypeIndex - LocalBaseTypeIndex));
2018
2019 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2020 }
2021 break;
2022 }
2023
2024 case DECL_OFFSET: {
2025 if (F.LocalNumDecls != 0) {
2026 Error("duplicate DECL_OFFSET record in AST file");
2027 return true;
2028 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002029 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002030 F.LocalNumDecls = Record[0];
2031 unsigned LocalBaseDeclID = Record[1];
2032 F.BaseDeclID = getTotalNumDecls();
2033
2034 if (F.LocalNumDecls > 0) {
2035 // Introduce the global -> local mapping for declarations within this
2036 // module.
2037 GlobalDeclMap.insert(
2038 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2039
2040 // Introduce the local -> global mapping for declarations within this
2041 // module.
2042 F.DeclRemap.insertOrReplace(
2043 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2044
2045 // Introduce the global -> local mapping for declarations within this
2046 // module.
2047 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2048
2049 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2050 }
2051 break;
2052 }
2053
2054 case TU_UPDATE_LEXICAL: {
2055 DeclContext *TU = Context.getTranslationUnitDecl();
2056 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002057 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002058 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002059 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002060 TU->setHasExternalLexicalStorage(true);
2061 break;
2062 }
2063
2064 case UPDATE_VISIBLE: {
2065 unsigned Idx = 0;
2066 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2067 ASTDeclContextNameLookupTable *Table =
2068 ASTDeclContextNameLookupTable::Create(
Chris Lattner0e6c9402013-01-20 02:38:54 +00002069 (const unsigned char *)Blob.data() + Record[Idx++],
2070 (const unsigned char *)Blob.data(),
Guy Benyei11169dd2012-12-18 14:30:41 +00002071 ASTDeclContextNameLookupTrait(*this, F));
2072 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2073 DeclContext *TU = Context.getTranslationUnitDecl();
2074 F.DeclContextInfos[TU].NameLookupTableData = Table;
2075 TU->setHasExternalVisibleStorage(true);
2076 } else
2077 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2078 break;
2079 }
2080
2081 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002082 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002083 if (Record[0]) {
2084 F.IdentifierLookupTable
2085 = ASTIdentifierLookupTable::Create(
2086 (const unsigned char *)F.IdentifierTableData + Record[0],
2087 (const unsigned char *)F.IdentifierTableData,
2088 ASTIdentifierLookupTrait(*this, F));
2089
2090 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2091 }
2092 break;
2093
2094 case IDENTIFIER_OFFSET: {
2095 if (F.LocalNumIdentifiers != 0) {
2096 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2097 return true;
2098 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002099 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002100 F.LocalNumIdentifiers = Record[0];
2101 unsigned LocalBaseIdentifierID = Record[1];
2102 F.BaseIdentifierID = getTotalNumIdentifiers();
2103
2104 if (F.LocalNumIdentifiers > 0) {
2105 // Introduce the global -> local mapping for identifiers within this
2106 // module.
2107 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2108 &F));
2109
2110 // Introduce the local -> global mapping for identifiers within this
2111 // module.
2112 F.IdentifierRemap.insertOrReplace(
2113 std::make_pair(LocalBaseIdentifierID,
2114 F.BaseIdentifierID - LocalBaseIdentifierID));
2115
2116 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2117 + F.LocalNumIdentifiers);
2118 }
2119 break;
2120 }
2121
2122 case EXTERNAL_DEFINITIONS:
2123 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2124 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2125 break;
2126
2127 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002128 if (SpecialTypes.empty()) {
2129 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2130 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2131 break;
2132 }
2133
2134 if (SpecialTypes.size() != Record.size()) {
2135 Error("invalid special-types record");
2136 return true;
2137 }
2138
2139 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2140 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2141 if (!SpecialTypes[I])
2142 SpecialTypes[I] = ID;
2143 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2144 // merge step?
2145 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002146 break;
2147
2148 case STATISTICS:
2149 TotalNumStatements += Record[0];
2150 TotalNumMacros += Record[1];
2151 TotalLexicalDeclContexts += Record[2];
2152 TotalVisibleDeclContexts += Record[3];
2153 break;
2154
2155 case UNUSED_FILESCOPED_DECLS:
2156 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2157 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2158 break;
2159
2160 case DELEGATING_CTORS:
2161 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2162 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2163 break;
2164
2165 case WEAK_UNDECLARED_IDENTIFIERS:
2166 if (Record.size() % 4 != 0) {
2167 Error("invalid weak identifiers record");
2168 return true;
2169 }
2170
2171 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2172 // files. This isn't the way to do it :)
2173 WeakUndeclaredIdentifiers.clear();
2174
2175 // Translate the weak, undeclared identifiers into global IDs.
2176 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2177 WeakUndeclaredIdentifiers.push_back(
2178 getGlobalIdentifierID(F, Record[I++]));
2179 WeakUndeclaredIdentifiers.push_back(
2180 getGlobalIdentifierID(F, Record[I++]));
2181 WeakUndeclaredIdentifiers.push_back(
2182 ReadSourceLocation(F, Record, I).getRawEncoding());
2183 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2184 }
2185 break;
2186
Richard Smith78165b52013-01-10 23:43:47 +00002187 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002188 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith78165b52013-01-10 23:43:47 +00002189 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002190 break;
2191
2192 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002193 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002194 F.LocalNumSelectors = Record[0];
2195 unsigned LocalBaseSelectorID = Record[1];
2196 F.BaseSelectorID = getTotalNumSelectors();
2197
2198 if (F.LocalNumSelectors > 0) {
2199 // Introduce the global -> local mapping for selectors within this
2200 // module.
2201 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2202
2203 // Introduce the local -> global mapping for selectors within this
2204 // module.
2205 F.SelectorRemap.insertOrReplace(
2206 std::make_pair(LocalBaseSelectorID,
2207 F.BaseSelectorID - LocalBaseSelectorID));
2208
2209 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2210 }
2211 break;
2212 }
2213
2214 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002215 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002216 if (Record[0])
2217 F.SelectorLookupTable
2218 = ASTSelectorLookupTable::Create(
2219 F.SelectorLookupTableData + Record[0],
2220 F.SelectorLookupTableData,
2221 ASTSelectorLookupTrait(*this, F));
2222 TotalNumMethodPoolEntries += Record[1];
2223 break;
2224
2225 case REFERENCED_SELECTOR_POOL:
2226 if (!Record.empty()) {
2227 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2228 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2229 Record[Idx++]));
2230 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2231 getRawEncoding());
2232 }
2233 }
2234 break;
2235
2236 case PP_COUNTER_VALUE:
2237 if (!Record.empty() && Listener)
2238 Listener->ReadCounter(F, Record[0]);
2239 break;
2240
2241 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002242 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002243 F.NumFileSortedDecls = Record[0];
2244 break;
2245
2246 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002247 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002248 F.LocalNumSLocEntries = Record[0];
2249 unsigned SLocSpaceSize = Record[1];
2250 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
2251 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2252 SLocSpaceSize);
2253 // Make our entry in the range map. BaseID is negative and growing, so
2254 // we invert it. Because we invert it, though, we need the other end of
2255 // the range.
2256 unsigned RangeStart =
2257 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2258 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2259 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2260
2261 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2262 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2263 GlobalSLocOffsetMap.insert(
2264 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2265 - SLocSpaceSize,&F));
2266
2267 // Initialize the remapping table.
2268 // Invalid stays invalid.
2269 F.SLocRemap.insert(std::make_pair(0U, 0));
2270 // This module. Base was 2 when being compiled.
2271 F.SLocRemap.insert(std::make_pair(2U,
2272 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2273
2274 TotalNumSLocEntries += F.LocalNumSLocEntries;
2275 break;
2276 }
2277
2278 case MODULE_OFFSET_MAP: {
2279 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002280 const unsigned char *Data = (const unsigned char*)Blob.data();
2281 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei11169dd2012-12-18 14:30:41 +00002282
2283 // Continuous range maps we may be updating in our module.
2284 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2285 ContinuousRangeMap<uint32_t, int, 2>::Builder
2286 IdentifierRemap(F.IdentifierRemap);
2287 ContinuousRangeMap<uint32_t, int, 2>::Builder
2288 MacroRemap(F.MacroRemap);
2289 ContinuousRangeMap<uint32_t, int, 2>::Builder
2290 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2291 ContinuousRangeMap<uint32_t, int, 2>::Builder
2292 SubmoduleRemap(F.SubmoduleRemap);
2293 ContinuousRangeMap<uint32_t, int, 2>::Builder
2294 SelectorRemap(F.SelectorRemap);
2295 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2296 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2297
2298 while(Data < DataEnd) {
2299 uint16_t Len = io::ReadUnalignedLE16(Data);
2300 StringRef Name = StringRef((const char*)Data, Len);
2301 Data += Len;
2302 ModuleFile *OM = ModuleMgr.lookup(Name);
2303 if (!OM) {
2304 Error("SourceLocation remap refers to unknown module");
2305 return true;
2306 }
2307
2308 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2309 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2310 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2311 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2312 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2313 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2314 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2315 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2316
2317 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2318 SLocRemap.insert(std::make_pair(SLocOffset,
2319 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2320 IdentifierRemap.insert(
2321 std::make_pair(IdentifierIDOffset,
2322 OM->BaseIdentifierID - IdentifierIDOffset));
2323 MacroRemap.insert(std::make_pair(MacroIDOffset,
2324 OM->BaseMacroID - MacroIDOffset));
2325 PreprocessedEntityRemap.insert(
2326 std::make_pair(PreprocessedEntityIDOffset,
2327 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2328 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2329 OM->BaseSubmoduleID - SubmoduleIDOffset));
2330 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2331 OM->BaseSelectorID - SelectorIDOffset));
2332 DeclRemap.insert(std::make_pair(DeclIDOffset,
2333 OM->BaseDeclID - DeclIDOffset));
2334
2335 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2336 OM->BaseTypeIndex - TypeIndexOffset));
2337
2338 // Global -> local mappings.
2339 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2340 }
2341 break;
2342 }
2343
2344 case SOURCE_MANAGER_LINE_TABLE:
2345 if (ParseLineTable(F, Record))
2346 return true;
2347 break;
2348
2349 case SOURCE_LOCATION_PRELOADS: {
2350 // Need to transform from the local view (1-based IDs) to the global view,
2351 // which is based off F.SLocEntryBaseID.
2352 if (!F.PreloadSLocEntries.empty()) {
2353 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2354 return true;
2355 }
2356
2357 F.PreloadSLocEntries.swap(Record);
2358 break;
2359 }
2360
2361 case EXT_VECTOR_DECLS:
2362 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2363 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2364 break;
2365
2366 case VTABLE_USES:
2367 if (Record.size() % 3 != 0) {
2368 Error("Invalid VTABLE_USES record");
2369 return true;
2370 }
2371
2372 // Later tables overwrite earlier ones.
2373 // FIXME: Modules will have some trouble with this. This is clearly not
2374 // the right way to do this.
2375 VTableUses.clear();
2376
2377 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2378 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2379 VTableUses.push_back(
2380 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2381 VTableUses.push_back(Record[Idx++]);
2382 }
2383 break;
2384
2385 case DYNAMIC_CLASSES:
2386 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2387 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2388 break;
2389
2390 case PENDING_IMPLICIT_INSTANTIATIONS:
2391 if (PendingInstantiations.size() % 2 != 0) {
2392 Error("Invalid existing PendingInstantiations");
2393 return true;
2394 }
2395
2396 if (Record.size() % 2 != 0) {
2397 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2398 return true;
2399 }
2400
2401 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2402 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2403 PendingInstantiations.push_back(
2404 ReadSourceLocation(F, Record, I).getRawEncoding());
2405 }
2406 break;
2407
2408 case SEMA_DECL_REFS:
2409 // Later tables overwrite earlier ones.
2410 // FIXME: Modules will have some trouble with this.
2411 SemaDeclRefs.clear();
2412 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2413 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2414 break;
2415
2416 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002417 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2418 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2419 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002420
2421 unsigned LocalBasePreprocessedEntityID = Record[0];
2422
2423 unsigned StartingID;
2424 if (!PP.getPreprocessingRecord())
2425 PP.createPreprocessingRecord();
2426 if (!PP.getPreprocessingRecord()->getExternalSource())
2427 PP.getPreprocessingRecord()->SetExternalSource(*this);
2428 StartingID
2429 = PP.getPreprocessingRecord()
2430 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2431 F.BasePreprocessedEntityID = StartingID;
2432
2433 if (F.NumPreprocessedEntities > 0) {
2434 // Introduce the global -> local mapping for preprocessed entities in
2435 // this module.
2436 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2437
2438 // Introduce the local -> global mapping for preprocessed entities in
2439 // this module.
2440 F.PreprocessedEntityRemap.insertOrReplace(
2441 std::make_pair(LocalBasePreprocessedEntityID,
2442 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2443 }
2444
2445 break;
2446 }
2447
2448 case DECL_UPDATE_OFFSETS: {
2449 if (Record.size() % 2 != 0) {
2450 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2451 return true;
2452 }
2453 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2454 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2455 .push_back(std::make_pair(&F, Record[I+1]));
2456 break;
2457 }
2458
2459 case DECL_REPLACEMENTS: {
2460 if (Record.size() % 3 != 0) {
2461 Error("invalid DECL_REPLACEMENTS block in AST file");
2462 return true;
2463 }
2464 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2465 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2466 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2467 break;
2468 }
2469
2470 case OBJC_CATEGORIES_MAP: {
2471 if (F.LocalNumObjCCategoriesInMap != 0) {
2472 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2473 return true;
2474 }
2475
2476 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002477 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002478 break;
2479 }
2480
2481 case OBJC_CATEGORIES:
2482 F.ObjCCategories.swap(Record);
2483 break;
2484
2485 case CXX_BASE_SPECIFIER_OFFSETS: {
2486 if (F.LocalNumCXXBaseSpecifiers != 0) {
2487 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2488 return true;
2489 }
2490
2491 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002492 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002493 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2494 break;
2495 }
2496
2497 case DIAG_PRAGMA_MAPPINGS:
2498 if (F.PragmaDiagMappings.empty())
2499 F.PragmaDiagMappings.swap(Record);
2500 else
2501 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2502 Record.begin(), Record.end());
2503 break;
2504
2505 case CUDA_SPECIAL_DECL_REFS:
2506 // Later tables overwrite earlier ones.
2507 // FIXME: Modules will have trouble with this.
2508 CUDASpecialDeclRefs.clear();
2509 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2510 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2511 break;
2512
2513 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002514 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002515 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00002516 if (Record[0]) {
2517 F.HeaderFileInfoTable
2518 = HeaderFileInfoLookupTable::Create(
2519 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2520 (const unsigned char *)F.HeaderFileInfoTableData,
2521 HeaderFileInfoTrait(*this, F,
2522 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00002523 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002524
2525 PP.getHeaderSearchInfo().SetExternalSource(this);
2526 if (!PP.getHeaderSearchInfo().getExternalLookup())
2527 PP.getHeaderSearchInfo().SetExternalLookup(this);
2528 }
2529 break;
2530 }
2531
2532 case FP_PRAGMA_OPTIONS:
2533 // Later tables overwrite earlier ones.
2534 FPPragmaOptions.swap(Record);
2535 break;
2536
2537 case OPENCL_EXTENSIONS:
2538 // Later tables overwrite earlier ones.
2539 OpenCLExtensions.swap(Record);
2540 break;
2541
2542 case TENTATIVE_DEFINITIONS:
2543 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2544 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2545 break;
2546
2547 case KNOWN_NAMESPACES:
2548 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2549 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2550 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00002551
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002552 case UNDEFINED_BUT_USED:
2553 if (UndefinedButUsed.size() % 2 != 0) {
2554 Error("Invalid existing UndefinedButUsed");
Nick Lewycky8334af82013-01-26 00:35:08 +00002555 return true;
2556 }
2557
2558 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002559 Error("invalid undefined-but-used record");
Nick Lewycky8334af82013-01-26 00:35:08 +00002560 return true;
2561 }
2562 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002563 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2564 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00002565 ReadSourceLocation(F, Record, I).getRawEncoding());
2566 }
2567 break;
2568
Guy Benyei11169dd2012-12-18 14:30:41 +00002569 case IMPORTED_MODULES: {
2570 if (F.Kind != MK_Module) {
2571 // If we aren't loading a module (which has its own exports), make
2572 // all of the imported modules visible.
2573 // FIXME: Deal with macros-only imports.
2574 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2575 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2576 ImportedModules.push_back(GlobalID);
2577 }
2578 }
2579 break;
2580 }
2581
2582 case LOCAL_REDECLARATIONS: {
2583 F.RedeclarationChains.swap(Record);
2584 break;
2585 }
2586
2587 case LOCAL_REDECLARATIONS_MAP: {
2588 if (F.LocalNumRedeclarationsInMap != 0) {
2589 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
2590 return true;
2591 }
2592
2593 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002594 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002595 break;
2596 }
2597
2598 case MERGED_DECLARATIONS: {
2599 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2600 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2601 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2602 for (unsigned N = Record[Idx++]; N > 0; --N)
2603 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2604 }
2605 break;
2606 }
2607
2608 case MACRO_OFFSET: {
2609 if (F.LocalNumMacros != 0) {
2610 Error("duplicate MACRO_OFFSET record in AST file");
2611 return true;
2612 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002613 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002614 F.LocalNumMacros = Record[0];
2615 unsigned LocalBaseMacroID = Record[1];
2616 F.BaseMacroID = getTotalNumMacros();
2617
2618 if (F.LocalNumMacros > 0) {
2619 // Introduce the global -> local mapping for macros within this module.
2620 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2621
2622 // Introduce the local -> global mapping for macros within this module.
2623 F.MacroRemap.insertOrReplace(
2624 std::make_pair(LocalBaseMacroID,
2625 F.BaseMacroID - LocalBaseMacroID));
2626
2627 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2628 }
2629 break;
2630 }
2631
2632 case MACRO_UPDATES: {
2633 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2634 MacroID ID = getGlobalMacroID(F, Record[I++]);
2635 if (I == N)
2636 break;
2637
2638 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2639 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2640 MacroUpdate Update;
2641 Update.UndefLoc = UndefLoc;
2642 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
2643 }
2644 break;
2645 }
2646 }
2647 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002648}
2649
Douglas Gregorc1489562013-02-12 23:36:21 +00002650/// \brief Move the given method to the back of the global list of methods.
2651static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
2652 // Find the entry for this selector in the method pool.
2653 Sema::GlobalMethodPool::iterator Known
2654 = S.MethodPool.find(Method->getSelector());
2655 if (Known == S.MethodPool.end())
2656 return;
2657
2658 // Retrieve the appropriate method list.
2659 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
2660 : Known->second.second;
2661 bool Found = false;
2662 for (ObjCMethodList *List = &Start; List; List = List->Next) {
2663 if (!Found) {
2664 if (List->Method == Method) {
2665 Found = true;
2666 } else {
2667 // Keep searching.
2668 continue;
2669 }
2670 }
2671
2672 if (List->Next)
2673 List->Method = List->Next->Method;
2674 else
2675 List->Method = Method;
2676 }
2677}
2678
Guy Benyei11169dd2012-12-18 14:30:41 +00002679void ASTReader::makeNamesVisible(const HiddenNames &Names) {
2680 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2681 switch (Names[I].getKind()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00002682 case HiddenName::Declaration: {
2683 Decl *D = Names[I].getDecl();
2684 bool wasHidden = D->Hidden;
2685 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00002686
Douglas Gregorc1489562013-02-12 23:36:21 +00002687 if (wasHidden && SemaObj) {
2688 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
2689 moveMethodToBackOfGlobalList(*SemaObj, Method);
2690 }
2691 }
2692 break;
2693 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002694 case HiddenName::MacroVisibility: {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002695 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Guy Benyei11169dd2012-12-18 14:30:41 +00002696 Macro.second->setHidden(!Macro.second->isPublic());
2697 if (Macro.second->isDefined()) {
2698 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2699 }
2700 break;
2701 }
2702
2703 case HiddenName::MacroUndef: {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002704 std::pair<IdentifierInfo *, MacroDirective *> Macro = Names[I].getMacro();
Guy Benyei11169dd2012-12-18 14:30:41 +00002705 if (Macro.second->isDefined()) {
2706 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2707 if (PPMutationListener *Listener = PP.getPPMutationListener())
2708 Listener->UndefinedMacro(Macro.second);
2709 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2710 }
2711 break;
2712 }
2713 }
2714 }
2715}
2716
2717void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00002718 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00002719 SourceLocation ImportLoc,
2720 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002721 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002722 SmallVector<Module *, 4> Stack;
Guy Benyei11169dd2012-12-18 14:30:41 +00002723 Stack.push_back(Mod);
2724 while (!Stack.empty()) {
2725 Mod = Stack.back();
2726 Stack.pop_back();
2727
2728 if (NameVisibility <= Mod->NameVisibility) {
2729 // This module already has this level of visibility (or greater), so
2730 // there is nothing more to do.
2731 continue;
2732 }
2733
2734 if (!Mod->isAvailable()) {
2735 // Modules that aren't available cannot be made visible.
2736 continue;
2737 }
2738
2739 // Update the module's name visibility.
2740 Mod->NameVisibility = NameVisibility;
2741
2742 // If we've already deserialized any names from this module,
2743 // mark them as visible.
2744 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2745 if (Hidden != HiddenNamesMap.end()) {
2746 makeNamesVisible(Hidden->second);
2747 HiddenNamesMap.erase(Hidden);
2748 }
2749
2750 // Push any non-explicit submodules onto the stack to be marked as
2751 // visible.
2752 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2753 SubEnd = Mod->submodule_end();
2754 Sub != SubEnd; ++Sub) {
2755 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2756 Stack.push_back(*Sub);
2757 }
2758
2759 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00002760 SmallVector<Module *, 16> Exports;
2761 Mod->getExportedModules(Exports);
2762 for (SmallVectorImpl<Module *>::iterator
2763 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
2764 Module *Exported = *I;
2765 if (Visited.insert(Exported))
2766 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00002767 }
Douglas Gregorfb912652013-03-20 21:10:35 +00002768
2769 // Detect any conflicts.
2770 if (Complain) {
2771 assert(ImportLoc.isValid() && "Missing import location");
2772 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2773 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
2774 Diag(ImportLoc, diag::warn_module_conflict)
2775 << Mod->getFullModuleName()
2776 << Mod->Conflicts[I].Other->getFullModuleName()
2777 << Mod->Conflicts[I].Message;
2778 // FIXME: Need note where the other module was imported.
2779 }
2780 }
2781 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002782 }
2783}
2784
Douglas Gregore060e572013-01-25 01:03:03 +00002785bool ASTReader::loadGlobalIndex() {
2786 if (GlobalIndex)
2787 return false;
2788
2789 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
2790 !Context.getLangOpts().Modules)
2791 return true;
2792
2793 // Try to load the global index.
2794 TriedLoadingGlobalIndex = true;
2795 StringRef ModuleCachePath
2796 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
2797 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00002798 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00002799 if (!Result.first)
2800 return true;
2801
2802 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00002803 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00002804 return false;
2805}
2806
2807bool ASTReader::isGlobalIndexUnavailable() const {
2808 return Context.getLangOpts().Modules && UseGlobalIndex &&
2809 !hasGlobalIndex() && TriedLoadingGlobalIndex;
2810}
2811
Guy Benyei11169dd2012-12-18 14:30:41 +00002812ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
2813 ModuleKind Type,
2814 SourceLocation ImportLoc,
2815 unsigned ClientLoadCapabilities) {
2816 // Bump the generation number.
2817 unsigned PreviousGeneration = CurrentGeneration++;
2818
2819 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002820 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00002821 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
2822 /*ImportedBy=*/0, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00002823 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00002824 ClientLoadCapabilities)) {
2825 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00002826 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002827 case OutOfDate:
2828 case VersionMismatch:
2829 case ConfigurationMismatch:
2830 case HadErrors:
Douglas Gregor7029ce12013-03-19 00:28:20 +00002831 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
2832 Context.getLangOpts().Modules
2833 ? &PP.getHeaderSearchInfo().getModuleMap()
2834 : 0);
Douglas Gregore060e572013-01-25 01:03:03 +00002835
2836 // If we find that any modules are unusable, the global index is going
2837 // to be out-of-date. Just remove it.
2838 GlobalIndex.reset();
Douglas Gregor7211ac12013-01-25 23:32:03 +00002839 ModuleMgr.setGlobalIndex(0);
Guy Benyei11169dd2012-12-18 14:30:41 +00002840 return ReadResult;
2841
2842 case Success:
2843 break;
2844 }
2845
2846 // Here comes stuff that we only do once the entire chain is loaded.
2847
2848 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002849 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2850 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00002851 M != MEnd; ++M) {
2852 ModuleFile &F = *M->Mod;
2853
2854 // Read the AST block.
2855 if (ReadASTBlock(F))
2856 return Failure;
2857
2858 // Once read, set the ModuleFile bit base offset and update the size in
2859 // bits of all files we've seen.
2860 F.GlobalBitOffset = TotalModulesSizeInBits;
2861 TotalModulesSizeInBits += F.SizeInBits;
2862 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2863
2864 // Preload SLocEntries.
2865 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2866 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
2867 // Load it through the SourceManager and don't call ReadSLocEntry()
2868 // directly because the entry may have already been loaded in which case
2869 // calling ReadSLocEntry() directly would trigger an assertion in
2870 // SourceManager.
2871 SourceMgr.getLoadedSLocEntryByID(Index);
2872 }
2873 }
2874
Douglas Gregor603cd862013-03-22 18:50:14 +00002875 // Setup the import locations and notify the module manager that we've
2876 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002877 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2878 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00002879 M != MEnd; ++M) {
2880 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00002881
2882 ModuleMgr.moduleFileAccepted(&F);
2883
2884 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00002885 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00002886 if (!M->ImportedBy)
2887 F.ImportLoc = M->ImportLoc;
2888 else
2889 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2890 M->ImportLoc.getRawEncoding());
2891 }
2892
2893 // Mark all of the identifiers in the identifier table as being out of date,
2894 // so that various accessors know to check the loaded modules when the
2895 // identifier is used.
2896 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2897 IdEnd = PP.getIdentifierTable().end();
2898 Id != IdEnd; ++Id)
2899 Id->second->setOutOfDate(true);
2900
2901 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00002902 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
2903 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00002904 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
2905 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00002906
2907 switch (Unresolved.Kind) {
2908 case UnresolvedModuleRef::Conflict:
2909 if (ResolvedMod) {
2910 Module::Conflict Conflict;
2911 Conflict.Other = ResolvedMod;
2912 Conflict.Message = Unresolved.String.str();
2913 Unresolved.Mod->Conflicts.push_back(Conflict);
2914 }
2915 continue;
2916
2917 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00002918 if (ResolvedMod)
2919 Unresolved.Mod->Imports.push_back(ResolvedMod);
2920 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002921
Douglas Gregorfb912652013-03-20 21:10:35 +00002922 case UnresolvedModuleRef::Export:
2923 if (ResolvedMod || Unresolved.IsWildcard)
2924 Unresolved.Mod->Exports.push_back(
2925 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
2926 continue;
2927 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002928 }
Douglas Gregorfb912652013-03-20 21:10:35 +00002929 UnresolvedModuleRefs.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00002930
2931 InitializeContext();
2932
2933 if (DeserializationListener)
2934 DeserializationListener->ReaderInitialized(this);
2935
2936 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2937 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2938 PrimaryModule.OriginalSourceFileID
2939 = FileID::get(PrimaryModule.SLocEntryBaseID
2940 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
2941
2942 // If this AST file is a precompiled preamble, then set the
2943 // preamble file ID of the source manager to the file source file
2944 // from which the preamble was built.
2945 if (Type == MK_Preamble) {
2946 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
2947 } else if (Type == MK_MainFile) {
2948 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
2949 }
2950 }
2951
2952 // For any Objective-C class definitions we have already loaded, make sure
2953 // that we load any additional categories.
2954 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2955 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2956 ObjCClassesLoaded[I],
2957 PreviousGeneration);
2958 }
Douglas Gregore060e572013-01-25 01:03:03 +00002959
Guy Benyei11169dd2012-12-18 14:30:41 +00002960 return Success;
2961}
2962
2963ASTReader::ASTReadResult
2964ASTReader::ReadASTCore(StringRef FileName,
2965 ModuleKind Type,
2966 SourceLocation ImportLoc,
2967 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002968 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00002969 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00002970 unsigned ClientLoadCapabilities) {
2971 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00002972 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002973 ModuleManager::AddModuleResult AddResult
2974 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
2975 CurrentGeneration, ExpectedSize, ExpectedModTime,
2976 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00002977
Douglas Gregor7029ce12013-03-19 00:28:20 +00002978 switch (AddResult) {
2979 case ModuleManager::AlreadyLoaded:
2980 return Success;
2981
2982 case ModuleManager::NewlyLoaded:
2983 // Load module file below.
2984 break;
2985
2986 case ModuleManager::Missing:
2987 // The module file was missing; if the client handle handle, that, return
2988 // it.
2989 if (ClientLoadCapabilities & ARR_Missing)
2990 return Missing;
2991
2992 // Otherwise, return an error.
2993 {
2994 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2995 + ErrorStr;
2996 Error(Msg);
2997 }
2998 return Failure;
2999
3000 case ModuleManager::OutOfDate:
3001 // We couldn't load the module file because it is out-of-date. If the
3002 // client can handle out-of-date, return it.
3003 if (ClientLoadCapabilities & ARR_OutOfDate)
3004 return OutOfDate;
3005
3006 // Otherwise, return an error.
3007 {
3008 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3009 + ErrorStr;
3010 Error(Msg);
3011 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003012 return Failure;
3013 }
3014
Douglas Gregor7029ce12013-03-19 00:28:20 +00003015 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003016
3017 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3018 // module?
3019 if (FileName != "-") {
3020 CurrentDir = llvm::sys::path::parent_path(FileName);
3021 if (CurrentDir.empty()) CurrentDir = ".";
3022 }
3023
3024 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003025 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003026 Stream.init(F.StreamFile);
3027 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3028
3029 // Sniff for the signature.
3030 if (Stream.Read(8) != 'C' ||
3031 Stream.Read(8) != 'P' ||
3032 Stream.Read(8) != 'C' ||
3033 Stream.Read(8) != 'H') {
3034 Diag(diag::err_not_a_pch_file) << FileName;
3035 return Failure;
3036 }
3037
3038 // This is used for compatibility with older PCH formats.
3039 bool HaveReadControlBlock = false;
3040
Chris Lattnerefa77172013-01-20 00:00:22 +00003041 while (1) {
3042 llvm::BitstreamEntry Entry = Stream.advance();
3043
3044 switch (Entry.Kind) {
3045 case llvm::BitstreamEntry::Error:
3046 case llvm::BitstreamEntry::EndBlock:
3047 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003048 Error("invalid record at top-level of AST file");
3049 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003050
3051 case llvm::BitstreamEntry::SubBlock:
3052 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003053 }
3054
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003056 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003057 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3058 if (Stream.ReadBlockInfoBlock()) {
3059 Error("malformed BlockInfoBlock in AST file");
3060 return Failure;
3061 }
3062 break;
3063 case CONTROL_BLOCK_ID:
3064 HaveReadControlBlock = true;
3065 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3066 case Success:
3067 break;
3068
3069 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003070 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 case OutOfDate: return OutOfDate;
3072 case VersionMismatch: return VersionMismatch;
3073 case ConfigurationMismatch: return ConfigurationMismatch;
3074 case HadErrors: return HadErrors;
3075 }
3076 break;
3077 case AST_BLOCK_ID:
3078 if (!HaveReadControlBlock) {
3079 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
3080 Diag(diag::warn_pch_version_too_old);
3081 return VersionMismatch;
3082 }
3083
3084 // Record that we've loaded this module.
3085 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3086 return Success;
3087
3088 default:
3089 if (Stream.SkipBlock()) {
3090 Error("malformed block record in AST file");
3091 return Failure;
3092 }
3093 break;
3094 }
3095 }
3096
3097 return Success;
3098}
3099
3100void ASTReader::InitializeContext() {
3101 // If there's a listener, notify them that we "read" the translation unit.
3102 if (DeserializationListener)
3103 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3104 Context.getTranslationUnitDecl());
3105
3106 // Make sure we load the declaration update records for the translation unit,
3107 // if there are any.
3108 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3109 Context.getTranslationUnitDecl());
3110
3111 // FIXME: Find a better way to deal with collisions between these
3112 // built-in types. Right now, we just ignore the problem.
3113
3114 // Load the special types.
3115 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3116 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3117 if (!Context.CFConstantStringTypeDecl)
3118 Context.setCFConstantStringType(GetType(String));
3119 }
3120
3121 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3122 QualType FileType = GetType(File);
3123 if (FileType.isNull()) {
3124 Error("FILE type is NULL");
3125 return;
3126 }
3127
3128 if (!Context.FILEDecl) {
3129 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3130 Context.setFILEDecl(Typedef->getDecl());
3131 else {
3132 const TagType *Tag = FileType->getAs<TagType>();
3133 if (!Tag) {
3134 Error("Invalid FILE type in AST file");
3135 return;
3136 }
3137 Context.setFILEDecl(Tag->getDecl());
3138 }
3139 }
3140 }
3141
3142 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3143 QualType Jmp_bufType = GetType(Jmp_buf);
3144 if (Jmp_bufType.isNull()) {
3145 Error("jmp_buf type is NULL");
3146 return;
3147 }
3148
3149 if (!Context.jmp_bufDecl) {
3150 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3151 Context.setjmp_bufDecl(Typedef->getDecl());
3152 else {
3153 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3154 if (!Tag) {
3155 Error("Invalid jmp_buf type in AST file");
3156 return;
3157 }
3158 Context.setjmp_bufDecl(Tag->getDecl());
3159 }
3160 }
3161 }
3162
3163 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3164 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3165 if (Sigjmp_bufType.isNull()) {
3166 Error("sigjmp_buf type is NULL");
3167 return;
3168 }
3169
3170 if (!Context.sigjmp_bufDecl) {
3171 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3172 Context.setsigjmp_bufDecl(Typedef->getDecl());
3173 else {
3174 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3175 assert(Tag && "Invalid sigjmp_buf type in AST file");
3176 Context.setsigjmp_bufDecl(Tag->getDecl());
3177 }
3178 }
3179 }
3180
3181 if (unsigned ObjCIdRedef
3182 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3183 if (Context.ObjCIdRedefinitionType.isNull())
3184 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3185 }
3186
3187 if (unsigned ObjCClassRedef
3188 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3189 if (Context.ObjCClassRedefinitionType.isNull())
3190 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3191 }
3192
3193 if (unsigned ObjCSelRedef
3194 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3195 if (Context.ObjCSelRedefinitionType.isNull())
3196 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3197 }
3198
3199 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3200 QualType Ucontext_tType = GetType(Ucontext_t);
3201 if (Ucontext_tType.isNull()) {
3202 Error("ucontext_t type is NULL");
3203 return;
3204 }
3205
3206 if (!Context.ucontext_tDecl) {
3207 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3208 Context.setucontext_tDecl(Typedef->getDecl());
3209 else {
3210 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3211 assert(Tag && "Invalid ucontext_t type in AST file");
3212 Context.setucontext_tDecl(Tag->getDecl());
3213 }
3214 }
3215 }
3216 }
3217
3218 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3219
3220 // If there were any CUDA special declarations, deserialize them.
3221 if (!CUDASpecialDeclRefs.empty()) {
3222 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3223 Context.setcudaConfigureCallDecl(
3224 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3225 }
3226
3227 // Re-export any modules that were imported by a non-module AST file.
3228 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3229 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003230 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregorfb912652013-03-20 21:10:35 +00003231 /*ImportLoc=*/SourceLocation(),
3232 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00003233 }
3234 ImportedModules.clear();
3235}
3236
3237void ASTReader::finalizeForWriting() {
3238 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3239 HiddenEnd = HiddenNamesMap.end();
3240 Hidden != HiddenEnd; ++Hidden) {
3241 makeNamesVisible(Hidden->second);
3242 }
3243 HiddenNamesMap.clear();
3244}
3245
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003246/// SkipCursorToControlBlock - Given a cursor at the start of an AST file, scan
3247/// ahead and drop the cursor into the start of the CONTROL_BLOCK, returning
3248/// false on success and true on failure.
3249static bool SkipCursorToControlBlock(BitstreamCursor &Cursor) {
3250 while (1) {
3251 llvm::BitstreamEntry Entry = Cursor.advance();
3252 switch (Entry.Kind) {
3253 case llvm::BitstreamEntry::Error:
3254 case llvm::BitstreamEntry::EndBlock:
3255 return true;
3256
3257 case llvm::BitstreamEntry::Record:
3258 // Ignore top-level records.
3259 Cursor.skipRecord(Entry.ID);
3260 break;
3261
3262 case llvm::BitstreamEntry::SubBlock:
3263 if (Entry.ID == CONTROL_BLOCK_ID) {
3264 if (Cursor.EnterSubBlock(CONTROL_BLOCK_ID))
3265 return true;
3266 // Found it!
3267 return false;
3268 }
3269
3270 if (Cursor.SkipBlock())
3271 return true;
3272 }
3273 }
3274}
3275
Guy Benyei11169dd2012-12-18 14:30:41 +00003276/// \brief Retrieve the name of the original source file name
3277/// directly from the AST file, without actually loading the AST
3278/// file.
3279std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3280 FileManager &FileMgr,
3281 DiagnosticsEngine &Diags) {
3282 // Open the AST file.
3283 std::string ErrStr;
3284 OwningPtr<llvm::MemoryBuffer> Buffer;
3285 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3286 if (!Buffer) {
3287 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3288 return std::string();
3289 }
3290
3291 // Initialize the stream
3292 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003293 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003294 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3295 (const unsigned char *)Buffer->getBufferEnd());
3296 Stream.init(StreamFile);
3297
3298 // Sniff for the signature.
3299 if (Stream.Read(8) != 'C' ||
3300 Stream.Read(8) != 'P' ||
3301 Stream.Read(8) != 'C' ||
3302 Stream.Read(8) != 'H') {
3303 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3304 return std::string();
3305 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003306
Chris Lattnere7b154b2013-01-19 21:39:22 +00003307 // Scan for the CONTROL_BLOCK_ID block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003308 if (SkipCursorToControlBlock(Stream)) {
3309 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3310 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003311 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003312
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003313 // Scan for ORIGINAL_FILE inside the control block.
3314 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003315 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003316 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003317 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3318 return std::string();
3319
3320 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3321 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3322 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00003323 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00003324
Guy Benyei11169dd2012-12-18 14:30:41 +00003325 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003326 StringRef Blob;
3327 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3328 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00003329 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003330}
3331
3332namespace {
3333 class SimplePCHValidator : public ASTReaderListener {
3334 const LangOptions &ExistingLangOpts;
3335 const TargetOptions &ExistingTargetOpts;
3336 const PreprocessorOptions &ExistingPPOpts;
3337 FileManager &FileMgr;
3338
3339 public:
3340 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3341 const TargetOptions &ExistingTargetOpts,
3342 const PreprocessorOptions &ExistingPPOpts,
3343 FileManager &FileMgr)
3344 : ExistingLangOpts(ExistingLangOpts),
3345 ExistingTargetOpts(ExistingTargetOpts),
3346 ExistingPPOpts(ExistingPPOpts),
3347 FileMgr(FileMgr)
3348 {
3349 }
3350
3351 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3352 bool Complain) {
3353 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3354 }
3355 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3356 bool Complain) {
3357 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3358 }
3359 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3360 bool Complain,
3361 std::string &SuggestedPredefines) {
3362 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3363 SuggestedPredefines);
3364 }
3365 };
3366}
3367
3368bool ASTReader::readASTFileControlBlock(StringRef Filename,
3369 FileManager &FileMgr,
3370 ASTReaderListener &Listener) {
3371 // Open the AST file.
3372 std::string ErrStr;
3373 OwningPtr<llvm::MemoryBuffer> Buffer;
3374 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3375 if (!Buffer) {
3376 return true;
3377 }
3378
3379 // Initialize the stream
3380 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003381 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003382 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3383 (const unsigned char *)Buffer->getBufferEnd());
3384 Stream.init(StreamFile);
3385
3386 // Sniff for the signature.
3387 if (Stream.Read(8) != 'C' ||
3388 Stream.Read(8) != 'P' ||
3389 Stream.Read(8) != 'C' ||
3390 Stream.Read(8) != 'H') {
3391 return true;
3392 }
3393
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003394 // Scan for the CONTROL_BLOCK_ID block.
3395 if (SkipCursorToControlBlock(Stream))
3396 return true;
3397
3398 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00003399 RecordData Record;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003400 while (1) {
3401 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3402 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3403 return false;
3404
3405 if (Entry.Kind != llvm::BitstreamEntry::Record)
3406 return true;
3407
Guy Benyei11169dd2012-12-18 14:30:41 +00003408 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003409 StringRef Blob;
3410 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003411 switch ((ControlRecordTypes)RecCode) {
3412 case METADATA: {
3413 if (Record[0] != VERSION_MAJOR)
3414 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003415
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003416 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003417 if (StringRef(CurBranch) != Blob)
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003418 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003419
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003420 break;
3421 }
3422 case LANGUAGE_OPTIONS:
3423 if (ParseLanguageOptions(Record, false, Listener))
3424 return true;
3425 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003426
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003427 case TARGET_OPTIONS:
3428 if (ParseTargetOptions(Record, false, Listener))
3429 return true;
3430 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003431
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003432 case DIAGNOSTIC_OPTIONS:
3433 if (ParseDiagnosticOptions(Record, false, Listener))
3434 return true;
3435 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003436
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003437 case FILE_SYSTEM_OPTIONS:
3438 if (ParseFileSystemOptions(Record, false, Listener))
3439 return true;
3440 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003441
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003442 case HEADER_SEARCH_OPTIONS:
3443 if (ParseHeaderSearchOptions(Record, false, Listener))
3444 return true;
3445 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003446
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003447 case PREPROCESSOR_OPTIONS: {
3448 std::string IgnoredSuggestedPredefines;
3449 if (ParsePreprocessorOptions(Record, false, Listener,
3450 IgnoredSuggestedPredefines))
3451 return true;
3452 break;
3453 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003454
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003455 default:
3456 // No other validation to perform.
3457 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003458 }
3459 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003460}
3461
3462
3463bool ASTReader::isAcceptableASTFile(StringRef Filename,
3464 FileManager &FileMgr,
3465 const LangOptions &LangOpts,
3466 const TargetOptions &TargetOpts,
3467 const PreprocessorOptions &PPOpts) {
3468 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3469 return !readASTFileControlBlock(Filename, FileMgr, validator);
3470}
3471
3472bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3473 // Enter the submodule block.
3474 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3475 Error("malformed submodule block record in AST file");
3476 return true;
3477 }
3478
3479 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3480 bool First = true;
3481 Module *CurrentModule = 0;
3482 RecordData Record;
3483 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003484 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3485
3486 switch (Entry.Kind) {
3487 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3488 case llvm::BitstreamEntry::Error:
3489 Error("malformed block record in AST file");
3490 return true;
3491 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003492 return false;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003493 case llvm::BitstreamEntry::Record:
3494 // The interesting case.
3495 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003496 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003497
Guy Benyei11169dd2012-12-18 14:30:41 +00003498 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00003499 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003500 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003501 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003502 default: // Default behavior: ignore.
3503 break;
3504
3505 case SUBMODULE_DEFINITION: {
3506 if (First) {
3507 Error("missing submodule metadata record at beginning of block");
3508 return true;
3509 }
3510
Douglas Gregor8d932422013-03-20 03:59:18 +00003511 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003512 Error("malformed module definition");
3513 return true;
3514 }
3515
Chris Lattner0e6c9402013-01-20 02:38:54 +00003516 StringRef Name = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003517 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3518 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3519 bool IsFramework = Record[2];
3520 bool IsExplicit = Record[3];
3521 bool IsSystem = Record[4];
3522 bool InferSubmodules = Record[5];
3523 bool InferExplicitSubmodules = Record[6];
3524 bool InferExportWildcard = Record[7];
Douglas Gregor8d932422013-03-20 03:59:18 +00003525 bool ConfigMacrosExhaustive = Record[8];
3526
Guy Benyei11169dd2012-12-18 14:30:41 +00003527 Module *ParentModule = 0;
3528 if (Parent)
3529 ParentModule = getSubmodule(Parent);
3530
3531 // Retrieve this (sub)module from the module map, creating it if
3532 // necessary.
3533 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3534 IsFramework,
3535 IsExplicit).first;
3536 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3537 if (GlobalIndex >= SubmodulesLoaded.size() ||
3538 SubmodulesLoaded[GlobalIndex]) {
3539 Error("too many submodules");
3540 return true;
3541 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003542
Douglas Gregor7029ce12013-03-19 00:28:20 +00003543 if (!ParentModule) {
3544 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
3545 if (CurFile != F.File) {
3546 if (!Diags.isDiagnosticInFlight()) {
3547 Diag(diag::err_module_file_conflict)
3548 << CurrentModule->getTopLevelModuleName()
3549 << CurFile->getName()
3550 << F.File->getName();
3551 }
3552 return true;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003553 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003554 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00003555
3556 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00003557 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00003558
Guy Benyei11169dd2012-12-18 14:30:41 +00003559 CurrentModule->IsFromModuleFile = true;
3560 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
3561 CurrentModule->InferSubmodules = InferSubmodules;
3562 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3563 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00003564 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00003565 if (DeserializationListener)
3566 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
3567
3568 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003569
Douglas Gregorfb912652013-03-20 21:10:35 +00003570 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003571 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00003572 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00003573 CurrentModule->UnresolvedConflicts.clear();
3574 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00003575 break;
3576 }
3577
3578 case SUBMODULE_UMBRELLA_HEADER: {
3579 if (First) {
3580 Error("missing submodule metadata record at beginning of block");
3581 return true;
3582 }
3583
3584 if (!CurrentModule)
3585 break;
3586
Chris Lattner0e6c9402013-01-20 02:38:54 +00003587 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003588 if (!CurrentModule->getUmbrellaHeader())
3589 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
3590 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
3591 Error("mismatched umbrella headers in submodule");
3592 return true;
3593 }
3594 }
3595 break;
3596 }
3597
3598 case SUBMODULE_HEADER: {
3599 if (First) {
3600 Error("missing submodule metadata record at beginning of block");
3601 return true;
3602 }
3603
3604 if (!CurrentModule)
3605 break;
3606
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00003607 // We lazily associate headers with their modules via the HeaderInfoTable.
3608 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3609 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00003610 break;
3611 }
3612
3613 case SUBMODULE_EXCLUDED_HEADER: {
3614 if (First) {
3615 Error("missing submodule metadata record at beginning of block");
3616 return true;
3617 }
3618
3619 if (!CurrentModule)
3620 break;
3621
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00003622 // We lazily associate headers with their modules via the HeaderInfoTable.
3623 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
3624 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00003625 break;
3626 }
3627
3628 case SUBMODULE_TOPHEADER: {
3629 if (First) {
3630 Error("missing submodule metadata record at beginning of block");
3631 return true;
3632 }
3633
3634 if (!CurrentModule)
3635 break;
3636
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00003637 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00003638 break;
3639 }
3640
3641 case SUBMODULE_UMBRELLA_DIR: {
3642 if (First) {
3643 Error("missing submodule metadata record at beginning of block");
3644 return true;
3645 }
3646
3647 if (!CurrentModule)
3648 break;
3649
Guy Benyei11169dd2012-12-18 14:30:41 +00003650 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00003651 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003652 if (!CurrentModule->getUmbrellaDir())
3653 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3654 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3655 Error("mismatched umbrella directories in submodule");
3656 return true;
3657 }
3658 }
3659 break;
3660 }
3661
3662 case SUBMODULE_METADATA: {
3663 if (!First) {
3664 Error("submodule metadata record not at beginning of block");
3665 return true;
3666 }
3667 First = false;
3668
3669 F.BaseSubmoduleID = getTotalNumSubmodules();
3670 F.LocalNumSubmodules = Record[0];
3671 unsigned LocalBaseSubmoduleID = Record[1];
3672 if (F.LocalNumSubmodules > 0) {
3673 // Introduce the global -> local mapping for submodules within this
3674 // module.
3675 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3676
3677 // Introduce the local -> global mapping for submodules within this
3678 // module.
3679 F.SubmoduleRemap.insertOrReplace(
3680 std::make_pair(LocalBaseSubmoduleID,
3681 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3682
3683 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3684 }
3685 break;
3686 }
3687
3688 case SUBMODULE_IMPORTS: {
3689 if (First) {
3690 Error("missing submodule metadata record at beginning of block");
3691 return true;
3692 }
3693
3694 if (!CurrentModule)
3695 break;
3696
3697 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00003698 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00003699 Unresolved.File = &F;
3700 Unresolved.Mod = CurrentModule;
3701 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00003702 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00003703 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00003704 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00003705 }
3706 break;
3707 }
3708
3709 case SUBMODULE_EXPORTS: {
3710 if (First) {
3711 Error("missing submodule metadata record at beginning of block");
3712 return true;
3713 }
3714
3715 if (!CurrentModule)
3716 break;
3717
3718 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00003719 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00003720 Unresolved.File = &F;
3721 Unresolved.Mod = CurrentModule;
3722 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00003723 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00003724 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00003725 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00003726 }
3727
3728 // Once we've loaded the set of exports, there's no reason to keep
3729 // the parsed, unresolved exports around.
3730 CurrentModule->UnresolvedExports.clear();
3731 break;
3732 }
3733 case SUBMODULE_REQUIRES: {
3734 if (First) {
3735 Error("missing submodule metadata record at beginning of block");
3736 return true;
3737 }
3738
3739 if (!CurrentModule)
3740 break;
3741
Chris Lattner0e6c9402013-01-20 02:38:54 +00003742 CurrentModule->addRequirement(Blob, Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00003743 Context.getTargetInfo());
3744 break;
3745 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003746
3747 case SUBMODULE_LINK_LIBRARY:
3748 if (First) {
3749 Error("missing submodule metadata record at beginning of block");
3750 return true;
3751 }
3752
3753 if (!CurrentModule)
3754 break;
3755
3756 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00003757 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00003758 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00003759
3760 case SUBMODULE_CONFIG_MACRO:
3761 if (First) {
3762 Error("missing submodule metadata record at beginning of block");
3763 return true;
3764 }
3765
3766 if (!CurrentModule)
3767 break;
3768
3769 CurrentModule->ConfigMacros.push_back(Blob.str());
3770 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00003771
3772 case SUBMODULE_CONFLICT: {
3773 if (First) {
3774 Error("missing submodule metadata record at beginning of block");
3775 return true;
3776 }
3777
3778 if (!CurrentModule)
3779 break;
3780
3781 UnresolvedModuleRef Unresolved;
3782 Unresolved.File = &F;
3783 Unresolved.Mod = CurrentModule;
3784 Unresolved.ID = Record[0];
3785 Unresolved.Kind = UnresolvedModuleRef::Conflict;
3786 Unresolved.IsWildcard = false;
3787 Unresolved.String = Blob;
3788 UnresolvedModuleRefs.push_back(Unresolved);
3789 break;
3790 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003791 }
3792 }
3793}
3794
3795/// \brief Parse the record that corresponds to a LangOptions data
3796/// structure.
3797///
3798/// This routine parses the language options from the AST file and then gives
3799/// them to the AST listener if one is set.
3800///
3801/// \returns true if the listener deems the file unacceptable, false otherwise.
3802bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3803 bool Complain,
3804 ASTReaderListener &Listener) {
3805 LangOptions LangOpts;
3806 unsigned Idx = 0;
3807#define LANGOPT(Name, Bits, Default, Description) \
3808 LangOpts.Name = Record[Idx++];
3809#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3810 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3811#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00003812#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
3813#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00003814
3815 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3816 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3817 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3818
3819 unsigned Length = Record[Idx++];
3820 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3821 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00003822
3823 Idx += Length;
3824
3825 // Comment options.
3826 for (unsigned N = Record[Idx++]; N; --N) {
3827 LangOpts.CommentOpts.BlockCommandNames.push_back(
3828 ReadString(Record, Idx));
3829 }
3830
Guy Benyei11169dd2012-12-18 14:30:41 +00003831 return Listener.ReadLanguageOptions(LangOpts, Complain);
3832}
3833
3834bool ASTReader::ParseTargetOptions(const RecordData &Record,
3835 bool Complain,
3836 ASTReaderListener &Listener) {
3837 unsigned Idx = 0;
3838 TargetOptions TargetOpts;
3839 TargetOpts.Triple = ReadString(Record, Idx);
3840 TargetOpts.CPU = ReadString(Record, Idx);
3841 TargetOpts.ABI = ReadString(Record, Idx);
3842 TargetOpts.CXXABI = ReadString(Record, Idx);
3843 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3844 for (unsigned N = Record[Idx++]; N; --N) {
3845 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3846 }
3847 for (unsigned N = Record[Idx++]; N; --N) {
3848 TargetOpts.Features.push_back(ReadString(Record, Idx));
3849 }
3850
3851 return Listener.ReadTargetOptions(TargetOpts, Complain);
3852}
3853
3854bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3855 ASTReaderListener &Listener) {
3856 DiagnosticOptions DiagOpts;
3857 unsigned Idx = 0;
3858#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3859#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3860 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3861#include "clang/Basic/DiagnosticOptions.def"
3862
3863 for (unsigned N = Record[Idx++]; N; --N) {
3864 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3865 }
3866
3867 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3868}
3869
3870bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3871 ASTReaderListener &Listener) {
3872 FileSystemOptions FSOpts;
3873 unsigned Idx = 0;
3874 FSOpts.WorkingDir = ReadString(Record, Idx);
3875 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3876}
3877
3878bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3879 bool Complain,
3880 ASTReaderListener &Listener) {
3881 HeaderSearchOptions HSOpts;
3882 unsigned Idx = 0;
3883 HSOpts.Sysroot = ReadString(Record, Idx);
3884
3885 // Include entries.
3886 for (unsigned N = Record[Idx++]; N; --N) {
3887 std::string Path = ReadString(Record, Idx);
3888 frontend::IncludeDirGroup Group
3889 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00003890 bool IsFramework = Record[Idx++];
3891 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00003892 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00003893 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00003894 }
3895
3896 // System header prefixes.
3897 for (unsigned N = Record[Idx++]; N; --N) {
3898 std::string Prefix = ReadString(Record, Idx);
3899 bool IsSystemHeader = Record[Idx++];
3900 HSOpts.SystemHeaderPrefixes.push_back(
3901 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3902 }
3903
3904 HSOpts.ResourceDir = ReadString(Record, Idx);
3905 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3906 HSOpts.DisableModuleHash = Record[Idx++];
3907 HSOpts.UseBuiltinIncludes = Record[Idx++];
3908 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3909 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3910 HSOpts.UseLibcxx = Record[Idx++];
3911
3912 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3913}
3914
3915bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3916 bool Complain,
3917 ASTReaderListener &Listener,
3918 std::string &SuggestedPredefines) {
3919 PreprocessorOptions PPOpts;
3920 unsigned Idx = 0;
3921
3922 // Macro definitions/undefs
3923 for (unsigned N = Record[Idx++]; N; --N) {
3924 std::string Macro = ReadString(Record, Idx);
3925 bool IsUndef = Record[Idx++];
3926 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3927 }
3928
3929 // Includes
3930 for (unsigned N = Record[Idx++]; N; --N) {
3931 PPOpts.Includes.push_back(ReadString(Record, Idx));
3932 }
3933
3934 // Macro Includes
3935 for (unsigned N = Record[Idx++]; N; --N) {
3936 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3937 }
3938
3939 PPOpts.UsePredefines = Record[Idx++];
3940 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3941 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3942 PPOpts.ObjCXXARCStandardLibrary =
3943 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
3944 SuggestedPredefines.clear();
3945 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3946 SuggestedPredefines);
3947}
3948
3949std::pair<ModuleFile *, unsigned>
3950ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
3951 GlobalPreprocessedEntityMapType::iterator
3952 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
3953 assert(I != GlobalPreprocessedEntityMap.end() &&
3954 "Corrupted global preprocessed entity map");
3955 ModuleFile *M = I->second;
3956 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3957 return std::make_pair(M, LocalIndex);
3958}
3959
3960std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3961ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3962 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3963 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3964 Mod.NumPreprocessedEntities);
3965
3966 return std::make_pair(PreprocessingRecord::iterator(),
3967 PreprocessingRecord::iterator());
3968}
3969
3970std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3971ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3972 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3973 ModuleDeclIterator(this, &Mod,
3974 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3975}
3976
3977PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3978 PreprocessedEntityID PPID = Index+1;
3979 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3980 ModuleFile &M = *PPInfo.first;
3981 unsigned LocalIndex = PPInfo.second;
3982 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3983
Guy Benyei11169dd2012-12-18 14:30:41 +00003984 if (!PP.getPreprocessingRecord()) {
3985 Error("no preprocessing record");
3986 return 0;
3987 }
3988
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003989 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
3990 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
3991
3992 llvm::BitstreamEntry Entry =
3993 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
3994 if (Entry.Kind != llvm::BitstreamEntry::Record)
3995 return 0;
3996
Guy Benyei11169dd2012-12-18 14:30:41 +00003997 // Read the record.
3998 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3999 ReadSourceLocation(M, PPOffs.End));
4000 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004001 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004002 RecordData Record;
4003 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004004 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4005 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004006 switch (RecType) {
4007 case PPD_MACRO_EXPANSION: {
4008 bool isBuiltin = Record[0];
4009 IdentifierInfo *Name = 0;
4010 MacroDefinition *Def = 0;
4011 if (isBuiltin)
4012 Name = getLocalIdentifier(M, Record[1]);
4013 else {
4014 PreprocessedEntityID
4015 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4016 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4017 }
4018
4019 MacroExpansion *ME;
4020 if (isBuiltin)
4021 ME = new (PPRec) MacroExpansion(Name, Range);
4022 else
4023 ME = new (PPRec) MacroExpansion(Def, Range);
4024
4025 return ME;
4026 }
4027
4028 case PPD_MACRO_DEFINITION: {
4029 // Decode the identifier info and then check again; if the macro is
4030 // still defined and associated with the identifier,
4031 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4032 MacroDefinition *MD
4033 = new (PPRec) MacroDefinition(II, Range);
4034
4035 if (DeserializationListener)
4036 DeserializationListener->MacroDefinitionRead(PPID, MD);
4037
4038 return MD;
4039 }
4040
4041 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004042 const char *FullFileNameStart = Blob.data() + Record[0];
4043 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004044 const FileEntry *File = 0;
4045 if (!FullFileName.empty())
4046 File = PP.getFileManager().getFile(FullFileName);
4047
4048 // FIXME: Stable encoding
4049 InclusionDirective::InclusionKind Kind
4050 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4051 InclusionDirective *ID
4052 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004053 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004054 Record[1], Record[3],
4055 File,
4056 Range);
4057 return ID;
4058 }
4059 }
4060
4061 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4062}
4063
4064/// \brief \arg SLocMapI points at a chunk of a module that contains no
4065/// preprocessed entities or the entities it contains are not the ones we are
4066/// looking for. Find the next module that contains entities and return the ID
4067/// of the first entry.
4068PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4069 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4070 ++SLocMapI;
4071 for (GlobalSLocOffsetMapType::const_iterator
4072 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4073 ModuleFile &M = *SLocMapI->second;
4074 if (M.NumPreprocessedEntities)
4075 return M.BasePreprocessedEntityID;
4076 }
4077
4078 return getTotalNumPreprocessedEntities();
4079}
4080
4081namespace {
4082
4083template <unsigned PPEntityOffset::*PPLoc>
4084struct PPEntityComp {
4085 const ASTReader &Reader;
4086 ModuleFile &M;
4087
4088 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4089
4090 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4091 SourceLocation LHS = getLoc(L);
4092 SourceLocation RHS = getLoc(R);
4093 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4094 }
4095
4096 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4097 SourceLocation LHS = getLoc(L);
4098 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4099 }
4100
4101 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4102 SourceLocation RHS = getLoc(R);
4103 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4104 }
4105
4106 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4107 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4108 }
4109};
4110
4111}
4112
4113/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4114PreprocessedEntityID
4115ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4116 if (SourceMgr.isLocalSourceLocation(BLoc))
4117 return getTotalNumPreprocessedEntities();
4118
4119 GlobalSLocOffsetMapType::const_iterator
4120 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004121 BLoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004122 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4123 "Corrupted global sloc offset map");
4124
4125 if (SLocMapI->second->NumPreprocessedEntities == 0)
4126 return findNextPreprocessedEntity(SLocMapI);
4127
4128 ModuleFile &M = *SLocMapI->second;
4129 typedef const PPEntityOffset *pp_iterator;
4130 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4131 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4132
4133 size_t Count = M.NumPreprocessedEntities;
4134 size_t Half;
4135 pp_iterator First = pp_begin;
4136 pp_iterator PPI;
4137
4138 // Do a binary search manually instead of using std::lower_bound because
4139 // The end locations of entities may be unordered (when a macro expansion
4140 // is inside another macro argument), but for this case it is not important
4141 // whether we get the first macro expansion or its containing macro.
4142 while (Count > 0) {
4143 Half = Count/2;
4144 PPI = First;
4145 std::advance(PPI, Half);
4146 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4147 BLoc)){
4148 First = PPI;
4149 ++First;
4150 Count = Count - Half - 1;
4151 } else
4152 Count = Half;
4153 }
4154
4155 if (PPI == pp_end)
4156 return findNextPreprocessedEntity(SLocMapI);
4157
4158 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4159}
4160
4161/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4162PreprocessedEntityID
4163ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4164 if (SourceMgr.isLocalSourceLocation(ELoc))
4165 return getTotalNumPreprocessedEntities();
4166
4167 GlobalSLocOffsetMapType::const_iterator
4168 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004169 ELoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004170 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4171 "Corrupted global sloc offset map");
4172
4173 if (SLocMapI->second->NumPreprocessedEntities == 0)
4174 return findNextPreprocessedEntity(SLocMapI);
4175
4176 ModuleFile &M = *SLocMapI->second;
4177 typedef const PPEntityOffset *pp_iterator;
4178 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4179 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4180 pp_iterator PPI =
4181 std::upper_bound(pp_begin, pp_end, ELoc,
4182 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4183
4184 if (PPI == pp_end)
4185 return findNextPreprocessedEntity(SLocMapI);
4186
4187 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4188}
4189
4190/// \brief Returns a pair of [Begin, End) indices of preallocated
4191/// preprocessed entities that \arg Range encompasses.
4192std::pair<unsigned, unsigned>
4193 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4194 if (Range.isInvalid())
4195 return std::make_pair(0,0);
4196 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4197
4198 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4199 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4200 return std::make_pair(BeginID, EndID);
4201}
4202
4203/// \brief Optionally returns true or false if the preallocated preprocessed
4204/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00004205Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00004206 FileID FID) {
4207 if (FID.isInvalid())
4208 return false;
4209
4210 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4211 ModuleFile &M = *PPInfo.first;
4212 unsigned LocalIndex = PPInfo.second;
4213 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4214
4215 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4216 if (Loc.isInvalid())
4217 return false;
4218
4219 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4220 return true;
4221 else
4222 return false;
4223}
4224
4225namespace {
4226 /// \brief Visitor used to search for information about a header file.
4227 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00004228 const FileEntry *FE;
4229
David Blaikie05785d12013-02-20 22:23:23 +00004230 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004231
4232 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004233 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4234 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00004235
4236 static bool visit(ModuleFile &M, void *UserData) {
4237 HeaderFileInfoVisitor *This
4238 = static_cast<HeaderFileInfoVisitor *>(UserData);
4239
Guy Benyei11169dd2012-12-18 14:30:41 +00004240 HeaderFileInfoLookupTable *Table
4241 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4242 if (!Table)
4243 return false;
4244
4245 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00004246 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004247 if (Pos == Table->end())
4248 return false;
4249
4250 This->HFI = *Pos;
4251 return true;
4252 }
4253
David Blaikie05785d12013-02-20 22:23:23 +00004254 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00004255 };
4256}
4257
4258HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004259 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004260 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
David Blaikie05785d12013-02-20 22:23:23 +00004261 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004262 if (Listener)
4263 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4264 return *HFI;
4265 }
4266
4267 return HeaderFileInfo();
4268}
4269
4270void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4271 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004272 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00004273 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4274 ModuleFile &F = *(*I);
4275 unsigned Idx = 0;
4276 DiagStates.clear();
4277 assert(!Diag.DiagStates.empty());
4278 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4279 while (Idx < F.PragmaDiagMappings.size()) {
4280 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4281 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4282 if (DiagStateID != 0) {
4283 Diag.DiagStatePoints.push_back(
4284 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4285 FullSourceLoc(Loc, SourceMgr)));
4286 continue;
4287 }
4288
4289 assert(DiagStateID == 0);
4290 // A new DiagState was created here.
4291 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4292 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4293 DiagStates.push_back(NewState);
4294 Diag.DiagStatePoints.push_back(
4295 DiagnosticsEngine::DiagStatePoint(NewState,
4296 FullSourceLoc(Loc, SourceMgr)));
4297 while (1) {
4298 assert(Idx < F.PragmaDiagMappings.size() &&
4299 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4300 if (Idx >= F.PragmaDiagMappings.size()) {
4301 break; // Something is messed up but at least avoid infinite loop in
4302 // release build.
4303 }
4304 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4305 if (DiagID == (unsigned)-1) {
4306 break; // no more diag/map pairs for this location.
4307 }
4308 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4309 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4310 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4311 }
4312 }
4313 }
4314}
4315
4316/// \brief Get the correct cursor and offset for loading a type.
4317ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4318 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4319 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4320 ModuleFile *M = I->second;
4321 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4322}
4323
4324/// \brief Read and return the type with the given index..
4325///
4326/// The index is the type ID, shifted and minus the number of predefs. This
4327/// routine actually reads the record corresponding to the type at the given
4328/// location. It is a helper routine for GetType, which deals with reading type
4329/// IDs.
4330QualType ASTReader::readTypeRecord(unsigned Index) {
4331 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004332 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00004333
4334 // Keep track of where we are in the stream, then jump back there
4335 // after reading this type.
4336 SavedStreamPosition SavedPosition(DeclsCursor);
4337
4338 ReadingKindTracker ReadingKind(Read_Type, *this);
4339
4340 // Note that we are loading a type record.
4341 Deserializing AType(this);
4342
4343 unsigned Idx = 0;
4344 DeclsCursor.JumpToBit(Loc.Offset);
4345 RecordData Record;
4346 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004347 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004348 case TYPE_EXT_QUAL: {
4349 if (Record.size() != 2) {
4350 Error("Incorrect encoding of extended qualifier type");
4351 return QualType();
4352 }
4353 QualType Base = readType(*Loc.F, Record, Idx);
4354 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4355 return Context.getQualifiedType(Base, Quals);
4356 }
4357
4358 case TYPE_COMPLEX: {
4359 if (Record.size() != 1) {
4360 Error("Incorrect encoding of complex type");
4361 return QualType();
4362 }
4363 QualType ElemType = readType(*Loc.F, Record, Idx);
4364 return Context.getComplexType(ElemType);
4365 }
4366
4367 case TYPE_POINTER: {
4368 if (Record.size() != 1) {
4369 Error("Incorrect encoding of pointer type");
4370 return QualType();
4371 }
4372 QualType PointeeType = readType(*Loc.F, Record, Idx);
4373 return Context.getPointerType(PointeeType);
4374 }
4375
4376 case TYPE_BLOCK_POINTER: {
4377 if (Record.size() != 1) {
4378 Error("Incorrect encoding of block pointer type");
4379 return QualType();
4380 }
4381 QualType PointeeType = readType(*Loc.F, Record, Idx);
4382 return Context.getBlockPointerType(PointeeType);
4383 }
4384
4385 case TYPE_LVALUE_REFERENCE: {
4386 if (Record.size() != 2) {
4387 Error("Incorrect encoding of lvalue reference type");
4388 return QualType();
4389 }
4390 QualType PointeeType = readType(*Loc.F, Record, Idx);
4391 return Context.getLValueReferenceType(PointeeType, Record[1]);
4392 }
4393
4394 case TYPE_RVALUE_REFERENCE: {
4395 if (Record.size() != 1) {
4396 Error("Incorrect encoding of rvalue reference type");
4397 return QualType();
4398 }
4399 QualType PointeeType = readType(*Loc.F, Record, Idx);
4400 return Context.getRValueReferenceType(PointeeType);
4401 }
4402
4403 case TYPE_MEMBER_POINTER: {
4404 if (Record.size() != 2) {
4405 Error("Incorrect encoding of member pointer type");
4406 return QualType();
4407 }
4408 QualType PointeeType = readType(*Loc.F, Record, Idx);
4409 QualType ClassType = readType(*Loc.F, Record, Idx);
4410 if (PointeeType.isNull() || ClassType.isNull())
4411 return QualType();
4412
4413 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4414 }
4415
4416 case TYPE_CONSTANT_ARRAY: {
4417 QualType ElementType = readType(*Loc.F, Record, Idx);
4418 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4419 unsigned IndexTypeQuals = Record[2];
4420 unsigned Idx = 3;
4421 llvm::APInt Size = ReadAPInt(Record, Idx);
4422 return Context.getConstantArrayType(ElementType, Size,
4423 ASM, IndexTypeQuals);
4424 }
4425
4426 case TYPE_INCOMPLETE_ARRAY: {
4427 QualType ElementType = readType(*Loc.F, Record, Idx);
4428 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4429 unsigned IndexTypeQuals = Record[2];
4430 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4431 }
4432
4433 case TYPE_VARIABLE_ARRAY: {
4434 QualType ElementType = readType(*Loc.F, Record, Idx);
4435 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4436 unsigned IndexTypeQuals = Record[2];
4437 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4438 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4439 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4440 ASM, IndexTypeQuals,
4441 SourceRange(LBLoc, RBLoc));
4442 }
4443
4444 case TYPE_VECTOR: {
4445 if (Record.size() != 3) {
4446 Error("incorrect encoding of vector type in AST file");
4447 return QualType();
4448 }
4449
4450 QualType ElementType = readType(*Loc.F, Record, Idx);
4451 unsigned NumElements = Record[1];
4452 unsigned VecKind = Record[2];
4453 return Context.getVectorType(ElementType, NumElements,
4454 (VectorType::VectorKind)VecKind);
4455 }
4456
4457 case TYPE_EXT_VECTOR: {
4458 if (Record.size() != 3) {
4459 Error("incorrect encoding of extended vector type in AST file");
4460 return QualType();
4461 }
4462
4463 QualType ElementType = readType(*Loc.F, Record, Idx);
4464 unsigned NumElements = Record[1];
4465 return Context.getExtVectorType(ElementType, NumElements);
4466 }
4467
4468 case TYPE_FUNCTION_NO_PROTO: {
4469 if (Record.size() != 6) {
4470 Error("incorrect encoding of no-proto function type");
4471 return QualType();
4472 }
4473 QualType ResultType = readType(*Loc.F, Record, Idx);
4474 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4475 (CallingConv)Record[4], Record[5]);
4476 return Context.getFunctionNoProtoType(ResultType, Info);
4477 }
4478
4479 case TYPE_FUNCTION_PROTO: {
4480 QualType ResultType = readType(*Loc.F, Record, Idx);
4481
4482 FunctionProtoType::ExtProtoInfo EPI;
4483 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
4484 /*hasregparm*/ Record[2],
4485 /*regparm*/ Record[3],
4486 static_cast<CallingConv>(Record[4]),
4487 /*produces*/ Record[5]);
4488
4489 unsigned Idx = 6;
4490 unsigned NumParams = Record[Idx++];
4491 SmallVector<QualType, 16> ParamTypes;
4492 for (unsigned I = 0; I != NumParams; ++I)
4493 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
4494
4495 EPI.Variadic = Record[Idx++];
4496 EPI.HasTrailingReturn = Record[Idx++];
4497 EPI.TypeQuals = Record[Idx++];
4498 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
4499 ExceptionSpecificationType EST =
4500 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4501 EPI.ExceptionSpecType = EST;
4502 SmallVector<QualType, 2> Exceptions;
4503 if (EST == EST_Dynamic) {
4504 EPI.NumExceptions = Record[Idx++];
4505 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
4506 Exceptions.push_back(readType(*Loc.F, Record, Idx));
4507 EPI.Exceptions = Exceptions.data();
4508 } else if (EST == EST_ComputedNoexcept) {
4509 EPI.NoexceptExpr = ReadExpr(*Loc.F);
4510 } else if (EST == EST_Uninstantiated) {
4511 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4512 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4513 } else if (EST == EST_Unevaluated) {
4514 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4515 }
Jordan Rose5c382722013-03-08 21:51:21 +00004516 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00004517 }
4518
4519 case TYPE_UNRESOLVED_USING: {
4520 unsigned Idx = 0;
4521 return Context.getTypeDeclType(
4522 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4523 }
4524
4525 case TYPE_TYPEDEF: {
4526 if (Record.size() != 2) {
4527 Error("incorrect encoding of typedef type");
4528 return QualType();
4529 }
4530 unsigned Idx = 0;
4531 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
4532 QualType Canonical = readType(*Loc.F, Record, Idx);
4533 if (!Canonical.isNull())
4534 Canonical = Context.getCanonicalType(Canonical);
4535 return Context.getTypedefType(Decl, Canonical);
4536 }
4537
4538 case TYPE_TYPEOF_EXPR:
4539 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
4540
4541 case TYPE_TYPEOF: {
4542 if (Record.size() != 1) {
4543 Error("incorrect encoding of typeof(type) in AST file");
4544 return QualType();
4545 }
4546 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4547 return Context.getTypeOfType(UnderlyingType);
4548 }
4549
4550 case TYPE_DECLTYPE: {
4551 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4552 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4553 }
4554
4555 case TYPE_UNARY_TRANSFORM: {
4556 QualType BaseType = readType(*Loc.F, Record, Idx);
4557 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4558 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
4559 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
4560 }
4561
4562 case TYPE_AUTO:
4563 return Context.getAutoType(readType(*Loc.F, Record, Idx));
4564
4565 case TYPE_RECORD: {
4566 if (Record.size() != 2) {
4567 Error("incorrect encoding of record type");
4568 return QualType();
4569 }
4570 unsigned Idx = 0;
4571 bool IsDependent = Record[Idx++];
4572 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4573 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4574 QualType T = Context.getRecordType(RD);
4575 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4576 return T;
4577 }
4578
4579 case TYPE_ENUM: {
4580 if (Record.size() != 2) {
4581 Error("incorrect encoding of enum type");
4582 return QualType();
4583 }
4584 unsigned Idx = 0;
4585 bool IsDependent = Record[Idx++];
4586 QualType T
4587 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
4588 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4589 return T;
4590 }
4591
4592 case TYPE_ATTRIBUTED: {
4593 if (Record.size() != 3) {
4594 Error("incorrect encoding of attributed type");
4595 return QualType();
4596 }
4597 QualType modifiedType = readType(*Loc.F, Record, Idx);
4598 QualType equivalentType = readType(*Loc.F, Record, Idx);
4599 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
4600 return Context.getAttributedType(kind, modifiedType, equivalentType);
4601 }
4602
4603 case TYPE_PAREN: {
4604 if (Record.size() != 1) {
4605 Error("incorrect encoding of paren type");
4606 return QualType();
4607 }
4608 QualType InnerType = readType(*Loc.F, Record, Idx);
4609 return Context.getParenType(InnerType);
4610 }
4611
4612 case TYPE_PACK_EXPANSION: {
4613 if (Record.size() != 2) {
4614 Error("incorrect encoding of pack expansion type");
4615 return QualType();
4616 }
4617 QualType Pattern = readType(*Loc.F, Record, Idx);
4618 if (Pattern.isNull())
4619 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00004620 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00004621 if (Record[1])
4622 NumExpansions = Record[1] - 1;
4623 return Context.getPackExpansionType(Pattern, NumExpansions);
4624 }
4625
4626 case TYPE_ELABORATED: {
4627 unsigned Idx = 0;
4628 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4629 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4630 QualType NamedType = readType(*Loc.F, Record, Idx);
4631 return Context.getElaboratedType(Keyword, NNS, NamedType);
4632 }
4633
4634 case TYPE_OBJC_INTERFACE: {
4635 unsigned Idx = 0;
4636 ObjCInterfaceDecl *ItfD
4637 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
4638 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
4639 }
4640
4641 case TYPE_OBJC_OBJECT: {
4642 unsigned Idx = 0;
4643 QualType Base = readType(*Loc.F, Record, Idx);
4644 unsigned NumProtos = Record[Idx++];
4645 SmallVector<ObjCProtocolDecl*, 4> Protos;
4646 for (unsigned I = 0; I != NumProtos; ++I)
4647 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
4648 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
4649 }
4650
4651 case TYPE_OBJC_OBJECT_POINTER: {
4652 unsigned Idx = 0;
4653 QualType Pointee = readType(*Loc.F, Record, Idx);
4654 return Context.getObjCObjectPointerType(Pointee);
4655 }
4656
4657 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
4658 unsigned Idx = 0;
4659 QualType Parm = readType(*Loc.F, Record, Idx);
4660 QualType Replacement = readType(*Loc.F, Record, Idx);
4661 return
4662 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
4663 Replacement);
4664 }
4665
4666 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4667 unsigned Idx = 0;
4668 QualType Parm = readType(*Loc.F, Record, Idx);
4669 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
4670 return Context.getSubstTemplateTypeParmPackType(
4671 cast<TemplateTypeParmType>(Parm),
4672 ArgPack);
4673 }
4674
4675 case TYPE_INJECTED_CLASS_NAME: {
4676 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
4677 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
4678 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
4679 // for AST reading, too much interdependencies.
4680 return
4681 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
4682 }
4683
4684 case TYPE_TEMPLATE_TYPE_PARM: {
4685 unsigned Idx = 0;
4686 unsigned Depth = Record[Idx++];
4687 unsigned Index = Record[Idx++];
4688 bool Pack = Record[Idx++];
4689 TemplateTypeParmDecl *D
4690 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
4691 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
4692 }
4693
4694 case TYPE_DEPENDENT_NAME: {
4695 unsigned Idx = 0;
4696 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4697 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4698 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4699 QualType Canon = readType(*Loc.F, Record, Idx);
4700 if (!Canon.isNull())
4701 Canon = Context.getCanonicalType(Canon);
4702 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
4703 }
4704
4705 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
4706 unsigned Idx = 0;
4707 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
4708 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
4709 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
4710 unsigned NumArgs = Record[Idx++];
4711 SmallVector<TemplateArgument, 8> Args;
4712 Args.reserve(NumArgs);
4713 while (NumArgs--)
4714 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
4715 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
4716 Args.size(), Args.data());
4717 }
4718
4719 case TYPE_DEPENDENT_SIZED_ARRAY: {
4720 unsigned Idx = 0;
4721
4722 // ArrayType
4723 QualType ElementType = readType(*Loc.F, Record, Idx);
4724 ArrayType::ArraySizeModifier ASM
4725 = (ArrayType::ArraySizeModifier)Record[Idx++];
4726 unsigned IndexTypeQuals = Record[Idx++];
4727
4728 // DependentSizedArrayType
4729 Expr *NumElts = ReadExpr(*Loc.F);
4730 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
4731
4732 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
4733 IndexTypeQuals, Brackets);
4734 }
4735
4736 case TYPE_TEMPLATE_SPECIALIZATION: {
4737 unsigned Idx = 0;
4738 bool IsDependent = Record[Idx++];
4739 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
4740 SmallVector<TemplateArgument, 8> Args;
4741 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
4742 QualType Underlying = readType(*Loc.F, Record, Idx);
4743 QualType T;
4744 if (Underlying.isNull())
4745 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
4746 Args.size());
4747 else
4748 T = Context.getTemplateSpecializationType(Name, Args.data(),
4749 Args.size(), Underlying);
4750 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
4751 return T;
4752 }
4753
4754 case TYPE_ATOMIC: {
4755 if (Record.size() != 1) {
4756 Error("Incorrect encoding of atomic type");
4757 return QualType();
4758 }
4759 QualType ValueType = readType(*Loc.F, Record, Idx);
4760 return Context.getAtomicType(ValueType);
4761 }
4762 }
4763 llvm_unreachable("Invalid TypeCode!");
4764}
4765
4766class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
4767 ASTReader &Reader;
4768 ModuleFile &F;
4769 const ASTReader::RecordData &Record;
4770 unsigned &Idx;
4771
4772 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4773 unsigned &I) {
4774 return Reader.ReadSourceLocation(F, R, I);
4775 }
4776
4777 template<typename T>
4778 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4779 return Reader.ReadDeclAs<T>(F, Record, Idx);
4780 }
4781
4782public:
4783 TypeLocReader(ASTReader &Reader, ModuleFile &F,
4784 const ASTReader::RecordData &Record, unsigned &Idx)
4785 : Reader(Reader), F(F), Record(Record), Idx(Idx)
4786 { }
4787
4788 // We want compile-time assurance that we've enumerated all of
4789 // these, so unfortunately we have to declare them first, then
4790 // define them out-of-line.
4791#define ABSTRACT_TYPELOC(CLASS, PARENT)
4792#define TYPELOC(CLASS, PARENT) \
4793 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
4794#include "clang/AST/TypeLocNodes.def"
4795
4796 void VisitFunctionTypeLoc(FunctionTypeLoc);
4797 void VisitArrayTypeLoc(ArrayTypeLoc);
4798};
4799
4800void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
4801 // nothing to do
4802}
4803void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
4804 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
4805 if (TL.needsExtraLocalData()) {
4806 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4807 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4808 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4809 TL.setModeAttr(Record[Idx++]);
4810 }
4811}
4812void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
4813 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4814}
4815void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
4816 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4817}
4818void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
4819 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
4820}
4821void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
4822 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
4823}
4824void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
4825 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
4826}
4827void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
4828 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4829 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4830}
4831void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
4832 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4833 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
4834 if (Record[Idx++])
4835 TL.setSizeExpr(Reader.ReadExpr(F));
4836 else
4837 TL.setSizeExpr(0);
4838}
4839void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4840 VisitArrayTypeLoc(TL);
4841}
4842void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4843 VisitArrayTypeLoc(TL);
4844}
4845void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4846 VisitArrayTypeLoc(TL);
4847}
4848void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4849 DependentSizedArrayTypeLoc TL) {
4850 VisitArrayTypeLoc(TL);
4851}
4852void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4853 DependentSizedExtVectorTypeLoc TL) {
4854 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4855}
4856void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
4857 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4858}
4859void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
4860 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4861}
4862void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
4863 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4864 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4865 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4866 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
4867 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
4868 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
4869 }
4870}
4871void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4872 VisitFunctionTypeLoc(TL);
4873}
4874void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4875 VisitFunctionTypeLoc(TL);
4876}
4877void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
4878 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4879}
4880void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
4881 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4882}
4883void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
4884 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4885 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4886 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4887}
4888void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
4889 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4890 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4891 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4892 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4893}
4894void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
4895 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4896}
4897void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4898 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4899 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4900 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4901 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4902}
4903void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4904 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4905}
4906void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
4907 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4908}
4909void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
4910 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4911}
4912void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4913 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4914 if (TL.hasAttrOperand()) {
4915 SourceRange range;
4916 range.setBegin(ReadSourceLocation(Record, Idx));
4917 range.setEnd(ReadSourceLocation(Record, Idx));
4918 TL.setAttrOperandParensRange(range);
4919 }
4920 if (TL.hasAttrExprOperand()) {
4921 if (Record[Idx++])
4922 TL.setAttrExprOperand(Reader.ReadExpr(F));
4923 else
4924 TL.setAttrExprOperand(0);
4925 } else if (TL.hasAttrEnumOperand())
4926 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4927}
4928void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
4929 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4930}
4931void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4932 SubstTemplateTypeParmTypeLoc TL) {
4933 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4934}
4935void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4936 SubstTemplateTypeParmPackTypeLoc TL) {
4937 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4938}
4939void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4940 TemplateSpecializationTypeLoc TL) {
4941 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4942 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4943 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4944 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4945 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4946 TL.setArgLocInfo(i,
4947 Reader.GetTemplateArgumentLocInfo(F,
4948 TL.getTypePtr()->getArg(i).getKind(),
4949 Record, Idx));
4950}
4951void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4952 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4953 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4954}
4955void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
4956 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4957 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4958}
4959void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
4960 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4961}
4962void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
4963 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4964 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4965 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4966}
4967void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4968 DependentTemplateSpecializationTypeLoc TL) {
4969 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
4970 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
4971 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
4972 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4973 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4974 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4975 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4976 TL.setArgLocInfo(I,
4977 Reader.GetTemplateArgumentLocInfo(F,
4978 TL.getTypePtr()->getArg(I).getKind(),
4979 Record, Idx));
4980}
4981void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4982 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4983}
4984void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
4985 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4986}
4987void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4988 TL.setHasBaseTypeAsWritten(Record[Idx++]);
4989 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4990 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
4991 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
4992 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
4993}
4994void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
4995 TL.setStarLoc(ReadSourceLocation(Record, Idx));
4996}
4997void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4998 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4999 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5000 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5001}
5002
5003TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5004 const RecordData &Record,
5005 unsigned &Idx) {
5006 QualType InfoTy = readType(F, Record, Idx);
5007 if (InfoTy.isNull())
5008 return 0;
5009
5010 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5011 TypeLocReader TLR(*this, F, Record, Idx);
5012 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5013 TLR.Visit(TL);
5014 return TInfo;
5015}
5016
5017QualType ASTReader::GetType(TypeID ID) {
5018 unsigned FastQuals = ID & Qualifiers::FastMask;
5019 unsigned Index = ID >> Qualifiers::FastWidth;
5020
5021 if (Index < NUM_PREDEF_TYPE_IDS) {
5022 QualType T;
5023 switch ((PredefinedTypeIDs)Index) {
5024 case PREDEF_TYPE_NULL_ID: return QualType();
5025 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5026 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5027
5028 case PREDEF_TYPE_CHAR_U_ID:
5029 case PREDEF_TYPE_CHAR_S_ID:
5030 // FIXME: Check that the signedness of CharTy is correct!
5031 T = Context.CharTy;
5032 break;
5033
5034 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5035 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5036 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5037 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5038 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5039 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5040 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5041 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5042 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5043 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5044 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5045 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5046 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5047 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5048 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5049 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5050 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5051 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5052 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5053 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5054 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5055 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5056 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5057 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5058 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5059 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5060 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5061 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005062 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5063 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5064 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5065 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5066 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5067 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005068 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005069 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005070 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5071
5072 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5073 T = Context.getAutoRRefDeductType();
5074 break;
5075
5076 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5077 T = Context.ARCUnbridgedCastTy;
5078 break;
5079
5080 case PREDEF_TYPE_VA_LIST_TAG:
5081 T = Context.getVaListTagType();
5082 break;
5083
5084 case PREDEF_TYPE_BUILTIN_FN:
5085 T = Context.BuiltinFnTy;
5086 break;
5087 }
5088
5089 assert(!T.isNull() && "Unknown predefined type");
5090 return T.withFastQualifiers(FastQuals);
5091 }
5092
5093 Index -= NUM_PREDEF_TYPE_IDS;
5094 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5095 if (TypesLoaded[Index].isNull()) {
5096 TypesLoaded[Index] = readTypeRecord(Index);
5097 if (TypesLoaded[Index].isNull())
5098 return QualType();
5099
5100 TypesLoaded[Index]->setFromAST();
5101 if (DeserializationListener)
5102 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5103 TypesLoaded[Index]);
5104 }
5105
5106 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5107}
5108
5109QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5110 return GetType(getGlobalTypeID(F, LocalID));
5111}
5112
5113serialization::TypeID
5114ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5115 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5116 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5117
5118 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5119 return LocalID;
5120
5121 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5122 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5123 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5124
5125 unsigned GlobalIndex = LocalIndex + I->second;
5126 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5127}
5128
5129TemplateArgumentLocInfo
5130ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5131 TemplateArgument::ArgKind Kind,
5132 const RecordData &Record,
5133 unsigned &Index) {
5134 switch (Kind) {
5135 case TemplateArgument::Expression:
5136 return ReadExpr(F);
5137 case TemplateArgument::Type:
5138 return GetTypeSourceInfo(F, Record, Index);
5139 case TemplateArgument::Template: {
5140 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5141 Index);
5142 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5143 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5144 SourceLocation());
5145 }
5146 case TemplateArgument::TemplateExpansion: {
5147 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5148 Index);
5149 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5150 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5151 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5152 EllipsisLoc);
5153 }
5154 case TemplateArgument::Null:
5155 case TemplateArgument::Integral:
5156 case TemplateArgument::Declaration:
5157 case TemplateArgument::NullPtr:
5158 case TemplateArgument::Pack:
5159 // FIXME: Is this right?
5160 return TemplateArgumentLocInfo();
5161 }
5162 llvm_unreachable("unexpected template argument loc");
5163}
5164
5165TemplateArgumentLoc
5166ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5167 const RecordData &Record, unsigned &Index) {
5168 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5169
5170 if (Arg.getKind() == TemplateArgument::Expression) {
5171 if (Record[Index++]) // bool InfoHasSameExpr.
5172 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5173 }
5174 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5175 Record, Index));
5176}
5177
5178Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5179 return GetDecl(ID);
5180}
5181
5182uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5183 unsigned &Idx){
5184 if (Idx >= Record.size())
5185 return 0;
5186
5187 unsigned LocalID = Record[Idx++];
5188 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5189}
5190
5191CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5192 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005193 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005194 SavedStreamPosition SavedPosition(Cursor);
5195 Cursor.JumpToBit(Loc.Offset);
5196 ReadingKindTracker ReadingKind(Read_Decl, *this);
5197 RecordData Record;
5198 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005199 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00005200 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5201 Error("Malformed AST file: missing C++ base specifiers");
5202 return 0;
5203 }
5204
5205 unsigned Idx = 0;
5206 unsigned NumBases = Record[Idx++];
5207 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5208 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5209 for (unsigned I = 0; I != NumBases; ++I)
5210 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5211 return Bases;
5212}
5213
5214serialization::DeclID
5215ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5216 if (LocalID < NUM_PREDEF_DECL_IDS)
5217 return LocalID;
5218
5219 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5220 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5221 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5222
5223 return LocalID + I->second;
5224}
5225
5226bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5227 ModuleFile &M) const {
5228 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5229 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5230 return &M == I->second;
5231}
5232
Douglas Gregor9f782892013-01-21 15:25:38 +00005233ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005234 if (!D->isFromASTFile())
5235 return 0;
5236 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5237 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5238 return I->second;
5239}
5240
5241SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5242 if (ID < NUM_PREDEF_DECL_IDS)
5243 return SourceLocation();
5244
5245 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5246
5247 if (Index > DeclsLoaded.size()) {
5248 Error("declaration ID out-of-range for AST file");
5249 return SourceLocation();
5250 }
5251
5252 if (Decl *D = DeclsLoaded[Index])
5253 return D->getLocation();
5254
5255 unsigned RawLocation = 0;
5256 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5257 return ReadSourceLocation(*Rec.F, RawLocation);
5258}
5259
5260Decl *ASTReader::GetDecl(DeclID ID) {
5261 if (ID < NUM_PREDEF_DECL_IDS) {
5262 switch ((PredefinedDeclIDs)ID) {
5263 case PREDEF_DECL_NULL_ID:
5264 return 0;
5265
5266 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5267 return Context.getTranslationUnitDecl();
5268
5269 case PREDEF_DECL_OBJC_ID_ID:
5270 return Context.getObjCIdDecl();
5271
5272 case PREDEF_DECL_OBJC_SEL_ID:
5273 return Context.getObjCSelDecl();
5274
5275 case PREDEF_DECL_OBJC_CLASS_ID:
5276 return Context.getObjCClassDecl();
5277
5278 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5279 return Context.getObjCProtocolDecl();
5280
5281 case PREDEF_DECL_INT_128_ID:
5282 return Context.getInt128Decl();
5283
5284 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5285 return Context.getUInt128Decl();
5286
5287 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5288 return Context.getObjCInstanceTypeDecl();
5289
5290 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5291 return Context.getBuiltinVaListDecl();
5292 }
5293 }
5294
5295 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5296
5297 if (Index >= DeclsLoaded.size()) {
5298 assert(0 && "declaration ID out-of-range for AST file");
5299 Error("declaration ID out-of-range for AST file");
5300 return 0;
5301 }
5302
5303 if (!DeclsLoaded[Index]) {
5304 ReadDeclRecord(ID);
5305 if (DeserializationListener)
5306 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5307 }
5308
5309 return DeclsLoaded[Index];
5310}
5311
5312DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5313 DeclID GlobalID) {
5314 if (GlobalID < NUM_PREDEF_DECL_IDS)
5315 return GlobalID;
5316
5317 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5318 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5319 ModuleFile *Owner = I->second;
5320
5321 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5322 = M.GlobalToLocalDeclIDs.find(Owner);
5323 if (Pos == M.GlobalToLocalDeclIDs.end())
5324 return 0;
5325
5326 return GlobalID - Owner->BaseDeclID + Pos->second;
5327}
5328
5329serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5330 const RecordData &Record,
5331 unsigned &Idx) {
5332 if (Idx >= Record.size()) {
5333 Error("Corrupted AST file");
5334 return 0;
5335 }
5336
5337 return getGlobalDeclID(F, Record[Idx++]);
5338}
5339
5340/// \brief Resolve the offset of a statement into a statement.
5341///
5342/// This operation will read a new statement from the external
5343/// source each time it is called, and is meant to be used via a
5344/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5345Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5346 // Switch case IDs are per Decl.
5347 ClearSwitchCaseIDs();
5348
5349 // Offset here is a global offset across the entire chain.
5350 RecordLocation Loc = getLocalBitOffset(Offset);
5351 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5352 return ReadStmtFromStream(*Loc.F);
5353}
5354
5355namespace {
5356 class FindExternalLexicalDeclsVisitor {
5357 ASTReader &Reader;
5358 const DeclContext *DC;
5359 bool (*isKindWeWant)(Decl::Kind);
5360
5361 SmallVectorImpl<Decl*> &Decls;
5362 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5363
5364 public:
5365 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5366 bool (*isKindWeWant)(Decl::Kind),
5367 SmallVectorImpl<Decl*> &Decls)
5368 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5369 {
5370 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5371 PredefsVisited[I] = false;
5372 }
5373
5374 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5375 if (Preorder)
5376 return false;
5377
5378 FindExternalLexicalDeclsVisitor *This
5379 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5380
5381 ModuleFile::DeclContextInfosMap::iterator Info
5382 = M.DeclContextInfos.find(This->DC);
5383 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5384 return false;
5385
5386 // Load all of the declaration IDs
5387 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5388 *IDE = ID + Info->second.NumLexicalDecls;
5389 ID != IDE; ++ID) {
5390 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5391 continue;
5392
5393 // Don't add predefined declarations to the lexical context more
5394 // than once.
5395 if (ID->second < NUM_PREDEF_DECL_IDS) {
5396 if (This->PredefsVisited[ID->second])
5397 continue;
5398
5399 This->PredefsVisited[ID->second] = true;
5400 }
5401
5402 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5403 if (!This->DC->isDeclInLexicalTraversal(D))
5404 This->Decls.push_back(D);
5405 }
5406 }
5407
5408 return false;
5409 }
5410 };
5411}
5412
5413ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5414 bool (*isKindWeWant)(Decl::Kind),
5415 SmallVectorImpl<Decl*> &Decls) {
5416 // There might be lexical decls in multiple modules, for the TU at
5417 // least. Walk all of the modules in the order they were loaded.
5418 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5419 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5420 ++NumLexicalDeclContextsRead;
5421 return ELR_Success;
5422}
5423
5424namespace {
5425
5426class DeclIDComp {
5427 ASTReader &Reader;
5428 ModuleFile &Mod;
5429
5430public:
5431 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5432
5433 bool operator()(LocalDeclID L, LocalDeclID R) const {
5434 SourceLocation LHS = getLocation(L);
5435 SourceLocation RHS = getLocation(R);
5436 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5437 }
5438
5439 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5440 SourceLocation RHS = getLocation(R);
5441 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5442 }
5443
5444 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5445 SourceLocation LHS = getLocation(L);
5446 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5447 }
5448
5449 SourceLocation getLocation(LocalDeclID ID) const {
5450 return Reader.getSourceManager().getFileLoc(
5451 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5452 }
5453};
5454
5455}
5456
5457void ASTReader::FindFileRegionDecls(FileID File,
5458 unsigned Offset, unsigned Length,
5459 SmallVectorImpl<Decl *> &Decls) {
5460 SourceManager &SM = getSourceManager();
5461
5462 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5463 if (I == FileDeclIDs.end())
5464 return;
5465
5466 FileDeclsInfo &DInfo = I->second;
5467 if (DInfo.Decls.empty())
5468 return;
5469
5470 SourceLocation
5471 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5472 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5473
5474 DeclIDComp DIDComp(*this, *DInfo.Mod);
5475 ArrayRef<serialization::LocalDeclID>::iterator
5476 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5477 BeginLoc, DIDComp);
5478 if (BeginIt != DInfo.Decls.begin())
5479 --BeginIt;
5480
5481 // If we are pointing at a top-level decl inside an objc container, we need
5482 // to backtrack until we find it otherwise we will fail to report that the
5483 // region overlaps with an objc container.
5484 while (BeginIt != DInfo.Decls.begin() &&
5485 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5486 ->isTopLevelDeclInObjCContainer())
5487 --BeginIt;
5488
5489 ArrayRef<serialization::LocalDeclID>::iterator
5490 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5491 EndLoc, DIDComp);
5492 if (EndIt != DInfo.Decls.end())
5493 ++EndIt;
5494
5495 for (ArrayRef<serialization::LocalDeclID>::iterator
5496 DIt = BeginIt; DIt != EndIt; ++DIt)
5497 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5498}
5499
5500namespace {
5501 /// \brief ModuleFile visitor used to perform name lookup into a
5502 /// declaration context.
5503 class DeclContextNameLookupVisitor {
5504 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005505 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00005506 DeclarationName Name;
5507 SmallVectorImpl<NamedDecl *> &Decls;
5508
5509 public:
5510 DeclContextNameLookupVisitor(ASTReader &Reader,
5511 SmallVectorImpl<const DeclContext *> &Contexts,
5512 DeclarationName Name,
5513 SmallVectorImpl<NamedDecl *> &Decls)
5514 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
5515
5516 static bool visit(ModuleFile &M, void *UserData) {
5517 DeclContextNameLookupVisitor *This
5518 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5519
5520 // Check whether we have any visible declaration information for
5521 // this context in this module.
5522 ModuleFile::DeclContextInfosMap::iterator Info;
5523 bool FoundInfo = false;
5524 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5525 Info = M.DeclContextInfos.find(This->Contexts[I]);
5526 if (Info != M.DeclContextInfos.end() &&
5527 Info->second.NameLookupTableData) {
5528 FoundInfo = true;
5529 break;
5530 }
5531 }
5532
5533 if (!FoundInfo)
5534 return false;
5535
5536 // Look for this name within this module.
5537 ASTDeclContextNameLookupTable *LookupTable =
5538 Info->second.NameLookupTableData;
5539 ASTDeclContextNameLookupTable::iterator Pos
5540 = LookupTable->find(This->Name);
5541 if (Pos == LookupTable->end())
5542 return false;
5543
5544 bool FoundAnything = false;
5545 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5546 for (; Data.first != Data.second; ++Data.first) {
5547 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5548 if (!ND)
5549 continue;
5550
5551 if (ND->getDeclName() != This->Name) {
5552 // A name might be null because the decl's redeclarable part is
5553 // currently read before reading its name. The lookup is triggered by
5554 // building that decl (likely indirectly), and so it is later in the
5555 // sense of "already existing" and can be ignored here.
5556 continue;
5557 }
5558
5559 // Record this declaration.
5560 FoundAnything = true;
5561 This->Decls.push_back(ND);
5562 }
5563
5564 return FoundAnything;
5565 }
5566 };
5567}
5568
Douglas Gregor9f782892013-01-21 15:25:38 +00005569/// \brief Retrieve the "definitive" module file for the definition of the
5570/// given declaration context, if there is one.
5571///
5572/// The "definitive" module file is the only place where we need to look to
5573/// find information about the declarations within the given declaration
5574/// context. For example, C++ and Objective-C classes, C structs/unions, and
5575/// Objective-C protocols, categories, and extensions are all defined in a
5576/// single place in the source code, so they have definitive module files
5577/// associated with them. C++ namespaces, on the other hand, can have
5578/// definitions in multiple different module files.
5579///
5580/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
5581/// NDEBUG checking.
5582static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
5583 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00005584 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
5585 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00005586
5587 return 0;
5588}
5589
Richard Smith9ce12e32013-02-07 03:30:24 +00005590bool
Guy Benyei11169dd2012-12-18 14:30:41 +00005591ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
5592 DeclarationName Name) {
5593 assert(DC->hasExternalVisibleStorage() &&
5594 "DeclContext has no visible decls in storage");
5595 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00005596 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00005597
5598 SmallVector<NamedDecl *, 64> Decls;
5599
5600 // Compute the declaration contexts we need to look into. Multiple such
5601 // declaration contexts occur when two declaration contexts from disjoint
5602 // modules get merged, e.g., when two namespaces with the same name are
5603 // independently defined in separate modules.
5604 SmallVector<const DeclContext *, 2> Contexts;
5605 Contexts.push_back(DC);
5606
5607 if (DC->isNamespace()) {
5608 MergedDeclsMap::iterator Merged
5609 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5610 if (Merged != MergedDecls.end()) {
5611 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5612 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5613 }
5614 }
5615
5616 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor9f782892013-01-21 15:25:38 +00005617
5618 // If we can definitively determine which module file to look into,
5619 // only look there. Otherwise, look in all module files.
5620 ModuleFile *Definitive;
5621 if (Contexts.size() == 1 &&
5622 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
5623 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
5624 } else {
5625 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
5626 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005627 ++NumVisibleDeclContextsRead;
5628 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00005629 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00005630}
5631
5632namespace {
5633 /// \brief ModuleFile visitor used to retrieve all visible names in a
5634 /// declaration context.
5635 class DeclContextAllNamesVisitor {
5636 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005637 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00005638 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005639 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00005640
5641 public:
5642 DeclContextAllNamesVisitor(ASTReader &Reader,
5643 SmallVectorImpl<const DeclContext *> &Contexts,
5644 llvm::DenseMap<DeclarationName,
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005645 SmallVector<NamedDecl *, 8> > &Decls,
5646 bool VisitAll)
5647 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00005648
5649 static bool visit(ModuleFile &M, void *UserData) {
5650 DeclContextAllNamesVisitor *This
5651 = static_cast<DeclContextAllNamesVisitor *>(UserData);
5652
5653 // Check whether we have any visible declaration information for
5654 // this context in this module.
5655 ModuleFile::DeclContextInfosMap::iterator Info;
5656 bool FoundInfo = false;
5657 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5658 Info = M.DeclContextInfos.find(This->Contexts[I]);
5659 if (Info != M.DeclContextInfos.end() &&
5660 Info->second.NameLookupTableData) {
5661 FoundInfo = true;
5662 break;
5663 }
5664 }
5665
5666 if (!FoundInfo)
5667 return false;
5668
5669 ASTDeclContextNameLookupTable *LookupTable =
5670 Info->second.NameLookupTableData;
5671 bool FoundAnything = false;
5672 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00005673 I = LookupTable->data_begin(), E = LookupTable->data_end();
5674 I != E;
5675 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005676 ASTDeclContextNameLookupTrait::data_type Data = *I;
5677 for (; Data.first != Data.second; ++Data.first) {
5678 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5679 *Data.first);
5680 if (!ND)
5681 continue;
5682
5683 // Record this declaration.
5684 FoundAnything = true;
5685 This->Decls[ND->getDeclName()].push_back(ND);
5686 }
5687 }
5688
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005689 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00005690 }
5691 };
5692}
5693
5694void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
5695 if (!DC->hasExternalVisibleStorage())
5696 return;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005697 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00005698
5699 // Compute the declaration contexts we need to look into. Multiple such
5700 // declaration contexts occur when two declaration contexts from disjoint
5701 // modules get merged, e.g., when two namespaces with the same name are
5702 // independently defined in separate modules.
5703 SmallVector<const DeclContext *, 2> Contexts;
5704 Contexts.push_back(DC);
5705
5706 if (DC->isNamespace()) {
5707 MergedDeclsMap::iterator Merged
5708 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5709 if (Merged != MergedDecls.end()) {
5710 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5711 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5712 }
5713 }
5714
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00005715 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
5716 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00005717 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5718 ++NumVisibleDeclContextsRead;
5719
5720 for (llvm::DenseMap<DeclarationName,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00005721 SmallVector<NamedDecl *, 8> >::iterator
Guy Benyei11169dd2012-12-18 14:30:41 +00005722 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5723 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5724 }
5725 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
5726}
5727
5728/// \brief Under non-PCH compilation the consumer receives the objc methods
5729/// before receiving the implementation, and codegen depends on this.
5730/// We simulate this by deserializing and passing to consumer the methods of the
5731/// implementation before passing the deserialized implementation decl.
5732static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5733 ASTConsumer *Consumer) {
5734 assert(ImplD && Consumer);
5735
5736 for (ObjCImplDecl::method_iterator
5737 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
5738 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
5739
5740 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5741}
5742
5743void ASTReader::PassInterestingDeclsToConsumer() {
5744 assert(Consumer);
5745 while (!InterestingDecls.empty()) {
5746 Decl *D = InterestingDecls.front();
5747 InterestingDecls.pop_front();
5748
5749 PassInterestingDeclToConsumer(D);
5750 }
5751}
5752
5753void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5754 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5755 PassObjCImplDeclToConsumer(ImplD, Consumer);
5756 else
5757 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5758}
5759
5760void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
5761 this->Consumer = Consumer;
5762
5763 if (!Consumer)
5764 return;
5765
5766 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
5767 // Force deserialization of this decl, which will cause it to be queued for
5768 // passing to the consumer.
5769 GetDecl(ExternalDefinitions[I]);
5770 }
5771 ExternalDefinitions.clear();
5772
5773 PassInterestingDeclsToConsumer();
5774}
5775
5776void ASTReader::PrintStats() {
5777 std::fprintf(stderr, "*** AST File Statistics:\n");
5778
5779 unsigned NumTypesLoaded
5780 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
5781 QualType());
5782 unsigned NumDeclsLoaded
5783 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5784 (Decl *)0);
5785 unsigned NumIdentifiersLoaded
5786 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5787 IdentifiersLoaded.end(),
5788 (IdentifierInfo *)0);
5789 unsigned NumMacrosLoaded
5790 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5791 MacrosLoaded.end(),
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00005792 (MacroDirective *)0);
Guy Benyei11169dd2012-12-18 14:30:41 +00005793 unsigned NumSelectorsLoaded
5794 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5795 SelectorsLoaded.end(),
5796 Selector());
5797
5798 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
5799 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5800 NumSLocEntriesRead, TotalNumSLocEntries,
5801 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
5802 if (!TypesLoaded.empty())
5803 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
5804 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5805 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5806 if (!DeclsLoaded.empty())
5807 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
5808 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5809 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
5810 if (!IdentifiersLoaded.empty())
5811 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
5812 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5813 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
5814 if (!MacrosLoaded.empty())
5815 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5816 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5817 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
5818 if (!SelectorsLoaded.empty())
5819 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
5820 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5821 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
5822 if (TotalNumStatements)
5823 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5824 NumStatementsRead, TotalNumStatements,
5825 ((float)NumStatementsRead/TotalNumStatements * 100));
5826 if (TotalNumMacros)
5827 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5828 NumMacrosRead, TotalNumMacros,
5829 ((float)NumMacrosRead/TotalNumMacros * 100));
5830 if (TotalLexicalDeclContexts)
5831 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5832 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5833 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5834 * 100));
5835 if (TotalVisibleDeclContexts)
5836 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5837 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5838 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5839 * 100));
5840 if (TotalNumMethodPoolEntries) {
5841 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
5842 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5843 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
5844 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00005845 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00005846 if (NumMethodPoolLookups) {
5847 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
5848 NumMethodPoolHits, NumMethodPoolLookups,
5849 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
5850 }
5851 if (NumMethodPoolTableLookups) {
5852 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
5853 NumMethodPoolTableHits, NumMethodPoolTableLookups,
5854 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
5855 * 100.0));
5856 }
5857
Douglas Gregor00a50f72013-01-25 00:38:33 +00005858 if (NumIdentifierLookupHits) {
5859 std::fprintf(stderr,
5860 " %u / %u identifier table lookups succeeded (%f%%)\n",
5861 NumIdentifierLookupHits, NumIdentifierLookups,
5862 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
5863 }
5864
Douglas Gregore060e572013-01-25 01:03:03 +00005865 if (GlobalIndex) {
5866 std::fprintf(stderr, "\n");
5867 GlobalIndex->printStats();
5868 }
5869
Guy Benyei11169dd2012-12-18 14:30:41 +00005870 std::fprintf(stderr, "\n");
5871 dump();
5872 std::fprintf(stderr, "\n");
5873}
5874
5875template<typename Key, typename ModuleFile, unsigned InitialCapacity>
5876static void
5877dumpModuleIDMap(StringRef Name,
5878 const ContinuousRangeMap<Key, ModuleFile *,
5879 InitialCapacity> &Map) {
5880 if (Map.begin() == Map.end())
5881 return;
5882
5883 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
5884 llvm::errs() << Name << ":\n";
5885 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5886 I != IEnd; ++I) {
5887 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5888 << "\n";
5889 }
5890}
5891
5892void ASTReader::dump() {
5893 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
5894 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
5895 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
5896 dumpModuleIDMap("Global type map", GlobalTypeMap);
5897 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
5898 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
5899 dumpModuleIDMap("Global macro map", GlobalMacroMap);
5900 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
5901 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
5902 dumpModuleIDMap("Global preprocessed entity map",
5903 GlobalPreprocessedEntityMap);
5904
5905 llvm::errs() << "\n*** PCH/Modules Loaded:";
5906 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5907 MEnd = ModuleMgr.end();
5908 M != MEnd; ++M)
5909 (*M)->dump();
5910}
5911
5912/// Return the amount of memory used by memory buffers, breaking down
5913/// by heap-backed versus mmap'ed memory.
5914void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
5915 for (ModuleConstIterator I = ModuleMgr.begin(),
5916 E = ModuleMgr.end(); I != E; ++I) {
5917 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
5918 size_t bytes = buf->getBufferSize();
5919 switch (buf->getBufferKind()) {
5920 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5921 sizes.malloc_bytes += bytes;
5922 break;
5923 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5924 sizes.mmap_bytes += bytes;
5925 break;
5926 }
5927 }
5928 }
5929}
5930
5931void ASTReader::InitializeSema(Sema &S) {
5932 SemaObj = &S;
5933 S.addExternalSource(this);
5934
5935 // Makes sure any declarations that were deserialized "too early"
5936 // still get added to the identifier's declaration chains.
5937 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00005938 NamedDecl *ND = cast<NamedDecl>(PreloadedDecls[I]->getMostRecentDecl());
5939 SemaObj->pushExternalDeclIntoScope(ND, PreloadedDecls[I]->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00005940 }
5941 PreloadedDecls.clear();
5942
5943 // Load the offsets of the declarations that Sema references.
5944 // They will be lazily deserialized when needed.
5945 if (!SemaDeclRefs.empty()) {
5946 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
5947 if (!SemaObj->StdNamespace)
5948 SemaObj->StdNamespace = SemaDeclRefs[0];
5949 if (!SemaObj->StdBadAlloc)
5950 SemaObj->StdBadAlloc = SemaDeclRefs[1];
5951 }
5952
5953 if (!FPPragmaOptions.empty()) {
5954 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5955 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5956 }
5957
5958 if (!OpenCLExtensions.empty()) {
5959 unsigned I = 0;
5960#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5961#include "clang/Basic/OpenCLExtensions.def"
5962
5963 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5964 }
5965}
5966
5967IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
5968 // Note that we are loading an identifier.
5969 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00005970 StringRef Name(NameStart, NameEnd - NameStart);
5971
5972 // If there is a global index, look there first to determine which modules
5973 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00005974 GlobalModuleIndex::HitSet Hits;
5975 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00005976 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00005977 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
5978 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00005979 }
5980 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00005981 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00005982 NumIdentifierLookups,
5983 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00005984 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00005985 IdentifierInfo *II = Visitor.getIdentifierInfo();
5986 markIdentifierUpToDate(II);
5987 return II;
5988}
5989
5990namespace clang {
5991 /// \brief An identifier-lookup iterator that enumerates all of the
5992 /// identifiers stored within a set of AST files.
5993 class ASTIdentifierIterator : public IdentifierIterator {
5994 /// \brief The AST reader whose identifiers are being enumerated.
5995 const ASTReader &Reader;
5996
5997 /// \brief The current index into the chain of AST files stored in
5998 /// the AST reader.
5999 unsigned Index;
6000
6001 /// \brief The current position within the identifier lookup table
6002 /// of the current AST file.
6003 ASTIdentifierLookupTable::key_iterator Current;
6004
6005 /// \brief The end position within the identifier lookup table of
6006 /// the current AST file.
6007 ASTIdentifierLookupTable::key_iterator End;
6008
6009 public:
6010 explicit ASTIdentifierIterator(const ASTReader &Reader);
6011
6012 virtual StringRef Next();
6013 };
6014}
6015
6016ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6017 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6018 ASTIdentifierLookupTable *IdTable
6019 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6020 Current = IdTable->key_begin();
6021 End = IdTable->key_end();
6022}
6023
6024StringRef ASTIdentifierIterator::Next() {
6025 while (Current == End) {
6026 // If we have exhausted all of our AST files, we're done.
6027 if (Index == 0)
6028 return StringRef();
6029
6030 --Index;
6031 ASTIdentifierLookupTable *IdTable
6032 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6033 IdentifierLookupTable;
6034 Current = IdTable->key_begin();
6035 End = IdTable->key_end();
6036 }
6037
6038 // We have any identifiers remaining in the current AST file; return
6039 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006040 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00006041 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006042 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00006043}
6044
6045IdentifierIterator *ASTReader::getIdentifiers() const {
6046 return new ASTIdentifierIterator(*this);
6047}
6048
6049namespace clang { namespace serialization {
6050 class ReadMethodPoolVisitor {
6051 ASTReader &Reader;
6052 Selector Sel;
6053 unsigned PriorGeneration;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006054 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6055 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00006056
6057 public:
6058 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6059 unsigned PriorGeneration)
6060 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
6061
6062 static bool visit(ModuleFile &M, void *UserData) {
6063 ReadMethodPoolVisitor *This
6064 = static_cast<ReadMethodPoolVisitor *>(UserData);
6065
6066 if (!M.SelectorLookupTable)
6067 return false;
6068
6069 // If we've already searched this module file, skip it now.
6070 if (M.Generation <= This->PriorGeneration)
6071 return true;
6072
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006073 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006074 ASTSelectorLookupTable *PoolTable
6075 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6076 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6077 if (Pos == PoolTable->end())
6078 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006079
6080 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006081 ++This->Reader.NumSelectorsRead;
6082 // FIXME: Not quite happy with the statistics here. We probably should
6083 // disable this tracking when called via LoadSelector.
6084 // Also, should entries without methods count as misses?
6085 ++This->Reader.NumMethodPoolEntriesRead;
6086 ASTSelectorLookupTrait::data_type Data = *Pos;
6087 if (This->Reader.DeserializationListener)
6088 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6089 This->Sel);
6090
6091 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6092 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
6093 return true;
6094 }
6095
6096 /// \brief Retrieve the instance methods found by this visitor.
6097 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6098 return InstanceMethods;
6099 }
6100
6101 /// \brief Retrieve the instance methods found by this visitor.
6102 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6103 return FactoryMethods;
6104 }
6105 };
6106} } // end namespace clang::serialization
6107
6108/// \brief Add the given set of methods to the method list.
6109static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6110 ObjCMethodList &List) {
6111 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6112 S.addMethodToGlobalList(&List, Methods[I]);
6113 }
6114}
6115
6116void ASTReader::ReadMethodPool(Selector Sel) {
6117 // Get the selector generation and update it to the current generation.
6118 unsigned &Generation = SelectorGeneration[Sel];
6119 unsigned PriorGeneration = Generation;
6120 Generation = CurrentGeneration;
6121
6122 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006123 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006124 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6125 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6126
6127 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006128 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00006129 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006130
6131 ++NumMethodPoolHits;
6132
Guy Benyei11169dd2012-12-18 14:30:41 +00006133 if (!getSema())
6134 return;
6135
6136 Sema &S = *getSema();
6137 Sema::GlobalMethodPool::iterator Pos
6138 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6139
6140 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6141 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
6142}
6143
6144void ASTReader::ReadKnownNamespaces(
6145 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6146 Namespaces.clear();
6147
6148 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6149 if (NamespaceDecl *Namespace
6150 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6151 Namespaces.push_back(Namespace);
6152 }
6153}
6154
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006155void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00006156 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006157 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6158 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00006159 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006160 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00006161 Undefined.insert(std::make_pair(D, Loc));
6162 }
6163}
Nick Lewycky8334af82013-01-26 00:35:08 +00006164
Guy Benyei11169dd2012-12-18 14:30:41 +00006165void ASTReader::ReadTentativeDefinitions(
6166 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6167 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6168 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6169 if (Var)
6170 TentativeDefs.push_back(Var);
6171 }
6172 TentativeDefinitions.clear();
6173}
6174
6175void ASTReader::ReadUnusedFileScopedDecls(
6176 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6177 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6178 DeclaratorDecl *D
6179 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6180 if (D)
6181 Decls.push_back(D);
6182 }
6183 UnusedFileScopedDecls.clear();
6184}
6185
6186void ASTReader::ReadDelegatingConstructors(
6187 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6188 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6189 CXXConstructorDecl *D
6190 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6191 if (D)
6192 Decls.push_back(D);
6193 }
6194 DelegatingCtorDecls.clear();
6195}
6196
6197void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6198 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6199 TypedefNameDecl *D
6200 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6201 if (D)
6202 Decls.push_back(D);
6203 }
6204 ExtVectorDecls.clear();
6205}
6206
6207void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6208 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6209 CXXRecordDecl *D
6210 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6211 if (D)
6212 Decls.push_back(D);
6213 }
6214 DynamicClasses.clear();
6215}
6216
6217void
Richard Smith78165b52013-01-10 23:43:47 +00006218ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6219 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6220 NamedDecl *D
6221 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00006222 if (D)
6223 Decls.push_back(D);
6224 }
Richard Smith78165b52013-01-10 23:43:47 +00006225 LocallyScopedExternCDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006226}
6227
6228void ASTReader::ReadReferencedSelectors(
6229 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6230 if (ReferencedSelectorsData.empty())
6231 return;
6232
6233 // If there are @selector references added them to its pool. This is for
6234 // implementation of -Wselector.
6235 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6236 unsigned I = 0;
6237 while (I < DataSize) {
6238 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6239 SourceLocation SelLoc
6240 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6241 Sels.push_back(std::make_pair(Sel, SelLoc));
6242 }
6243 ReferencedSelectorsData.clear();
6244}
6245
6246void ASTReader::ReadWeakUndeclaredIdentifiers(
6247 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6248 if (WeakUndeclaredIdentifiers.empty())
6249 return;
6250
6251 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6252 IdentifierInfo *WeakId
6253 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6254 IdentifierInfo *AliasId
6255 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6256 SourceLocation Loc
6257 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6258 bool Used = WeakUndeclaredIdentifiers[I++];
6259 WeakInfo WI(AliasId, Loc);
6260 WI.setUsed(Used);
6261 WeakIDs.push_back(std::make_pair(WeakId, WI));
6262 }
6263 WeakUndeclaredIdentifiers.clear();
6264}
6265
6266void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6267 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6268 ExternalVTableUse VT;
6269 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6270 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6271 VT.DefinitionRequired = VTableUses[Idx++];
6272 VTables.push_back(VT);
6273 }
6274
6275 VTableUses.clear();
6276}
6277
6278void ASTReader::ReadPendingInstantiations(
6279 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6280 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6281 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6282 SourceLocation Loc
6283 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6284
6285 Pending.push_back(std::make_pair(D, Loc));
6286 }
6287 PendingInstantiations.clear();
6288}
6289
6290void ASTReader::LoadSelector(Selector Sel) {
6291 // It would be complicated to avoid reading the methods anyway. So don't.
6292 ReadMethodPool(Sel);
6293}
6294
6295void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6296 assert(ID && "Non-zero identifier ID required");
6297 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6298 IdentifiersLoaded[ID - 1] = II;
6299 if (DeserializationListener)
6300 DeserializationListener->IdentifierRead(ID, II);
6301}
6302
6303/// \brief Set the globally-visible declarations associated with the given
6304/// identifier.
6305///
6306/// If the AST reader is currently in a state where the given declaration IDs
6307/// cannot safely be resolved, they are queued until it is safe to resolve
6308/// them.
6309///
6310/// \param II an IdentifierInfo that refers to one or more globally-visible
6311/// declarations.
6312///
6313/// \param DeclIDs the set of declaration IDs with the name @p II that are
6314/// visible at global scope.
6315///
Douglas Gregor6168bd22013-02-18 15:53:43 +00006316/// \param Decls if non-null, this vector will be populated with the set of
6317/// deserialized declarations. These declarations will not be pushed into
6318/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00006319void
6320ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6321 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00006322 SmallVectorImpl<Decl *> *Decls) {
6323 if (NumCurrentElementsDeserializing && !Decls) {
6324 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00006325 return;
6326 }
6327
6328 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6329 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6330 if (SemaObj) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00006331 // If we're simply supposed to record the declarations, do so now.
6332 if (Decls) {
6333 Decls->push_back(D);
6334 continue;
6335 }
6336
Guy Benyei11169dd2012-12-18 14:30:41 +00006337 // Introduce this declaration into the translation-unit scope
6338 // and add it to the declaration chain for this identifier, so
6339 // that (unqualified) name lookup will find it.
Douglas Gregor6168bd22013-02-18 15:53:43 +00006340 NamedDecl *ND = cast<NamedDecl>(D->getMostRecentDecl());
6341 SemaObj->pushExternalDeclIntoScope(ND, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00006342 } else {
6343 // Queue this declaration so that it will be added to the
6344 // translation unit scope and identifier's declaration chain
6345 // once a Sema object is known.
6346 PreloadedDecls.push_back(D);
6347 }
6348 }
6349}
6350
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006351IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006352 if (ID == 0)
6353 return 0;
6354
6355 if (IdentifiersLoaded.empty()) {
6356 Error("no identifier table in AST file");
6357 return 0;
6358 }
6359
6360 ID -= 1;
6361 if (!IdentifiersLoaded[ID]) {
6362 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6363 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6364 ModuleFile *M = I->second;
6365 unsigned Index = ID - M->BaseIdentifierID;
6366 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6367
6368 // All of the strings in the AST file are preceded by a 16-bit length.
6369 // Extract that 16-bit length to avoid having to execute strlen().
6370 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6371 // unsigned integers. This is important to avoid integer overflow when
6372 // we cast them to 'unsigned'.
6373 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6374 unsigned StrLen = (((unsigned) StrLenPtr[0])
6375 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006376 IdentifiersLoaded[ID]
6377 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00006378 if (DeserializationListener)
6379 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6380 }
6381
6382 return IdentifiersLoaded[ID];
6383}
6384
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006385IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6386 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00006387}
6388
6389IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6390 if (LocalID < NUM_PREDEF_IDENT_IDS)
6391 return LocalID;
6392
6393 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6394 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6395 assert(I != M.IdentifierRemap.end()
6396 && "Invalid index into identifier index remap");
6397
6398 return LocalID + I->second;
6399}
6400
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00006401MacroDirective *ASTReader::getMacro(MacroID ID, MacroDirective *Hint) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006402 if (ID == 0)
6403 return 0;
6404
6405 if (MacrosLoaded.empty()) {
6406 Error("no macro table in AST file");
6407 return 0;
6408 }
6409
6410 ID -= NUM_PREDEF_MACRO_IDS;
6411 if (!MacrosLoaded[ID]) {
6412 GlobalMacroMapType::iterator I
6413 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6414 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6415 ModuleFile *M = I->second;
6416 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00006417 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Guy Benyei11169dd2012-12-18 14:30:41 +00006418 }
6419
6420 return MacrosLoaded[ID];
6421}
6422
6423MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6424 if (LocalID < NUM_PREDEF_MACRO_IDS)
6425 return LocalID;
6426
6427 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6428 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6429 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6430
6431 return LocalID + I->second;
6432}
6433
6434serialization::SubmoduleID
6435ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6436 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6437 return LocalID;
6438
6439 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6440 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6441 assert(I != M.SubmoduleRemap.end()
6442 && "Invalid index into submodule index remap");
6443
6444 return LocalID + I->second;
6445}
6446
6447Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6448 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6449 assert(GlobalID == 0 && "Unhandled global submodule ID");
6450 return 0;
6451 }
6452
6453 if (GlobalID > SubmodulesLoaded.size()) {
6454 Error("submodule ID out of range in AST file");
6455 return 0;
6456 }
6457
6458 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6459}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00006460
6461Module *ASTReader::getModule(unsigned ID) {
6462 return getSubmodule(ID);
6463}
6464
Guy Benyei11169dd2012-12-18 14:30:41 +00006465Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
6466 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6467}
6468
6469Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
6470 if (ID == 0)
6471 return Selector();
6472
6473 if (ID > SelectorsLoaded.size()) {
6474 Error("selector ID out of range in AST file");
6475 return Selector();
6476 }
6477
6478 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
6479 // Load this selector from the selector table.
6480 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6481 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
6482 ModuleFile &M = *I->second;
6483 ASTSelectorLookupTrait Trait(*this, M);
6484 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
6485 SelectorsLoaded[ID - 1] =
6486 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
6487 if (DeserializationListener)
6488 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
6489 }
6490
6491 return SelectorsLoaded[ID - 1];
6492}
6493
6494Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
6495 return DecodeSelector(ID);
6496}
6497
6498uint32_t ASTReader::GetNumExternalSelectors() {
6499 // ID 0 (the null selector) is considered an external selector.
6500 return getTotalNumSelectors() + 1;
6501}
6502
6503serialization::SelectorID
6504ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
6505 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6506 return LocalID;
6507
6508 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6509 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6510 assert(I != M.SelectorRemap.end()
6511 && "Invalid index into selector index remap");
6512
6513 return LocalID + I->second;
6514}
6515
6516DeclarationName
6517ASTReader::ReadDeclarationName(ModuleFile &F,
6518 const RecordData &Record, unsigned &Idx) {
6519 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6520 switch (Kind) {
6521 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006522 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00006523
6524 case DeclarationName::ObjCZeroArgSelector:
6525 case DeclarationName::ObjCOneArgSelector:
6526 case DeclarationName::ObjCMultiArgSelector:
6527 return DeclarationName(ReadSelector(F, Record, Idx));
6528
6529 case DeclarationName::CXXConstructorName:
6530 return Context.DeclarationNames.getCXXConstructorName(
6531 Context.getCanonicalType(readType(F, Record, Idx)));
6532
6533 case DeclarationName::CXXDestructorName:
6534 return Context.DeclarationNames.getCXXDestructorName(
6535 Context.getCanonicalType(readType(F, Record, Idx)));
6536
6537 case DeclarationName::CXXConversionFunctionName:
6538 return Context.DeclarationNames.getCXXConversionFunctionName(
6539 Context.getCanonicalType(readType(F, Record, Idx)));
6540
6541 case DeclarationName::CXXOperatorName:
6542 return Context.DeclarationNames.getCXXOperatorName(
6543 (OverloadedOperatorKind)Record[Idx++]);
6544
6545 case DeclarationName::CXXLiteralOperatorName:
6546 return Context.DeclarationNames.getCXXLiteralOperatorName(
6547 GetIdentifierInfo(F, Record, Idx));
6548
6549 case DeclarationName::CXXUsingDirective:
6550 return DeclarationName::getUsingDirectiveName();
6551 }
6552
6553 llvm_unreachable("Invalid NameKind!");
6554}
6555
6556void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
6557 DeclarationNameLoc &DNLoc,
6558 DeclarationName Name,
6559 const RecordData &Record, unsigned &Idx) {
6560 switch (Name.getNameKind()) {
6561 case DeclarationName::CXXConstructorName:
6562 case DeclarationName::CXXDestructorName:
6563 case DeclarationName::CXXConversionFunctionName:
6564 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6565 break;
6566
6567 case DeclarationName::CXXOperatorName:
6568 DNLoc.CXXOperatorName.BeginOpNameLoc
6569 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6570 DNLoc.CXXOperatorName.EndOpNameLoc
6571 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6572 break;
6573
6574 case DeclarationName::CXXLiteralOperatorName:
6575 DNLoc.CXXLiteralOperatorName.OpNameLoc
6576 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6577 break;
6578
6579 case DeclarationName::Identifier:
6580 case DeclarationName::ObjCZeroArgSelector:
6581 case DeclarationName::ObjCOneArgSelector:
6582 case DeclarationName::ObjCMultiArgSelector:
6583 case DeclarationName::CXXUsingDirective:
6584 break;
6585 }
6586}
6587
6588void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
6589 DeclarationNameInfo &NameInfo,
6590 const RecordData &Record, unsigned &Idx) {
6591 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
6592 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6593 DeclarationNameLoc DNLoc;
6594 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6595 NameInfo.setInfo(DNLoc);
6596}
6597
6598void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
6599 const RecordData &Record, unsigned &Idx) {
6600 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
6601 unsigned NumTPLists = Record[Idx++];
6602 Info.NumTemplParamLists = NumTPLists;
6603 if (NumTPLists) {
6604 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
6605 for (unsigned i=0; i != NumTPLists; ++i)
6606 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6607 }
6608}
6609
6610TemplateName
6611ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
6612 unsigned &Idx) {
6613 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
6614 switch (Kind) {
6615 case TemplateName::Template:
6616 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
6617
6618 case TemplateName::OverloadedTemplate: {
6619 unsigned size = Record[Idx++];
6620 UnresolvedSet<8> Decls;
6621 while (size--)
6622 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
6623
6624 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
6625 }
6626
6627 case TemplateName::QualifiedTemplate: {
6628 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6629 bool hasTemplKeyword = Record[Idx++];
6630 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
6631 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
6632 }
6633
6634 case TemplateName::DependentTemplate: {
6635 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
6636 if (Record[Idx++]) // isIdentifier
6637 return Context.getDependentTemplateName(NNS,
6638 GetIdentifierInfo(F, Record,
6639 Idx));
6640 return Context.getDependentTemplateName(NNS,
6641 (OverloadedOperatorKind)Record[Idx++]);
6642 }
6643
6644 case TemplateName::SubstTemplateTemplateParm: {
6645 TemplateTemplateParmDecl *param
6646 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6647 if (!param) return TemplateName();
6648 TemplateName replacement = ReadTemplateName(F, Record, Idx);
6649 return Context.getSubstTemplateTemplateParm(param, replacement);
6650 }
6651
6652 case TemplateName::SubstTemplateTemplateParmPack: {
6653 TemplateTemplateParmDecl *Param
6654 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
6655 if (!Param)
6656 return TemplateName();
6657
6658 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6659 if (ArgPack.getKind() != TemplateArgument::Pack)
6660 return TemplateName();
6661
6662 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
6663 }
6664 }
6665
6666 llvm_unreachable("Unhandled template name kind!");
6667}
6668
6669TemplateArgument
6670ASTReader::ReadTemplateArgument(ModuleFile &F,
6671 const RecordData &Record, unsigned &Idx) {
6672 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6673 switch (Kind) {
6674 case TemplateArgument::Null:
6675 return TemplateArgument();
6676 case TemplateArgument::Type:
6677 return TemplateArgument(readType(F, Record, Idx));
6678 case TemplateArgument::Declaration: {
6679 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6680 bool ForReferenceParam = Record[Idx++];
6681 return TemplateArgument(D, ForReferenceParam);
6682 }
6683 case TemplateArgument::NullPtr:
6684 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
6685 case TemplateArgument::Integral: {
6686 llvm::APSInt Value = ReadAPSInt(Record, Idx);
6687 QualType T = readType(F, Record, Idx);
6688 return TemplateArgument(Context, Value, T);
6689 }
6690 case TemplateArgument::Template:
6691 return TemplateArgument(ReadTemplateName(F, Record, Idx));
6692 case TemplateArgument::TemplateExpansion: {
6693 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00006694 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00006695 if (unsigned NumExpansions = Record[Idx++])
6696 NumTemplateExpansions = NumExpansions - 1;
6697 return TemplateArgument(Name, NumTemplateExpansions);
6698 }
6699 case TemplateArgument::Expression:
6700 return TemplateArgument(ReadExpr(F));
6701 case TemplateArgument::Pack: {
6702 unsigned NumArgs = Record[Idx++];
6703 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
6704 for (unsigned I = 0; I != NumArgs; ++I)
6705 Args[I] = ReadTemplateArgument(F, Record, Idx);
6706 return TemplateArgument(Args, NumArgs);
6707 }
6708 }
6709
6710 llvm_unreachable("Unhandled template argument kind!");
6711}
6712
6713TemplateParameterList *
6714ASTReader::ReadTemplateParameterList(ModuleFile &F,
6715 const RecordData &Record, unsigned &Idx) {
6716 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6717 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6718 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
6719
6720 unsigned NumParams = Record[Idx++];
6721 SmallVector<NamedDecl *, 16> Params;
6722 Params.reserve(NumParams);
6723 while (NumParams--)
6724 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
6725
6726 TemplateParameterList* TemplateParams =
6727 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
6728 Params.data(), Params.size(), RAngleLoc);
6729 return TemplateParams;
6730}
6731
6732void
6733ASTReader::
6734ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
6735 ModuleFile &F, const RecordData &Record,
6736 unsigned &Idx) {
6737 unsigned NumTemplateArgs = Record[Idx++];
6738 TemplArgs.reserve(NumTemplateArgs);
6739 while (NumTemplateArgs--)
6740 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
6741}
6742
6743/// \brief Read a UnresolvedSet structure.
6744void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
6745 const RecordData &Record, unsigned &Idx) {
6746 unsigned NumDecls = Record[Idx++];
6747 Set.reserve(Context, NumDecls);
6748 while (NumDecls--) {
6749 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
6750 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6751 Set.addDecl(Context, D, AS);
6752 }
6753}
6754
6755CXXBaseSpecifier
6756ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
6757 const RecordData &Record, unsigned &Idx) {
6758 bool isVirtual = static_cast<bool>(Record[Idx++]);
6759 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6760 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
6761 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
6762 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6763 SourceRange Range = ReadSourceRange(F, Record, Idx);
6764 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
6765 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
6766 EllipsisLoc);
6767 Result.setInheritConstructors(inheritConstructors);
6768 return Result;
6769}
6770
6771std::pair<CXXCtorInitializer **, unsigned>
6772ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
6773 unsigned &Idx) {
6774 CXXCtorInitializer **CtorInitializers = 0;
6775 unsigned NumInitializers = Record[Idx++];
6776 if (NumInitializers) {
6777 CtorInitializers
6778 = new (Context) CXXCtorInitializer*[NumInitializers];
6779 for (unsigned i=0; i != NumInitializers; ++i) {
6780 TypeSourceInfo *TInfo = 0;
6781 bool IsBaseVirtual = false;
6782 FieldDecl *Member = 0;
6783 IndirectFieldDecl *IndirectMember = 0;
6784
6785 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6786 switch (Type) {
6787 case CTOR_INITIALIZER_BASE:
6788 TInfo = GetTypeSourceInfo(F, Record, Idx);
6789 IsBaseVirtual = Record[Idx++];
6790 break;
6791
6792 case CTOR_INITIALIZER_DELEGATING:
6793 TInfo = GetTypeSourceInfo(F, Record, Idx);
6794 break;
6795
6796 case CTOR_INITIALIZER_MEMBER:
6797 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
6798 break;
6799
6800 case CTOR_INITIALIZER_INDIRECT_MEMBER:
6801 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
6802 break;
6803 }
6804
6805 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
6806 Expr *Init = ReadExpr(F);
6807 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6808 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
6809 bool IsWritten = Record[Idx++];
6810 unsigned SourceOrderOrNumArrayIndices;
6811 SmallVector<VarDecl *, 8> Indices;
6812 if (IsWritten) {
6813 SourceOrderOrNumArrayIndices = Record[Idx++];
6814 } else {
6815 SourceOrderOrNumArrayIndices = Record[Idx++];
6816 Indices.reserve(SourceOrderOrNumArrayIndices);
6817 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
6818 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
6819 }
6820
6821 CXXCtorInitializer *BOMInit;
6822 if (Type == CTOR_INITIALIZER_BASE) {
6823 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
6824 LParenLoc, Init, RParenLoc,
6825 MemberOrEllipsisLoc);
6826 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
6827 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6828 Init, RParenLoc);
6829 } else if (IsWritten) {
6830 if (Member)
6831 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
6832 LParenLoc, Init, RParenLoc);
6833 else
6834 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
6835 MemberOrEllipsisLoc, LParenLoc,
6836 Init, RParenLoc);
6837 } else {
6838 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
6839 LParenLoc, Init, RParenLoc,
6840 Indices.data(), Indices.size());
6841 }
6842
6843 if (IsWritten)
6844 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
6845 CtorInitializers[i] = BOMInit;
6846 }
6847 }
6848
6849 return std::make_pair(CtorInitializers, NumInitializers);
6850}
6851
6852NestedNameSpecifier *
6853ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
6854 const RecordData &Record, unsigned &Idx) {
6855 unsigned N = Record[Idx++];
6856 NestedNameSpecifier *NNS = 0, *Prev = 0;
6857 for (unsigned I = 0; I != N; ++I) {
6858 NestedNameSpecifier::SpecifierKind Kind
6859 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6860 switch (Kind) {
6861 case NestedNameSpecifier::Identifier: {
6862 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6863 NNS = NestedNameSpecifier::Create(Context, Prev, II);
6864 break;
6865 }
6866
6867 case NestedNameSpecifier::Namespace: {
6868 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6869 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
6870 break;
6871 }
6872
6873 case NestedNameSpecifier::NamespaceAlias: {
6874 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6875 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
6876 break;
6877 }
6878
6879 case NestedNameSpecifier::TypeSpec:
6880 case NestedNameSpecifier::TypeSpecWithTemplate: {
6881 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
6882 if (!T)
6883 return 0;
6884
6885 bool Template = Record[Idx++];
6886 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
6887 break;
6888 }
6889
6890 case NestedNameSpecifier::Global: {
6891 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
6892 // No associated value, and there can't be a prefix.
6893 break;
6894 }
6895 }
6896 Prev = NNS;
6897 }
6898 return NNS;
6899}
6900
6901NestedNameSpecifierLoc
6902ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
6903 unsigned &Idx) {
6904 unsigned N = Record[Idx++];
6905 NestedNameSpecifierLocBuilder Builder;
6906 for (unsigned I = 0; I != N; ++I) {
6907 NestedNameSpecifier::SpecifierKind Kind
6908 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6909 switch (Kind) {
6910 case NestedNameSpecifier::Identifier: {
6911 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
6912 SourceRange Range = ReadSourceRange(F, Record, Idx);
6913 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
6914 break;
6915 }
6916
6917 case NestedNameSpecifier::Namespace: {
6918 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
6919 SourceRange Range = ReadSourceRange(F, Record, Idx);
6920 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
6921 break;
6922 }
6923
6924 case NestedNameSpecifier::NamespaceAlias: {
6925 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
6926 SourceRange Range = ReadSourceRange(F, Record, Idx);
6927 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
6928 break;
6929 }
6930
6931 case NestedNameSpecifier::TypeSpec:
6932 case NestedNameSpecifier::TypeSpecWithTemplate: {
6933 bool Template = Record[Idx++];
6934 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6935 if (!T)
6936 return NestedNameSpecifierLoc();
6937 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6938
6939 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
6940 Builder.Extend(Context,
6941 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6942 T->getTypeLoc(), ColonColonLoc);
6943 break;
6944 }
6945
6946 case NestedNameSpecifier::Global: {
6947 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
6948 Builder.MakeGlobal(Context, ColonColonLoc);
6949 break;
6950 }
6951 }
6952 }
6953
6954 return Builder.getWithLocInContext(Context);
6955}
6956
6957SourceRange
6958ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
6959 unsigned &Idx) {
6960 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6961 SourceLocation end = ReadSourceLocation(F, Record, Idx);
6962 return SourceRange(beg, end);
6963}
6964
6965/// \brief Read an integral value
6966llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
6967 unsigned BitWidth = Record[Idx++];
6968 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6969 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6970 Idx += NumWords;
6971 return Result;
6972}
6973
6974/// \brief Read a signed integral value
6975llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
6976 bool isUnsigned = Record[Idx++];
6977 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6978}
6979
6980/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00006981llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
6982 const llvm::fltSemantics &Sem,
6983 unsigned &Idx) {
6984 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00006985}
6986
6987// \brief Read a string
6988std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
6989 unsigned Len = Record[Idx++];
6990 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
6991 Idx += Len;
6992 return Result;
6993}
6994
6995VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6996 unsigned &Idx) {
6997 unsigned Major = Record[Idx++];
6998 unsigned Minor = Record[Idx++];
6999 unsigned Subminor = Record[Idx++];
7000 if (Minor == 0)
7001 return VersionTuple(Major);
7002 if (Subminor == 0)
7003 return VersionTuple(Major, Minor - 1);
7004 return VersionTuple(Major, Minor - 1, Subminor - 1);
7005}
7006
7007CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7008 const RecordData &Record,
7009 unsigned &Idx) {
7010 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7011 return CXXTemporary::Create(Context, Decl);
7012}
7013
7014DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
7015 return Diag(SourceLocation(), DiagID);
7016}
7017
7018DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7019 return Diags.Report(Loc, DiagID);
7020}
7021
7022/// \brief Retrieve the identifier table associated with the
7023/// preprocessor.
7024IdentifierTable &ASTReader::getIdentifierTable() {
7025 return PP.getIdentifierTable();
7026}
7027
7028/// \brief Record that the given ID maps to the given switch-case
7029/// statement.
7030void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7031 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7032 "Already have a SwitchCase with this ID");
7033 (*CurrSwitchCaseStmts)[ID] = SC;
7034}
7035
7036/// \brief Retrieve the switch-case statement with the given ID.
7037SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7038 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7039 return (*CurrSwitchCaseStmts)[ID];
7040}
7041
7042void ASTReader::ClearSwitchCaseIDs() {
7043 CurrSwitchCaseStmts->clear();
7044}
7045
7046void ASTReader::ReadComments() {
7047 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007048 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00007049 serialization::ModuleFile *> >::iterator
7050 I = CommentsCursors.begin(),
7051 E = CommentsCursors.end();
7052 I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007053 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00007054 serialization::ModuleFile &F = *I->second;
7055 SavedStreamPosition SavedPosition(Cursor);
7056
7057 RecordData Record;
7058 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007059 llvm::BitstreamEntry Entry =
7060 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7061
7062 switch (Entry.Kind) {
7063 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7064 case llvm::BitstreamEntry::Error:
7065 Error("malformed block record in AST file");
7066 return;
7067 case llvm::BitstreamEntry::EndBlock:
7068 goto NextCursor;
7069 case llvm::BitstreamEntry::Record:
7070 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00007071 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007072 }
7073
7074 // Read a record.
7075 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007076 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007077 case COMMENTS_RAW_COMMENT: {
7078 unsigned Idx = 0;
7079 SourceRange SR = ReadSourceRange(F, Record, Idx);
7080 RawComment::CommentKind Kind =
7081 (RawComment::CommentKind) Record[Idx++];
7082 bool IsTrailingComment = Record[Idx++];
7083 bool IsAlmostTrailingComment = Record[Idx++];
7084 Comments.push_back(new (Context) RawComment(SR, Kind,
7085 IsTrailingComment,
7086 IsAlmostTrailingComment));
7087 break;
7088 }
7089 }
7090 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007091 NextCursor:;
Guy Benyei11169dd2012-12-18 14:30:41 +00007092 }
7093 Context.Comments.addCommentsToFront(Comments);
7094}
7095
7096void ASTReader::finishPendingActions() {
7097 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00007098 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007099 // If any identifiers with corresponding top-level declarations have
7100 // been loaded, load those declarations now.
Douglas Gregor6168bd22013-02-18 15:53:43 +00007101 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> > TopLevelDecls;
Guy Benyei11169dd2012-12-18 14:30:41 +00007102 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007103 // FIXME: std::move
7104 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7105 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcb15f082013-02-19 18:26:28 +00007106 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00007107
7108 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007109 }
7110
7111 // Load pending declaration chains.
7112 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7113 loadPendingDeclChain(PendingDeclChains[I]);
7114 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7115 }
7116 PendingDeclChains.clear();
7117
Douglas Gregor6168bd22013-02-18 15:53:43 +00007118 // Make the most recent of the top-level declarations visible.
7119 for (llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >::iterator
7120 TLD = TopLevelDecls.begin(), TLDEnd = TopLevelDecls.end();
7121 TLD != TLDEnd; ++TLD) {
7122 IdentifierInfo *II = TLD->first;
7123 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
7124 NamedDecl *ND = cast<NamedDecl>(TLD->second[I]->getMostRecentDecl());
7125 SemaObj->pushExternalDeclIntoScope(ND, II);
7126 }
7127 }
7128
Guy Benyei11169dd2012-12-18 14:30:41 +00007129 // Load any pending macro definitions.
7130 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00007131 // FIXME: std::move here
7132 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00007133 MacroDirective *Hint = 0;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00007134 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
7135 ++IDIdx) {
7136 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Guy Benyei11169dd2012-12-18 14:30:41 +00007137 }
7138 }
7139 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00007140
7141 // Wire up the DeclContexts for Decls that we delayed setting until
7142 // recursive loading is completed.
7143 while (!PendingDeclContextInfos.empty()) {
7144 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7145 PendingDeclContextInfos.pop_front();
7146 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7147 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7148 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7149 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007150 }
7151
7152 // If we deserialized any C++ or Objective-C class definitions, any
7153 // Objective-C protocol definitions, or any redeclarable templates, make sure
7154 // that all redeclarations point to the definitions. Note that this can only
7155 // happen now, after the redeclaration chains have been fully wired.
7156 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7157 DEnd = PendingDefinitions.end();
7158 D != DEnd; ++D) {
7159 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7160 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7161 // Make sure that the TagType points at the definition.
7162 const_cast<TagType*>(TagT)->decl = TD;
7163 }
7164
7165 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
7166 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
7167 REnd = RD->redecls_end();
7168 R != REnd; ++R)
7169 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
7170
7171 }
7172
7173 continue;
7174 }
7175
7176 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
7177 // Make sure that the ObjCInterfaceType points at the definition.
7178 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7179 ->Decl = ID;
7180
7181 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
7182 REnd = ID->redecls_end();
7183 R != REnd; ++R)
7184 R->Data = ID->Data;
7185
7186 continue;
7187 }
7188
7189 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7190 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
7191 REnd = PD->redecls_end();
7192 R != REnd; ++R)
7193 R->Data = PD->Data;
7194
7195 continue;
7196 }
7197
7198 RedeclarableTemplateDecl *RTD
7199 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7200 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
7201 REnd = RTD->redecls_end();
7202 R != REnd; ++R)
7203 R->Common = RTD->Common;
7204 }
7205 PendingDefinitions.clear();
7206
7207 // Load the bodies of any functions or methods we've encountered. We do
7208 // this now (delayed) so that we can be sure that the declaration chains
7209 // have been fully wired up.
7210 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7211 PBEnd = PendingBodies.end();
7212 PB != PBEnd; ++PB) {
7213 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7214 // FIXME: Check for =delete/=default?
7215 // FIXME: Complain about ODR violations here?
7216 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7217 FD->setLazyBody(PB->second);
7218 continue;
7219 }
7220
7221 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7222 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7223 MD->setLazyBody(PB->second);
7224 }
7225 PendingBodies.clear();
7226}
7227
7228void ASTReader::FinishedDeserializing() {
7229 assert(NumCurrentElementsDeserializing &&
7230 "FinishedDeserializing not paired with StartedDeserializing");
7231 if (NumCurrentElementsDeserializing == 1) {
7232 // We decrease NumCurrentElementsDeserializing only after pending actions
7233 // are finished, to avoid recursively re-calling finishPendingActions().
7234 finishPendingActions();
7235 }
7236 --NumCurrentElementsDeserializing;
7237
7238 if (NumCurrentElementsDeserializing == 0 &&
7239 Consumer && !PassingDeclsToConsumer) {
7240 // Guard variable to avoid recursively redoing the process of passing
7241 // decls to consumer.
7242 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7243 true);
7244
7245 while (!InterestingDecls.empty()) {
7246 // We are not in recursive loading, so it's safe to pass the "interesting"
7247 // decls to the consumer.
7248 Decl *D = InterestingDecls.front();
7249 InterestingDecls.pop_front();
7250 PassInterestingDeclToConsumer(D);
7251 }
7252 }
7253}
7254
7255ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7256 StringRef isysroot, bool DisableValidation,
Douglas Gregorc1bbec82013-01-25 00:45:27 +00007257 bool AllowASTWithCompilerErrors, bool UseGlobalIndex)
Guy Benyei11169dd2012-12-18 14:30:41 +00007258 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7259 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7260 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7261 Consumer(0), ModuleMgr(PP.getFileManager()),
7262 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007263 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Douglas Gregorc1bbec82013-01-25 00:45:27 +00007264 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei11169dd2012-12-18 14:30:41 +00007265 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7266 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007267 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7268 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7269 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007270 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7271 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7272 TotalNumMethodPoolEntries(0),
Guy Benyei11169dd2012-12-18 14:30:41 +00007273 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7274 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7275 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7276 PassingDeclsToConsumer(false),
7277 NumCXXBaseSpecifiersLoaded(0)
7278{
7279 SourceMgr.setExternalSLocEntrySource(this);
7280}
7281
7282ASTReader::~ASTReader() {
7283 for (DeclContextVisibleUpdatesPending::iterator
7284 I = PendingVisibleUpdates.begin(),
7285 E = PendingVisibleUpdates.end();
7286 I != E; ++I) {
7287 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7288 F = I->second.end();
7289 J != F; ++J)
7290 delete J->first;
7291 }
7292}