blob: 595edfc4c5b84ad5a4e41aaf54db15f15acb1670 [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"
Dmitri Gribenkof430da42014-02-12 10:33:14 +000052#include "llvm/Support/raw_ostream.h"
Guy Benyei11169dd2012-12-18 14:30:41 +000053#include "llvm/Support/system_error.h"
54#include <algorithm>
Chris Lattner91f373e2013-01-20 00:57:52 +000055#include <cstdio>
Guy Benyei11169dd2012-12-18 14:30:41 +000056#include <iterator>
57
58using namespace clang;
59using namespace clang::serialization;
60using namespace clang::serialization::reader;
Chris Lattner7fb3bef2013-01-20 00:56:42 +000061using llvm::BitstreamCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +000062
Ben Langmuircb69b572014-03-07 06:40:32 +000063
64//===----------------------------------------------------------------------===//
65// ChainedASTReaderListener implementation
66//===----------------------------------------------------------------------===//
67
68bool
69ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
70 return First->ReadFullVersionInformation(FullVersion) ||
71 Second->ReadFullVersionInformation(FullVersion);
72}
73bool ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
74 bool Complain) {
75 return First->ReadLanguageOptions(LangOpts, Complain) ||
76 Second->ReadLanguageOptions(LangOpts, Complain);
77}
78bool
79ChainedASTReaderListener::ReadTargetOptions(const TargetOptions &TargetOpts,
80 bool Complain) {
81 return First->ReadTargetOptions(TargetOpts, Complain) ||
82 Second->ReadTargetOptions(TargetOpts, Complain);
83}
84bool ChainedASTReaderListener::ReadDiagnosticOptions(
85 const DiagnosticOptions &DiagOpts, bool Complain) {
86 return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
87 Second->ReadDiagnosticOptions(DiagOpts, Complain);
88}
89bool
90ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
91 bool Complain) {
92 return First->ReadFileSystemOptions(FSOpts, Complain) ||
93 Second->ReadFileSystemOptions(FSOpts, Complain);
94}
95
96bool ChainedASTReaderListener::ReadHeaderSearchOptions(
97 const HeaderSearchOptions &HSOpts, bool Complain) {
98 return First->ReadHeaderSearchOptions(HSOpts, Complain) ||
99 Second->ReadHeaderSearchOptions(HSOpts, Complain);
100}
101bool ChainedASTReaderListener::ReadPreprocessorOptions(
102 const PreprocessorOptions &PPOpts, bool Complain,
103 std::string &SuggestedPredefines) {
104 return First->ReadPreprocessorOptions(PPOpts, Complain,
105 SuggestedPredefines) ||
106 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
107}
108void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
109 unsigned Value) {
110 First->ReadCounter(M, Value);
111 Second->ReadCounter(M, Value);
112}
113bool ChainedASTReaderListener::needsInputFileVisitation() {
114 return First->needsInputFileVisitation() ||
115 Second->needsInputFileVisitation();
116}
117bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
118 return First->needsSystemInputFileVisitation() ||
119 Second->needsSystemInputFileVisitation();
120}
121bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
122 bool isSystem) {
123 return First->visitInputFile(Filename, isSystem) ||
124 Second->visitInputFile(Filename, isSystem);
125}
126
Guy Benyei11169dd2012-12-18 14:30:41 +0000127//===----------------------------------------------------------------------===//
128// PCH validator implementation
129//===----------------------------------------------------------------------===//
130
131ASTReaderListener::~ASTReaderListener() {}
132
133/// \brief Compare the given set of language options against an existing set of
134/// language options.
135///
136/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
137///
138/// \returns true if the languagae options mis-match, false otherwise.
139static bool checkLanguageOptions(const LangOptions &LangOpts,
140 const LangOptions &ExistingLangOpts,
141 DiagnosticsEngine *Diags) {
142#define LANGOPT(Name, Bits, Default, Description) \
143 if (ExistingLangOpts.Name != LangOpts.Name) { \
144 if (Diags) \
145 Diags->Report(diag::err_pch_langopt_mismatch) \
146 << Description << LangOpts.Name << ExistingLangOpts.Name; \
147 return true; \
148 }
149
150#define VALUE_LANGOPT(Name, Bits, Default, Description) \
151 if (ExistingLangOpts.Name != LangOpts.Name) { \
152 if (Diags) \
153 Diags->Report(diag::err_pch_langopt_value_mismatch) \
154 << Description; \
155 return true; \
156 }
157
158#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
159 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
160 if (Diags) \
161 Diags->Report(diag::err_pch_langopt_value_mismatch) \
162 << Description; \
163 return true; \
164 }
165
166#define BENIGN_LANGOPT(Name, Bits, Default, Description)
167#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
168#include "clang/Basic/LangOptions.def"
169
170 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
171 if (Diags)
172 Diags->Report(diag::err_pch_langopt_value_mismatch)
173 << "target Objective-C runtime";
174 return true;
175 }
176
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000177 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
178 LangOpts.CommentOpts.BlockCommandNames) {
179 if (Diags)
180 Diags->Report(diag::err_pch_langopt_value_mismatch)
181 << "block command names";
182 return true;
183 }
184
Guy Benyei11169dd2012-12-18 14:30:41 +0000185 return false;
186}
187
188/// \brief Compare the given set of target options against an existing set of
189/// target options.
190///
191/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
192///
193/// \returns true if the target options mis-match, false otherwise.
194static bool checkTargetOptions(const TargetOptions &TargetOpts,
195 const TargetOptions &ExistingTargetOpts,
196 DiagnosticsEngine *Diags) {
197#define CHECK_TARGET_OPT(Field, Name) \
198 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
199 if (Diags) \
200 Diags->Report(diag::err_pch_targetopt_mismatch) \
201 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
202 return true; \
203 }
204
205 CHECK_TARGET_OPT(Triple, "target");
206 CHECK_TARGET_OPT(CPU, "target CPU");
207 CHECK_TARGET_OPT(ABI, "target ABI");
Guy Benyei11169dd2012-12-18 14:30:41 +0000208 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
209#undef CHECK_TARGET_OPT
210
211 // Compare feature sets.
212 SmallVector<StringRef, 4> ExistingFeatures(
213 ExistingTargetOpts.FeaturesAsWritten.begin(),
214 ExistingTargetOpts.FeaturesAsWritten.end());
215 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
216 TargetOpts.FeaturesAsWritten.end());
217 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
218 std::sort(ReadFeatures.begin(), ReadFeatures.end());
219
220 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
221 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
222 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
223 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
224 ++ExistingIdx;
225 ++ReadIdx;
226 continue;
227 }
228
229 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
230 if (Diags)
231 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
232 << false << ReadFeatures[ReadIdx];
233 return true;
234 }
235
236 if (Diags)
237 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
238 << true << ExistingFeatures[ExistingIdx];
239 return true;
240 }
241
242 if (ExistingIdx < ExistingN) {
243 if (Diags)
244 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
245 << true << ExistingFeatures[ExistingIdx];
246 return true;
247 }
248
249 if (ReadIdx < ReadN) {
250 if (Diags)
251 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
252 << false << ReadFeatures[ReadIdx];
253 return true;
254 }
255
256 return false;
257}
258
259bool
260PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
261 bool Complain) {
262 const LangOptions &ExistingLangOpts = PP.getLangOpts();
263 return checkLanguageOptions(LangOpts, ExistingLangOpts,
264 Complain? &Reader.Diags : 0);
265}
266
267bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
268 bool Complain) {
269 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
270 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
271 Complain? &Reader.Diags : 0);
272}
273
274namespace {
275 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
276 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000277 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
278 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000279}
280
281/// \brief Collect the macro definitions provided by the given preprocessor
282/// options.
283static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
284 MacroDefinitionsMap &Macros,
285 SmallVectorImpl<StringRef> *MacroNames = 0){
286 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
287 StringRef Macro = PPOpts.Macros[I].first;
288 bool IsUndef = PPOpts.Macros[I].second;
289
290 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
291 StringRef MacroName = MacroPair.first;
292 StringRef MacroBody = MacroPair.second;
293
294 // For an #undef'd macro, we only care about the name.
295 if (IsUndef) {
296 if (MacroNames && !Macros.count(MacroName))
297 MacroNames->push_back(MacroName);
298
299 Macros[MacroName] = std::make_pair("", true);
300 continue;
301 }
302
303 // For a #define'd macro, figure out the actual definition.
304 if (MacroName.size() == Macro.size())
305 MacroBody = "1";
306 else {
307 // Note: GCC drops anything following an end-of-line character.
308 StringRef::size_type End = MacroBody.find_first_of("\n\r");
309 MacroBody = MacroBody.substr(0, End);
310 }
311
312 if (MacroNames && !Macros.count(MacroName))
313 MacroNames->push_back(MacroName);
314 Macros[MacroName] = std::make_pair(MacroBody, false);
315 }
316}
317
318/// \brief Check the preprocessor options deserialized from the control block
319/// against the preprocessor options in an existing preprocessor.
320///
321/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
322static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
323 const PreprocessorOptions &ExistingPPOpts,
324 DiagnosticsEngine *Diags,
325 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000326 std::string &SuggestedPredefines,
327 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000328 // Check macro definitions.
329 MacroDefinitionsMap ASTFileMacros;
330 collectMacroDefinitions(PPOpts, ASTFileMacros);
331 MacroDefinitionsMap ExistingMacros;
332 SmallVector<StringRef, 4> ExistingMacroNames;
333 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
334
335 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
336 // Dig out the macro definition in the existing preprocessor options.
337 StringRef MacroName = ExistingMacroNames[I];
338 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
339
340 // Check whether we know anything about this macro name or not.
341 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
342 = ASTFileMacros.find(MacroName);
343 if (Known == ASTFileMacros.end()) {
344 // FIXME: Check whether this identifier was referenced anywhere in the
345 // AST file. If so, we should reject the AST file. Unfortunately, this
346 // information isn't in the control block. What shall we do about it?
347
348 if (Existing.second) {
349 SuggestedPredefines += "#undef ";
350 SuggestedPredefines += MacroName.str();
351 SuggestedPredefines += '\n';
352 } else {
353 SuggestedPredefines += "#define ";
354 SuggestedPredefines += MacroName.str();
355 SuggestedPredefines += ' ';
356 SuggestedPredefines += Existing.first.str();
357 SuggestedPredefines += '\n';
358 }
359 continue;
360 }
361
362 // If the macro was defined in one but undef'd in the other, we have a
363 // conflict.
364 if (Existing.second != Known->second.second) {
365 if (Diags) {
366 Diags->Report(diag::err_pch_macro_def_undef)
367 << MacroName << Known->second.second;
368 }
369 return true;
370 }
371
372 // If the macro was #undef'd in both, or if the macro bodies are identical,
373 // it's fine.
374 if (Existing.second || Existing.first == Known->second.first)
375 continue;
376
377 // The macro bodies differ; complain.
378 if (Diags) {
379 Diags->Report(diag::err_pch_macro_def_conflict)
380 << MacroName << Known->second.first << Existing.first;
381 }
382 return true;
383 }
384
385 // Check whether we're using predefines.
386 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
387 if (Diags) {
388 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
389 }
390 return true;
391 }
392
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000393 // Detailed record is important since it is used for the module cache hash.
394 if (LangOpts.Modules &&
395 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
396 if (Diags) {
397 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
398 }
399 return true;
400 }
401
Guy Benyei11169dd2012-12-18 14:30:41 +0000402 // Compute the #include and #include_macros lines we need.
403 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
404 StringRef File = ExistingPPOpts.Includes[I];
405 if (File == ExistingPPOpts.ImplicitPCHInclude)
406 continue;
407
408 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
409 != PPOpts.Includes.end())
410 continue;
411
412 SuggestedPredefines += "#include \"";
413 SuggestedPredefines +=
414 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
415 SuggestedPredefines += "\"\n";
416 }
417
418 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
419 StringRef File = ExistingPPOpts.MacroIncludes[I];
420 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
421 File)
422 != PPOpts.MacroIncludes.end())
423 continue;
424
425 SuggestedPredefines += "#__include_macros \"";
426 SuggestedPredefines +=
427 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
428 SuggestedPredefines += "\"\n##\n";
429 }
430
431 return false;
432}
433
434bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
435 bool Complain,
436 std::string &SuggestedPredefines) {
437 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
438
439 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
440 Complain? &Reader.Diags : 0,
441 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000442 SuggestedPredefines,
443 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000444}
445
Guy Benyei11169dd2012-12-18 14:30:41 +0000446void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
447 PP.setCounterValue(Value);
448}
449
450//===----------------------------------------------------------------------===//
451// AST reader implementation
452//===----------------------------------------------------------------------===//
453
454void
455ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
456 DeserializationListener = Listener;
457}
458
459
460
461unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
462 return serialization::ComputeHash(Sel);
463}
464
465
466std::pair<unsigned, unsigned>
467ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
468 using namespace clang::io;
469 unsigned KeyLen = ReadUnalignedLE16(d);
470 unsigned DataLen = ReadUnalignedLE16(d);
471 return std::make_pair(KeyLen, DataLen);
472}
473
474ASTSelectorLookupTrait::internal_key_type
475ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
476 using namespace clang::io;
477 SelectorTable &SelTable = Reader.getContext().Selectors;
478 unsigned N = ReadUnalignedLE16(d);
479 IdentifierInfo *FirstII
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000480 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000481 if (N == 0)
482 return SelTable.getNullarySelector(FirstII);
483 else if (N == 1)
484 return SelTable.getUnarySelector(FirstII);
485
486 SmallVector<IdentifierInfo *, 16> Args;
487 Args.push_back(FirstII);
488 for (unsigned I = 1; I != N; ++I)
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000489 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000490
491 return SelTable.getSelector(N, Args.data());
492}
493
494ASTSelectorLookupTrait::data_type
495ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
496 unsigned DataLen) {
497 using namespace clang::io;
498
499 data_type Result;
500
501 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000502 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
503 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
504 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
505 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
506 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
507 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei11169dd2012-12-18 14:30:41 +0000508
509 // Load instance methods
510 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
511 if (ObjCMethodDecl *Method
512 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
513 Result.Instance.push_back(Method);
514 }
515
516 // Load factory methods
517 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
518 if (ObjCMethodDecl *Method
519 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
520 Result.Factory.push_back(Method);
521 }
522
523 return Result;
524}
525
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000526unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
527 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000528}
529
530std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000531ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000532 using namespace clang::io;
533 unsigned DataLen = ReadUnalignedLE16(d);
534 unsigned KeyLen = ReadUnalignedLE16(d);
535 return std::make_pair(KeyLen, DataLen);
536}
537
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000538ASTIdentifierLookupTraitBase::internal_key_type
539ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000540 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000541 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000542}
543
Douglas Gregordcf25082013-02-11 18:16:18 +0000544/// \brief Whether the given identifier is "interesting".
545static bool isInterestingIdentifier(IdentifierInfo &II) {
546 return II.isPoisoned() ||
547 II.isExtensionToken() ||
548 II.getObjCOrBuiltinID() ||
549 II.hasRevertedTokenIDToIdentifier() ||
550 II.hadMacroDefinition() ||
551 II.getFETokenInfo<void>();
552}
553
Guy Benyei11169dd2012-12-18 14:30:41 +0000554IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
555 const unsigned char* d,
556 unsigned DataLen) {
557 using namespace clang::io;
558 unsigned RawID = ReadUnalignedLE32(d);
559 bool IsInteresting = RawID & 0x01;
560
561 // Wipe out the "is interesting" bit.
562 RawID = RawID >> 1;
563
564 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
565 if (!IsInteresting) {
566 // For uninteresting identifiers, just build the IdentifierInfo
567 // and associate it with the persistent ID.
568 IdentifierInfo *II = KnownII;
569 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000570 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000571 KnownII = II;
572 }
573 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000574 if (!II->isFromAST()) {
575 bool WasInteresting = isInterestingIdentifier(*II);
576 II->setIsFromAST();
577 if (WasInteresting)
578 II->setChangedSinceDeserialization();
579 }
580 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000581 return II;
582 }
583
584 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
585 unsigned Bits = ReadUnalignedLE16(d);
586 bool CPlusPlusOperatorKeyword = Bits & 0x01;
587 Bits >>= 1;
588 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
589 Bits >>= 1;
590 bool Poisoned = Bits & 0x01;
591 Bits >>= 1;
592 bool ExtensionToken = Bits & 0x01;
593 Bits >>= 1;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000594 bool hasSubmoduleMacros = Bits & 0x01;
595 Bits >>= 1;
Guy Benyei11169dd2012-12-18 14:30:41 +0000596 bool hadMacroDefinition = Bits & 0x01;
597 Bits >>= 1;
598
599 assert(Bits == 0 && "Extra bits in the identifier?");
600 DataLen -= 8;
601
602 // Build the IdentifierInfo itself and link the identifier ID with
603 // the new IdentifierInfo.
604 IdentifierInfo *II = KnownII;
605 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000606 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000607 KnownII = II;
608 }
609 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000610 if (!II->isFromAST()) {
611 bool WasInteresting = isInterestingIdentifier(*II);
612 II->setIsFromAST();
613 if (WasInteresting)
614 II->setChangedSinceDeserialization();
615 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000616
617 // Set or check the various bits in the IdentifierInfo structure.
618 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000619 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000620 II->RevertTokenIDToIdentifier();
621 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
622 assert(II->isExtensionToken() == ExtensionToken &&
623 "Incorrect extension token flag");
624 (void)ExtensionToken;
625 if (Poisoned)
626 II->setIsPoisoned(true);
627 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
628 "Incorrect C++ operator keyword flag");
629 (void)CPlusPlusOperatorKeyword;
630
631 // If this identifier is a macro, deserialize the macro
632 // definition.
633 if (hadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000634 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
635 DataLen -= 4;
636 SmallVector<uint32_t, 8> LocalMacroIDs;
637 if (hasSubmoduleMacros) {
638 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
639 DataLen -= 4;
640 LocalMacroIDs.push_back(LocalMacroID);
641 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000642 DataLen -= 4;
643 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000644
645 if (F.Kind == MK_Module) {
Richard Smith49f906a2014-03-01 00:08:04 +0000646 // Macro definitions are stored from newest to oldest, so reverse them
647 // before registering them.
648 llvm::SmallVector<unsigned, 8> MacroSizes;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000649 for (SmallVectorImpl<uint32_t>::iterator
Richard Smith49f906a2014-03-01 00:08:04 +0000650 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
651 unsigned Size = 1;
652
653 static const uint32_t HasOverridesFlag = 0x80000000U;
654 if (I + 1 != E && (I[1] & HasOverridesFlag))
655 Size += 1 + (I[1] & ~HasOverridesFlag);
656
657 MacroSizes.push_back(Size);
658 I += Size;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000659 }
Richard Smith49f906a2014-03-01 00:08:04 +0000660
661 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
662 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
663 SE = MacroSizes.rend();
664 SI != SE; ++SI) {
665 I -= *SI;
666
667 uint32_t LocalMacroID = *I;
668 llvm::ArrayRef<uint32_t> Overrides;
669 if (*SI != 1)
670 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
671 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
672 }
673 assert(I == LocalMacroIDs.begin());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000674 } else {
675 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
676 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000677 }
678
679 Reader.SetIdentifierInfo(ID, II);
680
681 // Read all of the declarations visible at global scope with this
682 // name.
683 if (DataLen > 0) {
684 SmallVector<uint32_t, 4> DeclIDs;
685 for (; DataLen > 0; DataLen -= 4)
686 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
687 Reader.SetGloballyVisibleDecls(II, DeclIDs);
688 }
689
690 return II;
691}
692
693unsigned
694ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
695 llvm::FoldingSetNodeID ID;
696 ID.AddInteger(Key.Kind);
697
698 switch (Key.Kind) {
699 case DeclarationName::Identifier:
700 case DeclarationName::CXXLiteralOperatorName:
701 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
702 break;
703 case DeclarationName::ObjCZeroArgSelector:
704 case DeclarationName::ObjCOneArgSelector:
705 case DeclarationName::ObjCMultiArgSelector:
706 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
707 break;
708 case DeclarationName::CXXOperatorName:
709 ID.AddInteger((OverloadedOperatorKind)Key.Data);
710 break;
711 case DeclarationName::CXXConstructorName:
712 case DeclarationName::CXXDestructorName:
713 case DeclarationName::CXXConversionFunctionName:
714 case DeclarationName::CXXUsingDirective:
715 break;
716 }
717
718 return ID.ComputeHash();
719}
720
721ASTDeclContextNameLookupTrait::internal_key_type
722ASTDeclContextNameLookupTrait::GetInternalKey(
723 const external_key_type& Name) const {
724 DeclNameKey Key;
725 Key.Kind = Name.getNameKind();
726 switch (Name.getNameKind()) {
727 case DeclarationName::Identifier:
728 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
729 break;
730 case DeclarationName::ObjCZeroArgSelector:
731 case DeclarationName::ObjCOneArgSelector:
732 case DeclarationName::ObjCMultiArgSelector:
733 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
734 break;
735 case DeclarationName::CXXOperatorName:
736 Key.Data = Name.getCXXOverloadedOperator();
737 break;
738 case DeclarationName::CXXLiteralOperatorName:
739 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
740 break;
741 case DeclarationName::CXXConstructorName:
742 case DeclarationName::CXXDestructorName:
743 case DeclarationName::CXXConversionFunctionName:
744 case DeclarationName::CXXUsingDirective:
745 Key.Data = 0;
746 break;
747 }
748
749 return Key;
750}
751
752std::pair<unsigned, unsigned>
753ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
754 using namespace clang::io;
755 unsigned KeyLen = ReadUnalignedLE16(d);
756 unsigned DataLen = ReadUnalignedLE16(d);
757 return std::make_pair(KeyLen, DataLen);
758}
759
760ASTDeclContextNameLookupTrait::internal_key_type
761ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
762 using namespace clang::io;
763
764 DeclNameKey Key;
765 Key.Kind = (DeclarationName::NameKind)*d++;
766 switch (Key.Kind) {
767 case DeclarationName::Identifier:
768 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
769 break;
770 case DeclarationName::ObjCZeroArgSelector:
771 case DeclarationName::ObjCOneArgSelector:
772 case DeclarationName::ObjCMultiArgSelector:
773 Key.Data =
774 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
775 .getAsOpaquePtr();
776 break;
777 case DeclarationName::CXXOperatorName:
778 Key.Data = *d++; // OverloadedOperatorKind
779 break;
780 case DeclarationName::CXXLiteralOperatorName:
781 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
782 break;
783 case DeclarationName::CXXConstructorName:
784 case DeclarationName::CXXDestructorName:
785 case DeclarationName::CXXConversionFunctionName:
786 case DeclarationName::CXXUsingDirective:
787 Key.Data = 0;
788 break;
789 }
790
791 return Key;
792}
793
794ASTDeclContextNameLookupTrait::data_type
795ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
796 const unsigned char* d,
797 unsigned DataLen) {
798 using namespace clang::io;
799 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000800 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
801 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000802 return std::make_pair(Start, Start + NumDecls);
803}
804
805bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000806 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000807 const std::pair<uint64_t, uint64_t> &Offsets,
808 DeclContextInfo &Info) {
809 SavedStreamPosition SavedPosition(Cursor);
810 // First the lexical decls.
811 if (Offsets.first != 0) {
812 Cursor.JumpToBit(Offsets.first);
813
814 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000815 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000816 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000817 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000818 if (RecCode != DECL_CONTEXT_LEXICAL) {
819 Error("Expected lexical block");
820 return true;
821 }
822
Chris Lattner0e6c9402013-01-20 02:38:54 +0000823 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
824 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000825 }
826
827 // Now the lookup table.
828 if (Offsets.second != 0) {
829 Cursor.JumpToBit(Offsets.second);
830
831 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000832 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000833 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000834 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000835 if (RecCode != DECL_CONTEXT_VISIBLE) {
836 Error("Expected visible lookup table block");
837 return true;
838 }
Richard Smith52e3fba2014-03-11 07:17:35 +0000839 Info.NameLookupTableData
840 = ASTDeclContextNameLookupTable::Create(
841 (const unsigned char *)Blob.data() + Record[0],
842 (const unsigned char *)Blob.data(),
843 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei11169dd2012-12-18 14:30:41 +0000844 }
845
846 return false;
847}
848
849void ASTReader::Error(StringRef Msg) {
850 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregor940e8052013-05-10 22:15:13 +0000851 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
852 Diag(diag::note_module_cache_path)
853 << PP.getHeaderSearchInfo().getModuleCachePath();
854 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000855}
856
857void ASTReader::Error(unsigned DiagID,
858 StringRef Arg1, StringRef Arg2) {
859 if (Diags.isDiagnosticInFlight())
860 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
861 else
862 Diag(DiagID) << Arg1 << Arg2;
863}
864
865//===----------------------------------------------------------------------===//
866// Source Manager Deserialization
867//===----------------------------------------------------------------------===//
868
869/// \brief Read the line table in the source manager block.
870/// \returns true if there was an error.
871bool ASTReader::ParseLineTable(ModuleFile &F,
872 SmallVectorImpl<uint64_t> &Record) {
873 unsigned Idx = 0;
874 LineTableInfo &LineTable = SourceMgr.getLineTable();
875
876 // Parse the file names
877 std::map<int, int> FileIDs;
878 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
879 // Extract the file name
880 unsigned FilenameLen = Record[Idx++];
881 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
882 Idx += FilenameLen;
883 MaybeAddSystemRootToFilename(F, Filename);
884 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
885 }
886
887 // Parse the line entries
888 std::vector<LineEntry> Entries;
889 while (Idx < Record.size()) {
890 int FID = Record[Idx++];
891 assert(FID >= 0 && "Serialized line entries for non-local file.");
892 // Remap FileID from 1-based old view.
893 FID += F.SLocEntryBaseID - 1;
894
895 // Extract the line entries
896 unsigned NumEntries = Record[Idx++];
897 assert(NumEntries && "Numentries is 00000");
898 Entries.clear();
899 Entries.reserve(NumEntries);
900 for (unsigned I = 0; I != NumEntries; ++I) {
901 unsigned FileOffset = Record[Idx++];
902 unsigned LineNo = Record[Idx++];
903 int FilenameID = FileIDs[Record[Idx++]];
904 SrcMgr::CharacteristicKind FileKind
905 = (SrcMgr::CharacteristicKind)Record[Idx++];
906 unsigned IncludeOffset = Record[Idx++];
907 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
908 FileKind, IncludeOffset));
909 }
910 LineTable.AddEntry(FileID::get(FID), Entries);
911 }
912
913 return false;
914}
915
916/// \brief Read a source manager block
917bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
918 using namespace SrcMgr;
919
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000920 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000921
922 // Set the source-location entry cursor to the current position in
923 // the stream. This cursor will be used to read the contents of the
924 // source manager block initially, and then lazily read
925 // source-location entries as needed.
926 SLocEntryCursor = F.Stream;
927
928 // The stream itself is going to skip over the source manager block.
929 if (F.Stream.SkipBlock()) {
930 Error("malformed block record in AST file");
931 return true;
932 }
933
934 // Enter the source manager block.
935 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
936 Error("malformed source manager block record in AST file");
937 return true;
938 }
939
940 RecordData Record;
941 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +0000942 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
943
944 switch (E.Kind) {
945 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
946 case llvm::BitstreamEntry::Error:
947 Error("malformed block record in AST file");
948 return true;
949 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +0000950 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +0000951 case llvm::BitstreamEntry::Record:
952 // The interesting case.
953 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000954 }
Chris Lattnere7b154b2013-01-19 21:39:22 +0000955
Guy Benyei11169dd2012-12-18 14:30:41 +0000956 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +0000957 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +0000958 StringRef Blob;
959 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000960 default: // Default behavior: ignore.
961 break;
962
963 case SM_SLOC_FILE_ENTRY:
964 case SM_SLOC_BUFFER_ENTRY:
965 case SM_SLOC_EXPANSION_ENTRY:
966 // Once we hit one of the source location entries, we're done.
967 return false;
968 }
969 }
970}
971
972/// \brief If a header file is not found at the path that we expect it to be
973/// and the PCH file was moved from its original location, try to resolve the
974/// file by assuming that header+PCH were moved together and the header is in
975/// the same place relative to the PCH.
976static std::string
977resolveFileRelativeToOriginalDir(const std::string &Filename,
978 const std::string &OriginalDir,
979 const std::string &CurrDir) {
980 assert(OriginalDir != CurrDir &&
981 "No point trying to resolve the file if the PCH dir didn't change");
982 using namespace llvm::sys;
983 SmallString<128> filePath(Filename);
984 fs::make_absolute(filePath);
985 assert(path::is_absolute(OriginalDir));
986 SmallString<128> currPCHPath(CurrDir);
987
988 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
989 fileDirE = path::end(path::parent_path(filePath));
990 path::const_iterator origDirI = path::begin(OriginalDir),
991 origDirE = path::end(OriginalDir);
992 // Skip the common path components from filePath and OriginalDir.
993 while (fileDirI != fileDirE && origDirI != origDirE &&
994 *fileDirI == *origDirI) {
995 ++fileDirI;
996 ++origDirI;
997 }
998 for (; origDirI != origDirE; ++origDirI)
999 path::append(currPCHPath, "..");
1000 path::append(currPCHPath, fileDirI, fileDirE);
1001 path::append(currPCHPath, path::filename(Filename));
1002 return currPCHPath.str();
1003}
1004
1005bool ASTReader::ReadSLocEntry(int ID) {
1006 if (ID == 0)
1007 return false;
1008
1009 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1010 Error("source location entry ID out-of-range for AST file");
1011 return true;
1012 }
1013
1014 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1015 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001016 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001017 unsigned BaseOffset = F->SLocEntryBaseOffset;
1018
1019 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001020 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1021 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001022 Error("incorrectly-formatted source location entry in AST file");
1023 return true;
1024 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001025
Guy Benyei11169dd2012-12-18 14:30:41 +00001026 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001027 StringRef Blob;
1028 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001029 default:
1030 Error("incorrectly-formatted source location entry in AST file");
1031 return true;
1032
1033 case SM_SLOC_FILE_ENTRY: {
1034 // We will detect whether a file changed and return 'Failure' for it, but
1035 // we will also try to fail gracefully by setting up the SLocEntry.
1036 unsigned InputID = Record[4];
1037 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001038 const FileEntry *File = IF.getFile();
1039 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001040
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001041 // Note that we only check if a File was returned. If it was out-of-date
1042 // we have complained but we will continue creating a FileID to recover
1043 // gracefully.
1044 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001045 return true;
1046
1047 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1048 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1049 // This is the module's main file.
1050 IncludeLoc = getImportLocation(F);
1051 }
1052 SrcMgr::CharacteristicKind
1053 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1054 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1055 ID, BaseOffset + Record[0]);
1056 SrcMgr::FileInfo &FileInfo =
1057 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1058 FileInfo.NumCreatedFIDs = Record[5];
1059 if (Record[3])
1060 FileInfo.setHasLineDirectives();
1061
1062 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1063 unsigned NumFileDecls = Record[7];
1064 if (NumFileDecls) {
1065 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1066 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1067 NumFileDecls));
1068 }
1069
1070 const SrcMgr::ContentCache *ContentCache
1071 = SourceMgr.getOrCreateContentCache(File,
1072 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1073 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1074 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1075 unsigned Code = SLocEntryCursor.ReadCode();
1076 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001077 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001078
1079 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1080 Error("AST record has invalid code");
1081 return true;
1082 }
1083
1084 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001085 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +00001086 SourceMgr.overrideFileContents(File, Buffer);
1087 }
1088
1089 break;
1090 }
1091
1092 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001093 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001094 unsigned Offset = Record[0];
1095 SrcMgr::CharacteristicKind
1096 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1097 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1098 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1099 IncludeLoc = getImportLocation(F);
1100 }
1101 unsigned Code = SLocEntryCursor.ReadCode();
1102 Record.clear();
1103 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001104 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001105
1106 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1107 Error("AST record has invalid code");
1108 return true;
1109 }
1110
1111 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001112 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei11169dd2012-12-18 14:30:41 +00001113 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1114 BaseOffset + Offset, IncludeLoc);
1115 break;
1116 }
1117
1118 case SM_SLOC_EXPANSION_ENTRY: {
1119 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1120 SourceMgr.createExpansionLoc(SpellingLoc,
1121 ReadSourceLocation(*F, Record[2]),
1122 ReadSourceLocation(*F, Record[3]),
1123 Record[4],
1124 ID,
1125 BaseOffset + Record[0]);
1126 break;
1127 }
1128 }
1129
1130 return false;
1131}
1132
1133std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1134 if (ID == 0)
1135 return std::make_pair(SourceLocation(), "");
1136
1137 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1138 Error("source location entry ID out-of-range for AST file");
1139 return std::make_pair(SourceLocation(), "");
1140 }
1141
1142 // Find which module file this entry lands in.
1143 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1144 if (M->Kind != MK_Module)
1145 return std::make_pair(SourceLocation(), "");
1146
1147 // FIXME: Can we map this down to a particular submodule? That would be
1148 // ideal.
1149 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1150}
1151
1152/// \brief Find the location where the module F is imported.
1153SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1154 if (F->ImportLoc.isValid())
1155 return F->ImportLoc;
1156
1157 // Otherwise we have a PCH. It's considered to be "imported" at the first
1158 // location of its includer.
1159 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1160 // Main file is the importer. We assume that it is the first entry in the
1161 // entry table. We can't ask the manager, because at the time of PCH loading
1162 // the main file entry doesn't exist yet.
1163 // The very first entry is the invalid instantiation loc, which takes up
1164 // offsets 0 and 1.
1165 return SourceLocation::getFromRawEncoding(2U);
1166 }
1167 //return F->Loaders[0]->FirstLoc;
1168 return F->ImportedBy[0]->FirstLoc;
1169}
1170
1171/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1172/// specified cursor. Read the abbreviations that are at the top of the block
1173/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001174bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001175 if (Cursor.EnterSubBlock(BlockID)) {
1176 Error("malformed block record in AST file");
1177 return Failure;
1178 }
1179
1180 while (true) {
1181 uint64_t Offset = Cursor.GetCurrentBitNo();
1182 unsigned Code = Cursor.ReadCode();
1183
1184 // We expect all abbrevs to be at the start of the block.
1185 if (Code != llvm::bitc::DEFINE_ABBREV) {
1186 Cursor.JumpToBit(Offset);
1187 return false;
1188 }
1189 Cursor.ReadAbbrevRecord();
1190 }
1191}
1192
Richard Smithe40f2ba2013-08-07 21:41:30 +00001193Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001194 unsigned &Idx) {
1195 Token Tok;
1196 Tok.startToken();
1197 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1198 Tok.setLength(Record[Idx++]);
1199 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1200 Tok.setIdentifierInfo(II);
1201 Tok.setKind((tok::TokenKind)Record[Idx++]);
1202 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1203 return Tok;
1204}
1205
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001206MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001207 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001208
1209 // Keep track of where we are in the stream, then jump back there
1210 // after reading this macro.
1211 SavedStreamPosition SavedPosition(Stream);
1212
1213 Stream.JumpToBit(Offset);
1214 RecordData Record;
1215 SmallVector<IdentifierInfo*, 16> MacroArgs;
1216 MacroInfo *Macro = 0;
1217
Guy Benyei11169dd2012-12-18 14:30:41 +00001218 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001219 // Advance to the next record, but if we get to the end of the block, don't
1220 // pop it (removing all the abbreviations from the cursor) since we want to
1221 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001222 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001223 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1224
1225 switch (Entry.Kind) {
1226 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1227 case llvm::BitstreamEntry::Error:
1228 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001229 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001230 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001231 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001232 case llvm::BitstreamEntry::Record:
1233 // The interesting case.
1234 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001235 }
1236
1237 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001238 Record.clear();
1239 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001240 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001241 switch (RecType) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001242 case PP_MACRO_DIRECTIVE_HISTORY:
1243 return Macro;
1244
Guy Benyei11169dd2012-12-18 14:30:41 +00001245 case PP_MACRO_OBJECT_LIKE:
1246 case PP_MACRO_FUNCTION_LIKE: {
1247 // If we already have a macro, that means that we've hit the end
1248 // of the definition of the macro we were looking for. We're
1249 // done.
1250 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001251 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001252
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001253 unsigned NextIndex = 1; // Skip identifier ID.
1254 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001255 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001256 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001257 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001258 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001259
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1261 // Decode function-like macro info.
1262 bool isC99VarArgs = Record[NextIndex++];
1263 bool isGNUVarArgs = Record[NextIndex++];
1264 bool hasCommaPasting = Record[NextIndex++];
1265 MacroArgs.clear();
1266 unsigned NumArgs = Record[NextIndex++];
1267 for (unsigned i = 0; i != NumArgs; ++i)
1268 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1269
1270 // Install function-like macro info.
1271 MI->setIsFunctionLike();
1272 if (isC99VarArgs) MI->setIsC99Varargs();
1273 if (isGNUVarArgs) MI->setIsGNUVarargs();
1274 if (hasCommaPasting) MI->setHasCommaPasting();
1275 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1276 PP.getPreprocessorAllocator());
1277 }
1278
Guy Benyei11169dd2012-12-18 14:30:41 +00001279 // Remember that we saw this macro last so that we add the tokens that
1280 // form its body to it.
1281 Macro = MI;
1282
1283 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1284 Record[NextIndex]) {
1285 // We have a macro definition. Register the association
1286 PreprocessedEntityID
1287 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1288 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001289 PreprocessingRecord::PPEntityID
1290 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1291 MacroDefinition *PPDef =
1292 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1293 if (PPDef)
1294 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001295 }
1296
1297 ++NumMacrosRead;
1298 break;
1299 }
1300
1301 case PP_TOKEN: {
1302 // If we see a TOKEN before a PP_MACRO_*, then the file is
1303 // erroneous, just pretend we didn't see this.
1304 if (Macro == 0) break;
1305
John McCallf413f5e2013-05-03 00:10:13 +00001306 unsigned Idx = 0;
1307 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001308 Macro->AddTokenToBody(Tok);
1309 break;
1310 }
1311 }
1312 }
1313}
1314
1315PreprocessedEntityID
1316ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1317 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1318 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1319 assert(I != M.PreprocessedEntityRemap.end()
1320 && "Invalid index into preprocessed entity index remap");
1321
1322 return LocalID + I->second;
1323}
1324
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001325unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1326 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001327}
1328
1329HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001330HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1331 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1332 FE->getName() };
1333 return ikey;
1334}
Guy Benyei11169dd2012-12-18 14:30:41 +00001335
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001336bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1337 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001338 return false;
1339
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001340 if (strcmp(a.Filename, b.Filename) == 0)
1341 return true;
1342
Guy Benyei11169dd2012-12-18 14:30:41 +00001343 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001344 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001345 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1346 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001347 return (FEA && FEA == FEB);
Guy Benyei11169dd2012-12-18 14:30:41 +00001348}
1349
1350std::pair<unsigned, unsigned>
1351HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1352 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1353 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001354 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001355}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001356
1357HeaderFileInfoTrait::internal_key_type
1358HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1359 internal_key_type ikey;
1360 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1361 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1362 ikey.Filename = (const char *)d;
1363 return ikey;
1364}
1365
Guy Benyei11169dd2012-12-18 14:30:41 +00001366HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001367HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001368 unsigned DataLen) {
1369 const unsigned char *End = d + DataLen;
1370 using namespace clang::io;
1371 HeaderFileInfo HFI;
1372 unsigned Flags = *d++;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001373 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1374 ((Flags >> 6) & 0x03);
Guy Benyei11169dd2012-12-18 14:30:41 +00001375 HFI.isImport = (Flags >> 5) & 0x01;
1376 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1377 HFI.DirInfo = (Flags >> 2) & 0x03;
1378 HFI.Resolved = (Flags >> 1) & 0x01;
1379 HFI.IndexHeaderMapHeader = Flags & 0x01;
1380 HFI.NumIncludes = ReadUnalignedLE16(d);
1381 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1382 ReadUnalignedLE32(d));
1383 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1384 // The framework offset is 1 greater than the actual offset,
1385 // since 0 is used as an indicator for "no framework name".
1386 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1387 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1388 }
1389
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001390 if (d != End) {
1391 uint32_t LocalSMID = ReadUnalignedLE32(d);
1392 if (LocalSMID) {
1393 // This header is part of a module. Associate it with the module to enable
1394 // implicit module import.
1395 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1396 Module *Mod = Reader.getSubmodule(GlobalSMID);
1397 HFI.isModuleHeader = true;
1398 FileManager &FileMgr = Reader.getFileManager();
1399 ModuleMap &ModMap =
1400 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001401 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001402 }
1403 }
1404
Guy Benyei11169dd2012-12-18 14:30:41 +00001405 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1406 (void)End;
1407
1408 // This HeaderFileInfo was externally loaded.
1409 HFI.External = true;
1410 return HFI;
1411}
1412
Richard Smith49f906a2014-03-01 00:08:04 +00001413void
1414ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1415 GlobalMacroID GMacID,
1416 llvm::ArrayRef<SubmoduleID> Overrides) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001417 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Richard Smith49f906a2014-03-01 00:08:04 +00001418 SubmoduleID *OverrideData = 0;
1419 if (!Overrides.empty()) {
1420 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1421 OverrideData[0] = Overrides.size();
1422 for (unsigned I = 0; I != Overrides.size(); ++I)
1423 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1424 }
1425 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001426}
1427
1428void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1429 ModuleFile *M,
1430 uint64_t MacroDirectivesOffset) {
1431 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1432 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001433}
1434
1435void ASTReader::ReadDefinedMacros() {
1436 // Note that we are loading defined macros.
1437 Deserializing Macros(this);
1438
1439 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1440 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001441 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001442
1443 // If there was no preprocessor block, skip this file.
1444 if (!MacroCursor.getBitStreamReader())
1445 continue;
1446
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001447 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001448 Cursor.JumpToBit((*I)->MacroStartOffset);
1449
1450 RecordData Record;
1451 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001452 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1453
1454 switch (E.Kind) {
1455 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1456 case llvm::BitstreamEntry::Error:
1457 Error("malformed block record in AST file");
1458 return;
1459 case llvm::BitstreamEntry::EndBlock:
1460 goto NextCursor;
1461
1462 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001463 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001464 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001465 default: // Default behavior: ignore.
1466 break;
1467
1468 case PP_MACRO_OBJECT_LIKE:
1469 case PP_MACRO_FUNCTION_LIKE:
1470 getLocalIdentifier(**I, Record[0]);
1471 break;
1472
1473 case PP_TOKEN:
1474 // Ignore tokens.
1475 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001476 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001477 break;
1478 }
1479 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001480 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 }
1482}
1483
1484namespace {
1485 /// \brief Visitor class used to look up identifirs in an AST file.
1486 class IdentifierLookupVisitor {
1487 StringRef Name;
1488 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001489 unsigned &NumIdentifierLookups;
1490 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001491 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001492
Guy Benyei11169dd2012-12-18 14:30:41 +00001493 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001494 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1495 unsigned &NumIdentifierLookups,
1496 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001497 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001498 NumIdentifierLookups(NumIdentifierLookups),
1499 NumIdentifierLookupHits(NumIdentifierLookupHits),
1500 Found()
1501 {
1502 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001503
1504 static bool visit(ModuleFile &M, void *UserData) {
1505 IdentifierLookupVisitor *This
1506 = static_cast<IdentifierLookupVisitor *>(UserData);
1507
1508 // If we've already searched this module file, skip it now.
1509 if (M.Generation <= This->PriorGeneration)
1510 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001511
Guy Benyei11169dd2012-12-18 14:30:41 +00001512 ASTIdentifierLookupTable *IdTable
1513 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1514 if (!IdTable)
1515 return false;
1516
1517 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1518 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001519 ++This->NumIdentifierLookups;
1520 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001521 if (Pos == IdTable->end())
1522 return false;
1523
1524 // Dereferencing the iterator has the effect of building the
1525 // IdentifierInfo node and populating it with the various
1526 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001527 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001528 This->Found = *Pos;
1529 return true;
1530 }
1531
1532 // \brief Retrieve the identifier info found within the module
1533 // files.
1534 IdentifierInfo *getIdentifierInfo() const { return Found; }
1535 };
1536}
1537
1538void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1539 // Note that we are loading an identifier.
1540 Deserializing AnIdentifier(this);
1541
1542 unsigned PriorGeneration = 0;
1543 if (getContext().getLangOpts().Modules)
1544 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001545
1546 // If there is a global index, look there first to determine which modules
1547 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001548 GlobalModuleIndex::HitSet Hits;
1549 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00001550 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001551 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1552 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001553 }
1554 }
1555
Douglas Gregor7211ac12013-01-25 23:32:03 +00001556 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001557 NumIdentifierLookups,
1558 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001559 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001560 markIdentifierUpToDate(&II);
1561}
1562
1563void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1564 if (!II)
1565 return;
1566
1567 II->setOutOfDate(false);
1568
1569 // Update the generation for this identifier.
1570 if (getContext().getLangOpts().Modules)
1571 IdentifierGeneration[II] = CurrentGeneration;
1572}
1573
Richard Smith49f906a2014-03-01 00:08:04 +00001574struct ASTReader::ModuleMacroInfo {
1575 SubmoduleID SubModID;
1576 MacroInfo *MI;
1577 SubmoduleID *Overrides;
1578 // FIXME: Remove this.
1579 ModuleFile *F;
1580
1581 bool isDefine() const { return MI; }
1582
1583 SubmoduleID getSubmoduleID() const { return SubModID; }
1584
1585 llvm::ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1586 if (!Overrides)
1587 return llvm::ArrayRef<SubmoduleID>();
1588 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1589 }
1590
1591 DefMacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1592 if (!MI)
1593 return 0;
1594 return PP.AllocateDefMacroDirective(MI, ImportLoc, /*isImported=*/true);
1595 }
1596};
1597
1598ASTReader::ModuleMacroInfo *
1599ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1600 ModuleMacroInfo Info;
1601
1602 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1603 if (ID & 1) {
1604 // Macro undefinition.
1605 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
1606 Info.MI = 0;
1607 } else {
1608 // Macro definition.
1609 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1610 assert(GMacID);
1611
1612 // If this macro has already been loaded, don't do so again.
1613 // FIXME: This is highly dubious. Multiple macro definitions can have the
1614 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1615 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1616 return 0;
1617
1618 Info.MI = getMacro(GMacID);
1619 Info.SubModID = Info.MI->getOwningModuleID();
1620 }
1621 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1622 Info.F = PMInfo.M;
1623
1624 return new (Context) ModuleMacroInfo(Info);
1625}
1626
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001627void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1628 const PendingMacroInfo &PMInfo) {
1629 assert(II);
1630
1631 if (PMInfo.M->Kind != MK_Module) {
1632 installPCHMacroDirectives(II, *PMInfo.M,
1633 PMInfo.PCHMacroData.MacroDirectivesOffset);
1634 return;
1635 }
Richard Smith49f906a2014-03-01 00:08:04 +00001636
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001637 // Module Macro.
1638
Richard Smith49f906a2014-03-01 00:08:04 +00001639 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1640 if (!MMI)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001641 return;
1642
Richard Smith49f906a2014-03-01 00:08:04 +00001643 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1644 if (Owner && Owner->NameVisibility == Module::Hidden) {
1645 // Macros in the owning module are hidden. Just remember this macro to
1646 // install if we make this module visible.
1647 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1648 } else {
1649 installImportedMacro(II, MMI, Owner);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001650 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001651}
1652
1653void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1654 ModuleFile &M, uint64_t Offset) {
1655 assert(M.Kind != MK_Module);
1656
1657 BitstreamCursor &Cursor = M.MacroCursor;
1658 SavedStreamPosition SavedPosition(Cursor);
1659 Cursor.JumpToBit(Offset);
1660
1661 llvm::BitstreamEntry Entry =
1662 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1663 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1664 Error("malformed block record in AST file");
1665 return;
1666 }
1667
1668 RecordData Record;
1669 PreprocessorRecordTypes RecType =
1670 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1671 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1672 Error("malformed block record in AST file");
1673 return;
1674 }
1675
1676 // Deserialize the macro directives history in reverse source-order.
1677 MacroDirective *Latest = 0, *Earliest = 0;
1678 unsigned Idx = 0, N = Record.size();
1679 while (Idx < N) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001680 MacroDirective *MD = 0;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001681 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001682 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1683 switch (K) {
1684 case MacroDirective::MD_Define: {
1685 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1686 MacroInfo *MI = getMacro(GMacID);
1687 bool isImported = Record[Idx++];
1688 bool isAmbiguous = Record[Idx++];
1689 DefMacroDirective *DefMD =
1690 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1691 DefMD->setAmbiguous(isAmbiguous);
1692 MD = DefMD;
1693 break;
1694 }
1695 case MacroDirective::MD_Undefine:
1696 MD = PP.AllocateUndefMacroDirective(Loc);
1697 break;
1698 case MacroDirective::MD_Visibility: {
1699 bool isPublic = Record[Idx++];
1700 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1701 break;
1702 }
1703 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001704
1705 if (!Latest)
1706 Latest = MD;
1707 if (Earliest)
1708 Earliest->setPrevious(MD);
1709 Earliest = MD;
1710 }
1711
1712 PP.setLoadedMacroDirective(II, Latest);
1713}
1714
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001715/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor0b202052013-04-12 21:00:54 +00001716/// modules.
1717static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregor5e461192013-06-07 22:56:11 +00001718 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001719 assert(PrevMI && NewMI);
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001720 Module *PrevOwner = 0;
1721 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1722 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregor5e461192013-06-07 22:56:11 +00001723 SourceManager &SrcMgr = Reader.getSourceManager();
1724 bool PrevInSystem
1725 = PrevOwner? PrevOwner->IsSystem
1726 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1727 bool NewInSystem
1728 = NewOwner? NewOwner->IsSystem
1729 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1730 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001731 return false;
Douglas Gregor5e461192013-06-07 22:56:11 +00001732 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001733}
1734
Richard Smith49f906a2014-03-01 00:08:04 +00001735void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1736 AmbiguousMacros &Ambig,
1737 llvm::ArrayRef<SubmoduleID> Overrides) {
1738 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1739 SubmoduleID OwnerID = Overrides[OI];
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001740
Richard Smith49f906a2014-03-01 00:08:04 +00001741 // If this macro is not yet visible, remove it from the hidden names list.
1742 Module *Owner = getSubmodule(OwnerID);
1743 HiddenNames &Hidden = HiddenNamesMap[Owner];
1744 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1745 if (HI != Hidden.HiddenMacros.end()) {
Richard Smith9d100862014-03-06 03:16:27 +00001746 auto SubOverrides = HI->second->getOverriddenSubmodules();
Richard Smith49f906a2014-03-01 00:08:04 +00001747 Hidden.HiddenMacros.erase(HI);
Richard Smith9d100862014-03-06 03:16:27 +00001748 removeOverriddenMacros(II, Ambig, SubOverrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001749 }
1750
1751 // If this macro is already in our list of conflicts, remove it from there.
Richard Smithbb29e512014-03-06 00:33:23 +00001752 Ambig.erase(
1753 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1754 return MD->getInfo()->getOwningModuleID() == OwnerID;
1755 }),
1756 Ambig.end());
Richard Smith49f906a2014-03-01 00:08:04 +00001757 }
1758}
1759
1760ASTReader::AmbiguousMacros *
1761ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1762 llvm::ArrayRef<SubmoduleID> Overrides) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001763 MacroDirective *Prev = PP.getMacroDirective(II);
Richard Smith49f906a2014-03-01 00:08:04 +00001764 if (!Prev && Overrides.empty())
1765 return 0;
1766
1767 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective() : 0;
1768 if (PrevDef && PrevDef->isAmbiguous()) {
1769 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1770 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1771 Ambig.push_back(PrevDef);
1772
1773 removeOverriddenMacros(II, Ambig, Overrides);
1774
1775 if (!Ambig.empty())
1776 return &Ambig;
1777
1778 AmbiguousMacroDefs.erase(II);
1779 } else {
1780 // There's no ambiguity yet. Maybe we're introducing one.
1781 llvm::SmallVector<DefMacroDirective*, 1> Ambig;
1782 if (PrevDef)
1783 Ambig.push_back(PrevDef);
1784
1785 removeOverriddenMacros(II, Ambig, Overrides);
1786
1787 if (!Ambig.empty()) {
1788 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
1789 Result.swap(Ambig);
1790 return &Result;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001791 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001792 }
Richard Smith49f906a2014-03-01 00:08:04 +00001793
1794 // We ended up with no ambiguity.
1795 return 0;
1796}
1797
1798void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1799 Module *Owner) {
1800 assert(II && Owner);
1801
1802 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
1803 if (ImportLoc.isInvalid()) {
1804 // FIXME: If we made macros from this module visible but didn't provide a
1805 // source location for the import, we don't have a location for the macro.
1806 // Use the location at which the containing module file was first imported
1807 // for now.
1808 ImportLoc = MMI->F->DirectImportLoc;
1809 }
1810
1811 llvm::SmallVectorImpl<DefMacroDirective*> *Prev =
1812 removeOverriddenMacros(II, MMI->getOverriddenSubmodules());
1813
1814
1815 // Create a synthetic macro definition corresponding to the import (or null
1816 // if this was an undefinition of the macro).
1817 DefMacroDirective *MD = MMI->import(PP, ImportLoc);
1818
1819 // If there's no ambiguity, just install the macro.
1820 if (!Prev) {
1821 if (MD)
1822 PP.appendMacroDirective(II, MD);
1823 else
1824 PP.appendMacroDirective(II, PP.AllocateUndefMacroDirective(ImportLoc));
1825 return;
1826 }
1827 assert(!Prev->empty());
1828
1829 if (!MD) {
1830 // We imported a #undef that didn't remove all prior definitions. The most
1831 // recent prior definition remains, and we install it in the place of the
1832 // imported directive.
1833 MacroInfo *NewMI = Prev->back()->getInfo();
1834 Prev->pop_back();
1835 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc, /*Imported*/true);
1836 }
1837
1838 // We're introducing a macro definition that creates or adds to an ambiguity.
1839 // We can resolve that ambiguity if this macro is token-for-token identical to
1840 // all of the existing definitions.
1841 MacroInfo *NewMI = MD->getInfo();
1842 assert(NewMI && "macro definition with no MacroInfo?");
1843 while (!Prev->empty()) {
1844 MacroInfo *PrevMI = Prev->back()->getInfo();
1845 assert(PrevMI && "macro definition with no MacroInfo?");
1846
1847 // Before marking the macros as ambiguous, check if this is a case where
1848 // both macros are in system headers. If so, we trust that the system
1849 // did not get it wrong. This also handles cases where Clang's own
1850 // headers have a different spelling of certain system macros:
1851 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1852 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
1853 //
1854 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
1855 // overrides the system limits.h's macros, so there's no conflict here.
1856 if (NewMI != PrevMI &&
1857 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
1858 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
1859 break;
1860
1861 // The previous definition is the same as this one (or both are defined in
1862 // system modules so we can assume they're equivalent); we don't need to
1863 // track it any more.
1864 Prev->pop_back();
1865 }
1866
1867 if (!Prev->empty())
1868 MD->setAmbiguous(true);
1869
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001870 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001871}
1872
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001873ASTReader::InputFileInfo
1874ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001875 // Go find this input file.
1876 BitstreamCursor &Cursor = F.InputFilesCursor;
1877 SavedStreamPosition SavedPosition(Cursor);
1878 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1879
1880 unsigned Code = Cursor.ReadCode();
1881 RecordData Record;
1882 StringRef Blob;
1883
1884 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1885 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1886 "invalid record type for input file");
1887 (void)Result;
1888
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001889 std::string Filename;
1890 off_t StoredSize;
1891 time_t StoredTime;
1892 bool Overridden;
1893
Ben Langmuir198c1682014-03-07 07:27:49 +00001894 assert(Record[0] == ID && "Bogus stored ID or offset");
1895 StoredSize = static_cast<off_t>(Record[1]);
1896 StoredTime = static_cast<time_t>(Record[2]);
1897 Overridden = static_cast<bool>(Record[3]);
1898 Filename = Blob;
1899 MaybeAddSystemRootToFilename(F, Filename);
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001900
1901 return { std::move(Filename), StoredSize, StoredTime, Overridden };
Ben Langmuir198c1682014-03-07 07:27:49 +00001902}
1903
1904std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001905 return readInputFileInfo(F, ID).Filename;
Ben Langmuir198c1682014-03-07 07:27:49 +00001906}
1907
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001908InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001909 // If this ID is bogus, just return an empty input file.
1910 if (ID == 0 || ID > F.InputFilesLoaded.size())
1911 return InputFile();
1912
1913 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001914 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001915 return F.InputFilesLoaded[ID-1];
1916
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00001917 if (F.InputFilesLoaded[ID-1].isNotFound())
1918 return InputFile();
1919
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001921 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001922 SavedStreamPosition SavedPosition(Cursor);
1923 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1924
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001925 InputFileInfo FI = readInputFileInfo(F, ID);
1926 off_t StoredSize = FI.StoredSize;
1927 time_t StoredTime = FI.StoredTime;
1928 bool Overridden = FI.Overridden;
1929 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001930
Ben Langmuir198c1682014-03-07 07:27:49 +00001931 const FileEntry *File
1932 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1933 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1934
1935 // If we didn't find the file, resolve it relative to the
1936 // original directory from which this AST file was created.
1937 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1938 F.OriginalDir != CurrentDir) {
1939 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1940 F.OriginalDir,
1941 CurrentDir);
1942 if (!Resolved.empty())
1943 File = FileMgr.getFile(Resolved);
1944 }
1945
1946 // For an overridden file, create a virtual file with the stored
1947 // size/timestamp.
1948 if (Overridden && File == 0) {
1949 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1950 }
1951
1952 if (File == 0) {
1953 if (Complain) {
1954 std::string ErrorStr = "could not find file '";
1955 ErrorStr += Filename;
1956 ErrorStr += "' referenced by AST file";
1957 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001958 }
Ben Langmuir198c1682014-03-07 07:27:49 +00001959 // Record that we didn't find the file.
1960 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1961 return InputFile();
1962 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001963
Ben Langmuir198c1682014-03-07 07:27:49 +00001964 // Check if there was a request to override the contents of the file
1965 // that was part of the precompiled header. Overridding such a file
1966 // can lead to problems when lexing using the source locations from the
1967 // PCH.
1968 SourceManager &SM = getSourceManager();
1969 if (!Overridden && SM.isFileOverridden(File)) {
1970 if (Complain)
1971 Error(diag::err_fe_pch_file_overridden, Filename);
1972 // After emitting the diagnostic, recover by disabling the override so
1973 // that the original file will be used.
1974 SM.disableFileContentsOverride(File);
1975 // The FileEntry is a virtual file entry with the size of the contents
1976 // that would override the original contents. Set it to the original's
1977 // size/time.
1978 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1979 StoredSize, StoredTime);
1980 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001981
Ben Langmuir198c1682014-03-07 07:27:49 +00001982 bool IsOutOfDate = false;
1983
1984 // For an overridden file, there is nothing to validate.
1985 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei11169dd2012-12-18 14:30:41 +00001986#if !defined(LLVM_ON_WIN32)
Ben Langmuir198c1682014-03-07 07:27:49 +00001987 // In our regression testing, the Windows file system seems to
1988 // have inconsistent modification times that sometimes
1989 // erroneously trigger this error-handling path.
1990 || StoredTime != File->getModificationTime()
Guy Benyei11169dd2012-12-18 14:30:41 +00001991#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00001992 )) {
1993 if (Complain) {
1994 // Build a list of the PCH imports that got us here (in reverse).
1995 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
1996 while (ImportStack.back()->ImportedBy.size() > 0)
1997 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00001998
Ben Langmuir198c1682014-03-07 07:27:49 +00001999 // The top-level PCH is stale.
2000 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2001 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002002
Ben Langmuir198c1682014-03-07 07:27:49 +00002003 // Print the import stack.
2004 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2005 Diag(diag::note_pch_required_by)
2006 << Filename << ImportStack[0]->FileName;
2007 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002008 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002009 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002010 }
2011
Ben Langmuir198c1682014-03-07 07:27:49 +00002012 if (!Diags.isDiagnosticInFlight())
2013 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002014 }
2015
Ben Langmuir198c1682014-03-07 07:27:49 +00002016 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002017 }
2018
Ben Langmuir198c1682014-03-07 07:27:49 +00002019 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2020
2021 // Note that we've loaded this input file.
2022 F.InputFilesLoaded[ID-1] = IF;
2023 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002024}
2025
2026const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2027 ModuleFile &M = ModuleMgr.getPrimaryModule();
2028 std::string Filename = filenameStrRef;
2029 MaybeAddSystemRootToFilename(M, Filename);
2030 const FileEntry *File = FileMgr.getFile(Filename);
2031 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
2032 M.OriginalDir != CurrentDir) {
2033 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2034 M.OriginalDir,
2035 CurrentDir);
2036 if (!resolved.empty())
2037 File = FileMgr.getFile(resolved);
2038 }
2039
2040 return File;
2041}
2042
2043/// \brief If we are loading a relocatable PCH file, and the filename is
2044/// not an absolute path, add the system root to the beginning of the file
2045/// name.
2046void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2047 std::string &Filename) {
2048 // If this is not a relocatable PCH file, there's nothing to do.
2049 if (!M.RelocatablePCH)
2050 return;
2051
2052 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2053 return;
2054
2055 if (isysroot.empty()) {
2056 // If no system root was given, default to '/'
2057 Filename.insert(Filename.begin(), '/');
2058 return;
2059 }
2060
2061 unsigned Length = isysroot.size();
2062 if (isysroot[Length - 1] != '/')
2063 Filename.insert(Filename.begin(), '/');
2064
2065 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2066}
2067
2068ASTReader::ASTReadResult
2069ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002070 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei11169dd2012-12-18 14:30:41 +00002071 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002072 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002073
2074 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2075 Error("malformed block record in AST file");
2076 return Failure;
2077 }
2078
2079 // Read all of the records and blocks in the control block.
2080 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002081 while (1) {
2082 llvm::BitstreamEntry Entry = Stream.advance();
2083
2084 switch (Entry.Kind) {
2085 case llvm::BitstreamEntry::Error:
2086 Error("malformed block record in AST file");
2087 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002088 case llvm::BitstreamEntry::EndBlock: {
2089 // Validate input files.
2090 const HeaderSearchOptions &HSOpts =
2091 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002092
2093 // All user input files reside at the index range [0, Record[1]), and
2094 // system input files reside at [Record[1], Record[0]).
2095 // Record is the one from INPUT_FILE_OFFSETS.
2096 unsigned NumInputs = Record[0];
2097 unsigned NumUserInputs = Record[1];
2098
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002099 if (!DisableValidation &&
2100 (!HSOpts.ModulesValidateOncePerBuildSession ||
2101 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002102 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002103
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002104 // If we are reading a module, we will create a verification timestamp,
2105 // so we verify all input files. Otherwise, verify only user input
2106 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002107
2108 unsigned N = NumUserInputs;
2109 if (ValidateSystemInputs ||
Ben Langmuircb69b572014-03-07 06:40:32 +00002110 (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module))
2111 N = NumInputs;
2112
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002113 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002114 InputFile IF = getInputFile(F, I+1, Complain);
2115 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002116 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002117 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002118 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002119
2120 if (Listener && Listener->needsInputFileVisitation()) {
2121 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2122 : NumUserInputs;
2123 for (unsigned I = 0; I < N; ++I)
2124 Listener->visitInputFile(getInputFileName(F, I+1), I >= NumUserInputs);
2125 }
2126
Guy Benyei11169dd2012-12-18 14:30:41 +00002127 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002128 }
2129
Chris Lattnere7b154b2013-01-19 21:39:22 +00002130 case llvm::BitstreamEntry::SubBlock:
2131 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002132 case INPUT_FILES_BLOCK_ID:
2133 F.InputFilesCursor = Stream;
2134 if (Stream.SkipBlock() || // Skip with the main cursor
2135 // Read the abbreviations
2136 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2137 Error("malformed block record in AST file");
2138 return Failure;
2139 }
2140 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002141
Guy Benyei11169dd2012-12-18 14:30:41 +00002142 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002143 if (Stream.SkipBlock()) {
2144 Error("malformed block record in AST file");
2145 return Failure;
2146 }
2147 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002148 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002149
2150 case llvm::BitstreamEntry::Record:
2151 // The interesting case.
2152 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002153 }
2154
2155 // Read and process a record.
2156 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002157 StringRef Blob;
2158 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 case METADATA: {
2160 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2161 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002162 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2163 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002164 return VersionMismatch;
2165 }
2166
2167 bool hasErrors = Record[5];
2168 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2169 Diag(diag::err_pch_with_compiler_errors);
2170 return HadErrors;
2171 }
2172
2173 F.RelocatablePCH = Record[4];
2174
2175 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002176 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002177 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2178 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002179 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002180 return VersionMismatch;
2181 }
2182 break;
2183 }
2184
2185 case IMPORTS: {
2186 // Load each of the imported PCH files.
2187 unsigned Idx = 0, N = Record.size();
2188 while (Idx < N) {
2189 // Read information about the AST file.
2190 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2191 // The import location will be the local one for now; we will adjust
2192 // all import locations of module imports after the global source
2193 // location info are setup.
2194 SourceLocation ImportLoc =
2195 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002196 off_t StoredSize = (off_t)Record[Idx++];
2197 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002198 unsigned Length = Record[Idx++];
2199 SmallString<128> ImportedFile(Record.begin() + Idx,
2200 Record.begin() + Idx + Length);
2201 Idx += Length;
2202
2203 // Load the AST file.
2204 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00002205 StoredSize, StoredModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00002206 ClientLoadCapabilities)) {
2207 case Failure: return Failure;
2208 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002209 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002210 case OutOfDate: return OutOfDate;
2211 case VersionMismatch: return VersionMismatch;
2212 case ConfigurationMismatch: return ConfigurationMismatch;
2213 case HadErrors: return HadErrors;
2214 case Success: break;
2215 }
2216 }
2217 break;
2218 }
2219
2220 case LANGUAGE_OPTIONS: {
2221 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2222 if (Listener && &F == *ModuleMgr.begin() &&
2223 ParseLanguageOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002224 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002225 return ConfigurationMismatch;
2226 break;
2227 }
2228
2229 case TARGET_OPTIONS: {
2230 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2231 if (Listener && &F == *ModuleMgr.begin() &&
2232 ParseTargetOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002233 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002234 return ConfigurationMismatch;
2235 break;
2236 }
2237
2238 case DIAGNOSTIC_OPTIONS: {
2239 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2240 if (Listener && &F == *ModuleMgr.begin() &&
2241 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002242 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002243 return ConfigurationMismatch;
2244 break;
2245 }
2246
2247 case FILE_SYSTEM_OPTIONS: {
2248 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2249 if (Listener && &F == *ModuleMgr.begin() &&
2250 ParseFileSystemOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002251 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002252 return ConfigurationMismatch;
2253 break;
2254 }
2255
2256 case HEADER_SEARCH_OPTIONS: {
2257 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2258 if (Listener && &F == *ModuleMgr.begin() &&
2259 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002260 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002261 return ConfigurationMismatch;
2262 break;
2263 }
2264
2265 case PREPROCESSOR_OPTIONS: {
2266 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2267 if (Listener && &F == *ModuleMgr.begin() &&
2268 ParsePreprocessorOptions(Record, Complain, *Listener,
2269 SuggestedPredefines) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002270 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002271 return ConfigurationMismatch;
2272 break;
2273 }
2274
2275 case ORIGINAL_FILE:
2276 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002277 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002278 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2279 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2280 break;
2281
2282 case ORIGINAL_FILE_ID:
2283 F.OriginalSourceFileID = FileID::get(Record[0]);
2284 break;
2285
2286 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002287 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002288 break;
2289
2290 case INPUT_FILE_OFFSETS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002291 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002292 F.InputFilesLoaded.resize(Record[0]);
2293 break;
2294 }
2295 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002296}
2297
2298bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002299 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002300
2301 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2302 Error("malformed block record in AST file");
2303 return true;
2304 }
2305
2306 // Read all of the records and blocks for the AST file.
2307 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002308 while (1) {
2309 llvm::BitstreamEntry Entry = Stream.advance();
2310
2311 switch (Entry.Kind) {
2312 case llvm::BitstreamEntry::Error:
2313 Error("error at end of module block in AST file");
2314 return true;
2315 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002316 // Outside of C++, we do not store a lookup map for the translation unit.
2317 // Instead, mark it as needing a lookup map to be built if this module
2318 // contains any declarations lexically within it (which it always does!).
2319 // This usually has no cost, since we very rarely need the lookup map for
2320 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002321 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002322 if (DC->hasExternalLexicalStorage() &&
2323 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002324 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002325
Guy Benyei11169dd2012-12-18 14:30:41 +00002326 return false;
2327 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002328 case llvm::BitstreamEntry::SubBlock:
2329 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002330 case DECLTYPES_BLOCK_ID:
2331 // We lazily load the decls block, but we want to set up the
2332 // DeclsCursor cursor to point into it. Clone our current bitcode
2333 // cursor to it, enter the block and read the abbrevs in that block.
2334 // With the main cursor, we just skip over it.
2335 F.DeclsCursor = Stream;
2336 if (Stream.SkipBlock() || // Skip with the main cursor.
2337 // Read the abbrevs.
2338 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2339 Error("malformed block record in AST file");
2340 return true;
2341 }
2342 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002343
Guy Benyei11169dd2012-12-18 14:30:41 +00002344 case DECL_UPDATES_BLOCK_ID:
2345 if (Stream.SkipBlock()) {
2346 Error("malformed block record in AST file");
2347 return true;
2348 }
2349 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002350
Guy Benyei11169dd2012-12-18 14:30:41 +00002351 case PREPROCESSOR_BLOCK_ID:
2352 F.MacroCursor = Stream;
2353 if (!PP.getExternalSource())
2354 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002355
Guy Benyei11169dd2012-12-18 14:30:41 +00002356 if (Stream.SkipBlock() ||
2357 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2358 Error("malformed block record in AST file");
2359 return true;
2360 }
2361 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2362 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002363
Guy Benyei11169dd2012-12-18 14:30:41 +00002364 case PREPROCESSOR_DETAIL_BLOCK_ID:
2365 F.PreprocessorDetailCursor = Stream;
2366 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002367 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002368 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002369 Error("malformed preprocessor detail record in AST file");
2370 return true;
2371 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002372 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002373 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2374
Guy Benyei11169dd2012-12-18 14:30:41 +00002375 if (!PP.getPreprocessingRecord())
2376 PP.createPreprocessingRecord();
2377 if (!PP.getPreprocessingRecord()->getExternalSource())
2378 PP.getPreprocessingRecord()->SetExternalSource(*this);
2379 break;
2380
2381 case SOURCE_MANAGER_BLOCK_ID:
2382 if (ReadSourceManagerBlock(F))
2383 return true;
2384 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002385
Guy Benyei11169dd2012-12-18 14:30:41 +00002386 case SUBMODULE_BLOCK_ID:
2387 if (ReadSubmoduleBlock(F))
2388 return true;
2389 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002390
Guy Benyei11169dd2012-12-18 14:30:41 +00002391 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002392 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002393 if (Stream.SkipBlock() ||
2394 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2395 Error("malformed comments block in AST file");
2396 return true;
2397 }
2398 CommentsCursors.push_back(std::make_pair(C, &F));
2399 break;
2400 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002401
Guy Benyei11169dd2012-12-18 14:30:41 +00002402 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002403 if (Stream.SkipBlock()) {
2404 Error("malformed block record in AST file");
2405 return true;
2406 }
2407 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002408 }
2409 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002410
2411 case llvm::BitstreamEntry::Record:
2412 // The interesting case.
2413 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002414 }
2415
2416 // Read and process a record.
2417 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002418 StringRef Blob;
2419 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002420 default: // Default behavior: ignore.
2421 break;
2422
2423 case TYPE_OFFSET: {
2424 if (F.LocalNumTypes != 0) {
2425 Error("duplicate TYPE_OFFSET record in AST file");
2426 return true;
2427 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002428 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002429 F.LocalNumTypes = Record[0];
2430 unsigned LocalBaseTypeIndex = Record[1];
2431 F.BaseTypeIndex = getTotalNumTypes();
2432
2433 if (F.LocalNumTypes > 0) {
2434 // Introduce the global -> local mapping for types within this module.
2435 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2436
2437 // Introduce the local -> global mapping for types within this module.
2438 F.TypeRemap.insertOrReplace(
2439 std::make_pair(LocalBaseTypeIndex,
2440 F.BaseTypeIndex - LocalBaseTypeIndex));
2441
2442 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2443 }
2444 break;
2445 }
2446
2447 case DECL_OFFSET: {
2448 if (F.LocalNumDecls != 0) {
2449 Error("duplicate DECL_OFFSET record in AST file");
2450 return true;
2451 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002452 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002453 F.LocalNumDecls = Record[0];
2454 unsigned LocalBaseDeclID = Record[1];
2455 F.BaseDeclID = getTotalNumDecls();
2456
2457 if (F.LocalNumDecls > 0) {
2458 // Introduce the global -> local mapping for declarations within this
2459 // module.
2460 GlobalDeclMap.insert(
2461 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2462
2463 // Introduce the local -> global mapping for declarations within this
2464 // module.
2465 F.DeclRemap.insertOrReplace(
2466 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2467
2468 // Introduce the global -> local mapping for declarations within this
2469 // module.
2470 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2471
2472 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2473 }
2474 break;
2475 }
2476
2477 case TU_UPDATE_LEXICAL: {
2478 DeclContext *TU = Context.getTranslationUnitDecl();
2479 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002480 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002481 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002482 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002483 TU->setHasExternalLexicalStorage(true);
2484 break;
2485 }
2486
2487 case UPDATE_VISIBLE: {
2488 unsigned Idx = 0;
2489 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2490 ASTDeclContextNameLookupTable *Table =
2491 ASTDeclContextNameLookupTable::Create(
Chris Lattner0e6c9402013-01-20 02:38:54 +00002492 (const unsigned char *)Blob.data() + Record[Idx++],
2493 (const unsigned char *)Blob.data(),
Guy Benyei11169dd2012-12-18 14:30:41 +00002494 ASTDeclContextNameLookupTrait(*this, F));
2495 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2496 DeclContext *TU = Context.getTranslationUnitDecl();
Richard Smith52e3fba2014-03-11 07:17:35 +00002497 F.DeclContextInfos[TU].NameLookupTableData = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002498 TU->setHasExternalVisibleStorage(true);
Richard Smithd9174792014-03-11 03:10:46 +00002499 } else if (Decl *D = DeclsLoaded[ID - NUM_PREDEF_DECL_IDS]) {
2500 auto *DC = cast<DeclContext>(D);
2501 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
Richard Smith52e3fba2014-03-11 07:17:35 +00002502 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2503 delete LookupTable;
2504 LookupTable = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002505 } else
2506 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2507 break;
2508 }
2509
2510 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002511 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002512 if (Record[0]) {
2513 F.IdentifierLookupTable
2514 = ASTIdentifierLookupTable::Create(
2515 (const unsigned char *)F.IdentifierTableData + Record[0],
2516 (const unsigned char *)F.IdentifierTableData,
2517 ASTIdentifierLookupTrait(*this, F));
2518
2519 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2520 }
2521 break;
2522
2523 case IDENTIFIER_OFFSET: {
2524 if (F.LocalNumIdentifiers != 0) {
2525 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2526 return true;
2527 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002528 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002529 F.LocalNumIdentifiers = Record[0];
2530 unsigned LocalBaseIdentifierID = Record[1];
2531 F.BaseIdentifierID = getTotalNumIdentifiers();
2532
2533 if (F.LocalNumIdentifiers > 0) {
2534 // Introduce the global -> local mapping for identifiers within this
2535 // module.
2536 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2537 &F));
2538
2539 // Introduce the local -> global mapping for identifiers within this
2540 // module.
2541 F.IdentifierRemap.insertOrReplace(
2542 std::make_pair(LocalBaseIdentifierID,
2543 F.BaseIdentifierID - LocalBaseIdentifierID));
2544
2545 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2546 + F.LocalNumIdentifiers);
2547 }
2548 break;
2549 }
2550
Ben Langmuir332aafe2014-01-31 01:06:56 +00002551 case EAGERLY_DESERIALIZED_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002552 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002553 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002554 break;
2555
2556 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002557 if (SpecialTypes.empty()) {
2558 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2559 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2560 break;
2561 }
2562
2563 if (SpecialTypes.size() != Record.size()) {
2564 Error("invalid special-types record");
2565 return true;
2566 }
2567
2568 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2569 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2570 if (!SpecialTypes[I])
2571 SpecialTypes[I] = ID;
2572 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2573 // merge step?
2574 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002575 break;
2576
2577 case STATISTICS:
2578 TotalNumStatements += Record[0];
2579 TotalNumMacros += Record[1];
2580 TotalLexicalDeclContexts += Record[2];
2581 TotalVisibleDeclContexts += Record[3];
2582 break;
2583
2584 case UNUSED_FILESCOPED_DECLS:
2585 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2586 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2587 break;
2588
2589 case DELEGATING_CTORS:
2590 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2591 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2592 break;
2593
2594 case WEAK_UNDECLARED_IDENTIFIERS:
2595 if (Record.size() % 4 != 0) {
2596 Error("invalid weak identifiers record");
2597 return true;
2598 }
2599
2600 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2601 // files. This isn't the way to do it :)
2602 WeakUndeclaredIdentifiers.clear();
2603
2604 // Translate the weak, undeclared identifiers into global IDs.
2605 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2606 WeakUndeclaredIdentifiers.push_back(
2607 getGlobalIdentifierID(F, Record[I++]));
2608 WeakUndeclaredIdentifiers.push_back(
2609 getGlobalIdentifierID(F, Record[I++]));
2610 WeakUndeclaredIdentifiers.push_back(
2611 ReadSourceLocation(F, Record, I).getRawEncoding());
2612 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2613 }
2614 break;
2615
Richard Smith78165b52013-01-10 23:43:47 +00002616 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002617 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith78165b52013-01-10 23:43:47 +00002618 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002619 break;
2620
2621 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002622 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002623 F.LocalNumSelectors = Record[0];
2624 unsigned LocalBaseSelectorID = Record[1];
2625 F.BaseSelectorID = getTotalNumSelectors();
2626
2627 if (F.LocalNumSelectors > 0) {
2628 // Introduce the global -> local mapping for selectors within this
2629 // module.
2630 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2631
2632 // Introduce the local -> global mapping for selectors within this
2633 // module.
2634 F.SelectorRemap.insertOrReplace(
2635 std::make_pair(LocalBaseSelectorID,
2636 F.BaseSelectorID - LocalBaseSelectorID));
2637
2638 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2639 }
2640 break;
2641 }
2642
2643 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002644 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002645 if (Record[0])
2646 F.SelectorLookupTable
2647 = ASTSelectorLookupTable::Create(
2648 F.SelectorLookupTableData + Record[0],
2649 F.SelectorLookupTableData,
2650 ASTSelectorLookupTrait(*this, F));
2651 TotalNumMethodPoolEntries += Record[1];
2652 break;
2653
2654 case REFERENCED_SELECTOR_POOL:
2655 if (!Record.empty()) {
2656 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2657 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2658 Record[Idx++]));
2659 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2660 getRawEncoding());
2661 }
2662 }
2663 break;
2664
2665 case PP_COUNTER_VALUE:
2666 if (!Record.empty() && Listener)
2667 Listener->ReadCounter(F, Record[0]);
2668 break;
2669
2670 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002671 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002672 F.NumFileSortedDecls = Record[0];
2673 break;
2674
2675 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002676 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002677 F.LocalNumSLocEntries = Record[0];
2678 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002679 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Guy Benyei11169dd2012-12-18 14:30:41 +00002680 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2681 SLocSpaceSize);
2682 // Make our entry in the range map. BaseID is negative and growing, so
2683 // we invert it. Because we invert it, though, we need the other end of
2684 // the range.
2685 unsigned RangeStart =
2686 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2687 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2688 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2689
2690 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2691 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2692 GlobalSLocOffsetMap.insert(
2693 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2694 - SLocSpaceSize,&F));
2695
2696 // Initialize the remapping table.
2697 // Invalid stays invalid.
2698 F.SLocRemap.insert(std::make_pair(0U, 0));
2699 // This module. Base was 2 when being compiled.
2700 F.SLocRemap.insert(std::make_pair(2U,
2701 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2702
2703 TotalNumSLocEntries += F.LocalNumSLocEntries;
2704 break;
2705 }
2706
2707 case MODULE_OFFSET_MAP: {
2708 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002709 const unsigned char *Data = (const unsigned char*)Blob.data();
2710 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei11169dd2012-12-18 14:30:41 +00002711
2712 // Continuous range maps we may be updating in our module.
2713 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2714 ContinuousRangeMap<uint32_t, int, 2>::Builder
2715 IdentifierRemap(F.IdentifierRemap);
2716 ContinuousRangeMap<uint32_t, int, 2>::Builder
2717 MacroRemap(F.MacroRemap);
2718 ContinuousRangeMap<uint32_t, int, 2>::Builder
2719 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2720 ContinuousRangeMap<uint32_t, int, 2>::Builder
2721 SubmoduleRemap(F.SubmoduleRemap);
2722 ContinuousRangeMap<uint32_t, int, 2>::Builder
2723 SelectorRemap(F.SelectorRemap);
2724 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2725 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2726
2727 while(Data < DataEnd) {
2728 uint16_t Len = io::ReadUnalignedLE16(Data);
2729 StringRef Name = StringRef((const char*)Data, Len);
2730 Data += Len;
2731 ModuleFile *OM = ModuleMgr.lookup(Name);
2732 if (!OM) {
2733 Error("SourceLocation remap refers to unknown module");
2734 return true;
2735 }
2736
2737 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2738 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2739 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2740 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2741 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2742 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2743 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2744 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2745
2746 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2747 SLocRemap.insert(std::make_pair(SLocOffset,
2748 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2749 IdentifierRemap.insert(
2750 std::make_pair(IdentifierIDOffset,
2751 OM->BaseIdentifierID - IdentifierIDOffset));
2752 MacroRemap.insert(std::make_pair(MacroIDOffset,
2753 OM->BaseMacroID - MacroIDOffset));
2754 PreprocessedEntityRemap.insert(
2755 std::make_pair(PreprocessedEntityIDOffset,
2756 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2757 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2758 OM->BaseSubmoduleID - SubmoduleIDOffset));
2759 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2760 OM->BaseSelectorID - SelectorIDOffset));
2761 DeclRemap.insert(std::make_pair(DeclIDOffset,
2762 OM->BaseDeclID - DeclIDOffset));
2763
2764 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2765 OM->BaseTypeIndex - TypeIndexOffset));
2766
2767 // Global -> local mappings.
2768 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2769 }
2770 break;
2771 }
2772
2773 case SOURCE_MANAGER_LINE_TABLE:
2774 if (ParseLineTable(F, Record))
2775 return true;
2776 break;
2777
2778 case SOURCE_LOCATION_PRELOADS: {
2779 // Need to transform from the local view (1-based IDs) to the global view,
2780 // which is based off F.SLocEntryBaseID.
2781 if (!F.PreloadSLocEntries.empty()) {
2782 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2783 return true;
2784 }
2785
2786 F.PreloadSLocEntries.swap(Record);
2787 break;
2788 }
2789
2790 case EXT_VECTOR_DECLS:
2791 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2792 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2793 break;
2794
2795 case VTABLE_USES:
2796 if (Record.size() % 3 != 0) {
2797 Error("Invalid VTABLE_USES record");
2798 return true;
2799 }
2800
2801 // Later tables overwrite earlier ones.
2802 // FIXME: Modules will have some trouble with this. This is clearly not
2803 // the right way to do this.
2804 VTableUses.clear();
2805
2806 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2807 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2808 VTableUses.push_back(
2809 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2810 VTableUses.push_back(Record[Idx++]);
2811 }
2812 break;
2813
2814 case DYNAMIC_CLASSES:
2815 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2816 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2817 break;
2818
2819 case PENDING_IMPLICIT_INSTANTIATIONS:
2820 if (PendingInstantiations.size() % 2 != 0) {
2821 Error("Invalid existing PendingInstantiations");
2822 return true;
2823 }
2824
2825 if (Record.size() % 2 != 0) {
2826 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2827 return true;
2828 }
2829
2830 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2831 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2832 PendingInstantiations.push_back(
2833 ReadSourceLocation(F, Record, I).getRawEncoding());
2834 }
2835 break;
2836
2837 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00002838 if (Record.size() != 2) {
2839 Error("Invalid SEMA_DECL_REFS block");
2840 return true;
2841 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002842 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2843 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2844 break;
2845
2846 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002847 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2848 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2849 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002850
2851 unsigned LocalBasePreprocessedEntityID = Record[0];
2852
2853 unsigned StartingID;
2854 if (!PP.getPreprocessingRecord())
2855 PP.createPreprocessingRecord();
2856 if (!PP.getPreprocessingRecord()->getExternalSource())
2857 PP.getPreprocessingRecord()->SetExternalSource(*this);
2858 StartingID
2859 = PP.getPreprocessingRecord()
2860 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2861 F.BasePreprocessedEntityID = StartingID;
2862
2863 if (F.NumPreprocessedEntities > 0) {
2864 // Introduce the global -> local mapping for preprocessed entities in
2865 // this module.
2866 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2867
2868 // Introduce the local -> global mapping for preprocessed entities in
2869 // this module.
2870 F.PreprocessedEntityRemap.insertOrReplace(
2871 std::make_pair(LocalBasePreprocessedEntityID,
2872 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2873 }
2874
2875 break;
2876 }
2877
2878 case DECL_UPDATE_OFFSETS: {
2879 if (Record.size() % 2 != 0) {
2880 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2881 return true;
2882 }
2883 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2884 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2885 .push_back(std::make_pair(&F, Record[I+1]));
2886 break;
2887 }
2888
2889 case DECL_REPLACEMENTS: {
2890 if (Record.size() % 3 != 0) {
2891 Error("invalid DECL_REPLACEMENTS block in AST file");
2892 return true;
2893 }
2894 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2895 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2896 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2897 break;
2898 }
2899
2900 case OBJC_CATEGORIES_MAP: {
2901 if (F.LocalNumObjCCategoriesInMap != 0) {
2902 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2903 return true;
2904 }
2905
2906 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002907 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002908 break;
2909 }
2910
2911 case OBJC_CATEGORIES:
2912 F.ObjCCategories.swap(Record);
2913 break;
2914
2915 case CXX_BASE_SPECIFIER_OFFSETS: {
2916 if (F.LocalNumCXXBaseSpecifiers != 0) {
2917 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2918 return true;
2919 }
2920
2921 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002922 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002923 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2924 break;
2925 }
2926
2927 case DIAG_PRAGMA_MAPPINGS:
2928 if (F.PragmaDiagMappings.empty())
2929 F.PragmaDiagMappings.swap(Record);
2930 else
2931 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2932 Record.begin(), Record.end());
2933 break;
2934
2935 case CUDA_SPECIAL_DECL_REFS:
2936 // Later tables overwrite earlier ones.
2937 // FIXME: Modules will have trouble with this.
2938 CUDASpecialDeclRefs.clear();
2939 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2940 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2941 break;
2942
2943 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002944 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002945 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00002946 if (Record[0]) {
2947 F.HeaderFileInfoTable
2948 = HeaderFileInfoLookupTable::Create(
2949 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2950 (const unsigned char *)F.HeaderFileInfoTableData,
2951 HeaderFileInfoTrait(*this, F,
2952 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00002953 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002954
2955 PP.getHeaderSearchInfo().SetExternalSource(this);
2956 if (!PP.getHeaderSearchInfo().getExternalLookup())
2957 PP.getHeaderSearchInfo().SetExternalLookup(this);
2958 }
2959 break;
2960 }
2961
2962 case FP_PRAGMA_OPTIONS:
2963 // Later tables overwrite earlier ones.
2964 FPPragmaOptions.swap(Record);
2965 break;
2966
2967 case OPENCL_EXTENSIONS:
2968 // Later tables overwrite earlier ones.
2969 OpenCLExtensions.swap(Record);
2970 break;
2971
2972 case TENTATIVE_DEFINITIONS:
2973 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2974 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2975 break;
2976
2977 case KNOWN_NAMESPACES:
2978 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2979 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2980 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00002981
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002982 case UNDEFINED_BUT_USED:
2983 if (UndefinedButUsed.size() % 2 != 0) {
2984 Error("Invalid existing UndefinedButUsed");
Nick Lewycky8334af82013-01-26 00:35:08 +00002985 return true;
2986 }
2987
2988 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002989 Error("invalid undefined-but-used record");
Nick Lewycky8334af82013-01-26 00:35:08 +00002990 return true;
2991 }
2992 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002993 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
2994 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00002995 ReadSourceLocation(F, Record, I).getRawEncoding());
2996 }
2997 break;
2998
Guy Benyei11169dd2012-12-18 14:30:41 +00002999 case IMPORTED_MODULES: {
3000 if (F.Kind != MK_Module) {
3001 // If we aren't loading a module (which has its own exports), make
3002 // all of the imported modules visible.
3003 // FIXME: Deal with macros-only imports.
3004 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3005 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
3006 ImportedModules.push_back(GlobalID);
3007 }
3008 }
3009 break;
3010 }
3011
3012 case LOCAL_REDECLARATIONS: {
3013 F.RedeclarationChains.swap(Record);
3014 break;
3015 }
3016
3017 case LOCAL_REDECLARATIONS_MAP: {
3018 if (F.LocalNumRedeclarationsInMap != 0) {
3019 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3020 return true;
3021 }
3022
3023 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003024 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003025 break;
3026 }
3027
3028 case MERGED_DECLARATIONS: {
3029 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3030 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3031 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3032 for (unsigned N = Record[Idx++]; N > 0; --N)
3033 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3034 }
3035 break;
3036 }
3037
3038 case MACRO_OFFSET: {
3039 if (F.LocalNumMacros != 0) {
3040 Error("duplicate MACRO_OFFSET record in AST file");
3041 return true;
3042 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003043 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003044 F.LocalNumMacros = Record[0];
3045 unsigned LocalBaseMacroID = Record[1];
3046 F.BaseMacroID = getTotalNumMacros();
3047
3048 if (F.LocalNumMacros > 0) {
3049 // Introduce the global -> local mapping for macros within this module.
3050 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3051
3052 // Introduce the local -> global mapping for macros within this module.
3053 F.MacroRemap.insertOrReplace(
3054 std::make_pair(LocalBaseMacroID,
3055 F.BaseMacroID - LocalBaseMacroID));
3056
3057 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3058 }
3059 break;
3060 }
3061
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003062 case MACRO_TABLE: {
3063 // FIXME: Not used yet.
Guy Benyei11169dd2012-12-18 14:30:41 +00003064 break;
3065 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00003066
3067 case LATE_PARSED_TEMPLATE: {
3068 LateParsedTemplates.append(Record.begin(), Record.end());
3069 break;
3070 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003071 }
3072 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003073}
3074
Douglas Gregorc1489562013-02-12 23:36:21 +00003075/// \brief Move the given method to the back of the global list of methods.
3076static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3077 // Find the entry for this selector in the method pool.
3078 Sema::GlobalMethodPool::iterator Known
3079 = S.MethodPool.find(Method->getSelector());
3080 if (Known == S.MethodPool.end())
3081 return;
3082
3083 // Retrieve the appropriate method list.
3084 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3085 : Known->second.second;
3086 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003087 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003088 if (!Found) {
3089 if (List->Method == Method) {
3090 Found = true;
3091 } else {
3092 // Keep searching.
3093 continue;
3094 }
3095 }
3096
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003097 if (List->getNext())
3098 List->Method = List->getNext()->Method;
Douglas Gregorc1489562013-02-12 23:36:21 +00003099 else
3100 List->Method = Method;
3101 }
3102}
3103
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003104void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith49f906a2014-03-01 00:08:04 +00003105 for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) {
3106 Decl *D = Names.HiddenDecls[I];
3107 bool wasHidden = D->Hidden;
3108 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003109
Richard Smith49f906a2014-03-01 00:08:04 +00003110 if (wasHidden && SemaObj) {
3111 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3112 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003113 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003114 }
3115 }
Richard Smith49f906a2014-03-01 00:08:04 +00003116
3117 for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(),
3118 E = Names.HiddenMacros.end();
3119 I != E; ++I)
3120 installImportedMacro(I->first, I->second, Owner);
Guy Benyei11169dd2012-12-18 14:30:41 +00003121}
3122
Richard Smith49f906a2014-03-01 00:08:04 +00003123void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003124 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00003125 SourceLocation ImportLoc,
3126 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003127 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003128 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003129 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003130 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003131 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003132
3133 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003134 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003135 // there is nothing more to do.
3136 continue;
3137 }
Richard Smith49f906a2014-03-01 00:08:04 +00003138
Guy Benyei11169dd2012-12-18 14:30:41 +00003139 if (!Mod->isAvailable()) {
3140 // Modules that aren't available cannot be made visible.
3141 continue;
3142 }
3143
3144 // Update the module's name visibility.
Richard Smith49f906a2014-03-01 00:08:04 +00003145 if (NameVisibility >= Module::MacrosVisible &&
3146 Mod->NameVisibility < Module::MacrosVisible)
3147 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003148 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003149
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 // If we've already deserialized any names from this module,
3151 // mark them as visible.
3152 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3153 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003154 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003155 HiddenNamesMap.erase(Hidden);
3156 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003157
Guy Benyei11169dd2012-12-18 14:30:41 +00003158 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003159 SmallVector<Module *, 16> Exports;
3160 Mod->getExportedModules(Exports);
3161 for (SmallVectorImpl<Module *>::iterator
3162 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3163 Module *Exported = *I;
3164 if (Visited.insert(Exported))
3165 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003166 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003167
3168 // Detect any conflicts.
3169 if (Complain) {
3170 assert(ImportLoc.isValid() && "Missing import location");
3171 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3172 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3173 Diag(ImportLoc, diag::warn_module_conflict)
3174 << Mod->getFullModuleName()
3175 << Mod->Conflicts[I].Other->getFullModuleName()
3176 << Mod->Conflicts[I].Message;
3177 // FIXME: Need note where the other module was imported.
3178 }
3179 }
3180 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003181 }
3182}
3183
Douglas Gregore060e572013-01-25 01:03:03 +00003184bool ASTReader::loadGlobalIndex() {
3185 if (GlobalIndex)
3186 return false;
3187
3188 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3189 !Context.getLangOpts().Modules)
3190 return true;
3191
3192 // Try to load the global index.
3193 TriedLoadingGlobalIndex = true;
3194 StringRef ModuleCachePath
3195 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3196 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003197 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003198 if (!Result.first)
3199 return true;
3200
3201 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003202 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003203 return false;
3204}
3205
3206bool ASTReader::isGlobalIndexUnavailable() const {
3207 return Context.getLangOpts().Modules && UseGlobalIndex &&
3208 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3209}
3210
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003211static void updateModuleTimestamp(ModuleFile &MF) {
3212 // Overwrite the timestamp file contents so that file's mtime changes.
3213 std::string TimestampFilename = MF.getTimestampFilename();
3214 std::string ErrorInfo;
Rafael Espindola04a13be2014-02-24 15:06:52 +00003215 llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo,
Rafael Espindola4fbd3732014-02-24 18:20:21 +00003216 llvm::sys::fs::F_Text);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003217 if (!ErrorInfo.empty())
3218 return;
3219 OS << "Timestamp file\n";
3220}
3221
Guy Benyei11169dd2012-12-18 14:30:41 +00003222ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3223 ModuleKind Type,
3224 SourceLocation ImportLoc,
3225 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003226 llvm::SaveAndRestore<SourceLocation>
3227 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3228
Guy Benyei11169dd2012-12-18 14:30:41 +00003229 // Bump the generation number.
3230 unsigned PreviousGeneration = CurrentGeneration++;
3231
3232 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003233 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003234 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3235 /*ImportedBy=*/0, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003236 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003237 ClientLoadCapabilities)) {
3238 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003239 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003240 case OutOfDate:
3241 case VersionMismatch:
3242 case ConfigurationMismatch:
3243 case HadErrors:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003244 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3245 Context.getLangOpts().Modules
3246 ? &PP.getHeaderSearchInfo().getModuleMap()
3247 : 0);
Douglas Gregore060e572013-01-25 01:03:03 +00003248
3249 // If we find that any modules are unusable, the global index is going
3250 // to be out-of-date. Just remove it.
3251 GlobalIndex.reset();
Douglas Gregor7211ac12013-01-25 23:32:03 +00003252 ModuleMgr.setGlobalIndex(0);
Guy Benyei11169dd2012-12-18 14:30:41 +00003253 return ReadResult;
3254
3255 case Success:
3256 break;
3257 }
3258
3259 // Here comes stuff that we only do once the entire chain is loaded.
3260
3261 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003262 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3263 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003264 M != MEnd; ++M) {
3265 ModuleFile &F = *M->Mod;
3266
3267 // Read the AST block.
3268 if (ReadASTBlock(F))
3269 return Failure;
3270
3271 // Once read, set the ModuleFile bit base offset and update the size in
3272 // bits of all files we've seen.
3273 F.GlobalBitOffset = TotalModulesSizeInBits;
3274 TotalModulesSizeInBits += F.SizeInBits;
3275 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3276
3277 // Preload SLocEntries.
3278 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3279 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3280 // Load it through the SourceManager and don't call ReadSLocEntry()
3281 // directly because the entry may have already been loaded in which case
3282 // calling ReadSLocEntry() directly would trigger an assertion in
3283 // SourceManager.
3284 SourceMgr.getLoadedSLocEntryByID(Index);
3285 }
3286 }
3287
Douglas Gregor603cd862013-03-22 18:50:14 +00003288 // Setup the import locations and notify the module manager that we've
3289 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003290 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3291 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003292 M != MEnd; ++M) {
3293 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003294
3295 ModuleMgr.moduleFileAccepted(&F);
3296
3297 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003298 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003299 if (!M->ImportedBy)
3300 F.ImportLoc = M->ImportLoc;
3301 else
3302 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3303 M->ImportLoc.getRawEncoding());
3304 }
3305
3306 // Mark all of the identifiers in the identifier table as being out of date,
3307 // so that various accessors know to check the loaded modules when the
3308 // identifier is used.
3309 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3310 IdEnd = PP.getIdentifierTable().end();
3311 Id != IdEnd; ++Id)
3312 Id->second->setOutOfDate(true);
3313
3314 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003315 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3316 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003317 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3318 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003319
3320 switch (Unresolved.Kind) {
3321 case UnresolvedModuleRef::Conflict:
3322 if (ResolvedMod) {
3323 Module::Conflict Conflict;
3324 Conflict.Other = ResolvedMod;
3325 Conflict.Message = Unresolved.String.str();
3326 Unresolved.Mod->Conflicts.push_back(Conflict);
3327 }
3328 continue;
3329
3330 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003331 if (ResolvedMod)
3332 Unresolved.Mod->Imports.push_back(ResolvedMod);
3333 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003334
Douglas Gregorfb912652013-03-20 21:10:35 +00003335 case UnresolvedModuleRef::Export:
3336 if (ResolvedMod || Unresolved.IsWildcard)
3337 Unresolved.Mod->Exports.push_back(
3338 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3339 continue;
3340 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003341 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003342 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003343
3344 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3345 // Might be unnecessary as use declarations are only used to build the
3346 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003347
3348 InitializeContext();
3349
Richard Smith3d8e97e2013-10-18 06:54:39 +00003350 if (SemaObj)
3351 UpdateSema();
3352
Guy Benyei11169dd2012-12-18 14:30:41 +00003353 if (DeserializationListener)
3354 DeserializationListener->ReaderInitialized(this);
3355
3356 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3357 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3358 PrimaryModule.OriginalSourceFileID
3359 = FileID::get(PrimaryModule.SLocEntryBaseID
3360 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3361
3362 // If this AST file is a precompiled preamble, then set the
3363 // preamble file ID of the source manager to the file source file
3364 // from which the preamble was built.
3365 if (Type == MK_Preamble) {
3366 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3367 } else if (Type == MK_MainFile) {
3368 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3369 }
3370 }
3371
3372 // For any Objective-C class definitions we have already loaded, make sure
3373 // that we load any additional categories.
3374 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3375 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3376 ObjCClassesLoaded[I],
3377 PreviousGeneration);
3378 }
Douglas Gregore060e572013-01-25 01:03:03 +00003379
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003380 if (PP.getHeaderSearchInfo()
3381 .getHeaderSearchOpts()
3382 .ModulesValidateOncePerBuildSession) {
3383 // Now we are certain that the module and all modules it depends on are
3384 // up to date. Create or update timestamp files for modules that are
3385 // located in the module cache (not for PCH files that could be anywhere
3386 // in the filesystem).
3387 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3388 ImportedModule &M = Loaded[I];
3389 if (M.Mod->Kind == MK_Module) {
3390 updateModuleTimestamp(*M.Mod);
3391 }
3392 }
3393 }
3394
Guy Benyei11169dd2012-12-18 14:30:41 +00003395 return Success;
3396}
3397
3398ASTReader::ASTReadResult
3399ASTReader::ReadASTCore(StringRef FileName,
3400 ModuleKind Type,
3401 SourceLocation ImportLoc,
3402 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003403 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003404 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00003405 unsigned ClientLoadCapabilities) {
3406 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003407 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003408 ModuleManager::AddModuleResult AddResult
3409 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3410 CurrentGeneration, ExpectedSize, ExpectedModTime,
3411 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003412
Douglas Gregor7029ce12013-03-19 00:28:20 +00003413 switch (AddResult) {
3414 case ModuleManager::AlreadyLoaded:
3415 return Success;
3416
3417 case ModuleManager::NewlyLoaded:
3418 // Load module file below.
3419 break;
3420
3421 case ModuleManager::Missing:
3422 // The module file was missing; if the client handle handle, that, return
3423 // it.
3424 if (ClientLoadCapabilities & ARR_Missing)
3425 return Missing;
3426
3427 // Otherwise, return an error.
3428 {
3429 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3430 + ErrorStr;
3431 Error(Msg);
3432 }
3433 return Failure;
3434
3435 case ModuleManager::OutOfDate:
3436 // We couldn't load the module file because it is out-of-date. If the
3437 // client can handle out-of-date, return it.
3438 if (ClientLoadCapabilities & ARR_OutOfDate)
3439 return OutOfDate;
3440
3441 // Otherwise, return an error.
3442 {
3443 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3444 + ErrorStr;
3445 Error(Msg);
3446 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003447 return Failure;
3448 }
3449
Douglas Gregor7029ce12013-03-19 00:28:20 +00003450 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003451
3452 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3453 // module?
3454 if (FileName != "-") {
3455 CurrentDir = llvm::sys::path::parent_path(FileName);
3456 if (CurrentDir.empty()) CurrentDir = ".";
3457 }
3458
3459 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003460 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003461 Stream.init(F.StreamFile);
3462 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3463
3464 // Sniff for the signature.
3465 if (Stream.Read(8) != 'C' ||
3466 Stream.Read(8) != 'P' ||
3467 Stream.Read(8) != 'C' ||
3468 Stream.Read(8) != 'H') {
3469 Diag(diag::err_not_a_pch_file) << FileName;
3470 return Failure;
3471 }
3472
3473 // This is used for compatibility with older PCH formats.
3474 bool HaveReadControlBlock = false;
3475
Chris Lattnerefa77172013-01-20 00:00:22 +00003476 while (1) {
3477 llvm::BitstreamEntry Entry = Stream.advance();
3478
3479 switch (Entry.Kind) {
3480 case llvm::BitstreamEntry::Error:
3481 case llvm::BitstreamEntry::EndBlock:
3482 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003483 Error("invalid record at top-level of AST file");
3484 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003485
3486 case llvm::BitstreamEntry::SubBlock:
3487 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003488 }
3489
Guy Benyei11169dd2012-12-18 14:30:41 +00003490 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003491 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003492 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3493 if (Stream.ReadBlockInfoBlock()) {
3494 Error("malformed BlockInfoBlock in AST file");
3495 return Failure;
3496 }
3497 break;
3498 case CONTROL_BLOCK_ID:
3499 HaveReadControlBlock = true;
3500 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3501 case Success:
3502 break;
3503
3504 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003505 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003506 case OutOfDate: return OutOfDate;
3507 case VersionMismatch: return VersionMismatch;
3508 case ConfigurationMismatch: return ConfigurationMismatch;
3509 case HadErrors: return HadErrors;
3510 }
3511 break;
3512 case AST_BLOCK_ID:
3513 if (!HaveReadControlBlock) {
3514 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003515 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003516 return VersionMismatch;
3517 }
3518
3519 // Record that we've loaded this module.
3520 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3521 return Success;
3522
3523 default:
3524 if (Stream.SkipBlock()) {
3525 Error("malformed block record in AST file");
3526 return Failure;
3527 }
3528 break;
3529 }
3530 }
3531
3532 return Success;
3533}
3534
3535void ASTReader::InitializeContext() {
3536 // If there's a listener, notify them that we "read" the translation unit.
3537 if (DeserializationListener)
3538 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3539 Context.getTranslationUnitDecl());
3540
3541 // Make sure we load the declaration update records for the translation unit,
3542 // if there are any.
3543 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3544 Context.getTranslationUnitDecl());
3545
3546 // FIXME: Find a better way to deal with collisions between these
3547 // built-in types. Right now, we just ignore the problem.
3548
3549 // Load the special types.
3550 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3551 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3552 if (!Context.CFConstantStringTypeDecl)
3553 Context.setCFConstantStringType(GetType(String));
3554 }
3555
3556 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3557 QualType FileType = GetType(File);
3558 if (FileType.isNull()) {
3559 Error("FILE type is NULL");
3560 return;
3561 }
3562
3563 if (!Context.FILEDecl) {
3564 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3565 Context.setFILEDecl(Typedef->getDecl());
3566 else {
3567 const TagType *Tag = FileType->getAs<TagType>();
3568 if (!Tag) {
3569 Error("Invalid FILE type in AST file");
3570 return;
3571 }
3572 Context.setFILEDecl(Tag->getDecl());
3573 }
3574 }
3575 }
3576
3577 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3578 QualType Jmp_bufType = GetType(Jmp_buf);
3579 if (Jmp_bufType.isNull()) {
3580 Error("jmp_buf type is NULL");
3581 return;
3582 }
3583
3584 if (!Context.jmp_bufDecl) {
3585 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3586 Context.setjmp_bufDecl(Typedef->getDecl());
3587 else {
3588 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3589 if (!Tag) {
3590 Error("Invalid jmp_buf type in AST file");
3591 return;
3592 }
3593 Context.setjmp_bufDecl(Tag->getDecl());
3594 }
3595 }
3596 }
3597
3598 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3599 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3600 if (Sigjmp_bufType.isNull()) {
3601 Error("sigjmp_buf type is NULL");
3602 return;
3603 }
3604
3605 if (!Context.sigjmp_bufDecl) {
3606 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3607 Context.setsigjmp_bufDecl(Typedef->getDecl());
3608 else {
3609 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3610 assert(Tag && "Invalid sigjmp_buf type in AST file");
3611 Context.setsigjmp_bufDecl(Tag->getDecl());
3612 }
3613 }
3614 }
3615
3616 if (unsigned ObjCIdRedef
3617 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3618 if (Context.ObjCIdRedefinitionType.isNull())
3619 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3620 }
3621
3622 if (unsigned ObjCClassRedef
3623 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3624 if (Context.ObjCClassRedefinitionType.isNull())
3625 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3626 }
3627
3628 if (unsigned ObjCSelRedef
3629 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3630 if (Context.ObjCSelRedefinitionType.isNull())
3631 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3632 }
3633
3634 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3635 QualType Ucontext_tType = GetType(Ucontext_t);
3636 if (Ucontext_tType.isNull()) {
3637 Error("ucontext_t type is NULL");
3638 return;
3639 }
3640
3641 if (!Context.ucontext_tDecl) {
3642 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3643 Context.setucontext_tDecl(Typedef->getDecl());
3644 else {
3645 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3646 assert(Tag && "Invalid ucontext_t type in AST file");
3647 Context.setucontext_tDecl(Tag->getDecl());
3648 }
3649 }
3650 }
3651 }
3652
3653 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3654
3655 // If there were any CUDA special declarations, deserialize them.
3656 if (!CUDASpecialDeclRefs.empty()) {
3657 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3658 Context.setcudaConfigureCallDecl(
3659 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3660 }
3661
3662 // Re-export any modules that were imported by a non-module AST file.
3663 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3664 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003665 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregorfb912652013-03-20 21:10:35 +00003666 /*ImportLoc=*/SourceLocation(),
3667 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00003668 }
3669 ImportedModules.clear();
3670}
3671
3672void ASTReader::finalizeForWriting() {
3673 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3674 HiddenEnd = HiddenNamesMap.end();
3675 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003676 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003677 }
3678 HiddenNamesMap.clear();
3679}
3680
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003681/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3682/// cursor into the start of the given block ID, returning false on success and
3683/// true on failure.
3684static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003685 while (1) {
3686 llvm::BitstreamEntry Entry = Cursor.advance();
3687 switch (Entry.Kind) {
3688 case llvm::BitstreamEntry::Error:
3689 case llvm::BitstreamEntry::EndBlock:
3690 return true;
3691
3692 case llvm::BitstreamEntry::Record:
3693 // Ignore top-level records.
3694 Cursor.skipRecord(Entry.ID);
3695 break;
3696
3697 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003698 if (Entry.ID == BlockID) {
3699 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003700 return true;
3701 // Found it!
3702 return false;
3703 }
3704
3705 if (Cursor.SkipBlock())
3706 return true;
3707 }
3708 }
3709}
3710
Guy Benyei11169dd2012-12-18 14:30:41 +00003711/// \brief Retrieve the name of the original source file name
3712/// directly from the AST file, without actually loading the AST
3713/// file.
3714std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3715 FileManager &FileMgr,
3716 DiagnosticsEngine &Diags) {
3717 // Open the AST file.
3718 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00003719 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003720 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3721 if (!Buffer) {
3722 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3723 return std::string();
3724 }
3725
3726 // Initialize the stream
3727 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003728 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003729 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3730 (const unsigned char *)Buffer->getBufferEnd());
3731 Stream.init(StreamFile);
3732
3733 // Sniff for the signature.
3734 if (Stream.Read(8) != 'C' ||
3735 Stream.Read(8) != 'P' ||
3736 Stream.Read(8) != 'C' ||
3737 Stream.Read(8) != 'H') {
3738 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3739 return std::string();
3740 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003741
Chris Lattnere7b154b2013-01-19 21:39:22 +00003742 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003743 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003744 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3745 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003746 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003747
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003748 // Scan for ORIGINAL_FILE inside the control block.
3749 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003750 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003751 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003752 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3753 return std::string();
3754
3755 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3756 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3757 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00003758 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00003759
Guy Benyei11169dd2012-12-18 14:30:41 +00003760 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003761 StringRef Blob;
3762 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3763 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00003764 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003765}
3766
3767namespace {
3768 class SimplePCHValidator : public ASTReaderListener {
3769 const LangOptions &ExistingLangOpts;
3770 const TargetOptions &ExistingTargetOpts;
3771 const PreprocessorOptions &ExistingPPOpts;
3772 FileManager &FileMgr;
3773
3774 public:
3775 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3776 const TargetOptions &ExistingTargetOpts,
3777 const PreprocessorOptions &ExistingPPOpts,
3778 FileManager &FileMgr)
3779 : ExistingLangOpts(ExistingLangOpts),
3780 ExistingTargetOpts(ExistingTargetOpts),
3781 ExistingPPOpts(ExistingPPOpts),
3782 FileMgr(FileMgr)
3783 {
3784 }
3785
Craig Topper3e89dfe2014-03-13 02:13:41 +00003786 bool ReadLanguageOptions(const LangOptions &LangOpts,
3787 bool Complain) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003788 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3789 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00003790 bool ReadTargetOptions(const TargetOptions &TargetOpts,
3791 bool Complain) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003792 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3793 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00003794 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3795 bool Complain,
3796 std::string &SuggestedPredefines) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003797 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00003798 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00003799 }
3800 };
3801}
3802
3803bool ASTReader::readASTFileControlBlock(StringRef Filename,
3804 FileManager &FileMgr,
3805 ASTReaderListener &Listener) {
3806 // Open the AST file.
3807 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00003808 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003809 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3810 if (!Buffer) {
3811 return true;
3812 }
3813
3814 // Initialize the stream
3815 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003816 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003817 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3818 (const unsigned char *)Buffer->getBufferEnd());
3819 Stream.init(StreamFile);
3820
3821 // Sniff for the signature.
3822 if (Stream.Read(8) != 'C' ||
3823 Stream.Read(8) != 'P' ||
3824 Stream.Read(8) != 'C' ||
3825 Stream.Read(8) != 'H') {
3826 return true;
3827 }
3828
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003829 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003830 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003831 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003832
3833 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00003834 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003835 BitstreamCursor InputFilesCursor;
3836 if (NeedsInputFiles) {
3837 InputFilesCursor = Stream;
3838 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3839 return true;
3840
3841 // Read the abbreviations
3842 while (true) {
3843 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3844 unsigned Code = InputFilesCursor.ReadCode();
3845
3846 // We expect all abbrevs to be at the start of the block.
3847 if (Code != llvm::bitc::DEFINE_ABBREV) {
3848 InputFilesCursor.JumpToBit(Offset);
3849 break;
3850 }
3851 InputFilesCursor.ReadAbbrevRecord();
3852 }
3853 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003854
3855 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00003856 RecordData Record;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003857 while (1) {
3858 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3859 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3860 return false;
3861
3862 if (Entry.Kind != llvm::BitstreamEntry::Record)
3863 return true;
3864
Guy Benyei11169dd2012-12-18 14:30:41 +00003865 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003866 StringRef Blob;
3867 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003868 switch ((ControlRecordTypes)RecCode) {
3869 case METADATA: {
3870 if (Record[0] != VERSION_MAJOR)
3871 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003872
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003873 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003874 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003875
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003876 break;
3877 }
3878 case LANGUAGE_OPTIONS:
3879 if (ParseLanguageOptions(Record, false, Listener))
3880 return true;
3881 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003882
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003883 case TARGET_OPTIONS:
3884 if (ParseTargetOptions(Record, false, Listener))
3885 return true;
3886 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003887
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003888 case DIAGNOSTIC_OPTIONS:
3889 if (ParseDiagnosticOptions(Record, false, Listener))
3890 return true;
3891 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003892
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003893 case FILE_SYSTEM_OPTIONS:
3894 if (ParseFileSystemOptions(Record, false, Listener))
3895 return true;
3896 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003897
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003898 case HEADER_SEARCH_OPTIONS:
3899 if (ParseHeaderSearchOptions(Record, false, Listener))
3900 return true;
3901 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003902
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003903 case PREPROCESSOR_OPTIONS: {
3904 std::string IgnoredSuggestedPredefines;
3905 if (ParsePreprocessorOptions(Record, false, Listener,
3906 IgnoredSuggestedPredefines))
3907 return true;
3908 break;
3909 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003910
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003911 case INPUT_FILE_OFFSETS: {
3912 if (!NeedsInputFiles)
3913 break;
3914
3915 unsigned NumInputFiles = Record[0];
3916 unsigned NumUserFiles = Record[1];
3917 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3918 for (unsigned I = 0; I != NumInputFiles; ++I) {
3919 // Go find this input file.
3920 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00003921
3922 if (isSystemFile && !NeedsSystemInputFiles)
3923 break; // the rest are system input files
3924
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003925 BitstreamCursor &Cursor = InputFilesCursor;
3926 SavedStreamPosition SavedPosition(Cursor);
3927 Cursor.JumpToBit(InputFileOffs[I]);
3928
3929 unsigned Code = Cursor.ReadCode();
3930 RecordData Record;
3931 StringRef Blob;
3932 bool shouldContinue = false;
3933 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3934 case INPUT_FILE:
3935 shouldContinue = Listener.visitInputFile(Blob, isSystemFile);
3936 break;
3937 }
3938 if (!shouldContinue)
3939 break;
3940 }
3941 break;
3942 }
3943
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003944 default:
3945 // No other validation to perform.
3946 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003947 }
3948 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003949}
3950
3951
3952bool ASTReader::isAcceptableASTFile(StringRef Filename,
3953 FileManager &FileMgr,
3954 const LangOptions &LangOpts,
3955 const TargetOptions &TargetOpts,
3956 const PreprocessorOptions &PPOpts) {
3957 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3958 return !readASTFileControlBlock(Filename, FileMgr, validator);
3959}
3960
3961bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3962 // Enter the submodule block.
3963 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3964 Error("malformed submodule block record in AST file");
3965 return true;
3966 }
3967
3968 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3969 bool First = true;
3970 Module *CurrentModule = 0;
3971 RecordData Record;
3972 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003973 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3974
3975 switch (Entry.Kind) {
3976 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3977 case llvm::BitstreamEntry::Error:
3978 Error("malformed block record in AST file");
3979 return true;
3980 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003981 return false;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003982 case llvm::BitstreamEntry::Record:
3983 // The interesting case.
3984 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003985 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003986
Guy Benyei11169dd2012-12-18 14:30:41 +00003987 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00003988 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00003989 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003990 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003991 default: // Default behavior: ignore.
3992 break;
3993
3994 case SUBMODULE_DEFINITION: {
3995 if (First) {
3996 Error("missing submodule metadata record at beginning of block");
3997 return true;
3998 }
3999
Douglas Gregor8d932422013-03-20 03:59:18 +00004000 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004001 Error("malformed module definition");
4002 return true;
4003 }
4004
Chris Lattner0e6c9402013-01-20 02:38:54 +00004005 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004006 unsigned Idx = 0;
4007 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4008 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4009 bool IsFramework = Record[Idx++];
4010 bool IsExplicit = Record[Idx++];
4011 bool IsSystem = Record[Idx++];
4012 bool IsExternC = Record[Idx++];
4013 bool InferSubmodules = Record[Idx++];
4014 bool InferExplicitSubmodules = Record[Idx++];
4015 bool InferExportWildcard = Record[Idx++];
4016 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004017
Guy Benyei11169dd2012-12-18 14:30:41 +00004018 Module *ParentModule = 0;
4019 if (Parent)
4020 ParentModule = getSubmodule(Parent);
4021
4022 // Retrieve this (sub)module from the module map, creating it if
4023 // necessary.
4024 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
4025 IsFramework,
4026 IsExplicit).first;
4027 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4028 if (GlobalIndex >= SubmodulesLoaded.size() ||
4029 SubmodulesLoaded[GlobalIndex]) {
4030 Error("too many submodules");
4031 return true;
4032 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004033
Douglas Gregor7029ce12013-03-19 00:28:20 +00004034 if (!ParentModule) {
4035 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4036 if (CurFile != F.File) {
4037 if (!Diags.isDiagnosticInFlight()) {
4038 Diag(diag::err_module_file_conflict)
4039 << CurrentModule->getTopLevelModuleName()
4040 << CurFile->getName()
4041 << F.File->getName();
4042 }
4043 return true;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004044 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004045 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004046
4047 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004048 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004049
Guy Benyei11169dd2012-12-18 14:30:41 +00004050 CurrentModule->IsFromModuleFile = true;
4051 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004052 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004053 CurrentModule->InferSubmodules = InferSubmodules;
4054 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4055 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004056 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004057 if (DeserializationListener)
4058 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4059
4060 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004061
Douglas Gregorfb912652013-03-20 21:10:35 +00004062 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004063 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004064 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004065 CurrentModule->UnresolvedConflicts.clear();
4066 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004067 break;
4068 }
4069
4070 case SUBMODULE_UMBRELLA_HEADER: {
4071 if (First) {
4072 Error("missing submodule metadata record at beginning of block");
4073 return true;
4074 }
4075
4076 if (!CurrentModule)
4077 break;
4078
Chris Lattner0e6c9402013-01-20 02:38:54 +00004079 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004080 if (!CurrentModule->getUmbrellaHeader())
4081 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4082 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
4083 Error("mismatched umbrella headers in submodule");
4084 return true;
4085 }
4086 }
4087 break;
4088 }
4089
4090 case SUBMODULE_HEADER: {
4091 if (First) {
4092 Error("missing submodule metadata record at beginning of block");
4093 return true;
4094 }
4095
4096 if (!CurrentModule)
4097 break;
4098
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004099 // We lazily associate headers with their modules via the HeaderInfoTable.
4100 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4101 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004102 break;
4103 }
4104
4105 case SUBMODULE_EXCLUDED_HEADER: {
4106 if (First) {
4107 Error("missing submodule metadata record at beginning of block");
4108 return true;
4109 }
4110
4111 if (!CurrentModule)
4112 break;
4113
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004114 // We lazily associate headers with their modules via the HeaderInfoTable.
4115 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4116 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004117 break;
4118 }
4119
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004120 case SUBMODULE_PRIVATE_HEADER: {
4121 if (First) {
4122 Error("missing submodule metadata record at beginning of block");
4123 return true;
4124 }
4125
4126 if (!CurrentModule)
4127 break;
4128
4129 // We lazily associate headers with their modules via the HeaderInfoTable.
4130 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4131 // of complete filenames or remove it entirely.
4132 break;
4133 }
4134
Guy Benyei11169dd2012-12-18 14:30:41 +00004135 case SUBMODULE_TOPHEADER: {
4136 if (First) {
4137 Error("missing submodule metadata record at beginning of block");
4138 return true;
4139 }
4140
4141 if (!CurrentModule)
4142 break;
4143
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004144 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004145 break;
4146 }
4147
4148 case SUBMODULE_UMBRELLA_DIR: {
4149 if (First) {
4150 Error("missing submodule metadata record at beginning of block");
4151 return true;
4152 }
4153
4154 if (!CurrentModule)
4155 break;
4156
Guy Benyei11169dd2012-12-18 14:30:41 +00004157 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00004158 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004159 if (!CurrentModule->getUmbrellaDir())
4160 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4161 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
4162 Error("mismatched umbrella directories in submodule");
4163 return true;
4164 }
4165 }
4166 break;
4167 }
4168
4169 case SUBMODULE_METADATA: {
4170 if (!First) {
4171 Error("submodule metadata record not at beginning of block");
4172 return true;
4173 }
4174 First = false;
4175
4176 F.BaseSubmoduleID = getTotalNumSubmodules();
4177 F.LocalNumSubmodules = Record[0];
4178 unsigned LocalBaseSubmoduleID = Record[1];
4179 if (F.LocalNumSubmodules > 0) {
4180 // Introduce the global -> local mapping for submodules within this
4181 // module.
4182 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4183
4184 // Introduce the local -> global mapping for submodules within this
4185 // module.
4186 F.SubmoduleRemap.insertOrReplace(
4187 std::make_pair(LocalBaseSubmoduleID,
4188 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4189
4190 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4191 }
4192 break;
4193 }
4194
4195 case SUBMODULE_IMPORTS: {
4196 if (First) {
4197 Error("missing submodule metadata record at beginning of block");
4198 return true;
4199 }
4200
4201 if (!CurrentModule)
4202 break;
4203
4204 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004205 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004206 Unresolved.File = &F;
4207 Unresolved.Mod = CurrentModule;
4208 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004209 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004210 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004211 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004212 }
4213 break;
4214 }
4215
4216 case SUBMODULE_EXPORTS: {
4217 if (First) {
4218 Error("missing submodule metadata record at beginning of block");
4219 return true;
4220 }
4221
4222 if (!CurrentModule)
4223 break;
4224
4225 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004226 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004227 Unresolved.File = &F;
4228 Unresolved.Mod = CurrentModule;
4229 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004230 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004231 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004232 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004233 }
4234
4235 // Once we've loaded the set of exports, there's no reason to keep
4236 // the parsed, unresolved exports around.
4237 CurrentModule->UnresolvedExports.clear();
4238 break;
4239 }
4240 case SUBMODULE_REQUIRES: {
4241 if (First) {
4242 Error("missing submodule metadata record at beginning of block");
4243 return true;
4244 }
4245
4246 if (!CurrentModule)
4247 break;
4248
Richard Smitha3feee22013-10-28 22:18:19 +00004249 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004250 Context.getTargetInfo());
4251 break;
4252 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004253
4254 case SUBMODULE_LINK_LIBRARY:
4255 if (First) {
4256 Error("missing submodule metadata record at beginning of block");
4257 return true;
4258 }
4259
4260 if (!CurrentModule)
4261 break;
4262
4263 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004264 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004265 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004266
4267 case SUBMODULE_CONFIG_MACRO:
4268 if (First) {
4269 Error("missing submodule metadata record at beginning of block");
4270 return true;
4271 }
4272
4273 if (!CurrentModule)
4274 break;
4275
4276 CurrentModule->ConfigMacros.push_back(Blob.str());
4277 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004278
4279 case SUBMODULE_CONFLICT: {
4280 if (First) {
4281 Error("missing submodule metadata record at beginning of block");
4282 return true;
4283 }
4284
4285 if (!CurrentModule)
4286 break;
4287
4288 UnresolvedModuleRef Unresolved;
4289 Unresolved.File = &F;
4290 Unresolved.Mod = CurrentModule;
4291 Unresolved.ID = Record[0];
4292 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4293 Unresolved.IsWildcard = false;
4294 Unresolved.String = Blob;
4295 UnresolvedModuleRefs.push_back(Unresolved);
4296 break;
4297 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004298 }
4299 }
4300}
4301
4302/// \brief Parse the record that corresponds to a LangOptions data
4303/// structure.
4304///
4305/// This routine parses the language options from the AST file and then gives
4306/// them to the AST listener if one is set.
4307///
4308/// \returns true if the listener deems the file unacceptable, false otherwise.
4309bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4310 bool Complain,
4311 ASTReaderListener &Listener) {
4312 LangOptions LangOpts;
4313 unsigned Idx = 0;
4314#define LANGOPT(Name, Bits, Default, Description) \
4315 LangOpts.Name = Record[Idx++];
4316#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4317 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4318#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00004319#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4320#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004321
4322 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4323 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4324 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4325
4326 unsigned Length = Record[Idx++];
4327 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4328 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004329
4330 Idx += Length;
4331
4332 // Comment options.
4333 for (unsigned N = Record[Idx++]; N; --N) {
4334 LangOpts.CommentOpts.BlockCommandNames.push_back(
4335 ReadString(Record, Idx));
4336 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004337 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004338
Guy Benyei11169dd2012-12-18 14:30:41 +00004339 return Listener.ReadLanguageOptions(LangOpts, Complain);
4340}
4341
4342bool ASTReader::ParseTargetOptions(const RecordData &Record,
4343 bool Complain,
4344 ASTReaderListener &Listener) {
4345 unsigned Idx = 0;
4346 TargetOptions TargetOpts;
4347 TargetOpts.Triple = ReadString(Record, Idx);
4348 TargetOpts.CPU = ReadString(Record, Idx);
4349 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004350 TargetOpts.LinkerVersion = ReadString(Record, Idx);
4351 for (unsigned N = Record[Idx++]; N; --N) {
4352 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4353 }
4354 for (unsigned N = Record[Idx++]; N; --N) {
4355 TargetOpts.Features.push_back(ReadString(Record, Idx));
4356 }
4357
4358 return Listener.ReadTargetOptions(TargetOpts, Complain);
4359}
4360
4361bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4362 ASTReaderListener &Listener) {
4363 DiagnosticOptions DiagOpts;
4364 unsigned Idx = 0;
4365#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
4366#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4367 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4368#include "clang/Basic/DiagnosticOptions.def"
4369
4370 for (unsigned N = Record[Idx++]; N; --N) {
4371 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4372 }
4373
4374 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4375}
4376
4377bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4378 ASTReaderListener &Listener) {
4379 FileSystemOptions FSOpts;
4380 unsigned Idx = 0;
4381 FSOpts.WorkingDir = ReadString(Record, Idx);
4382 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4383}
4384
4385bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4386 bool Complain,
4387 ASTReaderListener &Listener) {
4388 HeaderSearchOptions HSOpts;
4389 unsigned Idx = 0;
4390 HSOpts.Sysroot = ReadString(Record, Idx);
4391
4392 // Include entries.
4393 for (unsigned N = Record[Idx++]; N; --N) {
4394 std::string Path = ReadString(Record, Idx);
4395 frontend::IncludeDirGroup Group
4396 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004397 bool IsFramework = Record[Idx++];
4398 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004399 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00004400 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00004401 }
4402
4403 // System header prefixes.
4404 for (unsigned N = Record[Idx++]; N; --N) {
4405 std::string Prefix = ReadString(Record, Idx);
4406 bool IsSystemHeader = Record[Idx++];
4407 HSOpts.SystemHeaderPrefixes.push_back(
4408 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4409 }
4410
4411 HSOpts.ResourceDir = ReadString(Record, Idx);
4412 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004413 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004414 HSOpts.DisableModuleHash = Record[Idx++];
4415 HSOpts.UseBuiltinIncludes = Record[Idx++];
4416 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4417 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4418 HSOpts.UseLibcxx = Record[Idx++];
4419
4420 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4421}
4422
4423bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4424 bool Complain,
4425 ASTReaderListener &Listener,
4426 std::string &SuggestedPredefines) {
4427 PreprocessorOptions PPOpts;
4428 unsigned Idx = 0;
4429
4430 // Macro definitions/undefs
4431 for (unsigned N = Record[Idx++]; N; --N) {
4432 std::string Macro = ReadString(Record, Idx);
4433 bool IsUndef = Record[Idx++];
4434 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4435 }
4436
4437 // Includes
4438 for (unsigned N = Record[Idx++]; N; --N) {
4439 PPOpts.Includes.push_back(ReadString(Record, Idx));
4440 }
4441
4442 // Macro Includes
4443 for (unsigned N = Record[Idx++]; N; --N) {
4444 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4445 }
4446
4447 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004448 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004449 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4450 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4451 PPOpts.ObjCXXARCStandardLibrary =
4452 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4453 SuggestedPredefines.clear();
4454 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4455 SuggestedPredefines);
4456}
4457
4458std::pair<ModuleFile *, unsigned>
4459ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4460 GlobalPreprocessedEntityMapType::iterator
4461 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4462 assert(I != GlobalPreprocessedEntityMap.end() &&
4463 "Corrupted global preprocessed entity map");
4464 ModuleFile *M = I->second;
4465 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4466 return std::make_pair(M, LocalIndex);
4467}
4468
4469std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4470ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4471 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4472 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4473 Mod.NumPreprocessedEntities);
4474
4475 return std::make_pair(PreprocessingRecord::iterator(),
4476 PreprocessingRecord::iterator());
4477}
4478
4479std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4480ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4481 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4482 ModuleDeclIterator(this, &Mod,
4483 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4484}
4485
4486PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4487 PreprocessedEntityID PPID = Index+1;
4488 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4489 ModuleFile &M = *PPInfo.first;
4490 unsigned LocalIndex = PPInfo.second;
4491 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4492
Guy Benyei11169dd2012-12-18 14:30:41 +00004493 if (!PP.getPreprocessingRecord()) {
4494 Error("no preprocessing record");
4495 return 0;
4496 }
4497
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004498 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4499 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4500
4501 llvm::BitstreamEntry Entry =
4502 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4503 if (Entry.Kind != llvm::BitstreamEntry::Record)
4504 return 0;
4505
Guy Benyei11169dd2012-12-18 14:30:41 +00004506 // Read the record.
4507 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4508 ReadSourceLocation(M, PPOffs.End));
4509 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004510 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004511 RecordData Record;
4512 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004513 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4514 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004515 switch (RecType) {
4516 case PPD_MACRO_EXPANSION: {
4517 bool isBuiltin = Record[0];
4518 IdentifierInfo *Name = 0;
4519 MacroDefinition *Def = 0;
4520 if (isBuiltin)
4521 Name = getLocalIdentifier(M, Record[1]);
4522 else {
4523 PreprocessedEntityID
4524 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4525 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4526 }
4527
4528 MacroExpansion *ME;
4529 if (isBuiltin)
4530 ME = new (PPRec) MacroExpansion(Name, Range);
4531 else
4532 ME = new (PPRec) MacroExpansion(Def, Range);
4533
4534 return ME;
4535 }
4536
4537 case PPD_MACRO_DEFINITION: {
4538 // Decode the identifier info and then check again; if the macro is
4539 // still defined and associated with the identifier,
4540 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4541 MacroDefinition *MD
4542 = new (PPRec) MacroDefinition(II, Range);
4543
4544 if (DeserializationListener)
4545 DeserializationListener->MacroDefinitionRead(PPID, MD);
4546
4547 return MD;
4548 }
4549
4550 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004551 const char *FullFileNameStart = Blob.data() + Record[0];
4552 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004553 const FileEntry *File = 0;
4554 if (!FullFileName.empty())
4555 File = PP.getFileManager().getFile(FullFileName);
4556
4557 // FIXME: Stable encoding
4558 InclusionDirective::InclusionKind Kind
4559 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4560 InclusionDirective *ID
4561 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004562 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004563 Record[1], Record[3],
4564 File,
4565 Range);
4566 return ID;
4567 }
4568 }
4569
4570 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4571}
4572
4573/// \brief \arg SLocMapI points at a chunk of a module that contains no
4574/// preprocessed entities or the entities it contains are not the ones we are
4575/// looking for. Find the next module that contains entities and return the ID
4576/// of the first entry.
4577PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4578 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4579 ++SLocMapI;
4580 for (GlobalSLocOffsetMapType::const_iterator
4581 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4582 ModuleFile &M = *SLocMapI->second;
4583 if (M.NumPreprocessedEntities)
4584 return M.BasePreprocessedEntityID;
4585 }
4586
4587 return getTotalNumPreprocessedEntities();
4588}
4589
4590namespace {
4591
4592template <unsigned PPEntityOffset::*PPLoc>
4593struct PPEntityComp {
4594 const ASTReader &Reader;
4595 ModuleFile &M;
4596
4597 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4598
4599 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4600 SourceLocation LHS = getLoc(L);
4601 SourceLocation RHS = getLoc(R);
4602 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4603 }
4604
4605 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4606 SourceLocation LHS = getLoc(L);
4607 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4608 }
4609
4610 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4611 SourceLocation RHS = getLoc(R);
4612 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4613 }
4614
4615 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4616 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4617 }
4618};
4619
4620}
4621
4622/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4623PreprocessedEntityID
4624ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4625 if (SourceMgr.isLocalSourceLocation(BLoc))
4626 return getTotalNumPreprocessedEntities();
4627
4628 GlobalSLocOffsetMapType::const_iterator
4629 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004630 BLoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004631 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4632 "Corrupted global sloc offset map");
4633
4634 if (SLocMapI->second->NumPreprocessedEntities == 0)
4635 return findNextPreprocessedEntity(SLocMapI);
4636
4637 ModuleFile &M = *SLocMapI->second;
4638 typedef const PPEntityOffset *pp_iterator;
4639 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4640 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4641
4642 size_t Count = M.NumPreprocessedEntities;
4643 size_t Half;
4644 pp_iterator First = pp_begin;
4645 pp_iterator PPI;
4646
4647 // Do a binary search manually instead of using std::lower_bound because
4648 // The end locations of entities may be unordered (when a macro expansion
4649 // is inside another macro argument), but for this case it is not important
4650 // whether we get the first macro expansion or its containing macro.
4651 while (Count > 0) {
4652 Half = Count/2;
4653 PPI = First;
4654 std::advance(PPI, Half);
4655 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4656 BLoc)){
4657 First = PPI;
4658 ++First;
4659 Count = Count - Half - 1;
4660 } else
4661 Count = Half;
4662 }
4663
4664 if (PPI == pp_end)
4665 return findNextPreprocessedEntity(SLocMapI);
4666
4667 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4668}
4669
4670/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4671PreprocessedEntityID
4672ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4673 if (SourceMgr.isLocalSourceLocation(ELoc))
4674 return getTotalNumPreprocessedEntities();
4675
4676 GlobalSLocOffsetMapType::const_iterator
4677 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004678 ELoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004679 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4680 "Corrupted global sloc offset map");
4681
4682 if (SLocMapI->second->NumPreprocessedEntities == 0)
4683 return findNextPreprocessedEntity(SLocMapI);
4684
4685 ModuleFile &M = *SLocMapI->second;
4686 typedef const PPEntityOffset *pp_iterator;
4687 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4688 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4689 pp_iterator PPI =
4690 std::upper_bound(pp_begin, pp_end, ELoc,
4691 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4692
4693 if (PPI == pp_end)
4694 return findNextPreprocessedEntity(SLocMapI);
4695
4696 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4697}
4698
4699/// \brief Returns a pair of [Begin, End) indices of preallocated
4700/// preprocessed entities that \arg Range encompasses.
4701std::pair<unsigned, unsigned>
4702 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4703 if (Range.isInvalid())
4704 return std::make_pair(0,0);
4705 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4706
4707 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4708 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4709 return std::make_pair(BeginID, EndID);
4710}
4711
4712/// \brief Optionally returns true or false if the preallocated preprocessed
4713/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00004714Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00004715 FileID FID) {
4716 if (FID.isInvalid())
4717 return false;
4718
4719 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4720 ModuleFile &M = *PPInfo.first;
4721 unsigned LocalIndex = PPInfo.second;
4722 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4723
4724 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4725 if (Loc.isInvalid())
4726 return false;
4727
4728 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4729 return true;
4730 else
4731 return false;
4732}
4733
4734namespace {
4735 /// \brief Visitor used to search for information about a header file.
4736 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00004737 const FileEntry *FE;
4738
David Blaikie05785d12013-02-20 22:23:23 +00004739 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004740
4741 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004742 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4743 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00004744
4745 static bool visit(ModuleFile &M, void *UserData) {
4746 HeaderFileInfoVisitor *This
4747 = static_cast<HeaderFileInfoVisitor *>(UserData);
4748
Guy Benyei11169dd2012-12-18 14:30:41 +00004749 HeaderFileInfoLookupTable *Table
4750 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4751 if (!Table)
4752 return false;
4753
4754 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00004755 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004756 if (Pos == Table->end())
4757 return false;
4758
4759 This->HFI = *Pos;
4760 return true;
4761 }
4762
David Blaikie05785d12013-02-20 22:23:23 +00004763 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00004764 };
4765}
4766
4767HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004768 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004769 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00004770 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00004771 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004772
4773 return HeaderFileInfo();
4774}
4775
4776void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4777 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004778 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00004779 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4780 ModuleFile &F = *(*I);
4781 unsigned Idx = 0;
4782 DiagStates.clear();
4783 assert(!Diag.DiagStates.empty());
4784 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4785 while (Idx < F.PragmaDiagMappings.size()) {
4786 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4787 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4788 if (DiagStateID != 0) {
4789 Diag.DiagStatePoints.push_back(
4790 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4791 FullSourceLoc(Loc, SourceMgr)));
4792 continue;
4793 }
4794
4795 assert(DiagStateID == 0);
4796 // A new DiagState was created here.
4797 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4798 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4799 DiagStates.push_back(NewState);
4800 Diag.DiagStatePoints.push_back(
4801 DiagnosticsEngine::DiagStatePoint(NewState,
4802 FullSourceLoc(Loc, SourceMgr)));
4803 while (1) {
4804 assert(Idx < F.PragmaDiagMappings.size() &&
4805 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4806 if (Idx >= F.PragmaDiagMappings.size()) {
4807 break; // Something is messed up but at least avoid infinite loop in
4808 // release build.
4809 }
4810 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4811 if (DiagID == (unsigned)-1) {
4812 break; // no more diag/map pairs for this location.
4813 }
4814 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4815 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4816 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4817 }
4818 }
4819 }
4820}
4821
4822/// \brief Get the correct cursor and offset for loading a type.
4823ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4824 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4825 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4826 ModuleFile *M = I->second;
4827 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4828}
4829
4830/// \brief Read and return the type with the given index..
4831///
4832/// The index is the type ID, shifted and minus the number of predefs. This
4833/// routine actually reads the record corresponding to the type at the given
4834/// location. It is a helper routine for GetType, which deals with reading type
4835/// IDs.
4836QualType ASTReader::readTypeRecord(unsigned Index) {
4837 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004838 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00004839
4840 // Keep track of where we are in the stream, then jump back there
4841 // after reading this type.
4842 SavedStreamPosition SavedPosition(DeclsCursor);
4843
4844 ReadingKindTracker ReadingKind(Read_Type, *this);
4845
4846 // Note that we are loading a type record.
4847 Deserializing AType(this);
4848
4849 unsigned Idx = 0;
4850 DeclsCursor.JumpToBit(Loc.Offset);
4851 RecordData Record;
4852 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004853 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004854 case TYPE_EXT_QUAL: {
4855 if (Record.size() != 2) {
4856 Error("Incorrect encoding of extended qualifier type");
4857 return QualType();
4858 }
4859 QualType Base = readType(*Loc.F, Record, Idx);
4860 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4861 return Context.getQualifiedType(Base, Quals);
4862 }
4863
4864 case TYPE_COMPLEX: {
4865 if (Record.size() != 1) {
4866 Error("Incorrect encoding of complex type");
4867 return QualType();
4868 }
4869 QualType ElemType = readType(*Loc.F, Record, Idx);
4870 return Context.getComplexType(ElemType);
4871 }
4872
4873 case TYPE_POINTER: {
4874 if (Record.size() != 1) {
4875 Error("Incorrect encoding of pointer type");
4876 return QualType();
4877 }
4878 QualType PointeeType = readType(*Loc.F, Record, Idx);
4879 return Context.getPointerType(PointeeType);
4880 }
4881
Reid Kleckner8a365022013-06-24 17:51:48 +00004882 case TYPE_DECAYED: {
4883 if (Record.size() != 1) {
4884 Error("Incorrect encoding of decayed type");
4885 return QualType();
4886 }
4887 QualType OriginalType = readType(*Loc.F, Record, Idx);
4888 QualType DT = Context.getAdjustedParameterType(OriginalType);
4889 if (!isa<DecayedType>(DT))
4890 Error("Decayed type does not decay");
4891 return DT;
4892 }
4893
Reid Kleckner0503a872013-12-05 01:23:43 +00004894 case TYPE_ADJUSTED: {
4895 if (Record.size() != 2) {
4896 Error("Incorrect encoding of adjusted type");
4897 return QualType();
4898 }
4899 QualType OriginalTy = readType(*Loc.F, Record, Idx);
4900 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
4901 return Context.getAdjustedType(OriginalTy, AdjustedTy);
4902 }
4903
Guy Benyei11169dd2012-12-18 14:30:41 +00004904 case TYPE_BLOCK_POINTER: {
4905 if (Record.size() != 1) {
4906 Error("Incorrect encoding of block pointer type");
4907 return QualType();
4908 }
4909 QualType PointeeType = readType(*Loc.F, Record, Idx);
4910 return Context.getBlockPointerType(PointeeType);
4911 }
4912
4913 case TYPE_LVALUE_REFERENCE: {
4914 if (Record.size() != 2) {
4915 Error("Incorrect encoding of lvalue reference type");
4916 return QualType();
4917 }
4918 QualType PointeeType = readType(*Loc.F, Record, Idx);
4919 return Context.getLValueReferenceType(PointeeType, Record[1]);
4920 }
4921
4922 case TYPE_RVALUE_REFERENCE: {
4923 if (Record.size() != 1) {
4924 Error("Incorrect encoding of rvalue reference type");
4925 return QualType();
4926 }
4927 QualType PointeeType = readType(*Loc.F, Record, Idx);
4928 return Context.getRValueReferenceType(PointeeType);
4929 }
4930
4931 case TYPE_MEMBER_POINTER: {
4932 if (Record.size() != 2) {
4933 Error("Incorrect encoding of member pointer type");
4934 return QualType();
4935 }
4936 QualType PointeeType = readType(*Loc.F, Record, Idx);
4937 QualType ClassType = readType(*Loc.F, Record, Idx);
4938 if (PointeeType.isNull() || ClassType.isNull())
4939 return QualType();
4940
4941 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4942 }
4943
4944 case TYPE_CONSTANT_ARRAY: {
4945 QualType ElementType = readType(*Loc.F, Record, Idx);
4946 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4947 unsigned IndexTypeQuals = Record[2];
4948 unsigned Idx = 3;
4949 llvm::APInt Size = ReadAPInt(Record, Idx);
4950 return Context.getConstantArrayType(ElementType, Size,
4951 ASM, IndexTypeQuals);
4952 }
4953
4954 case TYPE_INCOMPLETE_ARRAY: {
4955 QualType ElementType = readType(*Loc.F, Record, Idx);
4956 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4957 unsigned IndexTypeQuals = Record[2];
4958 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4959 }
4960
4961 case TYPE_VARIABLE_ARRAY: {
4962 QualType ElementType = readType(*Loc.F, Record, Idx);
4963 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4964 unsigned IndexTypeQuals = Record[2];
4965 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4966 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4967 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4968 ASM, IndexTypeQuals,
4969 SourceRange(LBLoc, RBLoc));
4970 }
4971
4972 case TYPE_VECTOR: {
4973 if (Record.size() != 3) {
4974 Error("incorrect encoding of vector type in AST file");
4975 return QualType();
4976 }
4977
4978 QualType ElementType = readType(*Loc.F, Record, Idx);
4979 unsigned NumElements = Record[1];
4980 unsigned VecKind = Record[2];
4981 return Context.getVectorType(ElementType, NumElements,
4982 (VectorType::VectorKind)VecKind);
4983 }
4984
4985 case TYPE_EXT_VECTOR: {
4986 if (Record.size() != 3) {
4987 Error("incorrect encoding of extended vector type in AST file");
4988 return QualType();
4989 }
4990
4991 QualType ElementType = readType(*Loc.F, Record, Idx);
4992 unsigned NumElements = Record[1];
4993 return Context.getExtVectorType(ElementType, NumElements);
4994 }
4995
4996 case TYPE_FUNCTION_NO_PROTO: {
4997 if (Record.size() != 6) {
4998 Error("incorrect encoding of no-proto function type");
4999 return QualType();
5000 }
5001 QualType ResultType = readType(*Loc.F, Record, Idx);
5002 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5003 (CallingConv)Record[4], Record[5]);
5004 return Context.getFunctionNoProtoType(ResultType, Info);
5005 }
5006
5007 case TYPE_FUNCTION_PROTO: {
5008 QualType ResultType = readType(*Loc.F, Record, Idx);
5009
5010 FunctionProtoType::ExtProtoInfo EPI;
5011 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5012 /*hasregparm*/ Record[2],
5013 /*regparm*/ Record[3],
5014 static_cast<CallingConv>(Record[4]),
5015 /*produces*/ Record[5]);
5016
5017 unsigned Idx = 6;
5018 unsigned NumParams = Record[Idx++];
5019 SmallVector<QualType, 16> ParamTypes;
5020 for (unsigned I = 0; I != NumParams; ++I)
5021 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5022
5023 EPI.Variadic = Record[Idx++];
5024 EPI.HasTrailingReturn = Record[Idx++];
5025 EPI.TypeQuals = Record[Idx++];
5026 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5027 ExceptionSpecificationType EST =
5028 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5029 EPI.ExceptionSpecType = EST;
5030 SmallVector<QualType, 2> Exceptions;
5031 if (EST == EST_Dynamic) {
5032 EPI.NumExceptions = Record[Idx++];
5033 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
5034 Exceptions.push_back(readType(*Loc.F, Record, Idx));
5035 EPI.Exceptions = Exceptions.data();
5036 } else if (EST == EST_ComputedNoexcept) {
5037 EPI.NoexceptExpr = ReadExpr(*Loc.F);
5038 } else if (EST == EST_Uninstantiated) {
5039 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
5040 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
5041 } else if (EST == EST_Unevaluated) {
5042 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
5043 }
Jordan Rose5c382722013-03-08 21:51:21 +00005044 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005045 }
5046
5047 case TYPE_UNRESOLVED_USING: {
5048 unsigned Idx = 0;
5049 return Context.getTypeDeclType(
5050 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5051 }
5052
5053 case TYPE_TYPEDEF: {
5054 if (Record.size() != 2) {
5055 Error("incorrect encoding of typedef type");
5056 return QualType();
5057 }
5058 unsigned Idx = 0;
5059 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5060 QualType Canonical = readType(*Loc.F, Record, Idx);
5061 if (!Canonical.isNull())
5062 Canonical = Context.getCanonicalType(Canonical);
5063 return Context.getTypedefType(Decl, Canonical);
5064 }
5065
5066 case TYPE_TYPEOF_EXPR:
5067 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5068
5069 case TYPE_TYPEOF: {
5070 if (Record.size() != 1) {
5071 Error("incorrect encoding of typeof(type) in AST file");
5072 return QualType();
5073 }
5074 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5075 return Context.getTypeOfType(UnderlyingType);
5076 }
5077
5078 case TYPE_DECLTYPE: {
5079 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5080 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5081 }
5082
5083 case TYPE_UNARY_TRANSFORM: {
5084 QualType BaseType = readType(*Loc.F, Record, Idx);
5085 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5086 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5087 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5088 }
5089
Richard Smith74aeef52013-04-26 16:15:35 +00005090 case TYPE_AUTO: {
5091 QualType Deduced = readType(*Loc.F, Record, Idx);
5092 bool IsDecltypeAuto = Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005093 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005094 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005095 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005096
5097 case TYPE_RECORD: {
5098 if (Record.size() != 2) {
5099 Error("incorrect encoding of record type");
5100 return QualType();
5101 }
5102 unsigned Idx = 0;
5103 bool IsDependent = Record[Idx++];
5104 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5105 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5106 QualType T = Context.getRecordType(RD);
5107 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5108 return T;
5109 }
5110
5111 case TYPE_ENUM: {
5112 if (Record.size() != 2) {
5113 Error("incorrect encoding of enum type");
5114 return QualType();
5115 }
5116 unsigned Idx = 0;
5117 bool IsDependent = Record[Idx++];
5118 QualType T
5119 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5120 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5121 return T;
5122 }
5123
5124 case TYPE_ATTRIBUTED: {
5125 if (Record.size() != 3) {
5126 Error("incorrect encoding of attributed type");
5127 return QualType();
5128 }
5129 QualType modifiedType = readType(*Loc.F, Record, Idx);
5130 QualType equivalentType = readType(*Loc.F, Record, Idx);
5131 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5132 return Context.getAttributedType(kind, modifiedType, equivalentType);
5133 }
5134
5135 case TYPE_PAREN: {
5136 if (Record.size() != 1) {
5137 Error("incorrect encoding of paren type");
5138 return QualType();
5139 }
5140 QualType InnerType = readType(*Loc.F, Record, Idx);
5141 return Context.getParenType(InnerType);
5142 }
5143
5144 case TYPE_PACK_EXPANSION: {
5145 if (Record.size() != 2) {
5146 Error("incorrect encoding of pack expansion type");
5147 return QualType();
5148 }
5149 QualType Pattern = readType(*Loc.F, Record, Idx);
5150 if (Pattern.isNull())
5151 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005152 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005153 if (Record[1])
5154 NumExpansions = Record[1] - 1;
5155 return Context.getPackExpansionType(Pattern, NumExpansions);
5156 }
5157
5158 case TYPE_ELABORATED: {
5159 unsigned Idx = 0;
5160 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5161 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5162 QualType NamedType = readType(*Loc.F, Record, Idx);
5163 return Context.getElaboratedType(Keyword, NNS, NamedType);
5164 }
5165
5166 case TYPE_OBJC_INTERFACE: {
5167 unsigned Idx = 0;
5168 ObjCInterfaceDecl *ItfD
5169 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5170 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5171 }
5172
5173 case TYPE_OBJC_OBJECT: {
5174 unsigned Idx = 0;
5175 QualType Base = readType(*Loc.F, Record, Idx);
5176 unsigned NumProtos = Record[Idx++];
5177 SmallVector<ObjCProtocolDecl*, 4> Protos;
5178 for (unsigned I = 0; I != NumProtos; ++I)
5179 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5180 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5181 }
5182
5183 case TYPE_OBJC_OBJECT_POINTER: {
5184 unsigned Idx = 0;
5185 QualType Pointee = readType(*Loc.F, Record, Idx);
5186 return Context.getObjCObjectPointerType(Pointee);
5187 }
5188
5189 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5190 unsigned Idx = 0;
5191 QualType Parm = readType(*Loc.F, Record, Idx);
5192 QualType Replacement = readType(*Loc.F, Record, Idx);
5193 return
5194 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
5195 Replacement);
5196 }
5197
5198 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5199 unsigned Idx = 0;
5200 QualType Parm = readType(*Loc.F, Record, Idx);
5201 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5202 return Context.getSubstTemplateTypeParmPackType(
5203 cast<TemplateTypeParmType>(Parm),
5204 ArgPack);
5205 }
5206
5207 case TYPE_INJECTED_CLASS_NAME: {
5208 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5209 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5210 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5211 // for AST reading, too much interdependencies.
5212 return
5213 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
5214 }
5215
5216 case TYPE_TEMPLATE_TYPE_PARM: {
5217 unsigned Idx = 0;
5218 unsigned Depth = Record[Idx++];
5219 unsigned Index = Record[Idx++];
5220 bool Pack = Record[Idx++];
5221 TemplateTypeParmDecl *D
5222 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5223 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5224 }
5225
5226 case TYPE_DEPENDENT_NAME: {
5227 unsigned Idx = 0;
5228 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5229 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5230 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5231 QualType Canon = readType(*Loc.F, Record, Idx);
5232 if (!Canon.isNull())
5233 Canon = Context.getCanonicalType(Canon);
5234 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5235 }
5236
5237 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5238 unsigned Idx = 0;
5239 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5240 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5241 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5242 unsigned NumArgs = Record[Idx++];
5243 SmallVector<TemplateArgument, 8> Args;
5244 Args.reserve(NumArgs);
5245 while (NumArgs--)
5246 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5247 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5248 Args.size(), Args.data());
5249 }
5250
5251 case TYPE_DEPENDENT_SIZED_ARRAY: {
5252 unsigned Idx = 0;
5253
5254 // ArrayType
5255 QualType ElementType = readType(*Loc.F, Record, Idx);
5256 ArrayType::ArraySizeModifier ASM
5257 = (ArrayType::ArraySizeModifier)Record[Idx++];
5258 unsigned IndexTypeQuals = Record[Idx++];
5259
5260 // DependentSizedArrayType
5261 Expr *NumElts = ReadExpr(*Loc.F);
5262 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5263
5264 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5265 IndexTypeQuals, Brackets);
5266 }
5267
5268 case TYPE_TEMPLATE_SPECIALIZATION: {
5269 unsigned Idx = 0;
5270 bool IsDependent = Record[Idx++];
5271 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5272 SmallVector<TemplateArgument, 8> Args;
5273 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5274 QualType Underlying = readType(*Loc.F, Record, Idx);
5275 QualType T;
5276 if (Underlying.isNull())
5277 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5278 Args.size());
5279 else
5280 T = Context.getTemplateSpecializationType(Name, Args.data(),
5281 Args.size(), Underlying);
5282 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5283 return T;
5284 }
5285
5286 case TYPE_ATOMIC: {
5287 if (Record.size() != 1) {
5288 Error("Incorrect encoding of atomic type");
5289 return QualType();
5290 }
5291 QualType ValueType = readType(*Loc.F, Record, Idx);
5292 return Context.getAtomicType(ValueType);
5293 }
5294 }
5295 llvm_unreachable("Invalid TypeCode!");
5296}
5297
5298class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5299 ASTReader &Reader;
5300 ModuleFile &F;
5301 const ASTReader::RecordData &Record;
5302 unsigned &Idx;
5303
5304 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5305 unsigned &I) {
5306 return Reader.ReadSourceLocation(F, R, I);
5307 }
5308
5309 template<typename T>
5310 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5311 return Reader.ReadDeclAs<T>(F, Record, Idx);
5312 }
5313
5314public:
5315 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5316 const ASTReader::RecordData &Record, unsigned &Idx)
5317 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5318 { }
5319
5320 // We want compile-time assurance that we've enumerated all of
5321 // these, so unfortunately we have to declare them first, then
5322 // define them out-of-line.
5323#define ABSTRACT_TYPELOC(CLASS, PARENT)
5324#define TYPELOC(CLASS, PARENT) \
5325 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5326#include "clang/AST/TypeLocNodes.def"
5327
5328 void VisitFunctionTypeLoc(FunctionTypeLoc);
5329 void VisitArrayTypeLoc(ArrayTypeLoc);
5330};
5331
5332void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5333 // nothing to do
5334}
5335void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5336 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5337 if (TL.needsExtraLocalData()) {
5338 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5339 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5340 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5341 TL.setModeAttr(Record[Idx++]);
5342 }
5343}
5344void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5345 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5346}
5347void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5348 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5349}
Reid Kleckner8a365022013-06-24 17:51:48 +00005350void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5351 // nothing to do
5352}
Reid Kleckner0503a872013-12-05 01:23:43 +00005353void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5354 // nothing to do
5355}
Guy Benyei11169dd2012-12-18 14:30:41 +00005356void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5357 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5358}
5359void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5360 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5361}
5362void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5363 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5364}
5365void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5366 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5367 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5368}
5369void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5370 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5371 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5372 if (Record[Idx++])
5373 TL.setSizeExpr(Reader.ReadExpr(F));
5374 else
5375 TL.setSizeExpr(0);
5376}
5377void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5378 VisitArrayTypeLoc(TL);
5379}
5380void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5381 VisitArrayTypeLoc(TL);
5382}
5383void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5384 VisitArrayTypeLoc(TL);
5385}
5386void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5387 DependentSizedArrayTypeLoc TL) {
5388 VisitArrayTypeLoc(TL);
5389}
5390void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5391 DependentSizedExtVectorTypeLoc TL) {
5392 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5393}
5394void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5395 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5396}
5397void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5398 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5399}
5400void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5401 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5402 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5403 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5404 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005405 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5406 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005407 }
5408}
5409void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5410 VisitFunctionTypeLoc(TL);
5411}
5412void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5413 VisitFunctionTypeLoc(TL);
5414}
5415void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5416 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5417}
5418void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5419 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5420}
5421void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5422 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5423 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5424 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5425}
5426void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5427 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5428 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5429 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5430 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5431}
5432void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5433 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5434}
5435void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5436 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5437 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5438 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5439 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5440}
5441void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5442 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5443}
5444void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5445 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5446}
5447void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5448 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5449}
5450void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5451 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5452 if (TL.hasAttrOperand()) {
5453 SourceRange range;
5454 range.setBegin(ReadSourceLocation(Record, Idx));
5455 range.setEnd(ReadSourceLocation(Record, Idx));
5456 TL.setAttrOperandParensRange(range);
5457 }
5458 if (TL.hasAttrExprOperand()) {
5459 if (Record[Idx++])
5460 TL.setAttrExprOperand(Reader.ReadExpr(F));
5461 else
5462 TL.setAttrExprOperand(0);
5463 } else if (TL.hasAttrEnumOperand())
5464 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5465}
5466void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5467 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5468}
5469void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5470 SubstTemplateTypeParmTypeLoc TL) {
5471 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5472}
5473void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5474 SubstTemplateTypeParmPackTypeLoc TL) {
5475 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5476}
5477void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5478 TemplateSpecializationTypeLoc TL) {
5479 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5480 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5481 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5482 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5483 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5484 TL.setArgLocInfo(i,
5485 Reader.GetTemplateArgumentLocInfo(F,
5486 TL.getTypePtr()->getArg(i).getKind(),
5487 Record, Idx));
5488}
5489void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5490 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5491 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5492}
5493void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5494 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5495 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5496}
5497void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5498 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5499}
5500void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5501 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5502 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5503 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5504}
5505void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5506 DependentTemplateSpecializationTypeLoc TL) {
5507 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5508 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5509 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5510 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5511 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5512 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5513 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5514 TL.setArgLocInfo(I,
5515 Reader.GetTemplateArgumentLocInfo(F,
5516 TL.getTypePtr()->getArg(I).getKind(),
5517 Record, Idx));
5518}
5519void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5520 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5521}
5522void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5523 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5524}
5525void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5526 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5527 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5528 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5529 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5530 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5531}
5532void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5533 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5534}
5535void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5536 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5537 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5538 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5539}
5540
5541TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5542 const RecordData &Record,
5543 unsigned &Idx) {
5544 QualType InfoTy = readType(F, Record, Idx);
5545 if (InfoTy.isNull())
5546 return 0;
5547
5548 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5549 TypeLocReader TLR(*this, F, Record, Idx);
5550 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5551 TLR.Visit(TL);
5552 return TInfo;
5553}
5554
5555QualType ASTReader::GetType(TypeID ID) {
5556 unsigned FastQuals = ID & Qualifiers::FastMask;
5557 unsigned Index = ID >> Qualifiers::FastWidth;
5558
5559 if (Index < NUM_PREDEF_TYPE_IDS) {
5560 QualType T;
5561 switch ((PredefinedTypeIDs)Index) {
5562 case PREDEF_TYPE_NULL_ID: return QualType();
5563 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5564 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5565
5566 case PREDEF_TYPE_CHAR_U_ID:
5567 case PREDEF_TYPE_CHAR_S_ID:
5568 // FIXME: Check that the signedness of CharTy is correct!
5569 T = Context.CharTy;
5570 break;
5571
5572 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5573 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5574 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5575 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5576 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5577 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5578 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5579 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5580 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5581 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5582 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5583 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5584 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5585 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5586 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5587 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5588 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5589 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5590 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5591 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5592 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5593 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5594 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5595 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5596 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5597 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5598 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5599 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005600 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5601 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5602 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5603 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5604 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5605 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005606 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005607 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005608 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5609
5610 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5611 T = Context.getAutoRRefDeductType();
5612 break;
5613
5614 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5615 T = Context.ARCUnbridgedCastTy;
5616 break;
5617
5618 case PREDEF_TYPE_VA_LIST_TAG:
5619 T = Context.getVaListTagType();
5620 break;
5621
5622 case PREDEF_TYPE_BUILTIN_FN:
5623 T = Context.BuiltinFnTy;
5624 break;
5625 }
5626
5627 assert(!T.isNull() && "Unknown predefined type");
5628 return T.withFastQualifiers(FastQuals);
5629 }
5630
5631 Index -= NUM_PREDEF_TYPE_IDS;
5632 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5633 if (TypesLoaded[Index].isNull()) {
5634 TypesLoaded[Index] = readTypeRecord(Index);
5635 if (TypesLoaded[Index].isNull())
5636 return QualType();
5637
5638 TypesLoaded[Index]->setFromAST();
5639 if (DeserializationListener)
5640 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5641 TypesLoaded[Index]);
5642 }
5643
5644 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5645}
5646
5647QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5648 return GetType(getGlobalTypeID(F, LocalID));
5649}
5650
5651serialization::TypeID
5652ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5653 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5654 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5655
5656 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5657 return LocalID;
5658
5659 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5660 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5661 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5662
5663 unsigned GlobalIndex = LocalIndex + I->second;
5664 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5665}
5666
5667TemplateArgumentLocInfo
5668ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5669 TemplateArgument::ArgKind Kind,
5670 const RecordData &Record,
5671 unsigned &Index) {
5672 switch (Kind) {
5673 case TemplateArgument::Expression:
5674 return ReadExpr(F);
5675 case TemplateArgument::Type:
5676 return GetTypeSourceInfo(F, Record, Index);
5677 case TemplateArgument::Template: {
5678 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5679 Index);
5680 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5681 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5682 SourceLocation());
5683 }
5684 case TemplateArgument::TemplateExpansion: {
5685 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5686 Index);
5687 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5688 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5689 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5690 EllipsisLoc);
5691 }
5692 case TemplateArgument::Null:
5693 case TemplateArgument::Integral:
5694 case TemplateArgument::Declaration:
5695 case TemplateArgument::NullPtr:
5696 case TemplateArgument::Pack:
5697 // FIXME: Is this right?
5698 return TemplateArgumentLocInfo();
5699 }
5700 llvm_unreachable("unexpected template argument loc");
5701}
5702
5703TemplateArgumentLoc
5704ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5705 const RecordData &Record, unsigned &Index) {
5706 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5707
5708 if (Arg.getKind() == TemplateArgument::Expression) {
5709 if (Record[Index++]) // bool InfoHasSameExpr.
5710 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5711 }
5712 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5713 Record, Index));
5714}
5715
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005716const ASTTemplateArgumentListInfo*
5717ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5718 const RecordData &Record,
5719 unsigned &Index) {
5720 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5721 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5722 unsigned NumArgsAsWritten = Record[Index++];
5723 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5724 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5725 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5726 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5727}
5728
Guy Benyei11169dd2012-12-18 14:30:41 +00005729Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5730 return GetDecl(ID);
5731}
5732
5733uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5734 unsigned &Idx){
5735 if (Idx >= Record.size())
5736 return 0;
5737
5738 unsigned LocalID = Record[Idx++];
5739 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5740}
5741
5742CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5743 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005744 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005745 SavedStreamPosition SavedPosition(Cursor);
5746 Cursor.JumpToBit(Loc.Offset);
5747 ReadingKindTracker ReadingKind(Read_Decl, *this);
5748 RecordData Record;
5749 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005750 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00005751 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5752 Error("Malformed AST file: missing C++ base specifiers");
5753 return 0;
5754 }
5755
5756 unsigned Idx = 0;
5757 unsigned NumBases = Record[Idx++];
5758 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5759 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5760 for (unsigned I = 0; I != NumBases; ++I)
5761 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5762 return Bases;
5763}
5764
5765serialization::DeclID
5766ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5767 if (LocalID < NUM_PREDEF_DECL_IDS)
5768 return LocalID;
5769
5770 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5771 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5772 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5773
5774 return LocalID + I->second;
5775}
5776
5777bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5778 ModuleFile &M) const {
5779 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5780 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5781 return &M == I->second;
5782}
5783
Douglas Gregor9f782892013-01-21 15:25:38 +00005784ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005785 if (!D->isFromASTFile())
5786 return 0;
5787 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5788 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5789 return I->second;
5790}
5791
5792SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5793 if (ID < NUM_PREDEF_DECL_IDS)
5794 return SourceLocation();
5795
5796 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5797
5798 if (Index > DeclsLoaded.size()) {
5799 Error("declaration ID out-of-range for AST file");
5800 return SourceLocation();
5801 }
5802
5803 if (Decl *D = DeclsLoaded[Index])
5804 return D->getLocation();
5805
5806 unsigned RawLocation = 0;
5807 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5808 return ReadSourceLocation(*Rec.F, RawLocation);
5809}
5810
5811Decl *ASTReader::GetDecl(DeclID ID) {
5812 if (ID < NUM_PREDEF_DECL_IDS) {
5813 switch ((PredefinedDeclIDs)ID) {
5814 case PREDEF_DECL_NULL_ID:
5815 return 0;
5816
5817 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5818 return Context.getTranslationUnitDecl();
5819
5820 case PREDEF_DECL_OBJC_ID_ID:
5821 return Context.getObjCIdDecl();
5822
5823 case PREDEF_DECL_OBJC_SEL_ID:
5824 return Context.getObjCSelDecl();
5825
5826 case PREDEF_DECL_OBJC_CLASS_ID:
5827 return Context.getObjCClassDecl();
5828
5829 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5830 return Context.getObjCProtocolDecl();
5831
5832 case PREDEF_DECL_INT_128_ID:
5833 return Context.getInt128Decl();
5834
5835 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5836 return Context.getUInt128Decl();
5837
5838 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5839 return Context.getObjCInstanceTypeDecl();
5840
5841 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5842 return Context.getBuiltinVaListDecl();
5843 }
5844 }
5845
5846 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5847
5848 if (Index >= DeclsLoaded.size()) {
5849 assert(0 && "declaration ID out-of-range for AST file");
5850 Error("declaration ID out-of-range for AST file");
5851 return 0;
5852 }
5853
5854 if (!DeclsLoaded[Index]) {
5855 ReadDeclRecord(ID);
5856 if (DeserializationListener)
5857 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5858 }
5859
5860 return DeclsLoaded[Index];
5861}
5862
5863DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5864 DeclID GlobalID) {
5865 if (GlobalID < NUM_PREDEF_DECL_IDS)
5866 return GlobalID;
5867
5868 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5869 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5870 ModuleFile *Owner = I->second;
5871
5872 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5873 = M.GlobalToLocalDeclIDs.find(Owner);
5874 if (Pos == M.GlobalToLocalDeclIDs.end())
5875 return 0;
5876
5877 return GlobalID - Owner->BaseDeclID + Pos->second;
5878}
5879
5880serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5881 const RecordData &Record,
5882 unsigned &Idx) {
5883 if (Idx >= Record.size()) {
5884 Error("Corrupted AST file");
5885 return 0;
5886 }
5887
5888 return getGlobalDeclID(F, Record[Idx++]);
5889}
5890
5891/// \brief Resolve the offset of a statement into a statement.
5892///
5893/// This operation will read a new statement from the external
5894/// source each time it is called, and is meant to be used via a
5895/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5896Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5897 // Switch case IDs are per Decl.
5898 ClearSwitchCaseIDs();
5899
5900 // Offset here is a global offset across the entire chain.
5901 RecordLocation Loc = getLocalBitOffset(Offset);
5902 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5903 return ReadStmtFromStream(*Loc.F);
5904}
5905
5906namespace {
5907 class FindExternalLexicalDeclsVisitor {
5908 ASTReader &Reader;
5909 const DeclContext *DC;
5910 bool (*isKindWeWant)(Decl::Kind);
5911
5912 SmallVectorImpl<Decl*> &Decls;
5913 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5914
5915 public:
5916 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5917 bool (*isKindWeWant)(Decl::Kind),
5918 SmallVectorImpl<Decl*> &Decls)
5919 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5920 {
5921 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5922 PredefsVisited[I] = false;
5923 }
5924
5925 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5926 if (Preorder)
5927 return false;
5928
5929 FindExternalLexicalDeclsVisitor *This
5930 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5931
5932 ModuleFile::DeclContextInfosMap::iterator Info
5933 = M.DeclContextInfos.find(This->DC);
5934 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5935 return false;
5936
5937 // Load all of the declaration IDs
5938 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5939 *IDE = ID + Info->second.NumLexicalDecls;
5940 ID != IDE; ++ID) {
5941 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5942 continue;
5943
5944 // Don't add predefined declarations to the lexical context more
5945 // than once.
5946 if (ID->second < NUM_PREDEF_DECL_IDS) {
5947 if (This->PredefsVisited[ID->second])
5948 continue;
5949
5950 This->PredefsVisited[ID->second] = true;
5951 }
5952
5953 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5954 if (!This->DC->isDeclInLexicalTraversal(D))
5955 This->Decls.push_back(D);
5956 }
5957 }
5958
5959 return false;
5960 }
5961 };
5962}
5963
5964ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5965 bool (*isKindWeWant)(Decl::Kind),
5966 SmallVectorImpl<Decl*> &Decls) {
5967 // There might be lexical decls in multiple modules, for the TU at
5968 // least. Walk all of the modules in the order they were loaded.
5969 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5970 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5971 ++NumLexicalDeclContextsRead;
5972 return ELR_Success;
5973}
5974
5975namespace {
5976
5977class DeclIDComp {
5978 ASTReader &Reader;
5979 ModuleFile &Mod;
5980
5981public:
5982 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5983
5984 bool operator()(LocalDeclID L, LocalDeclID R) const {
5985 SourceLocation LHS = getLocation(L);
5986 SourceLocation RHS = getLocation(R);
5987 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5988 }
5989
5990 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5991 SourceLocation RHS = getLocation(R);
5992 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5993 }
5994
5995 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5996 SourceLocation LHS = getLocation(L);
5997 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5998 }
5999
6000 SourceLocation getLocation(LocalDeclID ID) const {
6001 return Reader.getSourceManager().getFileLoc(
6002 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6003 }
6004};
6005
6006}
6007
6008void ASTReader::FindFileRegionDecls(FileID File,
6009 unsigned Offset, unsigned Length,
6010 SmallVectorImpl<Decl *> &Decls) {
6011 SourceManager &SM = getSourceManager();
6012
6013 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6014 if (I == FileDeclIDs.end())
6015 return;
6016
6017 FileDeclsInfo &DInfo = I->second;
6018 if (DInfo.Decls.empty())
6019 return;
6020
6021 SourceLocation
6022 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6023 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6024
6025 DeclIDComp DIDComp(*this, *DInfo.Mod);
6026 ArrayRef<serialization::LocalDeclID>::iterator
6027 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6028 BeginLoc, DIDComp);
6029 if (BeginIt != DInfo.Decls.begin())
6030 --BeginIt;
6031
6032 // If we are pointing at a top-level decl inside an objc container, we need
6033 // to backtrack until we find it otherwise we will fail to report that the
6034 // region overlaps with an objc container.
6035 while (BeginIt != DInfo.Decls.begin() &&
6036 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6037 ->isTopLevelDeclInObjCContainer())
6038 --BeginIt;
6039
6040 ArrayRef<serialization::LocalDeclID>::iterator
6041 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6042 EndLoc, DIDComp);
6043 if (EndIt != DInfo.Decls.end())
6044 ++EndIt;
6045
6046 for (ArrayRef<serialization::LocalDeclID>::iterator
6047 DIt = BeginIt; DIt != EndIt; ++DIt)
6048 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6049}
6050
6051namespace {
6052 /// \brief ModuleFile visitor used to perform name lookup into a
6053 /// declaration context.
6054 class DeclContextNameLookupVisitor {
6055 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006056 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00006057 DeclarationName Name;
6058 SmallVectorImpl<NamedDecl *> &Decls;
6059
6060 public:
6061 DeclContextNameLookupVisitor(ASTReader &Reader,
6062 SmallVectorImpl<const DeclContext *> &Contexts,
6063 DeclarationName Name,
6064 SmallVectorImpl<NamedDecl *> &Decls)
6065 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6066
6067 static bool visit(ModuleFile &M, void *UserData) {
6068 DeclContextNameLookupVisitor *This
6069 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6070
6071 // Check whether we have any visible declaration information for
6072 // this context in this module.
6073 ModuleFile::DeclContextInfosMap::iterator Info;
6074 bool FoundInfo = false;
6075 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6076 Info = M.DeclContextInfos.find(This->Contexts[I]);
6077 if (Info != M.DeclContextInfos.end() &&
6078 Info->second.NameLookupTableData) {
6079 FoundInfo = true;
6080 break;
6081 }
6082 }
6083
6084 if (!FoundInfo)
6085 return false;
6086
6087 // Look for this name within this module.
Richard Smith52e3fba2014-03-11 07:17:35 +00006088 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006089 Info->second.NameLookupTableData;
6090 ASTDeclContextNameLookupTable::iterator Pos
6091 = LookupTable->find(This->Name);
6092 if (Pos == LookupTable->end())
6093 return false;
6094
6095 bool FoundAnything = false;
6096 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6097 for (; Data.first != Data.second; ++Data.first) {
6098 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6099 if (!ND)
6100 continue;
6101
6102 if (ND->getDeclName() != This->Name) {
6103 // A name might be null because the decl's redeclarable part is
6104 // currently read before reading its name. The lookup is triggered by
6105 // building that decl (likely indirectly), and so it is later in the
6106 // sense of "already existing" and can be ignored here.
6107 continue;
6108 }
6109
6110 // Record this declaration.
6111 FoundAnything = true;
6112 This->Decls.push_back(ND);
6113 }
6114
6115 return FoundAnything;
6116 }
6117 };
6118}
6119
Douglas Gregor9f782892013-01-21 15:25:38 +00006120/// \brief Retrieve the "definitive" module file for the definition of the
6121/// given declaration context, if there is one.
6122///
6123/// The "definitive" module file is the only place where we need to look to
6124/// find information about the declarations within the given declaration
6125/// context. For example, C++ and Objective-C classes, C structs/unions, and
6126/// Objective-C protocols, categories, and extensions are all defined in a
6127/// single place in the source code, so they have definitive module files
6128/// associated with them. C++ namespaces, on the other hand, can have
6129/// definitions in multiple different module files.
6130///
6131/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6132/// NDEBUG checking.
6133static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6134 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00006135 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6136 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00006137
6138 return 0;
6139}
6140
Richard Smith9ce12e32013-02-07 03:30:24 +00006141bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006142ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6143 DeclarationName Name) {
6144 assert(DC->hasExternalVisibleStorage() &&
6145 "DeclContext has no visible decls in storage");
6146 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006147 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006148
6149 SmallVector<NamedDecl *, 64> Decls;
6150
6151 // Compute the declaration contexts we need to look into. Multiple such
6152 // declaration contexts occur when two declaration contexts from disjoint
6153 // modules get merged, e.g., when two namespaces with the same name are
6154 // independently defined in separate modules.
6155 SmallVector<const DeclContext *, 2> Contexts;
6156 Contexts.push_back(DC);
6157
6158 if (DC->isNamespace()) {
6159 MergedDeclsMap::iterator Merged
6160 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6161 if (Merged != MergedDecls.end()) {
6162 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6163 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6164 }
6165 }
6166
6167 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor9f782892013-01-21 15:25:38 +00006168
6169 // If we can definitively determine which module file to look into,
6170 // only look there. Otherwise, look in all module files.
6171 ModuleFile *Definitive;
6172 if (Contexts.size() == 1 &&
6173 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
6174 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6175 } else {
6176 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6177 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006178 ++NumVisibleDeclContextsRead;
6179 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006180 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006181}
6182
6183namespace {
6184 /// \brief ModuleFile visitor used to retrieve all visible names in a
6185 /// declaration context.
6186 class DeclContextAllNamesVisitor {
6187 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006188 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper3598eb72013-07-05 04:43:31 +00006189 DeclsMap &Decls;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006190 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006191
6192 public:
6193 DeclContextAllNamesVisitor(ASTReader &Reader,
6194 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper3598eb72013-07-05 04:43:31 +00006195 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006196 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006197
6198 static bool visit(ModuleFile &M, void *UserData) {
6199 DeclContextAllNamesVisitor *This
6200 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6201
6202 // Check whether we have any visible declaration information for
6203 // this context in this module.
6204 ModuleFile::DeclContextInfosMap::iterator Info;
6205 bool FoundInfo = false;
6206 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6207 Info = M.DeclContextInfos.find(This->Contexts[I]);
6208 if (Info != M.DeclContextInfos.end() &&
6209 Info->second.NameLookupTableData) {
6210 FoundInfo = true;
6211 break;
6212 }
6213 }
6214
6215 if (!FoundInfo)
6216 return false;
6217
Richard Smith52e3fba2014-03-11 07:17:35 +00006218 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006219 Info->second.NameLookupTableData;
6220 bool FoundAnything = false;
6221 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00006222 I = LookupTable->data_begin(), E = LookupTable->data_end();
6223 I != E;
6224 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006225 ASTDeclContextNameLookupTrait::data_type Data = *I;
6226 for (; Data.first != Data.second; ++Data.first) {
6227 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6228 *Data.first);
6229 if (!ND)
6230 continue;
6231
6232 // Record this declaration.
6233 FoundAnything = true;
6234 This->Decls[ND->getDeclName()].push_back(ND);
6235 }
6236 }
6237
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006238 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006239 }
6240 };
6241}
6242
6243void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6244 if (!DC->hasExternalVisibleStorage())
6245 return;
Craig Topper79be4cd2013-07-05 04:33:53 +00006246 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006247
6248 // Compute the declaration contexts we need to look into. Multiple such
6249 // declaration contexts occur when two declaration contexts from disjoint
6250 // modules get merged, e.g., when two namespaces with the same name are
6251 // independently defined in separate modules.
6252 SmallVector<const DeclContext *, 2> Contexts;
6253 Contexts.push_back(DC);
6254
6255 if (DC->isNamespace()) {
6256 MergedDeclsMap::iterator Merged
6257 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6258 if (Merged != MergedDecls.end()) {
6259 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6260 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6261 }
6262 }
6263
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006264 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6265 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00006266 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6267 ++NumVisibleDeclContextsRead;
6268
Craig Topper79be4cd2013-07-05 04:33:53 +00006269 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006270 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6271 }
6272 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6273}
6274
6275/// \brief Under non-PCH compilation the consumer receives the objc methods
6276/// before receiving the implementation, and codegen depends on this.
6277/// We simulate this by deserializing and passing to consumer the methods of the
6278/// implementation before passing the deserialized implementation decl.
6279static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6280 ASTConsumer *Consumer) {
6281 assert(ImplD && Consumer);
6282
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006283 for (auto *I : ImplD->methods())
6284 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006285
6286 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6287}
6288
6289void ASTReader::PassInterestingDeclsToConsumer() {
6290 assert(Consumer);
6291 while (!InterestingDecls.empty()) {
6292 Decl *D = InterestingDecls.front();
6293 InterestingDecls.pop_front();
6294
6295 PassInterestingDeclToConsumer(D);
6296 }
6297}
6298
6299void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6300 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6301 PassObjCImplDeclToConsumer(ImplD, Consumer);
6302 else
6303 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6304}
6305
6306void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6307 this->Consumer = Consumer;
6308
6309 if (!Consumer)
6310 return;
6311
Ben Langmuir332aafe2014-01-31 01:06:56 +00006312 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006313 // Force deserialization of this decl, which will cause it to be queued for
6314 // passing to the consumer.
Ben Langmuir332aafe2014-01-31 01:06:56 +00006315 GetDecl(EagerlyDeserializedDecls[I]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006316 }
Ben Langmuir332aafe2014-01-31 01:06:56 +00006317 EagerlyDeserializedDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006318
6319 PassInterestingDeclsToConsumer();
6320}
6321
6322void ASTReader::PrintStats() {
6323 std::fprintf(stderr, "*** AST File Statistics:\n");
6324
6325 unsigned NumTypesLoaded
6326 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6327 QualType());
6328 unsigned NumDeclsLoaded
6329 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6330 (Decl *)0);
6331 unsigned NumIdentifiersLoaded
6332 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6333 IdentifiersLoaded.end(),
6334 (IdentifierInfo *)0);
6335 unsigned NumMacrosLoaded
6336 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6337 MacrosLoaded.end(),
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006338 (MacroInfo *)0);
Guy Benyei11169dd2012-12-18 14:30:41 +00006339 unsigned NumSelectorsLoaded
6340 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6341 SelectorsLoaded.end(),
6342 Selector());
6343
6344 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6345 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6346 NumSLocEntriesRead, TotalNumSLocEntries,
6347 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6348 if (!TypesLoaded.empty())
6349 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6350 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6351 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6352 if (!DeclsLoaded.empty())
6353 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6354 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6355 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6356 if (!IdentifiersLoaded.empty())
6357 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6358 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6359 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6360 if (!MacrosLoaded.empty())
6361 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6362 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6363 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6364 if (!SelectorsLoaded.empty())
6365 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6366 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6367 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6368 if (TotalNumStatements)
6369 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6370 NumStatementsRead, TotalNumStatements,
6371 ((float)NumStatementsRead/TotalNumStatements * 100));
6372 if (TotalNumMacros)
6373 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6374 NumMacrosRead, TotalNumMacros,
6375 ((float)NumMacrosRead/TotalNumMacros * 100));
6376 if (TotalLexicalDeclContexts)
6377 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6378 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6379 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6380 * 100));
6381 if (TotalVisibleDeclContexts)
6382 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6383 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6384 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6385 * 100));
6386 if (TotalNumMethodPoolEntries) {
6387 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6388 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6389 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6390 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006391 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006392 if (NumMethodPoolLookups) {
6393 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6394 NumMethodPoolHits, NumMethodPoolLookups,
6395 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6396 }
6397 if (NumMethodPoolTableLookups) {
6398 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6399 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6400 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6401 * 100.0));
6402 }
6403
Douglas Gregor00a50f72013-01-25 00:38:33 +00006404 if (NumIdentifierLookupHits) {
6405 std::fprintf(stderr,
6406 " %u / %u identifier table lookups succeeded (%f%%)\n",
6407 NumIdentifierLookupHits, NumIdentifierLookups,
6408 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6409 }
6410
Douglas Gregore060e572013-01-25 01:03:03 +00006411 if (GlobalIndex) {
6412 std::fprintf(stderr, "\n");
6413 GlobalIndex->printStats();
6414 }
6415
Guy Benyei11169dd2012-12-18 14:30:41 +00006416 std::fprintf(stderr, "\n");
6417 dump();
6418 std::fprintf(stderr, "\n");
6419}
6420
6421template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6422static void
6423dumpModuleIDMap(StringRef Name,
6424 const ContinuousRangeMap<Key, ModuleFile *,
6425 InitialCapacity> &Map) {
6426 if (Map.begin() == Map.end())
6427 return;
6428
6429 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6430 llvm::errs() << Name << ":\n";
6431 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6432 I != IEnd; ++I) {
6433 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6434 << "\n";
6435 }
6436}
6437
6438void ASTReader::dump() {
6439 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6440 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6441 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6442 dumpModuleIDMap("Global type map", GlobalTypeMap);
6443 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6444 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6445 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6446 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6447 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6448 dumpModuleIDMap("Global preprocessed entity map",
6449 GlobalPreprocessedEntityMap);
6450
6451 llvm::errs() << "\n*** PCH/Modules Loaded:";
6452 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6453 MEnd = ModuleMgr.end();
6454 M != MEnd; ++M)
6455 (*M)->dump();
6456}
6457
6458/// Return the amount of memory used by memory buffers, breaking down
6459/// by heap-backed versus mmap'ed memory.
6460void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6461 for (ModuleConstIterator I = ModuleMgr.begin(),
6462 E = ModuleMgr.end(); I != E; ++I) {
6463 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6464 size_t bytes = buf->getBufferSize();
6465 switch (buf->getBufferKind()) {
6466 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6467 sizes.malloc_bytes += bytes;
6468 break;
6469 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6470 sizes.mmap_bytes += bytes;
6471 break;
6472 }
6473 }
6474 }
6475}
6476
6477void ASTReader::InitializeSema(Sema &S) {
6478 SemaObj = &S;
6479 S.addExternalSource(this);
6480
6481 // Makes sure any declarations that were deserialized "too early"
6482 // still get added to the identifier's declaration chains.
6483 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00006484 pushExternalDeclIntoScope(PreloadedDecls[I],
6485 PreloadedDecls[I]->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006486 }
6487 PreloadedDecls.clear();
6488
Richard Smith3d8e97e2013-10-18 06:54:39 +00006489 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006490 if (!FPPragmaOptions.empty()) {
6491 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6492 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6493 }
6494
Richard Smith3d8e97e2013-10-18 06:54:39 +00006495 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006496 if (!OpenCLExtensions.empty()) {
6497 unsigned I = 0;
6498#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6499#include "clang/Basic/OpenCLExtensions.def"
6500
6501 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6502 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006503
6504 UpdateSema();
6505}
6506
6507void ASTReader::UpdateSema() {
6508 assert(SemaObj && "no Sema to update");
6509
6510 // Load the offsets of the declarations that Sema references.
6511 // They will be lazily deserialized when needed.
6512 if (!SemaDeclRefs.empty()) {
6513 assert(SemaDeclRefs.size() % 2 == 0);
6514 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6515 if (!SemaObj->StdNamespace)
6516 SemaObj->StdNamespace = SemaDeclRefs[I];
6517 if (!SemaObj->StdBadAlloc)
6518 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6519 }
6520 SemaDeclRefs.clear();
6521 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006522}
6523
6524IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6525 // Note that we are loading an identifier.
6526 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006527 StringRef Name(NameStart, NameEnd - NameStart);
6528
6529 // If there is a global index, look there first to determine which modules
6530 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00006531 GlobalModuleIndex::HitSet Hits;
6532 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00006533 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00006534 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6535 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00006536 }
6537 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00006538 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006539 NumIdentifierLookups,
6540 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00006541 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006542 IdentifierInfo *II = Visitor.getIdentifierInfo();
6543 markIdentifierUpToDate(II);
6544 return II;
6545}
6546
6547namespace clang {
6548 /// \brief An identifier-lookup iterator that enumerates all of the
6549 /// identifiers stored within a set of AST files.
6550 class ASTIdentifierIterator : public IdentifierIterator {
6551 /// \brief The AST reader whose identifiers are being enumerated.
6552 const ASTReader &Reader;
6553
6554 /// \brief The current index into the chain of AST files stored in
6555 /// the AST reader.
6556 unsigned Index;
6557
6558 /// \brief The current position within the identifier lookup table
6559 /// of the current AST file.
6560 ASTIdentifierLookupTable::key_iterator Current;
6561
6562 /// \brief The end position within the identifier lookup table of
6563 /// the current AST file.
6564 ASTIdentifierLookupTable::key_iterator End;
6565
6566 public:
6567 explicit ASTIdentifierIterator(const ASTReader &Reader);
6568
Craig Topper3e89dfe2014-03-13 02:13:41 +00006569 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00006570 };
6571}
6572
6573ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6574 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6575 ASTIdentifierLookupTable *IdTable
6576 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6577 Current = IdTable->key_begin();
6578 End = IdTable->key_end();
6579}
6580
6581StringRef ASTIdentifierIterator::Next() {
6582 while (Current == End) {
6583 // If we have exhausted all of our AST files, we're done.
6584 if (Index == 0)
6585 return StringRef();
6586
6587 --Index;
6588 ASTIdentifierLookupTable *IdTable
6589 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6590 IdentifierLookupTable;
6591 Current = IdTable->key_begin();
6592 End = IdTable->key_end();
6593 }
6594
6595 // We have any identifiers remaining in the current AST file; return
6596 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006597 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00006598 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006599 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00006600}
6601
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00006602IdentifierIterator *ASTReader::getIdentifiers() {
6603 if (!loadGlobalIndex())
6604 return GlobalIndex->createIdentifierIterator();
6605
Guy Benyei11169dd2012-12-18 14:30:41 +00006606 return new ASTIdentifierIterator(*this);
6607}
6608
6609namespace clang { namespace serialization {
6610 class ReadMethodPoolVisitor {
6611 ASTReader &Reader;
6612 Selector Sel;
6613 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006614 unsigned InstanceBits;
6615 unsigned FactoryBits;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006616 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6617 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00006618
6619 public:
6620 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6621 unsigned PriorGeneration)
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006622 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6623 InstanceBits(0), FactoryBits(0) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006624
6625 static bool visit(ModuleFile &M, void *UserData) {
6626 ReadMethodPoolVisitor *This
6627 = static_cast<ReadMethodPoolVisitor *>(UserData);
6628
6629 if (!M.SelectorLookupTable)
6630 return false;
6631
6632 // If we've already searched this module file, skip it now.
6633 if (M.Generation <= This->PriorGeneration)
6634 return true;
6635
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006636 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006637 ASTSelectorLookupTable *PoolTable
6638 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6639 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6640 if (Pos == PoolTable->end())
6641 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006642
6643 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006644 ++This->Reader.NumSelectorsRead;
6645 // FIXME: Not quite happy with the statistics here. We probably should
6646 // disable this tracking when called via LoadSelector.
6647 // Also, should entries without methods count as misses?
6648 ++This->Reader.NumMethodPoolEntriesRead;
6649 ASTSelectorLookupTrait::data_type Data = *Pos;
6650 if (This->Reader.DeserializationListener)
6651 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6652 This->Sel);
6653
6654 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6655 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006656 This->InstanceBits = Data.InstanceBits;
6657 This->FactoryBits = Data.FactoryBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006658 return true;
6659 }
6660
6661 /// \brief Retrieve the instance methods found by this visitor.
6662 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6663 return InstanceMethods;
6664 }
6665
6666 /// \brief Retrieve the instance methods found by this visitor.
6667 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6668 return FactoryMethods;
6669 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006670
6671 unsigned getInstanceBits() const { return InstanceBits; }
6672 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006673 };
6674} } // end namespace clang::serialization
6675
6676/// \brief Add the given set of methods to the method list.
6677static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6678 ObjCMethodList &List) {
6679 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6680 S.addMethodToGlobalList(&List, Methods[I]);
6681 }
6682}
6683
6684void ASTReader::ReadMethodPool(Selector Sel) {
6685 // Get the selector generation and update it to the current generation.
6686 unsigned &Generation = SelectorGeneration[Sel];
6687 unsigned PriorGeneration = Generation;
6688 Generation = CurrentGeneration;
6689
6690 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006691 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006692 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6693 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6694
6695 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006696 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00006697 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006698
6699 ++NumMethodPoolHits;
6700
Guy Benyei11169dd2012-12-18 14:30:41 +00006701 if (!getSema())
6702 return;
6703
6704 Sema &S = *getSema();
6705 Sema::GlobalMethodPool::iterator Pos
6706 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6707
6708 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6709 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006710 Pos->second.first.setBits(Visitor.getInstanceBits());
6711 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei11169dd2012-12-18 14:30:41 +00006712}
6713
6714void ASTReader::ReadKnownNamespaces(
6715 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6716 Namespaces.clear();
6717
6718 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6719 if (NamespaceDecl *Namespace
6720 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6721 Namespaces.push_back(Namespace);
6722 }
6723}
6724
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006725void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00006726 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006727 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6728 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00006729 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006730 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00006731 Undefined.insert(std::make_pair(D, Loc));
6732 }
6733}
Nick Lewycky8334af82013-01-26 00:35:08 +00006734
Guy Benyei11169dd2012-12-18 14:30:41 +00006735void ASTReader::ReadTentativeDefinitions(
6736 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6737 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6738 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6739 if (Var)
6740 TentativeDefs.push_back(Var);
6741 }
6742 TentativeDefinitions.clear();
6743}
6744
6745void ASTReader::ReadUnusedFileScopedDecls(
6746 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6747 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6748 DeclaratorDecl *D
6749 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6750 if (D)
6751 Decls.push_back(D);
6752 }
6753 UnusedFileScopedDecls.clear();
6754}
6755
6756void ASTReader::ReadDelegatingConstructors(
6757 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6758 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6759 CXXConstructorDecl *D
6760 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6761 if (D)
6762 Decls.push_back(D);
6763 }
6764 DelegatingCtorDecls.clear();
6765}
6766
6767void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6768 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6769 TypedefNameDecl *D
6770 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6771 if (D)
6772 Decls.push_back(D);
6773 }
6774 ExtVectorDecls.clear();
6775}
6776
6777void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6778 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6779 CXXRecordDecl *D
6780 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6781 if (D)
6782 Decls.push_back(D);
6783 }
6784 DynamicClasses.clear();
6785}
6786
6787void
Richard Smith78165b52013-01-10 23:43:47 +00006788ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6789 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6790 NamedDecl *D
6791 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00006792 if (D)
6793 Decls.push_back(D);
6794 }
Richard Smith78165b52013-01-10 23:43:47 +00006795 LocallyScopedExternCDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006796}
6797
6798void ASTReader::ReadReferencedSelectors(
6799 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6800 if (ReferencedSelectorsData.empty())
6801 return;
6802
6803 // If there are @selector references added them to its pool. This is for
6804 // implementation of -Wselector.
6805 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6806 unsigned I = 0;
6807 while (I < DataSize) {
6808 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6809 SourceLocation SelLoc
6810 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6811 Sels.push_back(std::make_pair(Sel, SelLoc));
6812 }
6813 ReferencedSelectorsData.clear();
6814}
6815
6816void ASTReader::ReadWeakUndeclaredIdentifiers(
6817 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6818 if (WeakUndeclaredIdentifiers.empty())
6819 return;
6820
6821 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6822 IdentifierInfo *WeakId
6823 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6824 IdentifierInfo *AliasId
6825 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6826 SourceLocation Loc
6827 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6828 bool Used = WeakUndeclaredIdentifiers[I++];
6829 WeakInfo WI(AliasId, Loc);
6830 WI.setUsed(Used);
6831 WeakIDs.push_back(std::make_pair(WeakId, WI));
6832 }
6833 WeakUndeclaredIdentifiers.clear();
6834}
6835
6836void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6837 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6838 ExternalVTableUse VT;
6839 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6840 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6841 VT.DefinitionRequired = VTableUses[Idx++];
6842 VTables.push_back(VT);
6843 }
6844
6845 VTableUses.clear();
6846}
6847
6848void ASTReader::ReadPendingInstantiations(
6849 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6850 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6851 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6852 SourceLocation Loc
6853 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6854
6855 Pending.push_back(std::make_pair(D, Loc));
6856 }
6857 PendingInstantiations.clear();
6858}
6859
Richard Smithe40f2ba2013-08-07 21:41:30 +00006860void ASTReader::ReadLateParsedTemplates(
6861 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
6862 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
6863 /* In loop */) {
6864 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
6865
6866 LateParsedTemplate *LT = new LateParsedTemplate;
6867 LT->D = GetDecl(LateParsedTemplates[Idx++]);
6868
6869 ModuleFile *F = getOwningModuleFile(LT->D);
6870 assert(F && "No module");
6871
6872 unsigned TokN = LateParsedTemplates[Idx++];
6873 LT->Toks.reserve(TokN);
6874 for (unsigned T = 0; T < TokN; ++T)
6875 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
6876
6877 LPTMap[FD] = LT;
6878 }
6879
6880 LateParsedTemplates.clear();
6881}
6882
Guy Benyei11169dd2012-12-18 14:30:41 +00006883void ASTReader::LoadSelector(Selector Sel) {
6884 // It would be complicated to avoid reading the methods anyway. So don't.
6885 ReadMethodPool(Sel);
6886}
6887
6888void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6889 assert(ID && "Non-zero identifier ID required");
6890 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6891 IdentifiersLoaded[ID - 1] = II;
6892 if (DeserializationListener)
6893 DeserializationListener->IdentifierRead(ID, II);
6894}
6895
6896/// \brief Set the globally-visible declarations associated with the given
6897/// identifier.
6898///
6899/// If the AST reader is currently in a state where the given declaration IDs
6900/// cannot safely be resolved, they are queued until it is safe to resolve
6901/// them.
6902///
6903/// \param II an IdentifierInfo that refers to one or more globally-visible
6904/// declarations.
6905///
6906/// \param DeclIDs the set of declaration IDs with the name @p II that are
6907/// visible at global scope.
6908///
Douglas Gregor6168bd22013-02-18 15:53:43 +00006909/// \param Decls if non-null, this vector will be populated with the set of
6910/// deserialized declarations. These declarations will not be pushed into
6911/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00006912void
6913ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6914 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00006915 SmallVectorImpl<Decl *> *Decls) {
6916 if (NumCurrentElementsDeserializing && !Decls) {
6917 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00006918 return;
6919 }
6920
6921 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6922 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6923 if (SemaObj) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00006924 // If we're simply supposed to record the declarations, do so now.
6925 if (Decls) {
6926 Decls->push_back(D);
6927 continue;
6928 }
6929
Guy Benyei11169dd2012-12-18 14:30:41 +00006930 // Introduce this declaration into the translation-unit scope
6931 // and add it to the declaration chain for this identifier, so
6932 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00006933 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00006934 } else {
6935 // Queue this declaration so that it will be added to the
6936 // translation unit scope and identifier's declaration chain
6937 // once a Sema object is known.
6938 PreloadedDecls.push_back(D);
6939 }
6940 }
6941}
6942
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006943IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006944 if (ID == 0)
6945 return 0;
6946
6947 if (IdentifiersLoaded.empty()) {
6948 Error("no identifier table in AST file");
6949 return 0;
6950 }
6951
6952 ID -= 1;
6953 if (!IdentifiersLoaded[ID]) {
6954 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6955 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6956 ModuleFile *M = I->second;
6957 unsigned Index = ID - M->BaseIdentifierID;
6958 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6959
6960 // All of the strings in the AST file are preceded by a 16-bit length.
6961 // Extract that 16-bit length to avoid having to execute strlen().
6962 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6963 // unsigned integers. This is important to avoid integer overflow when
6964 // we cast them to 'unsigned'.
6965 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6966 unsigned StrLen = (((unsigned) StrLenPtr[0])
6967 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006968 IdentifiersLoaded[ID]
6969 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00006970 if (DeserializationListener)
6971 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6972 }
6973
6974 return IdentifiersLoaded[ID];
6975}
6976
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006977IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6978 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00006979}
6980
6981IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6982 if (LocalID < NUM_PREDEF_IDENT_IDS)
6983 return LocalID;
6984
6985 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6986 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6987 assert(I != M.IdentifierRemap.end()
6988 && "Invalid index into identifier index remap");
6989
6990 return LocalID + I->second;
6991}
6992
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006993MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006994 if (ID == 0)
6995 return 0;
6996
6997 if (MacrosLoaded.empty()) {
6998 Error("no macro table in AST file");
6999 return 0;
7000 }
7001
7002 ID -= NUM_PREDEF_MACRO_IDS;
7003 if (!MacrosLoaded[ID]) {
7004 GlobalMacroMapType::iterator I
7005 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7006 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7007 ModuleFile *M = I->second;
7008 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007009 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7010
7011 if (DeserializationListener)
7012 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7013 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007014 }
7015
7016 return MacrosLoaded[ID];
7017}
7018
7019MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7020 if (LocalID < NUM_PREDEF_MACRO_IDS)
7021 return LocalID;
7022
7023 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7024 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7025 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7026
7027 return LocalID + I->second;
7028}
7029
7030serialization::SubmoduleID
7031ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7032 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7033 return LocalID;
7034
7035 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7036 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7037 assert(I != M.SubmoduleRemap.end()
7038 && "Invalid index into submodule index remap");
7039
7040 return LocalID + I->second;
7041}
7042
7043Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7044 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7045 assert(GlobalID == 0 && "Unhandled global submodule ID");
7046 return 0;
7047 }
7048
7049 if (GlobalID > SubmodulesLoaded.size()) {
7050 Error("submodule ID out of range in AST file");
7051 return 0;
7052 }
7053
7054 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7055}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007056
7057Module *ASTReader::getModule(unsigned ID) {
7058 return getSubmodule(ID);
7059}
7060
Guy Benyei11169dd2012-12-18 14:30:41 +00007061Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7062 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7063}
7064
7065Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7066 if (ID == 0)
7067 return Selector();
7068
7069 if (ID > SelectorsLoaded.size()) {
7070 Error("selector ID out of range in AST file");
7071 return Selector();
7072 }
7073
7074 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
7075 // Load this selector from the selector table.
7076 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7077 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7078 ModuleFile &M = *I->second;
7079 ASTSelectorLookupTrait Trait(*this, M);
7080 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7081 SelectorsLoaded[ID - 1] =
7082 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7083 if (DeserializationListener)
7084 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7085 }
7086
7087 return SelectorsLoaded[ID - 1];
7088}
7089
7090Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7091 return DecodeSelector(ID);
7092}
7093
7094uint32_t ASTReader::GetNumExternalSelectors() {
7095 // ID 0 (the null selector) is considered an external selector.
7096 return getTotalNumSelectors() + 1;
7097}
7098
7099serialization::SelectorID
7100ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7101 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7102 return LocalID;
7103
7104 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7105 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7106 assert(I != M.SelectorRemap.end()
7107 && "Invalid index into selector index remap");
7108
7109 return LocalID + I->second;
7110}
7111
7112DeclarationName
7113ASTReader::ReadDeclarationName(ModuleFile &F,
7114 const RecordData &Record, unsigned &Idx) {
7115 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7116 switch (Kind) {
7117 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007118 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007119
7120 case DeclarationName::ObjCZeroArgSelector:
7121 case DeclarationName::ObjCOneArgSelector:
7122 case DeclarationName::ObjCMultiArgSelector:
7123 return DeclarationName(ReadSelector(F, Record, Idx));
7124
7125 case DeclarationName::CXXConstructorName:
7126 return Context.DeclarationNames.getCXXConstructorName(
7127 Context.getCanonicalType(readType(F, Record, Idx)));
7128
7129 case DeclarationName::CXXDestructorName:
7130 return Context.DeclarationNames.getCXXDestructorName(
7131 Context.getCanonicalType(readType(F, Record, Idx)));
7132
7133 case DeclarationName::CXXConversionFunctionName:
7134 return Context.DeclarationNames.getCXXConversionFunctionName(
7135 Context.getCanonicalType(readType(F, Record, Idx)));
7136
7137 case DeclarationName::CXXOperatorName:
7138 return Context.DeclarationNames.getCXXOperatorName(
7139 (OverloadedOperatorKind)Record[Idx++]);
7140
7141 case DeclarationName::CXXLiteralOperatorName:
7142 return Context.DeclarationNames.getCXXLiteralOperatorName(
7143 GetIdentifierInfo(F, Record, Idx));
7144
7145 case DeclarationName::CXXUsingDirective:
7146 return DeclarationName::getUsingDirectiveName();
7147 }
7148
7149 llvm_unreachable("Invalid NameKind!");
7150}
7151
7152void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7153 DeclarationNameLoc &DNLoc,
7154 DeclarationName Name,
7155 const RecordData &Record, unsigned &Idx) {
7156 switch (Name.getNameKind()) {
7157 case DeclarationName::CXXConstructorName:
7158 case DeclarationName::CXXDestructorName:
7159 case DeclarationName::CXXConversionFunctionName:
7160 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7161 break;
7162
7163 case DeclarationName::CXXOperatorName:
7164 DNLoc.CXXOperatorName.BeginOpNameLoc
7165 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7166 DNLoc.CXXOperatorName.EndOpNameLoc
7167 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7168 break;
7169
7170 case DeclarationName::CXXLiteralOperatorName:
7171 DNLoc.CXXLiteralOperatorName.OpNameLoc
7172 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7173 break;
7174
7175 case DeclarationName::Identifier:
7176 case DeclarationName::ObjCZeroArgSelector:
7177 case DeclarationName::ObjCOneArgSelector:
7178 case DeclarationName::ObjCMultiArgSelector:
7179 case DeclarationName::CXXUsingDirective:
7180 break;
7181 }
7182}
7183
7184void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7185 DeclarationNameInfo &NameInfo,
7186 const RecordData &Record, unsigned &Idx) {
7187 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7188 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7189 DeclarationNameLoc DNLoc;
7190 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7191 NameInfo.setInfo(DNLoc);
7192}
7193
7194void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7195 const RecordData &Record, unsigned &Idx) {
7196 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7197 unsigned NumTPLists = Record[Idx++];
7198 Info.NumTemplParamLists = NumTPLists;
7199 if (NumTPLists) {
7200 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7201 for (unsigned i=0; i != NumTPLists; ++i)
7202 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7203 }
7204}
7205
7206TemplateName
7207ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7208 unsigned &Idx) {
7209 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7210 switch (Kind) {
7211 case TemplateName::Template:
7212 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7213
7214 case TemplateName::OverloadedTemplate: {
7215 unsigned size = Record[Idx++];
7216 UnresolvedSet<8> Decls;
7217 while (size--)
7218 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7219
7220 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7221 }
7222
7223 case TemplateName::QualifiedTemplate: {
7224 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7225 bool hasTemplKeyword = Record[Idx++];
7226 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7227 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7228 }
7229
7230 case TemplateName::DependentTemplate: {
7231 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7232 if (Record[Idx++]) // isIdentifier
7233 return Context.getDependentTemplateName(NNS,
7234 GetIdentifierInfo(F, Record,
7235 Idx));
7236 return Context.getDependentTemplateName(NNS,
7237 (OverloadedOperatorKind)Record[Idx++]);
7238 }
7239
7240 case TemplateName::SubstTemplateTemplateParm: {
7241 TemplateTemplateParmDecl *param
7242 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7243 if (!param) return TemplateName();
7244 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7245 return Context.getSubstTemplateTemplateParm(param, replacement);
7246 }
7247
7248 case TemplateName::SubstTemplateTemplateParmPack: {
7249 TemplateTemplateParmDecl *Param
7250 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7251 if (!Param)
7252 return TemplateName();
7253
7254 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7255 if (ArgPack.getKind() != TemplateArgument::Pack)
7256 return TemplateName();
7257
7258 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7259 }
7260 }
7261
7262 llvm_unreachable("Unhandled template name kind!");
7263}
7264
7265TemplateArgument
7266ASTReader::ReadTemplateArgument(ModuleFile &F,
7267 const RecordData &Record, unsigned &Idx) {
7268 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7269 switch (Kind) {
7270 case TemplateArgument::Null:
7271 return TemplateArgument();
7272 case TemplateArgument::Type:
7273 return TemplateArgument(readType(F, Record, Idx));
7274 case TemplateArgument::Declaration: {
7275 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7276 bool ForReferenceParam = Record[Idx++];
7277 return TemplateArgument(D, ForReferenceParam);
7278 }
7279 case TemplateArgument::NullPtr:
7280 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7281 case TemplateArgument::Integral: {
7282 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7283 QualType T = readType(F, Record, Idx);
7284 return TemplateArgument(Context, Value, T);
7285 }
7286 case TemplateArgument::Template:
7287 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7288 case TemplateArgument::TemplateExpansion: {
7289 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007290 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007291 if (unsigned NumExpansions = Record[Idx++])
7292 NumTemplateExpansions = NumExpansions - 1;
7293 return TemplateArgument(Name, NumTemplateExpansions);
7294 }
7295 case TemplateArgument::Expression:
7296 return TemplateArgument(ReadExpr(F));
7297 case TemplateArgument::Pack: {
7298 unsigned NumArgs = Record[Idx++];
7299 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7300 for (unsigned I = 0; I != NumArgs; ++I)
7301 Args[I] = ReadTemplateArgument(F, Record, Idx);
7302 return TemplateArgument(Args, NumArgs);
7303 }
7304 }
7305
7306 llvm_unreachable("Unhandled template argument kind!");
7307}
7308
7309TemplateParameterList *
7310ASTReader::ReadTemplateParameterList(ModuleFile &F,
7311 const RecordData &Record, unsigned &Idx) {
7312 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7313 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7314 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7315
7316 unsigned NumParams = Record[Idx++];
7317 SmallVector<NamedDecl *, 16> Params;
7318 Params.reserve(NumParams);
7319 while (NumParams--)
7320 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7321
7322 TemplateParameterList* TemplateParams =
7323 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7324 Params.data(), Params.size(), RAngleLoc);
7325 return TemplateParams;
7326}
7327
7328void
7329ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007330ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007331 ModuleFile &F, const RecordData &Record,
7332 unsigned &Idx) {
7333 unsigned NumTemplateArgs = Record[Idx++];
7334 TemplArgs.reserve(NumTemplateArgs);
7335 while (NumTemplateArgs--)
7336 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7337}
7338
7339/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007340void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007341 const RecordData &Record, unsigned &Idx) {
7342 unsigned NumDecls = Record[Idx++];
7343 Set.reserve(Context, NumDecls);
7344 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007345 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007346 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007347 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007348 }
7349}
7350
7351CXXBaseSpecifier
7352ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7353 const RecordData &Record, unsigned &Idx) {
7354 bool isVirtual = static_cast<bool>(Record[Idx++]);
7355 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7356 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7357 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7358 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7359 SourceRange Range = ReadSourceRange(F, Record, Idx);
7360 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7361 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7362 EllipsisLoc);
7363 Result.setInheritConstructors(inheritConstructors);
7364 return Result;
7365}
7366
7367std::pair<CXXCtorInitializer **, unsigned>
7368ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7369 unsigned &Idx) {
7370 CXXCtorInitializer **CtorInitializers = 0;
7371 unsigned NumInitializers = Record[Idx++];
7372 if (NumInitializers) {
7373 CtorInitializers
7374 = new (Context) CXXCtorInitializer*[NumInitializers];
7375 for (unsigned i=0; i != NumInitializers; ++i) {
7376 TypeSourceInfo *TInfo = 0;
7377 bool IsBaseVirtual = false;
7378 FieldDecl *Member = 0;
7379 IndirectFieldDecl *IndirectMember = 0;
7380
7381 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7382 switch (Type) {
7383 case CTOR_INITIALIZER_BASE:
7384 TInfo = GetTypeSourceInfo(F, Record, Idx);
7385 IsBaseVirtual = Record[Idx++];
7386 break;
7387
7388 case CTOR_INITIALIZER_DELEGATING:
7389 TInfo = GetTypeSourceInfo(F, Record, Idx);
7390 break;
7391
7392 case CTOR_INITIALIZER_MEMBER:
7393 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7394 break;
7395
7396 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7397 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7398 break;
7399 }
7400
7401 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7402 Expr *Init = ReadExpr(F);
7403 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7404 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7405 bool IsWritten = Record[Idx++];
7406 unsigned SourceOrderOrNumArrayIndices;
7407 SmallVector<VarDecl *, 8> Indices;
7408 if (IsWritten) {
7409 SourceOrderOrNumArrayIndices = Record[Idx++];
7410 } else {
7411 SourceOrderOrNumArrayIndices = Record[Idx++];
7412 Indices.reserve(SourceOrderOrNumArrayIndices);
7413 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7414 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7415 }
7416
7417 CXXCtorInitializer *BOMInit;
7418 if (Type == CTOR_INITIALIZER_BASE) {
7419 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7420 LParenLoc, Init, RParenLoc,
7421 MemberOrEllipsisLoc);
7422 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7423 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7424 Init, RParenLoc);
7425 } else if (IsWritten) {
7426 if (Member)
7427 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7428 LParenLoc, Init, RParenLoc);
7429 else
7430 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7431 MemberOrEllipsisLoc, LParenLoc,
7432 Init, RParenLoc);
7433 } else {
Argyrios Kyrtzidis794671d2013-05-30 23:59:46 +00007434 if (IndirectMember) {
7435 assert(Indices.empty() && "Indirect field improperly initialized");
7436 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7437 MemberOrEllipsisLoc, LParenLoc,
7438 Init, RParenLoc);
7439 } else {
7440 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7441 LParenLoc, Init, RParenLoc,
7442 Indices.data(), Indices.size());
7443 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007444 }
7445
7446 if (IsWritten)
7447 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7448 CtorInitializers[i] = BOMInit;
7449 }
7450 }
7451
7452 return std::make_pair(CtorInitializers, NumInitializers);
7453}
7454
7455NestedNameSpecifier *
7456ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7457 const RecordData &Record, unsigned &Idx) {
7458 unsigned N = Record[Idx++];
7459 NestedNameSpecifier *NNS = 0, *Prev = 0;
7460 for (unsigned I = 0; I != N; ++I) {
7461 NestedNameSpecifier::SpecifierKind Kind
7462 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7463 switch (Kind) {
7464 case NestedNameSpecifier::Identifier: {
7465 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7466 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7467 break;
7468 }
7469
7470 case NestedNameSpecifier::Namespace: {
7471 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7472 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7473 break;
7474 }
7475
7476 case NestedNameSpecifier::NamespaceAlias: {
7477 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7478 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7479 break;
7480 }
7481
7482 case NestedNameSpecifier::TypeSpec:
7483 case NestedNameSpecifier::TypeSpecWithTemplate: {
7484 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7485 if (!T)
7486 return 0;
7487
7488 bool Template = Record[Idx++];
7489 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7490 break;
7491 }
7492
7493 case NestedNameSpecifier::Global: {
7494 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7495 // No associated value, and there can't be a prefix.
7496 break;
7497 }
7498 }
7499 Prev = NNS;
7500 }
7501 return NNS;
7502}
7503
7504NestedNameSpecifierLoc
7505ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7506 unsigned &Idx) {
7507 unsigned N = Record[Idx++];
7508 NestedNameSpecifierLocBuilder Builder;
7509 for (unsigned I = 0; I != N; ++I) {
7510 NestedNameSpecifier::SpecifierKind Kind
7511 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7512 switch (Kind) {
7513 case NestedNameSpecifier::Identifier: {
7514 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7515 SourceRange Range = ReadSourceRange(F, Record, Idx);
7516 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7517 break;
7518 }
7519
7520 case NestedNameSpecifier::Namespace: {
7521 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7522 SourceRange Range = ReadSourceRange(F, Record, Idx);
7523 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7524 break;
7525 }
7526
7527 case NestedNameSpecifier::NamespaceAlias: {
7528 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7529 SourceRange Range = ReadSourceRange(F, Record, Idx);
7530 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7531 break;
7532 }
7533
7534 case NestedNameSpecifier::TypeSpec:
7535 case NestedNameSpecifier::TypeSpecWithTemplate: {
7536 bool Template = Record[Idx++];
7537 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7538 if (!T)
7539 return NestedNameSpecifierLoc();
7540 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7541
7542 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7543 Builder.Extend(Context,
7544 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7545 T->getTypeLoc(), ColonColonLoc);
7546 break;
7547 }
7548
7549 case NestedNameSpecifier::Global: {
7550 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7551 Builder.MakeGlobal(Context, ColonColonLoc);
7552 break;
7553 }
7554 }
7555 }
7556
7557 return Builder.getWithLocInContext(Context);
7558}
7559
7560SourceRange
7561ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7562 unsigned &Idx) {
7563 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7564 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7565 return SourceRange(beg, end);
7566}
7567
7568/// \brief Read an integral value
7569llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7570 unsigned BitWidth = Record[Idx++];
7571 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7572 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7573 Idx += NumWords;
7574 return Result;
7575}
7576
7577/// \brief Read a signed integral value
7578llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7579 bool isUnsigned = Record[Idx++];
7580 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7581}
7582
7583/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00007584llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7585 const llvm::fltSemantics &Sem,
7586 unsigned &Idx) {
7587 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007588}
7589
7590// \brief Read a string
7591std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7592 unsigned Len = Record[Idx++];
7593 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7594 Idx += Len;
7595 return Result;
7596}
7597
7598VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7599 unsigned &Idx) {
7600 unsigned Major = Record[Idx++];
7601 unsigned Minor = Record[Idx++];
7602 unsigned Subminor = Record[Idx++];
7603 if (Minor == 0)
7604 return VersionTuple(Major);
7605 if (Subminor == 0)
7606 return VersionTuple(Major, Minor - 1);
7607 return VersionTuple(Major, Minor - 1, Subminor - 1);
7608}
7609
7610CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7611 const RecordData &Record,
7612 unsigned &Idx) {
7613 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7614 return CXXTemporary::Create(Context, Decl);
7615}
7616
7617DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00007618 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007619}
7620
7621DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7622 return Diags.Report(Loc, DiagID);
7623}
7624
7625/// \brief Retrieve the identifier table associated with the
7626/// preprocessor.
7627IdentifierTable &ASTReader::getIdentifierTable() {
7628 return PP.getIdentifierTable();
7629}
7630
7631/// \brief Record that the given ID maps to the given switch-case
7632/// statement.
7633void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7634 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7635 "Already have a SwitchCase with this ID");
7636 (*CurrSwitchCaseStmts)[ID] = SC;
7637}
7638
7639/// \brief Retrieve the switch-case statement with the given ID.
7640SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7641 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7642 return (*CurrSwitchCaseStmts)[ID];
7643}
7644
7645void ASTReader::ClearSwitchCaseIDs() {
7646 CurrSwitchCaseStmts->clear();
7647}
7648
7649void ASTReader::ReadComments() {
7650 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007651 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00007652 serialization::ModuleFile *> >::iterator
7653 I = CommentsCursors.begin(),
7654 E = CommentsCursors.end();
7655 I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007656 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00007657 serialization::ModuleFile &F = *I->second;
7658 SavedStreamPosition SavedPosition(Cursor);
7659
7660 RecordData Record;
7661 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007662 llvm::BitstreamEntry Entry =
7663 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7664
7665 switch (Entry.Kind) {
7666 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7667 case llvm::BitstreamEntry::Error:
7668 Error("malformed block record in AST file");
7669 return;
7670 case llvm::BitstreamEntry::EndBlock:
7671 goto NextCursor;
7672 case llvm::BitstreamEntry::Record:
7673 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00007674 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007675 }
7676
7677 // Read a record.
7678 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007679 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007680 case COMMENTS_RAW_COMMENT: {
7681 unsigned Idx = 0;
7682 SourceRange SR = ReadSourceRange(F, Record, Idx);
7683 RawComment::CommentKind Kind =
7684 (RawComment::CommentKind) Record[Idx++];
7685 bool IsTrailingComment = Record[Idx++];
7686 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00007687 Comments.push_back(new (Context) RawComment(
7688 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7689 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00007690 break;
7691 }
7692 }
7693 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007694 NextCursor:;
Guy Benyei11169dd2012-12-18 14:30:41 +00007695 }
7696 Context.Comments.addCommentsToFront(Comments);
7697}
7698
7699void ASTReader::finishPendingActions() {
7700 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00007701 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
7702 !PendingOdrMergeChecks.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007703 // If any identifiers with corresponding top-level declarations have
7704 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00007705 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
7706 TopLevelDeclsMap;
7707 TopLevelDeclsMap TopLevelDecls;
7708
Guy Benyei11169dd2012-12-18 14:30:41 +00007709 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007710 // FIXME: std::move
7711 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7712 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcb15f082013-02-19 18:26:28 +00007713 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00007714
7715 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007716 }
7717
7718 // Load pending declaration chains.
7719 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7720 loadPendingDeclChain(PendingDeclChains[I]);
7721 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7722 }
7723 PendingDeclChains.clear();
7724
Douglas Gregor6168bd22013-02-18 15:53:43 +00007725 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00007726 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
7727 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007728 IdentifierInfo *II = TLD->first;
7729 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007730 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00007731 }
7732 }
7733
Guy Benyei11169dd2012-12-18 14:30:41 +00007734 // Load any pending macro definitions.
7735 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007736 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7737 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7738 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7739 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00007740 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00007741 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007742 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7743 if (Info.M->Kind != MK_Module)
7744 resolvePendingMacro(II, Info);
7745 }
7746 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00007747 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007748 ++IDIdx) {
7749 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7750 if (Info.M->Kind == MK_Module)
7751 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00007752 }
7753 }
7754 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00007755
7756 // Wire up the DeclContexts for Decls that we delayed setting until
7757 // recursive loading is completed.
7758 while (!PendingDeclContextInfos.empty()) {
7759 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7760 PendingDeclContextInfos.pop_front();
7761 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7762 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7763 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7764 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00007765
7766 // For each declaration from a merged context, check that the canonical
7767 // definition of that context also contains a declaration of the same
7768 // entity.
7769 while (!PendingOdrMergeChecks.empty()) {
7770 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
7771
7772 // FIXME: Skip over implicit declarations for now. This matters for things
7773 // like implicitly-declared special member functions. This isn't entirely
7774 // correct; we can end up with multiple unmerged declarations of the same
7775 // implicit entity.
7776 if (D->isImplicit())
7777 continue;
7778
7779 DeclContext *CanonDef = D->getDeclContext();
7780 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
7781
7782 bool Found = false;
7783 const Decl *DCanon = D->getCanonicalDecl();
7784
7785 llvm::SmallVector<const NamedDecl*, 4> Candidates;
7786 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
7787 !Found && I != E; ++I) {
Aaron Ballman86c93902014-03-06 23:45:36 +00007788 for (auto RI : (*I)->redecls()) {
7789 if (RI->getLexicalDeclContext() == CanonDef) {
Richard Smith2b9e3e32013-10-18 06:05:18 +00007790 // This declaration is present in the canonical definition. If it's
7791 // in the same redecl chain, it's the one we're looking for.
Aaron Ballman86c93902014-03-06 23:45:36 +00007792 if (RI->getCanonicalDecl() == DCanon)
Richard Smith2b9e3e32013-10-18 06:05:18 +00007793 Found = true;
7794 else
Aaron Ballman86c93902014-03-06 23:45:36 +00007795 Candidates.push_back(cast<NamedDecl>(RI));
Richard Smith2b9e3e32013-10-18 06:05:18 +00007796 break;
7797 }
7798 }
7799 }
7800
7801 if (!Found) {
7802 D->setInvalidDecl();
7803
7804 Module *CanonDefModule = cast<Decl>(CanonDef)->getOwningModule();
7805 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
7806 << D << D->getOwningModule()->getFullModuleName()
7807 << CanonDef << !CanonDefModule
7808 << (CanonDefModule ? CanonDefModule->getFullModuleName() : "");
7809
7810 if (Candidates.empty())
7811 Diag(cast<Decl>(CanonDef)->getLocation(),
7812 diag::note_module_odr_violation_no_possible_decls) << D;
7813 else {
7814 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
7815 Diag(Candidates[I]->getLocation(),
7816 diag::note_module_odr_violation_possible_decl)
7817 << Candidates[I];
7818 }
7819 }
7820 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007821 }
7822
7823 // If we deserialized any C++ or Objective-C class definitions, any
7824 // Objective-C protocol definitions, or any redeclarable templates, make sure
7825 // that all redeclarations point to the definitions. Note that this can only
7826 // happen now, after the redeclaration chains have been fully wired.
7827 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7828 DEnd = PendingDefinitions.end();
7829 D != DEnd; ++D) {
7830 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7831 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7832 // Make sure that the TagType points at the definition.
7833 const_cast<TagType*>(TagT)->decl = TD;
7834 }
7835
Aaron Ballman86c93902014-03-06 23:45:36 +00007836 if (auto RD = dyn_cast<CXXRecordDecl>(*D)) {
7837 for (auto R : RD->redecls())
7838 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Guy Benyei11169dd2012-12-18 14:30:41 +00007839
7840 }
7841
7842 continue;
7843 }
7844
Aaron Ballman86c93902014-03-06 23:45:36 +00007845 if (auto ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007846 // Make sure that the ObjCInterfaceType points at the definition.
7847 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7848 ->Decl = ID;
7849
Aaron Ballman86c93902014-03-06 23:45:36 +00007850 for (auto R : ID->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007851 R->Data = ID->Data;
7852
7853 continue;
7854 }
7855
Aaron Ballman86c93902014-03-06 23:45:36 +00007856 if (auto PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7857 for (auto R : PD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007858 R->Data = PD->Data;
7859
7860 continue;
7861 }
7862
Aaron Ballman86c93902014-03-06 23:45:36 +00007863 auto RTD = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7864 for (auto R : RTD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007865 R->Common = RTD->Common;
7866 }
7867 PendingDefinitions.clear();
7868
7869 // Load the bodies of any functions or methods we've encountered. We do
7870 // this now (delayed) so that we can be sure that the declaration chains
7871 // have been fully wired up.
7872 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7873 PBEnd = PendingBodies.end();
7874 PB != PBEnd; ++PB) {
7875 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7876 // FIXME: Check for =delete/=default?
7877 // FIXME: Complain about ODR violations here?
7878 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7879 FD->setLazyBody(PB->second);
7880 continue;
7881 }
7882
7883 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7884 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7885 MD->setLazyBody(PB->second);
7886 }
7887 PendingBodies.clear();
7888}
7889
7890void ASTReader::FinishedDeserializing() {
7891 assert(NumCurrentElementsDeserializing &&
7892 "FinishedDeserializing not paired with StartedDeserializing");
7893 if (NumCurrentElementsDeserializing == 1) {
7894 // We decrease NumCurrentElementsDeserializing only after pending actions
7895 // are finished, to avoid recursively re-calling finishPendingActions().
7896 finishPendingActions();
7897 }
7898 --NumCurrentElementsDeserializing;
7899
7900 if (NumCurrentElementsDeserializing == 0 &&
7901 Consumer && !PassingDeclsToConsumer) {
7902 // Guard variable to avoid recursively redoing the process of passing
7903 // decls to consumer.
7904 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7905 true);
7906
7907 while (!InterestingDecls.empty()) {
7908 // We are not in recursive loading, so it's safe to pass the "interesting"
7909 // decls to the consumer.
7910 Decl *D = InterestingDecls.front();
7911 InterestingDecls.pop_front();
7912 PassInterestingDeclToConsumer(D);
7913 }
7914 }
7915}
7916
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007917void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00007918 D = D->getMostRecentDecl();
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007919
7920 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7921 SemaObj->TUScope->AddDecl(D);
7922 } else if (SemaObj->TUScope) {
7923 // Adding the decl to IdResolver may have failed because it was already in
7924 // (even though it was not added in scope). If it is already in, make sure
7925 // it gets in the scope as well.
7926 if (std::find(SemaObj->IdResolver.begin(Name),
7927 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7928 SemaObj->TUScope->AddDecl(D);
7929 }
7930}
7931
Guy Benyei11169dd2012-12-18 14:30:41 +00007932ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7933 StringRef isysroot, bool DisableValidation,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007934 bool AllowASTWithCompilerErrors,
7935 bool AllowConfigurationMismatch,
Ben Langmuir3d4417c2014-02-07 17:31:11 +00007936 bool ValidateSystemInputs,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007937 bool UseGlobalIndex)
Guy Benyei11169dd2012-12-18 14:30:41 +00007938 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7939 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7940 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7941 Consumer(0), ModuleMgr(PP.getFileManager()),
7942 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007943 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007944 AllowConfigurationMismatch(AllowConfigurationMismatch),
Ben Langmuir3d4417c2014-02-07 17:31:11 +00007945 ValidateSystemInputs(ValidateSystemInputs),
Douglas Gregorc1bbec82013-01-25 00:45:27 +00007946 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei11169dd2012-12-18 14:30:41 +00007947 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7948 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007949 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7950 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7951 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007952 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7953 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7954 TotalNumMethodPoolEntries(0),
Guy Benyei11169dd2012-12-18 14:30:41 +00007955 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7956 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7957 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7958 PassingDeclsToConsumer(false),
Richard Smith629ff362013-07-31 00:26:46 +00007959 NumCXXBaseSpecifiersLoaded(0), ReadingKind(Read_None)
Guy Benyei11169dd2012-12-18 14:30:41 +00007960{
7961 SourceMgr.setExternalSLocEntrySource(this);
7962}
7963
7964ASTReader::~ASTReader() {
7965 for (DeclContextVisibleUpdatesPending::iterator
7966 I = PendingVisibleUpdates.begin(),
7967 E = PendingVisibleUpdates.end();
7968 I != E; ++I) {
7969 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7970 F = I->second.end();
7971 J != F; ++J)
7972 delete J->first;
7973 }
7974}