blob: 766fe27bc6c5f3ce2467e64b9e7fa62ea768d63b [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}
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +0000121void ChainedASTReaderListener::visitModuleFile(StringRef Filename) {
122 First->visitModuleFile(Filename);
123 Second->visitModuleFile(Filename);
124}
Ben Langmuircb69b572014-03-07 06:40:32 +0000125bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +0000126 bool isSystem,
127 bool isOverridden) {
128 return First->visitInputFile(Filename, isSystem, isOverridden) ||
129 Second->visitInputFile(Filename, isSystem, isOverridden);
Ben Langmuircb69b572014-03-07 06:40:32 +0000130}
131
Guy Benyei11169dd2012-12-18 14:30:41 +0000132//===----------------------------------------------------------------------===//
133// PCH validator implementation
134//===----------------------------------------------------------------------===//
135
136ASTReaderListener::~ASTReaderListener() {}
137
138/// \brief Compare the given set of language options against an existing set of
139/// language options.
140///
141/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
142///
143/// \returns true if the languagae options mis-match, false otherwise.
144static bool checkLanguageOptions(const LangOptions &LangOpts,
145 const LangOptions &ExistingLangOpts,
146 DiagnosticsEngine *Diags) {
147#define LANGOPT(Name, Bits, Default, Description) \
148 if (ExistingLangOpts.Name != LangOpts.Name) { \
149 if (Diags) \
150 Diags->Report(diag::err_pch_langopt_mismatch) \
151 << Description << LangOpts.Name << ExistingLangOpts.Name; \
152 return true; \
153 }
154
155#define VALUE_LANGOPT(Name, Bits, Default, Description) \
156 if (ExistingLangOpts.Name != LangOpts.Name) { \
157 if (Diags) \
158 Diags->Report(diag::err_pch_langopt_value_mismatch) \
159 << Description; \
160 return true; \
161 }
162
163#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
164 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
165 if (Diags) \
166 Diags->Report(diag::err_pch_langopt_value_mismatch) \
167 << Description; \
168 return true; \
169 }
170
171#define BENIGN_LANGOPT(Name, Bits, Default, Description)
172#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
173#include "clang/Basic/LangOptions.def"
174
175 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
176 if (Diags)
177 Diags->Report(diag::err_pch_langopt_value_mismatch)
178 << "target Objective-C runtime";
179 return true;
180 }
181
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +0000182 if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
183 LangOpts.CommentOpts.BlockCommandNames) {
184 if (Diags)
185 Diags->Report(diag::err_pch_langopt_value_mismatch)
186 << "block command names";
187 return true;
188 }
189
Guy Benyei11169dd2012-12-18 14:30:41 +0000190 return false;
191}
192
193/// \brief Compare the given set of target options against an existing set of
194/// target options.
195///
196/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
197///
198/// \returns true if the target options mis-match, false otherwise.
199static bool checkTargetOptions(const TargetOptions &TargetOpts,
200 const TargetOptions &ExistingTargetOpts,
201 DiagnosticsEngine *Diags) {
202#define CHECK_TARGET_OPT(Field, Name) \
203 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
204 if (Diags) \
205 Diags->Report(diag::err_pch_targetopt_mismatch) \
206 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
207 return true; \
208 }
209
210 CHECK_TARGET_OPT(Triple, "target");
211 CHECK_TARGET_OPT(CPU, "target CPU");
212 CHECK_TARGET_OPT(ABI, "target ABI");
Guy Benyei11169dd2012-12-18 14:30:41 +0000213 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
214#undef CHECK_TARGET_OPT
215
216 // Compare feature sets.
217 SmallVector<StringRef, 4> ExistingFeatures(
218 ExistingTargetOpts.FeaturesAsWritten.begin(),
219 ExistingTargetOpts.FeaturesAsWritten.end());
220 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
221 TargetOpts.FeaturesAsWritten.end());
222 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
223 std::sort(ReadFeatures.begin(), ReadFeatures.end());
224
225 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
226 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
227 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
228 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
229 ++ExistingIdx;
230 ++ReadIdx;
231 continue;
232 }
233
234 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
235 if (Diags)
236 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
237 << false << ReadFeatures[ReadIdx];
238 return true;
239 }
240
241 if (Diags)
242 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
243 << true << ExistingFeatures[ExistingIdx];
244 return true;
245 }
246
247 if (ExistingIdx < ExistingN) {
248 if (Diags)
249 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
250 << true << ExistingFeatures[ExistingIdx];
251 return true;
252 }
253
254 if (ReadIdx < ReadN) {
255 if (Diags)
256 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
257 << false << ReadFeatures[ReadIdx];
258 return true;
259 }
260
261 return false;
262}
263
264bool
265PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
266 bool Complain) {
267 const LangOptions &ExistingLangOpts = PP.getLangOpts();
268 return checkLanguageOptions(LangOpts, ExistingLangOpts,
269 Complain? &Reader.Diags : 0);
270}
271
272bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
273 bool Complain) {
274 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
275 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
276 Complain? &Reader.Diags : 0);
277}
278
279namespace {
280 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
281 MacroDefinitionsMap;
Craig Topper3598eb72013-07-05 04:43:31 +0000282 typedef llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> >
283 DeclsMap;
Guy Benyei11169dd2012-12-18 14:30:41 +0000284}
285
286/// \brief Collect the macro definitions provided by the given preprocessor
287/// options.
288static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
289 MacroDefinitionsMap &Macros,
290 SmallVectorImpl<StringRef> *MacroNames = 0){
291 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
292 StringRef Macro = PPOpts.Macros[I].first;
293 bool IsUndef = PPOpts.Macros[I].second;
294
295 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
296 StringRef MacroName = MacroPair.first;
297 StringRef MacroBody = MacroPair.second;
298
299 // For an #undef'd macro, we only care about the name.
300 if (IsUndef) {
301 if (MacroNames && !Macros.count(MacroName))
302 MacroNames->push_back(MacroName);
303
304 Macros[MacroName] = std::make_pair("", true);
305 continue;
306 }
307
308 // For a #define'd macro, figure out the actual definition.
309 if (MacroName.size() == Macro.size())
310 MacroBody = "1";
311 else {
312 // Note: GCC drops anything following an end-of-line character.
313 StringRef::size_type End = MacroBody.find_first_of("\n\r");
314 MacroBody = MacroBody.substr(0, End);
315 }
316
317 if (MacroNames && !Macros.count(MacroName))
318 MacroNames->push_back(MacroName);
319 Macros[MacroName] = std::make_pair(MacroBody, false);
320 }
321}
322
323/// \brief Check the preprocessor options deserialized from the control block
324/// against the preprocessor options in an existing preprocessor.
325///
326/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
327static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
328 const PreprocessorOptions &ExistingPPOpts,
329 DiagnosticsEngine *Diags,
330 FileManager &FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000331 std::string &SuggestedPredefines,
332 const LangOptions &LangOpts) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000333 // Check macro definitions.
334 MacroDefinitionsMap ASTFileMacros;
335 collectMacroDefinitions(PPOpts, ASTFileMacros);
336 MacroDefinitionsMap ExistingMacros;
337 SmallVector<StringRef, 4> ExistingMacroNames;
338 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
339
340 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
341 // Dig out the macro definition in the existing preprocessor options.
342 StringRef MacroName = ExistingMacroNames[I];
343 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
344
345 // Check whether we know anything about this macro name or not.
346 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
347 = ASTFileMacros.find(MacroName);
348 if (Known == ASTFileMacros.end()) {
349 // FIXME: Check whether this identifier was referenced anywhere in the
350 // AST file. If so, we should reject the AST file. Unfortunately, this
351 // information isn't in the control block. What shall we do about it?
352
353 if (Existing.second) {
354 SuggestedPredefines += "#undef ";
355 SuggestedPredefines += MacroName.str();
356 SuggestedPredefines += '\n';
357 } else {
358 SuggestedPredefines += "#define ";
359 SuggestedPredefines += MacroName.str();
360 SuggestedPredefines += ' ';
361 SuggestedPredefines += Existing.first.str();
362 SuggestedPredefines += '\n';
363 }
364 continue;
365 }
366
367 // If the macro was defined in one but undef'd in the other, we have a
368 // conflict.
369 if (Existing.second != Known->second.second) {
370 if (Diags) {
371 Diags->Report(diag::err_pch_macro_def_undef)
372 << MacroName << Known->second.second;
373 }
374 return true;
375 }
376
377 // If the macro was #undef'd in both, or if the macro bodies are identical,
378 // it's fine.
379 if (Existing.second || Existing.first == Known->second.first)
380 continue;
381
382 // The macro bodies differ; complain.
383 if (Diags) {
384 Diags->Report(diag::err_pch_macro_def_conflict)
385 << MacroName << Known->second.first << Existing.first;
386 }
387 return true;
388 }
389
390 // Check whether we're using predefines.
391 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
392 if (Diags) {
393 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
394 }
395 return true;
396 }
397
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000398 // Detailed record is important since it is used for the module cache hash.
399 if (LangOpts.Modules &&
400 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord) {
401 if (Diags) {
402 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
403 }
404 return true;
405 }
406
Guy Benyei11169dd2012-12-18 14:30:41 +0000407 // Compute the #include and #include_macros lines we need.
408 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
409 StringRef File = ExistingPPOpts.Includes[I];
410 if (File == ExistingPPOpts.ImplicitPCHInclude)
411 continue;
412
413 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
414 != PPOpts.Includes.end())
415 continue;
416
417 SuggestedPredefines += "#include \"";
418 SuggestedPredefines +=
419 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
420 SuggestedPredefines += "\"\n";
421 }
422
423 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
424 StringRef File = ExistingPPOpts.MacroIncludes[I];
425 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
426 File)
427 != PPOpts.MacroIncludes.end())
428 continue;
429
430 SuggestedPredefines += "#__include_macros \"";
431 SuggestedPredefines +=
432 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
433 SuggestedPredefines += "\"\n##\n";
434 }
435
436 return false;
437}
438
439bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
440 bool Complain,
441 std::string &SuggestedPredefines) {
442 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
443
444 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
445 Complain? &Reader.Diags : 0,
446 PP.getFileManager(),
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +0000447 SuggestedPredefines,
448 PP.getLangOpts());
Guy Benyei11169dd2012-12-18 14:30:41 +0000449}
450
Guy Benyei11169dd2012-12-18 14:30:41 +0000451void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
452 PP.setCounterValue(Value);
453}
454
455//===----------------------------------------------------------------------===//
456// AST reader implementation
457//===----------------------------------------------------------------------===//
458
459void
460ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
461 DeserializationListener = Listener;
462}
463
464
465
466unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
467 return serialization::ComputeHash(Sel);
468}
469
470
471std::pair<unsigned, unsigned>
472ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
473 using namespace clang::io;
474 unsigned KeyLen = ReadUnalignedLE16(d);
475 unsigned DataLen = ReadUnalignedLE16(d);
476 return std::make_pair(KeyLen, DataLen);
477}
478
479ASTSelectorLookupTrait::internal_key_type
480ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
481 using namespace clang::io;
482 SelectorTable &SelTable = Reader.getContext().Selectors;
483 unsigned N = ReadUnalignedLE16(d);
484 IdentifierInfo *FirstII
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000485 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000486 if (N == 0)
487 return SelTable.getNullarySelector(FirstII);
488 else if (N == 1)
489 return SelTable.getUnarySelector(FirstII);
490
491 SmallVector<IdentifierInfo *, 16> Args;
492 Args.push_back(FirstII);
493 for (unsigned I = 1; I != N; ++I)
Douglas Gregorc8a992f2013-01-21 16:52:34 +0000494 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
Guy Benyei11169dd2012-12-18 14:30:41 +0000495
496 return SelTable.getSelector(N, Args.data());
497}
498
499ASTSelectorLookupTrait::data_type
500ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
501 unsigned DataLen) {
502 using namespace clang::io;
503
504 data_type Result;
505
506 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +0000507 unsigned NumInstanceMethodsAndBits = ReadUnalignedLE16(d);
508 unsigned NumFactoryMethodsAndBits = ReadUnalignedLE16(d);
509 Result.InstanceBits = NumInstanceMethodsAndBits & 0x3;
510 Result.FactoryBits = NumFactoryMethodsAndBits & 0x3;
511 unsigned NumInstanceMethods = NumInstanceMethodsAndBits >> 2;
512 unsigned NumFactoryMethods = NumFactoryMethodsAndBits >> 2;
Guy Benyei11169dd2012-12-18 14:30:41 +0000513
514 // Load instance methods
515 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
516 if (ObjCMethodDecl *Method
517 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
518 Result.Instance.push_back(Method);
519 }
520
521 // Load factory methods
522 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
523 if (ObjCMethodDecl *Method
524 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
525 Result.Factory.push_back(Method);
526 }
527
528 return Result;
529}
530
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000531unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
532 return llvm::HashString(a);
Guy Benyei11169dd2012-12-18 14:30:41 +0000533}
534
535std::pair<unsigned, unsigned>
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000536ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000537 using namespace clang::io;
538 unsigned DataLen = ReadUnalignedLE16(d);
539 unsigned KeyLen = ReadUnalignedLE16(d);
540 return std::make_pair(KeyLen, DataLen);
541}
542
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000543ASTIdentifierLookupTraitBase::internal_key_type
544ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000545 assert(n >= 2 && d[n-1] == '\0');
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000546 return StringRef((const char*) d, n-1);
Guy Benyei11169dd2012-12-18 14:30:41 +0000547}
548
Douglas Gregordcf25082013-02-11 18:16:18 +0000549/// \brief Whether the given identifier is "interesting".
550static bool isInterestingIdentifier(IdentifierInfo &II) {
551 return II.isPoisoned() ||
552 II.isExtensionToken() ||
553 II.getObjCOrBuiltinID() ||
554 II.hasRevertedTokenIDToIdentifier() ||
555 II.hadMacroDefinition() ||
556 II.getFETokenInfo<void>();
557}
558
Guy Benyei11169dd2012-12-18 14:30:41 +0000559IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
560 const unsigned char* d,
561 unsigned DataLen) {
562 using namespace clang::io;
563 unsigned RawID = ReadUnalignedLE32(d);
564 bool IsInteresting = RawID & 0x01;
565
566 // Wipe out the "is interesting" bit.
567 RawID = RawID >> 1;
568
569 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
570 if (!IsInteresting) {
571 // For uninteresting identifiers, just build the IdentifierInfo
572 // and associate it with the persistent ID.
573 IdentifierInfo *II = KnownII;
574 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000575 II = &Reader.getIdentifierTable().getOwn(k);
Guy Benyei11169dd2012-12-18 14:30:41 +0000576 KnownII = II;
577 }
578 Reader.SetIdentifierInfo(ID, II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000579 if (!II->isFromAST()) {
580 bool WasInteresting = isInterestingIdentifier(*II);
581 II->setIsFromAST();
582 if (WasInteresting)
583 II->setChangedSinceDeserialization();
584 }
585 Reader.markIdentifierUpToDate(II);
Guy Benyei11169dd2012-12-18 14:30:41 +0000586 return II;
587 }
588
589 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
590 unsigned Bits = ReadUnalignedLE16(d);
591 bool CPlusPlusOperatorKeyword = Bits & 0x01;
592 Bits >>= 1;
593 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
594 Bits >>= 1;
595 bool Poisoned = Bits & 0x01;
596 Bits >>= 1;
597 bool ExtensionToken = Bits & 0x01;
598 Bits >>= 1;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000599 bool hasSubmoduleMacros = Bits & 0x01;
600 Bits >>= 1;
Guy Benyei11169dd2012-12-18 14:30:41 +0000601 bool hadMacroDefinition = Bits & 0x01;
602 Bits >>= 1;
603
604 assert(Bits == 0 && "Extra bits in the identifier?");
605 DataLen -= 8;
606
607 // Build the IdentifierInfo itself and link the identifier ID with
608 // the new IdentifierInfo.
609 IdentifierInfo *II = KnownII;
610 if (!II) {
Douglas Gregorbfd73d72013-01-23 18:53:14 +0000611 II = &Reader.getIdentifierTable().getOwn(StringRef(k));
Guy Benyei11169dd2012-12-18 14:30:41 +0000612 KnownII = II;
613 }
614 Reader.markIdentifierUpToDate(II);
Douglas Gregordcf25082013-02-11 18:16:18 +0000615 if (!II->isFromAST()) {
616 bool WasInteresting = isInterestingIdentifier(*II);
617 II->setIsFromAST();
618 if (WasInteresting)
619 II->setChangedSinceDeserialization();
620 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000621
622 // Set or check the various bits in the IdentifierInfo structure.
623 // Token IDs are read-only.
Argyrios Kyrtzidisddee8c92013-02-27 01:13:51 +0000624 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
Guy Benyei11169dd2012-12-18 14:30:41 +0000625 II->RevertTokenIDToIdentifier();
626 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
627 assert(II->isExtensionToken() == ExtensionToken &&
628 "Incorrect extension token flag");
629 (void)ExtensionToken;
630 if (Poisoned)
631 II->setIsPoisoned(true);
632 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
633 "Incorrect C++ operator keyword flag");
634 (void)CPlusPlusOperatorKeyword;
635
636 // If this identifier is a macro, deserialize the macro
637 // definition.
638 if (hadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000639 uint32_t MacroDirectivesOffset = ReadUnalignedLE32(d);
640 DataLen -= 4;
641 SmallVector<uint32_t, 8> LocalMacroIDs;
642 if (hasSubmoduleMacros) {
643 while (uint32_t LocalMacroID = ReadUnalignedLE32(d)) {
644 DataLen -= 4;
645 LocalMacroIDs.push_back(LocalMacroID);
646 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +0000647 DataLen -= 4;
648 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000649
650 if (F.Kind == MK_Module) {
Richard Smith49f906a2014-03-01 00:08:04 +0000651 // Macro definitions are stored from newest to oldest, so reverse them
652 // before registering them.
653 llvm::SmallVector<unsigned, 8> MacroSizes;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000654 for (SmallVectorImpl<uint32_t>::iterator
Richard Smith49f906a2014-03-01 00:08:04 +0000655 I = LocalMacroIDs.begin(), E = LocalMacroIDs.end(); I != E; /**/) {
656 unsigned Size = 1;
657
658 static const uint32_t HasOverridesFlag = 0x80000000U;
659 if (I + 1 != E && (I[1] & HasOverridesFlag))
660 Size += 1 + (I[1] & ~HasOverridesFlag);
661
662 MacroSizes.push_back(Size);
663 I += Size;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000664 }
Richard Smith49f906a2014-03-01 00:08:04 +0000665
666 SmallVectorImpl<uint32_t>::iterator I = LocalMacroIDs.end();
667 for (SmallVectorImpl<unsigned>::reverse_iterator SI = MacroSizes.rbegin(),
668 SE = MacroSizes.rend();
669 SI != SE; ++SI) {
670 I -= *SI;
671
672 uint32_t LocalMacroID = *I;
673 llvm::ArrayRef<uint32_t> Overrides;
674 if (*SI != 1)
675 Overrides = llvm::makeArrayRef(&I[2], *SI - 2);
676 Reader.addPendingMacroFromModule(II, &F, LocalMacroID, Overrides);
677 }
678 assert(I == LocalMacroIDs.begin());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000679 } else {
680 Reader.addPendingMacroFromPCH(II, &F, MacroDirectivesOffset);
681 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000682 }
683
684 Reader.SetIdentifierInfo(ID, II);
685
686 // Read all of the declarations visible at global scope with this
687 // name.
688 if (DataLen > 0) {
689 SmallVector<uint32_t, 4> DeclIDs;
690 for (; DataLen > 0; DataLen -= 4)
691 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
692 Reader.SetGloballyVisibleDecls(II, DeclIDs);
693 }
694
695 return II;
696}
697
698unsigned
699ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
700 llvm::FoldingSetNodeID ID;
701 ID.AddInteger(Key.Kind);
702
703 switch (Key.Kind) {
704 case DeclarationName::Identifier:
705 case DeclarationName::CXXLiteralOperatorName:
706 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
707 break;
708 case DeclarationName::ObjCZeroArgSelector:
709 case DeclarationName::ObjCOneArgSelector:
710 case DeclarationName::ObjCMultiArgSelector:
711 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
712 break;
713 case DeclarationName::CXXOperatorName:
714 ID.AddInteger((OverloadedOperatorKind)Key.Data);
715 break;
716 case DeclarationName::CXXConstructorName:
717 case DeclarationName::CXXDestructorName:
718 case DeclarationName::CXXConversionFunctionName:
719 case DeclarationName::CXXUsingDirective:
720 break;
721 }
722
723 return ID.ComputeHash();
724}
725
726ASTDeclContextNameLookupTrait::internal_key_type
727ASTDeclContextNameLookupTrait::GetInternalKey(
728 const external_key_type& Name) const {
729 DeclNameKey Key;
730 Key.Kind = Name.getNameKind();
731 switch (Name.getNameKind()) {
732 case DeclarationName::Identifier:
733 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
734 break;
735 case DeclarationName::ObjCZeroArgSelector:
736 case DeclarationName::ObjCOneArgSelector:
737 case DeclarationName::ObjCMultiArgSelector:
738 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
739 break;
740 case DeclarationName::CXXOperatorName:
741 Key.Data = Name.getCXXOverloadedOperator();
742 break;
743 case DeclarationName::CXXLiteralOperatorName:
744 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
745 break;
746 case DeclarationName::CXXConstructorName:
747 case DeclarationName::CXXDestructorName:
748 case DeclarationName::CXXConversionFunctionName:
749 case DeclarationName::CXXUsingDirective:
750 Key.Data = 0;
751 break;
752 }
753
754 return Key;
755}
756
757std::pair<unsigned, unsigned>
758ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
759 using namespace clang::io;
760 unsigned KeyLen = ReadUnalignedLE16(d);
761 unsigned DataLen = ReadUnalignedLE16(d);
762 return std::make_pair(KeyLen, DataLen);
763}
764
765ASTDeclContextNameLookupTrait::internal_key_type
766ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
767 using namespace clang::io;
768
769 DeclNameKey Key;
770 Key.Kind = (DeclarationName::NameKind)*d++;
771 switch (Key.Kind) {
772 case DeclarationName::Identifier:
773 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
774 break;
775 case DeclarationName::ObjCZeroArgSelector:
776 case DeclarationName::ObjCOneArgSelector:
777 case DeclarationName::ObjCMultiArgSelector:
778 Key.Data =
779 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
780 .getAsOpaquePtr();
781 break;
782 case DeclarationName::CXXOperatorName:
783 Key.Data = *d++; // OverloadedOperatorKind
784 break;
785 case DeclarationName::CXXLiteralOperatorName:
786 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
787 break;
788 case DeclarationName::CXXConstructorName:
789 case DeclarationName::CXXDestructorName:
790 case DeclarationName::CXXConversionFunctionName:
791 case DeclarationName::CXXUsingDirective:
792 Key.Data = 0;
793 break;
794 }
795
796 return Key;
797}
798
799ASTDeclContextNameLookupTrait::data_type
800ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
801 const unsigned char* d,
802 unsigned DataLen) {
803 using namespace clang::io;
804 unsigned NumDecls = ReadUnalignedLE16(d);
Argyrios Kyrtzidisc57e5032013-01-11 22:29:49 +0000805 LE32DeclID *Start = reinterpret_cast<LE32DeclID *>(
806 const_cast<unsigned char *>(d));
Guy Benyei11169dd2012-12-18 14:30:41 +0000807 return std::make_pair(Start, Start + NumDecls);
808}
809
810bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000811 BitstreamCursor &Cursor,
Guy Benyei11169dd2012-12-18 14:30:41 +0000812 const std::pair<uint64_t, uint64_t> &Offsets,
813 DeclContextInfo &Info) {
814 SavedStreamPosition SavedPosition(Cursor);
815 // First the lexical decls.
816 if (Offsets.first != 0) {
817 Cursor.JumpToBit(Offsets.first);
818
819 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000820 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000821 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000822 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000823 if (RecCode != DECL_CONTEXT_LEXICAL) {
824 Error("Expected lexical block");
825 return true;
826 }
827
Chris Lattner0e6c9402013-01-20 02:38:54 +0000828 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob.data());
829 Info.NumLexicalDecls = Blob.size() / sizeof(KindDeclIDPair);
Guy Benyei11169dd2012-12-18 14:30:41 +0000830 }
831
832 // Now the lookup table.
833 if (Offsets.second != 0) {
834 Cursor.JumpToBit(Offsets.second);
835
836 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +0000837 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +0000838 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +0000839 unsigned RecCode = Cursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +0000840 if (RecCode != DECL_CONTEXT_VISIBLE) {
841 Error("Expected visible lookup table block");
842 return true;
843 }
Richard Smith52e3fba2014-03-11 07:17:35 +0000844 Info.NameLookupTableData
845 = ASTDeclContextNameLookupTable::Create(
846 (const unsigned char *)Blob.data() + Record[0],
847 (const unsigned char *)Blob.data(),
848 ASTDeclContextNameLookupTrait(*this, M));
Guy Benyei11169dd2012-12-18 14:30:41 +0000849 }
850
851 return false;
852}
853
854void ASTReader::Error(StringRef Msg) {
855 Error(diag::err_fe_pch_malformed, Msg);
Douglas Gregor940e8052013-05-10 22:15:13 +0000856 if (Context.getLangOpts().Modules && !Diags.isDiagnosticInFlight()) {
857 Diag(diag::note_module_cache_path)
858 << PP.getHeaderSearchInfo().getModuleCachePath();
859 }
Guy Benyei11169dd2012-12-18 14:30:41 +0000860}
861
862void ASTReader::Error(unsigned DiagID,
863 StringRef Arg1, StringRef Arg2) {
864 if (Diags.isDiagnosticInFlight())
865 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
866 else
867 Diag(DiagID) << Arg1 << Arg2;
868}
869
870//===----------------------------------------------------------------------===//
871// Source Manager Deserialization
872//===----------------------------------------------------------------------===//
873
874/// \brief Read the line table in the source manager block.
875/// \returns true if there was an error.
876bool ASTReader::ParseLineTable(ModuleFile &F,
877 SmallVectorImpl<uint64_t> &Record) {
878 unsigned Idx = 0;
879 LineTableInfo &LineTable = SourceMgr.getLineTable();
880
881 // Parse the file names
882 std::map<int, int> FileIDs;
883 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
884 // Extract the file name
885 unsigned FilenameLen = Record[Idx++];
886 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
887 Idx += FilenameLen;
888 MaybeAddSystemRootToFilename(F, Filename);
889 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
890 }
891
892 // Parse the line entries
893 std::vector<LineEntry> Entries;
894 while (Idx < Record.size()) {
895 int FID = Record[Idx++];
896 assert(FID >= 0 && "Serialized line entries for non-local file.");
897 // Remap FileID from 1-based old view.
898 FID += F.SLocEntryBaseID - 1;
899
900 // Extract the line entries
901 unsigned NumEntries = Record[Idx++];
902 assert(NumEntries && "Numentries is 00000");
903 Entries.clear();
904 Entries.reserve(NumEntries);
905 for (unsigned I = 0; I != NumEntries; ++I) {
906 unsigned FileOffset = Record[Idx++];
907 unsigned LineNo = Record[Idx++];
908 int FilenameID = FileIDs[Record[Idx++]];
909 SrcMgr::CharacteristicKind FileKind
910 = (SrcMgr::CharacteristicKind)Record[Idx++];
911 unsigned IncludeOffset = Record[Idx++];
912 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
913 FileKind, IncludeOffset));
914 }
915 LineTable.AddEntry(FileID::get(FID), Entries);
916 }
917
918 return false;
919}
920
921/// \brief Read a source manager block
922bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
923 using namespace SrcMgr;
924
Chris Lattner7fb3bef2013-01-20 00:56:42 +0000925 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +0000926
927 // Set the source-location entry cursor to the current position in
928 // the stream. This cursor will be used to read the contents of the
929 // source manager block initially, and then lazily read
930 // source-location entries as needed.
931 SLocEntryCursor = F.Stream;
932
933 // The stream itself is going to skip over the source manager block.
934 if (F.Stream.SkipBlock()) {
935 Error("malformed block record in AST file");
936 return true;
937 }
938
939 // Enter the source manager block.
940 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
941 Error("malformed source manager block record in AST file");
942 return true;
943 }
944
945 RecordData Record;
946 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +0000947 llvm::BitstreamEntry E = SLocEntryCursor.advanceSkippingSubblocks();
948
949 switch (E.Kind) {
950 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
951 case llvm::BitstreamEntry::Error:
952 Error("malformed block record in AST file");
953 return true;
954 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +0000955 return false;
Chris Lattnere7b154b2013-01-19 21:39:22 +0000956 case llvm::BitstreamEntry::Record:
957 // The interesting case.
958 break;
Guy Benyei11169dd2012-12-18 14:30:41 +0000959 }
Chris Lattnere7b154b2013-01-19 21:39:22 +0000960
Guy Benyei11169dd2012-12-18 14:30:41 +0000961 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +0000962 Record.clear();
Chris Lattner15c3e7d2013-01-21 18:28:26 +0000963 StringRef Blob;
964 switch (SLocEntryCursor.readRecord(E.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +0000965 default: // Default behavior: ignore.
966 break;
967
968 case SM_SLOC_FILE_ENTRY:
969 case SM_SLOC_BUFFER_ENTRY:
970 case SM_SLOC_EXPANSION_ENTRY:
971 // Once we hit one of the source location entries, we're done.
972 return false;
973 }
974 }
975}
976
977/// \brief If a header file is not found at the path that we expect it to be
978/// and the PCH file was moved from its original location, try to resolve the
979/// file by assuming that header+PCH were moved together and the header is in
980/// the same place relative to the PCH.
981static std::string
982resolveFileRelativeToOriginalDir(const std::string &Filename,
983 const std::string &OriginalDir,
984 const std::string &CurrDir) {
985 assert(OriginalDir != CurrDir &&
986 "No point trying to resolve the file if the PCH dir didn't change");
987 using namespace llvm::sys;
988 SmallString<128> filePath(Filename);
989 fs::make_absolute(filePath);
990 assert(path::is_absolute(OriginalDir));
991 SmallString<128> currPCHPath(CurrDir);
992
993 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
994 fileDirE = path::end(path::parent_path(filePath));
995 path::const_iterator origDirI = path::begin(OriginalDir),
996 origDirE = path::end(OriginalDir);
997 // Skip the common path components from filePath and OriginalDir.
998 while (fileDirI != fileDirE && origDirI != origDirE &&
999 *fileDirI == *origDirI) {
1000 ++fileDirI;
1001 ++origDirI;
1002 }
1003 for (; origDirI != origDirE; ++origDirI)
1004 path::append(currPCHPath, "..");
1005 path::append(currPCHPath, fileDirI, fileDirE);
1006 path::append(currPCHPath, path::filename(Filename));
1007 return currPCHPath.str();
1008}
1009
1010bool ASTReader::ReadSLocEntry(int ID) {
1011 if (ID == 0)
1012 return false;
1013
1014 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1015 Error("source location entry ID out-of-range for AST file");
1016 return true;
1017 }
1018
1019 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1020 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001021 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001022 unsigned BaseOffset = F->SLocEntryBaseOffset;
1023
1024 ++NumSLocEntriesRead;
Chris Lattnere7b154b2013-01-19 21:39:22 +00001025 llvm::BitstreamEntry Entry = SLocEntryCursor.advance();
1026 if (Entry.Kind != llvm::BitstreamEntry::Record) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001027 Error("incorrectly-formatted source location entry in AST file");
1028 return true;
1029 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001030
Guy Benyei11169dd2012-12-18 14:30:41 +00001031 RecordData Record;
Chris Lattner0e6c9402013-01-20 02:38:54 +00001032 StringRef Blob;
1033 switch (SLocEntryCursor.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001034 default:
1035 Error("incorrectly-formatted source location entry in AST file");
1036 return true;
1037
1038 case SM_SLOC_FILE_ENTRY: {
1039 // We will detect whether a file changed and return 'Failure' for it, but
1040 // we will also try to fail gracefully by setting up the SLocEntry.
1041 unsigned InputID = Record[4];
1042 InputFile IF = getInputFile(*F, InputID);
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001043 const FileEntry *File = IF.getFile();
1044 bool OverriddenBuffer = IF.isOverridden();
Guy Benyei11169dd2012-12-18 14:30:41 +00001045
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001046 // Note that we only check if a File was returned. If it was out-of-date
1047 // we have complained but we will continue creating a FileID to recover
1048 // gracefully.
1049 if (!File)
Guy Benyei11169dd2012-12-18 14:30:41 +00001050 return true;
1051
1052 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1053 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1054 // This is the module's main file.
1055 IncludeLoc = getImportLocation(F);
1056 }
1057 SrcMgr::CharacteristicKind
1058 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1059 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1060 ID, BaseOffset + Record[0]);
1061 SrcMgr::FileInfo &FileInfo =
1062 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1063 FileInfo.NumCreatedFIDs = Record[5];
1064 if (Record[3])
1065 FileInfo.setHasLineDirectives();
1066
1067 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1068 unsigned NumFileDecls = Record[7];
1069 if (NumFileDecls) {
1070 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1071 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1072 NumFileDecls));
1073 }
1074
1075 const SrcMgr::ContentCache *ContentCache
1076 = SourceMgr.getOrCreateContentCache(File,
1077 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
1078 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1079 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
1080 unsigned Code = SLocEntryCursor.ReadCode();
1081 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001082 unsigned RecCode = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001083
1084 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1085 Error("AST record has invalid code");
1086 return true;
1087 }
1088
1089 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001090 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), File->getName());
Guy Benyei11169dd2012-12-18 14:30:41 +00001091 SourceMgr.overrideFileContents(File, Buffer);
1092 }
1093
1094 break;
1095 }
1096
1097 case SM_SLOC_BUFFER_ENTRY: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00001098 const char *Name = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00001099 unsigned Offset = Record[0];
1100 SrcMgr::CharacteristicKind
1101 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1102 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1103 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
1104 IncludeLoc = getImportLocation(F);
1105 }
1106 unsigned Code = SLocEntryCursor.ReadCode();
1107 Record.clear();
1108 unsigned RecCode
Chris Lattner0e6c9402013-01-20 02:38:54 +00001109 = SLocEntryCursor.readRecord(Code, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00001110
1111 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1112 Error("AST record has invalid code");
1113 return true;
1114 }
1115
1116 llvm::MemoryBuffer *Buffer
Chris Lattner0e6c9402013-01-20 02:38:54 +00001117 = llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name);
Guy Benyei11169dd2012-12-18 14:30:41 +00001118 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
1119 BaseOffset + Offset, IncludeLoc);
1120 break;
1121 }
1122
1123 case SM_SLOC_EXPANSION_ENTRY: {
1124 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1125 SourceMgr.createExpansionLoc(SpellingLoc,
1126 ReadSourceLocation(*F, Record[2]),
1127 ReadSourceLocation(*F, Record[3]),
1128 Record[4],
1129 ID,
1130 BaseOffset + Record[0]);
1131 break;
1132 }
1133 }
1134
1135 return false;
1136}
1137
1138std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1139 if (ID == 0)
1140 return std::make_pair(SourceLocation(), "");
1141
1142 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1143 Error("source location entry ID out-of-range for AST file");
1144 return std::make_pair(SourceLocation(), "");
1145 }
1146
1147 // Find which module file this entry lands in.
1148 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1149 if (M->Kind != MK_Module)
1150 return std::make_pair(SourceLocation(), "");
1151
1152 // FIXME: Can we map this down to a particular submodule? That would be
1153 // ideal.
1154 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1155}
1156
1157/// \brief Find the location where the module F is imported.
1158SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1159 if (F->ImportLoc.isValid())
1160 return F->ImportLoc;
1161
1162 // Otherwise we have a PCH. It's considered to be "imported" at the first
1163 // location of its includer.
1164 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1165 // Main file is the importer. We assume that it is the first entry in the
1166 // entry table. We can't ask the manager, because at the time of PCH loading
1167 // the main file entry doesn't exist yet.
1168 // The very first entry is the invalid instantiation loc, which takes up
1169 // offsets 0 and 1.
1170 return SourceLocation::getFromRawEncoding(2U);
1171 }
1172 //return F->Loaders[0]->FirstLoc;
1173 return F->ImportedBy[0]->FirstLoc;
1174}
1175
1176/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1177/// specified cursor. Read the abbreviations that are at the top of the block
1178/// and then leave the cursor pointing into the block.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001179bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001180 if (Cursor.EnterSubBlock(BlockID)) {
1181 Error("malformed block record in AST file");
1182 return Failure;
1183 }
1184
1185 while (true) {
1186 uint64_t Offset = Cursor.GetCurrentBitNo();
1187 unsigned Code = Cursor.ReadCode();
1188
1189 // We expect all abbrevs to be at the start of the block.
1190 if (Code != llvm::bitc::DEFINE_ABBREV) {
1191 Cursor.JumpToBit(Offset);
1192 return false;
1193 }
1194 Cursor.ReadAbbrevRecord();
1195 }
1196}
1197
Richard Smithe40f2ba2013-08-07 21:41:30 +00001198Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
John McCallf413f5e2013-05-03 00:10:13 +00001199 unsigned &Idx) {
1200 Token Tok;
1201 Tok.startToken();
1202 Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1203 Tok.setLength(Record[Idx++]);
1204 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1205 Tok.setIdentifierInfo(II);
1206 Tok.setKind((tok::TokenKind)Record[Idx++]);
1207 Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1208 return Tok;
1209}
1210
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001211MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001212 BitstreamCursor &Stream = F.MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001213
1214 // Keep track of where we are in the stream, then jump back there
1215 // after reading this macro.
1216 SavedStreamPosition SavedPosition(Stream);
1217
1218 Stream.JumpToBit(Offset);
1219 RecordData Record;
1220 SmallVector<IdentifierInfo*, 16> MacroArgs;
1221 MacroInfo *Macro = 0;
1222
Guy Benyei11169dd2012-12-18 14:30:41 +00001223 while (true) {
Chris Lattnerefa77172013-01-20 00:00:22 +00001224 // Advance to the next record, but if we get to the end of the block, don't
1225 // pop it (removing all the abbreviations from the cursor) since we want to
1226 // be able to reseek within the block and read entries.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001227 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
Chris Lattnerefa77172013-01-20 00:00:22 +00001228 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks(Flags);
1229
1230 switch (Entry.Kind) {
1231 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1232 case llvm::BitstreamEntry::Error:
1233 Error("malformed block record in AST file");
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001234 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001235 case llvm::BitstreamEntry::EndBlock:
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001236 return Macro;
Chris Lattnerefa77172013-01-20 00:00:22 +00001237 case llvm::BitstreamEntry::Record:
1238 // The interesting case.
1239 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001240 }
1241
1242 // Read a record.
Guy Benyei11169dd2012-12-18 14:30:41 +00001243 Record.clear();
1244 PreprocessorRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00001245 (PreprocessorRecordTypes)Stream.readRecord(Entry.ID, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00001246 switch (RecType) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001247 case PP_MACRO_DIRECTIVE_HISTORY:
1248 return Macro;
1249
Guy Benyei11169dd2012-12-18 14:30:41 +00001250 case PP_MACRO_OBJECT_LIKE:
1251 case PP_MACRO_FUNCTION_LIKE: {
1252 // If we already have a macro, that means that we've hit the end
1253 // of the definition of the macro we were looking for. We're
1254 // done.
1255 if (Macro)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001256 return Macro;
Guy Benyei11169dd2012-12-18 14:30:41 +00001257
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001258 unsigned NextIndex = 1; // Skip identifier ID.
1259 SubmoduleID SubModID = getGlobalSubmoduleID(F, Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001260 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001261 MacroInfo *MI = PP.AllocateDeserializedMacroInfo(Loc, SubModID);
Argyrios Kyrtzidis7572be22013-01-07 19:16:23 +00001262 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
Guy Benyei11169dd2012-12-18 14:30:41 +00001263 MI->setIsUsed(Record[NextIndex++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00001264
Guy Benyei11169dd2012-12-18 14:30:41 +00001265 if (RecType == PP_MACRO_FUNCTION_LIKE) {
1266 // Decode function-like macro info.
1267 bool isC99VarArgs = Record[NextIndex++];
1268 bool isGNUVarArgs = Record[NextIndex++];
1269 bool hasCommaPasting = Record[NextIndex++];
1270 MacroArgs.clear();
1271 unsigned NumArgs = Record[NextIndex++];
1272 for (unsigned i = 0; i != NumArgs; ++i)
1273 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1274
1275 // Install function-like macro info.
1276 MI->setIsFunctionLike();
1277 if (isC99VarArgs) MI->setIsC99Varargs();
1278 if (isGNUVarArgs) MI->setIsGNUVarargs();
1279 if (hasCommaPasting) MI->setHasCommaPasting();
1280 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
1281 PP.getPreprocessorAllocator());
1282 }
1283
Guy Benyei11169dd2012-12-18 14:30:41 +00001284 // Remember that we saw this macro last so that we add the tokens that
1285 // form its body to it.
1286 Macro = MI;
1287
1288 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1289 Record[NextIndex]) {
1290 // We have a macro definition. Register the association
1291 PreprocessedEntityID
1292 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1293 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Argyrios Kyrtzidis832de9f2013-02-22 18:35:59 +00001294 PreprocessingRecord::PPEntityID
1295 PPID = PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true);
1296 MacroDefinition *PPDef =
1297 cast_or_null<MacroDefinition>(PPRec.getPreprocessedEntity(PPID));
1298 if (PPDef)
1299 PPRec.RegisterMacroDefinition(Macro, PPDef);
Guy Benyei11169dd2012-12-18 14:30:41 +00001300 }
1301
1302 ++NumMacrosRead;
1303 break;
1304 }
1305
1306 case PP_TOKEN: {
1307 // If we see a TOKEN before a PP_MACRO_*, then the file is
1308 // erroneous, just pretend we didn't see this.
1309 if (Macro == 0) break;
1310
John McCallf413f5e2013-05-03 00:10:13 +00001311 unsigned Idx = 0;
1312 Token Tok = ReadToken(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00001313 Macro->AddTokenToBody(Tok);
1314 break;
1315 }
1316 }
1317 }
1318}
1319
1320PreprocessedEntityID
1321ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
1322 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1323 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1324 assert(I != M.PreprocessedEntityRemap.end()
1325 && "Invalid index into preprocessed entity index remap");
1326
1327 return LocalID + I->second;
1328}
1329
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001330unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1331 return llvm::hash_combine(ikey.Size, ikey.ModTime);
Guy Benyei11169dd2012-12-18 14:30:41 +00001332}
1333
1334HeaderFileInfoTrait::internal_key_type
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001335HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1336 internal_key_type ikey = { FE->getSize(), FE->getModificationTime(),
1337 FE->getName() };
1338 return ikey;
1339}
Guy Benyei11169dd2012-12-18 14:30:41 +00001340
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001341bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1342 if (a.Size != b.Size || a.ModTime != b.ModTime)
Guy Benyei11169dd2012-12-18 14:30:41 +00001343 return false;
1344
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001345 if (strcmp(a.Filename, b.Filename) == 0)
1346 return true;
1347
Guy Benyei11169dd2012-12-18 14:30:41 +00001348 // Determine whether the actual files are equivalent.
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001349 FileManager &FileMgr = Reader.getFileManager();
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001350 const FileEntry *FEA = FileMgr.getFile(a.Filename);
1351 const FileEntry *FEB = FileMgr.getFile(b.Filename);
Argyrios Kyrtzidis2a513e82013-03-04 20:33:40 +00001352 return (FEA && FEA == FEB);
Guy Benyei11169dd2012-12-18 14:30:41 +00001353}
1354
1355std::pair<unsigned, unsigned>
1356HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1357 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1358 unsigned DataLen = (unsigned) *d++;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001359 return std::make_pair(KeyLen, DataLen);
Guy Benyei11169dd2012-12-18 14:30:41 +00001360}
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001361
1362HeaderFileInfoTrait::internal_key_type
1363HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1364 internal_key_type ikey;
1365 ikey.Size = off_t(clang::io::ReadUnalignedLE64(d));
1366 ikey.ModTime = time_t(clang::io::ReadUnalignedLE64(d));
1367 ikey.Filename = (const char *)d;
1368 return ikey;
1369}
1370
Guy Benyei11169dd2012-12-18 14:30:41 +00001371HeaderFileInfoTrait::data_type
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001372HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
Guy Benyei11169dd2012-12-18 14:30:41 +00001373 unsigned DataLen) {
1374 const unsigned char *End = d + DataLen;
1375 using namespace clang::io;
1376 HeaderFileInfo HFI;
1377 unsigned Flags = *d++;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001378 HFI.HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>
1379 ((Flags >> 6) & 0x03);
Guy Benyei11169dd2012-12-18 14:30:41 +00001380 HFI.isImport = (Flags >> 5) & 0x01;
1381 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1382 HFI.DirInfo = (Flags >> 2) & 0x03;
1383 HFI.Resolved = (Flags >> 1) & 0x01;
1384 HFI.IndexHeaderMapHeader = Flags & 0x01;
1385 HFI.NumIncludes = ReadUnalignedLE16(d);
1386 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1387 ReadUnalignedLE32(d));
1388 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1389 // The framework offset is 1 greater than the actual offset,
1390 // since 0 is used as an indicator for "no framework name".
1391 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1392 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1393 }
1394
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001395 if (d != End) {
1396 uint32_t LocalSMID = ReadUnalignedLE32(d);
1397 if (LocalSMID) {
1398 // This header is part of a module. Associate it with the module to enable
1399 // implicit module import.
1400 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1401 Module *Mod = Reader.getSubmodule(GlobalSMID);
1402 HFI.isModuleHeader = true;
1403 FileManager &FileMgr = Reader.getFileManager();
1404 ModuleMap &ModMap =
1405 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001406 ModMap.addHeader(Mod, FileMgr.getFile(key.Filename), HFI.getHeaderRole());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001407 }
1408 }
1409
Guy Benyei11169dd2012-12-18 14:30:41 +00001410 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1411 (void)End;
1412
1413 // This HeaderFileInfo was externally loaded.
1414 HFI.External = true;
1415 return HFI;
1416}
1417
Richard Smith49f906a2014-03-01 00:08:04 +00001418void
1419ASTReader::addPendingMacroFromModule(IdentifierInfo *II, ModuleFile *M,
1420 GlobalMacroID GMacID,
1421 llvm::ArrayRef<SubmoduleID> Overrides) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001422 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
Richard Smith49f906a2014-03-01 00:08:04 +00001423 SubmoduleID *OverrideData = 0;
1424 if (!Overrides.empty()) {
1425 OverrideData = new (Context) SubmoduleID[Overrides.size() + 1];
1426 OverrideData[0] = Overrides.size();
1427 for (unsigned I = 0; I != Overrides.size(); ++I)
1428 OverrideData[I + 1] = getGlobalSubmoduleID(*M, Overrides[I]);
1429 }
1430 PendingMacroIDs[II].push_back(PendingMacroInfo(M, GMacID, OverrideData));
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001431}
1432
1433void ASTReader::addPendingMacroFromPCH(IdentifierInfo *II,
1434 ModuleFile *M,
1435 uint64_t MacroDirectivesOffset) {
1436 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1437 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
Guy Benyei11169dd2012-12-18 14:30:41 +00001438}
1439
1440void ASTReader::ReadDefinedMacros() {
1441 // Note that we are loading defined macros.
1442 Deserializing Macros(this);
1443
1444 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1445 E = ModuleMgr.rend(); I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001446 BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001447
1448 // If there was no preprocessor block, skip this file.
1449 if (!MacroCursor.getBitStreamReader())
1450 continue;
1451
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001452 BitstreamCursor Cursor = MacroCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001453 Cursor.JumpToBit((*I)->MacroStartOffset);
1454
1455 RecordData Record;
1456 while (true) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001457 llvm::BitstreamEntry E = Cursor.advanceSkippingSubblocks();
1458
1459 switch (E.Kind) {
1460 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1461 case llvm::BitstreamEntry::Error:
1462 Error("malformed block record in AST file");
1463 return;
1464 case llvm::BitstreamEntry::EndBlock:
1465 goto NextCursor;
1466
1467 case llvm::BitstreamEntry::Record:
Chris Lattnere7b154b2013-01-19 21:39:22 +00001468 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00001469 switch (Cursor.readRecord(E.ID, Record)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00001470 default: // Default behavior: ignore.
1471 break;
1472
1473 case PP_MACRO_OBJECT_LIKE:
1474 case PP_MACRO_FUNCTION_LIKE:
1475 getLocalIdentifier(**I, Record[0]);
1476 break;
1477
1478 case PP_TOKEN:
1479 // Ignore tokens.
1480 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00001481 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001482 break;
1483 }
1484 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00001485 NextCursor: ;
Guy Benyei11169dd2012-12-18 14:30:41 +00001486 }
1487}
1488
1489namespace {
1490 /// \brief Visitor class used to look up identifirs in an AST file.
1491 class IdentifierLookupVisitor {
1492 StringRef Name;
1493 unsigned PriorGeneration;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001494 unsigned &NumIdentifierLookups;
1495 unsigned &NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001496 IdentifierInfo *Found;
Douglas Gregor00a50f72013-01-25 00:38:33 +00001497
Guy Benyei11169dd2012-12-18 14:30:41 +00001498 public:
Douglas Gregor00a50f72013-01-25 00:38:33 +00001499 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
1500 unsigned &NumIdentifierLookups,
1501 unsigned &NumIdentifierLookupHits)
Douglas Gregor7211ac12013-01-25 23:32:03 +00001502 : Name(Name), PriorGeneration(PriorGeneration),
Douglas Gregor00a50f72013-01-25 00:38:33 +00001503 NumIdentifierLookups(NumIdentifierLookups),
1504 NumIdentifierLookupHits(NumIdentifierLookupHits),
1505 Found()
1506 {
1507 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001508
1509 static bool visit(ModuleFile &M, void *UserData) {
1510 IdentifierLookupVisitor *This
1511 = static_cast<IdentifierLookupVisitor *>(UserData);
1512
1513 // If we've already searched this module file, skip it now.
1514 if (M.Generation <= This->PriorGeneration)
1515 return true;
Douglas Gregore060e572013-01-25 01:03:03 +00001516
Guy Benyei11169dd2012-12-18 14:30:41 +00001517 ASTIdentifierLookupTable *IdTable
1518 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1519 if (!IdTable)
1520 return false;
1521
1522 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1523 M, This->Found);
Douglas Gregor00a50f72013-01-25 00:38:33 +00001524 ++This->NumIdentifierLookups;
1525 ASTIdentifierLookupTable::iterator Pos = IdTable->find(This->Name,&Trait);
Guy Benyei11169dd2012-12-18 14:30:41 +00001526 if (Pos == IdTable->end())
1527 return false;
1528
1529 // Dereferencing the iterator has the effect of building the
1530 // IdentifierInfo node and populating it with the various
1531 // declarations it needs.
Douglas Gregor00a50f72013-01-25 00:38:33 +00001532 ++This->NumIdentifierLookupHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00001533 This->Found = *Pos;
1534 return true;
1535 }
1536
1537 // \brief Retrieve the identifier info found within the module
1538 // files.
1539 IdentifierInfo *getIdentifierInfo() const { return Found; }
1540 };
1541}
1542
1543void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
1544 // Note that we are loading an identifier.
1545 Deserializing AnIdentifier(this);
1546
1547 unsigned PriorGeneration = 0;
1548 if (getContext().getLangOpts().Modules)
1549 PriorGeneration = IdentifierGeneration[&II];
Douglas Gregore060e572013-01-25 01:03:03 +00001550
1551 // If there is a global index, look there first to determine which modules
1552 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00001553 GlobalModuleIndex::HitSet Hits;
1554 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00001555 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00001556 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
1557 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00001558 }
1559 }
1560
Douglas Gregor7211ac12013-01-25 23:32:03 +00001561 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
Douglas Gregor00a50f72013-01-25 00:38:33 +00001562 NumIdentifierLookups,
1563 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00001564 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00001565 markIdentifierUpToDate(&II);
1566}
1567
1568void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1569 if (!II)
1570 return;
1571
1572 II->setOutOfDate(false);
1573
1574 // Update the generation for this identifier.
1575 if (getContext().getLangOpts().Modules)
1576 IdentifierGeneration[II] = CurrentGeneration;
1577}
1578
Richard Smith49f906a2014-03-01 00:08:04 +00001579struct ASTReader::ModuleMacroInfo {
1580 SubmoduleID SubModID;
1581 MacroInfo *MI;
1582 SubmoduleID *Overrides;
1583 // FIXME: Remove this.
1584 ModuleFile *F;
1585
1586 bool isDefine() const { return MI; }
1587
1588 SubmoduleID getSubmoduleID() const { return SubModID; }
1589
1590 llvm::ArrayRef<SubmoduleID> getOverriddenSubmodules() const {
1591 if (!Overrides)
1592 return llvm::ArrayRef<SubmoduleID>();
1593 return llvm::makeArrayRef(Overrides + 1, *Overrides);
1594 }
1595
1596 DefMacroDirective *import(Preprocessor &PP, SourceLocation ImportLoc) const {
1597 if (!MI)
1598 return 0;
1599 return PP.AllocateDefMacroDirective(MI, ImportLoc, /*isImported=*/true);
1600 }
1601};
1602
1603ASTReader::ModuleMacroInfo *
1604ASTReader::getModuleMacro(const PendingMacroInfo &PMInfo) {
1605 ModuleMacroInfo Info;
1606
1607 uint32_t ID = PMInfo.ModuleMacroData.MacID;
1608 if (ID & 1) {
1609 // Macro undefinition.
1610 Info.SubModID = getGlobalSubmoduleID(*PMInfo.M, ID >> 1);
1611 Info.MI = 0;
1612 } else {
1613 // Macro definition.
1614 GlobalMacroID GMacID = getGlobalMacroID(*PMInfo.M, ID >> 1);
1615 assert(GMacID);
1616
1617 // If this macro has already been loaded, don't do so again.
1618 // FIXME: This is highly dubious. Multiple macro definitions can have the
1619 // same MacroInfo (and hence the same GMacID) due to #pragma push_macro etc.
1620 if (MacrosLoaded[GMacID - NUM_PREDEF_MACRO_IDS])
1621 return 0;
1622
1623 Info.MI = getMacro(GMacID);
1624 Info.SubModID = Info.MI->getOwningModuleID();
1625 }
1626 Info.Overrides = PMInfo.ModuleMacroData.Overrides;
1627 Info.F = PMInfo.M;
1628
1629 return new (Context) ModuleMacroInfo(Info);
1630}
1631
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001632void ASTReader::resolvePendingMacro(IdentifierInfo *II,
1633 const PendingMacroInfo &PMInfo) {
1634 assert(II);
1635
1636 if (PMInfo.M->Kind != MK_Module) {
1637 installPCHMacroDirectives(II, *PMInfo.M,
1638 PMInfo.PCHMacroData.MacroDirectivesOffset);
1639 return;
1640 }
Richard Smith49f906a2014-03-01 00:08:04 +00001641
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001642 // Module Macro.
1643
Richard Smith49f906a2014-03-01 00:08:04 +00001644 ModuleMacroInfo *MMI = getModuleMacro(PMInfo);
1645 if (!MMI)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001646 return;
1647
Richard Smith49f906a2014-03-01 00:08:04 +00001648 Module *Owner = getSubmodule(MMI->getSubmoduleID());
1649 if (Owner && Owner->NameVisibility == Module::Hidden) {
1650 // Macros in the owning module are hidden. Just remember this macro to
1651 // install if we make this module visible.
1652 HiddenNamesMap[Owner].HiddenMacros.insert(std::make_pair(II, MMI));
1653 } else {
1654 installImportedMacro(II, MMI, Owner);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001655 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001656}
1657
1658void ASTReader::installPCHMacroDirectives(IdentifierInfo *II,
1659 ModuleFile &M, uint64_t Offset) {
1660 assert(M.Kind != MK_Module);
1661
1662 BitstreamCursor &Cursor = M.MacroCursor;
1663 SavedStreamPosition SavedPosition(Cursor);
1664 Cursor.JumpToBit(Offset);
1665
1666 llvm::BitstreamEntry Entry =
1667 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
1668 if (Entry.Kind != llvm::BitstreamEntry::Record) {
1669 Error("malformed block record in AST file");
1670 return;
1671 }
1672
1673 RecordData Record;
1674 PreprocessorRecordTypes RecType =
1675 (PreprocessorRecordTypes)Cursor.readRecord(Entry.ID, Record);
1676 if (RecType != PP_MACRO_DIRECTIVE_HISTORY) {
1677 Error("malformed block record in AST file");
1678 return;
1679 }
1680
1681 // Deserialize the macro directives history in reverse source-order.
1682 MacroDirective *Latest = 0, *Earliest = 0;
1683 unsigned Idx = 0, N = Record.size();
1684 while (Idx < N) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001685 MacroDirective *MD = 0;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001686 SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001687 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
1688 switch (K) {
1689 case MacroDirective::MD_Define: {
1690 GlobalMacroID GMacID = getGlobalMacroID(M, Record[Idx++]);
1691 MacroInfo *MI = getMacro(GMacID);
1692 bool isImported = Record[Idx++];
1693 bool isAmbiguous = Record[Idx++];
1694 DefMacroDirective *DefMD =
1695 PP.AllocateDefMacroDirective(MI, Loc, isImported);
1696 DefMD->setAmbiguous(isAmbiguous);
1697 MD = DefMD;
1698 break;
1699 }
1700 case MacroDirective::MD_Undefine:
1701 MD = PP.AllocateUndefMacroDirective(Loc);
1702 break;
1703 case MacroDirective::MD_Visibility: {
1704 bool isPublic = Record[Idx++];
1705 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
1706 break;
1707 }
1708 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001709
1710 if (!Latest)
1711 Latest = MD;
1712 if (Earliest)
1713 Earliest->setPrevious(MD);
1714 Earliest = MD;
1715 }
1716
1717 PP.setLoadedMacroDirective(II, Latest);
1718}
1719
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001720/// \brief For the given macro definitions, check if they are both in system
Douglas Gregor0b202052013-04-12 21:00:54 +00001721/// modules.
1722static bool areDefinedInSystemModules(MacroInfo *PrevMI, MacroInfo *NewMI,
Douglas Gregor5e461192013-06-07 22:56:11 +00001723 Module *NewOwner, ASTReader &Reader) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001724 assert(PrevMI && NewMI);
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001725 Module *PrevOwner = 0;
1726 if (SubmoduleID PrevModID = PrevMI->getOwningModuleID())
1727 PrevOwner = Reader.getSubmodule(PrevModID);
Douglas Gregor5e461192013-06-07 22:56:11 +00001728 SourceManager &SrcMgr = Reader.getSourceManager();
1729 bool PrevInSystem
1730 = PrevOwner? PrevOwner->IsSystem
1731 : SrcMgr.isInSystemHeader(PrevMI->getDefinitionLoc());
1732 bool NewInSystem
1733 = NewOwner? NewOwner->IsSystem
1734 : SrcMgr.isInSystemHeader(NewMI->getDefinitionLoc());
1735 if (PrevOwner && PrevOwner == NewOwner)
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001736 return false;
Douglas Gregor5e461192013-06-07 22:56:11 +00001737 return PrevInSystem && NewInSystem;
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00001738}
1739
Richard Smith49f906a2014-03-01 00:08:04 +00001740void ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1741 AmbiguousMacros &Ambig,
1742 llvm::ArrayRef<SubmoduleID> Overrides) {
1743 for (unsigned OI = 0, ON = Overrides.size(); OI != ON; ++OI) {
1744 SubmoduleID OwnerID = Overrides[OI];
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001745
Richard Smith49f906a2014-03-01 00:08:04 +00001746 // If this macro is not yet visible, remove it from the hidden names list.
1747 Module *Owner = getSubmodule(OwnerID);
1748 HiddenNames &Hidden = HiddenNamesMap[Owner];
1749 HiddenMacrosMap::iterator HI = Hidden.HiddenMacros.find(II);
1750 if (HI != Hidden.HiddenMacros.end()) {
Richard Smith9d100862014-03-06 03:16:27 +00001751 auto SubOverrides = HI->second->getOverriddenSubmodules();
Richard Smith49f906a2014-03-01 00:08:04 +00001752 Hidden.HiddenMacros.erase(HI);
Richard Smith9d100862014-03-06 03:16:27 +00001753 removeOverriddenMacros(II, Ambig, SubOverrides);
Richard Smith49f906a2014-03-01 00:08:04 +00001754 }
1755
1756 // If this macro is already in our list of conflicts, remove it from there.
Richard Smithbb29e512014-03-06 00:33:23 +00001757 Ambig.erase(
1758 std::remove_if(Ambig.begin(), Ambig.end(), [&](DefMacroDirective *MD) {
1759 return MD->getInfo()->getOwningModuleID() == OwnerID;
1760 }),
1761 Ambig.end());
Richard Smith49f906a2014-03-01 00:08:04 +00001762 }
1763}
1764
1765ASTReader::AmbiguousMacros *
1766ASTReader::removeOverriddenMacros(IdentifierInfo *II,
1767 llvm::ArrayRef<SubmoduleID> Overrides) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001768 MacroDirective *Prev = PP.getMacroDirective(II);
Richard Smith49f906a2014-03-01 00:08:04 +00001769 if (!Prev && Overrides.empty())
1770 return 0;
1771
1772 DefMacroDirective *PrevDef = Prev ? Prev->getDefinition().getDirective() : 0;
1773 if (PrevDef && PrevDef->isAmbiguous()) {
1774 // We had a prior ambiguity. Check whether we resolve it (or make it worse).
1775 AmbiguousMacros &Ambig = AmbiguousMacroDefs[II];
1776 Ambig.push_back(PrevDef);
1777
1778 removeOverriddenMacros(II, Ambig, Overrides);
1779
1780 if (!Ambig.empty())
1781 return &Ambig;
1782
1783 AmbiguousMacroDefs.erase(II);
1784 } else {
1785 // There's no ambiguity yet. Maybe we're introducing one.
1786 llvm::SmallVector<DefMacroDirective*, 1> Ambig;
1787 if (PrevDef)
1788 Ambig.push_back(PrevDef);
1789
1790 removeOverriddenMacros(II, Ambig, Overrides);
1791
1792 if (!Ambig.empty()) {
1793 AmbiguousMacros &Result = AmbiguousMacroDefs[II];
1794 Result.swap(Ambig);
1795 return &Result;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001796 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001797 }
Richard Smith49f906a2014-03-01 00:08:04 +00001798
1799 // We ended up with no ambiguity.
1800 return 0;
1801}
1802
1803void ASTReader::installImportedMacro(IdentifierInfo *II, ModuleMacroInfo *MMI,
1804 Module *Owner) {
1805 assert(II && Owner);
1806
1807 SourceLocation ImportLoc = Owner->MacroVisibilityLoc;
1808 if (ImportLoc.isInvalid()) {
1809 // FIXME: If we made macros from this module visible but didn't provide a
1810 // source location for the import, we don't have a location for the macro.
1811 // Use the location at which the containing module file was first imported
1812 // for now.
1813 ImportLoc = MMI->F->DirectImportLoc;
1814 }
1815
1816 llvm::SmallVectorImpl<DefMacroDirective*> *Prev =
1817 removeOverriddenMacros(II, MMI->getOverriddenSubmodules());
1818
1819
1820 // Create a synthetic macro definition corresponding to the import (or null
1821 // if this was an undefinition of the macro).
1822 DefMacroDirective *MD = MMI->import(PP, ImportLoc);
1823
1824 // If there's no ambiguity, just install the macro.
1825 if (!Prev) {
1826 if (MD)
1827 PP.appendMacroDirective(II, MD);
1828 else
1829 PP.appendMacroDirective(II, PP.AllocateUndefMacroDirective(ImportLoc));
1830 return;
1831 }
1832 assert(!Prev->empty());
1833
1834 if (!MD) {
1835 // We imported a #undef that didn't remove all prior definitions. The most
1836 // recent prior definition remains, and we install it in the place of the
1837 // imported directive.
1838 MacroInfo *NewMI = Prev->back()->getInfo();
1839 Prev->pop_back();
1840 MD = PP.AllocateDefMacroDirective(NewMI, ImportLoc, /*Imported*/true);
1841 }
1842
1843 // We're introducing a macro definition that creates or adds to an ambiguity.
1844 // We can resolve that ambiguity if this macro is token-for-token identical to
1845 // all of the existing definitions.
1846 MacroInfo *NewMI = MD->getInfo();
1847 assert(NewMI && "macro definition with no MacroInfo?");
1848 while (!Prev->empty()) {
1849 MacroInfo *PrevMI = Prev->back()->getInfo();
1850 assert(PrevMI && "macro definition with no MacroInfo?");
1851
1852 // Before marking the macros as ambiguous, check if this is a case where
1853 // both macros are in system headers. If so, we trust that the system
1854 // did not get it wrong. This also handles cases where Clang's own
1855 // headers have a different spelling of certain system macros:
1856 // #define LONG_MAX __LONG_MAX__ (clang's limits.h)
1857 // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h)
1858 //
1859 // FIXME: Remove the defined-in-system-headers check. clang's limits.h
1860 // overrides the system limits.h's macros, so there's no conflict here.
1861 if (NewMI != PrevMI &&
1862 !PrevMI->isIdenticalTo(*NewMI, PP, /*Syntactically=*/true) &&
1863 !areDefinedInSystemModules(PrevMI, NewMI, Owner, *this))
1864 break;
1865
1866 // The previous definition is the same as this one (or both are defined in
1867 // system modules so we can assume they're equivalent); we don't need to
1868 // track it any more.
1869 Prev->pop_back();
1870 }
1871
1872 if (!Prev->empty())
1873 MD->setAmbiguous(true);
1874
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001875 PP.appendMacroDirective(II, MD);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001876}
1877
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001878ASTReader::InputFileInfo
1879ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
Ben Langmuir198c1682014-03-07 07:27:49 +00001880 // Go find this input file.
1881 BitstreamCursor &Cursor = F.InputFilesCursor;
1882 SavedStreamPosition SavedPosition(Cursor);
1883 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1884
1885 unsigned Code = Cursor.ReadCode();
1886 RecordData Record;
1887 StringRef Blob;
1888
1889 unsigned Result = Cursor.readRecord(Code, Record, &Blob);
1890 assert(static_cast<InputFileRecordTypes>(Result) == INPUT_FILE &&
1891 "invalid record type for input file");
1892 (void)Result;
1893
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001894 std::string Filename;
1895 off_t StoredSize;
1896 time_t StoredTime;
1897 bool Overridden;
1898
Ben Langmuir198c1682014-03-07 07:27:49 +00001899 assert(Record[0] == ID && "Bogus stored ID or offset");
1900 StoredSize = static_cast<off_t>(Record[1]);
1901 StoredTime = static_cast<time_t>(Record[2]);
1902 Overridden = static_cast<bool>(Record[3]);
1903 Filename = Blob;
1904 MaybeAddSystemRootToFilename(F, Filename);
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001905
1906 return { std::move(Filename), StoredSize, StoredTime, Overridden };
Ben Langmuir198c1682014-03-07 07:27:49 +00001907}
1908
1909std::string ASTReader::getInputFileName(ModuleFile &F, unsigned int ID) {
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001910 return readInputFileInfo(F, ID).Filename;
Ben Langmuir198c1682014-03-07 07:27:49 +00001911}
1912
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001913InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00001914 // If this ID is bogus, just return an empty input file.
1915 if (ID == 0 || ID > F.InputFilesLoaded.size())
1916 return InputFile();
1917
1918 // If we've already loaded this input file, return it.
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001919 if (F.InputFilesLoaded[ID-1].getFile())
Guy Benyei11169dd2012-12-18 14:30:41 +00001920 return F.InputFilesLoaded[ID-1];
1921
Argyrios Kyrtzidis9308f0a2014-01-08 19:13:34 +00001922 if (F.InputFilesLoaded[ID-1].isNotFound())
1923 return InputFile();
1924
Guy Benyei11169dd2012-12-18 14:30:41 +00001925 // Go find this input file.
Chris Lattner7fb3bef2013-01-20 00:56:42 +00001926 BitstreamCursor &Cursor = F.InputFilesCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00001927 SavedStreamPosition SavedPosition(Cursor);
1928 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1929
Argyrios Kyrtzidisce9b49e2014-03-14 02:26:27 +00001930 InputFileInfo FI = readInputFileInfo(F, ID);
1931 off_t StoredSize = FI.StoredSize;
1932 time_t StoredTime = FI.StoredTime;
1933 bool Overridden = FI.Overridden;
1934 StringRef Filename = FI.Filename;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00001935
Ben Langmuir198c1682014-03-07 07:27:49 +00001936 const FileEntry *File
1937 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1938 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1939
1940 // If we didn't find the file, resolve it relative to the
1941 // original directory from which this AST file was created.
1942 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1943 F.OriginalDir != CurrentDir) {
1944 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1945 F.OriginalDir,
1946 CurrentDir);
1947 if (!Resolved.empty())
1948 File = FileMgr.getFile(Resolved);
1949 }
1950
1951 // For an overridden file, create a virtual file with the stored
1952 // size/timestamp.
1953 if (Overridden && File == 0) {
1954 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1955 }
1956
1957 if (File == 0) {
1958 if (Complain) {
1959 std::string ErrorStr = "could not find file '";
1960 ErrorStr += Filename;
1961 ErrorStr += "' referenced by AST file";
1962 Error(ErrorStr.c_str());
Guy Benyei11169dd2012-12-18 14:30:41 +00001963 }
Ben Langmuir198c1682014-03-07 07:27:49 +00001964 // Record that we didn't find the file.
1965 F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
1966 return InputFile();
1967 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001968
Ben Langmuir198c1682014-03-07 07:27:49 +00001969 // Check if there was a request to override the contents of the file
1970 // that was part of the precompiled header. Overridding such a file
1971 // can lead to problems when lexing using the source locations from the
1972 // PCH.
1973 SourceManager &SM = getSourceManager();
1974 if (!Overridden && SM.isFileOverridden(File)) {
1975 if (Complain)
1976 Error(diag::err_fe_pch_file_overridden, Filename);
1977 // After emitting the diagnostic, recover by disabling the override so
1978 // that the original file will be used.
1979 SM.disableFileContentsOverride(File);
1980 // The FileEntry is a virtual file entry with the size of the contents
1981 // that would override the original contents. Set it to the original's
1982 // size/time.
1983 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1984 StoredSize, StoredTime);
1985 }
Guy Benyei11169dd2012-12-18 14:30:41 +00001986
Ben Langmuir198c1682014-03-07 07:27:49 +00001987 bool IsOutOfDate = false;
1988
1989 // For an overridden file, there is nothing to validate.
1990 if (!Overridden && (StoredSize != File->getSize()
Guy Benyei11169dd2012-12-18 14:30:41 +00001991#if !defined(LLVM_ON_WIN32)
Ben Langmuir198c1682014-03-07 07:27:49 +00001992 // In our regression testing, the Windows file system seems to
1993 // have inconsistent modification times that sometimes
1994 // erroneously trigger this error-handling path.
1995 || StoredTime != File->getModificationTime()
Guy Benyei11169dd2012-12-18 14:30:41 +00001996#endif
Ben Langmuir198c1682014-03-07 07:27:49 +00001997 )) {
1998 if (Complain) {
1999 // Build a list of the PCH imports that got us here (in reverse).
2000 SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2001 while (ImportStack.back()->ImportedBy.size() > 0)
2002 ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
Ben Langmuire82630d2014-01-17 00:19:09 +00002003
Ben Langmuir198c1682014-03-07 07:27:49 +00002004 // The top-level PCH is stale.
2005 StringRef TopLevelPCHName(ImportStack.back()->FileName);
2006 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName);
Ben Langmuire82630d2014-01-17 00:19:09 +00002007
Ben Langmuir198c1682014-03-07 07:27:49 +00002008 // Print the import stack.
2009 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2010 Diag(diag::note_pch_required_by)
2011 << Filename << ImportStack[0]->FileName;
2012 for (unsigned I = 1; I < ImportStack.size(); ++I)
Ben Langmuire82630d2014-01-17 00:19:09 +00002013 Diag(diag::note_pch_required_by)
Ben Langmuir198c1682014-03-07 07:27:49 +00002014 << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
Douglas Gregor7029ce12013-03-19 00:28:20 +00002015 }
2016
Ben Langmuir198c1682014-03-07 07:27:49 +00002017 if (!Diags.isDiagnosticInFlight())
2018 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
Guy Benyei11169dd2012-12-18 14:30:41 +00002019 }
2020
Ben Langmuir198c1682014-03-07 07:27:49 +00002021 IsOutOfDate = true;
Guy Benyei11169dd2012-12-18 14:30:41 +00002022 }
2023
Ben Langmuir198c1682014-03-07 07:27:49 +00002024 InputFile IF = InputFile(File, Overridden, IsOutOfDate);
2025
2026 // Note that we've loaded this input file.
2027 F.InputFilesLoaded[ID-1] = IF;
2028 return IF;
Guy Benyei11169dd2012-12-18 14:30:41 +00002029}
2030
2031const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
2032 ModuleFile &M = ModuleMgr.getPrimaryModule();
2033 std::string Filename = filenameStrRef;
2034 MaybeAddSystemRootToFilename(M, Filename);
2035 const FileEntry *File = FileMgr.getFile(Filename);
2036 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
2037 M.OriginalDir != CurrentDir) {
2038 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
2039 M.OriginalDir,
2040 CurrentDir);
2041 if (!resolved.empty())
2042 File = FileMgr.getFile(resolved);
2043 }
2044
2045 return File;
2046}
2047
2048/// \brief If we are loading a relocatable PCH file, and the filename is
2049/// not an absolute path, add the system root to the beginning of the file
2050/// name.
2051void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
2052 std::string &Filename) {
2053 // If this is not a relocatable PCH file, there's nothing to do.
2054 if (!M.RelocatablePCH)
2055 return;
2056
2057 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2058 return;
2059
2060 if (isysroot.empty()) {
2061 // If no system root was given, default to '/'
2062 Filename.insert(Filename.begin(), '/');
2063 return;
2064 }
2065
2066 unsigned Length = isysroot.size();
2067 if (isysroot[Length - 1] != '/')
2068 Filename.insert(Filename.begin(), '/');
2069
2070 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
2071}
2072
2073ASTReader::ASTReadResult
2074ASTReader::ReadControlBlock(ModuleFile &F,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002075 SmallVectorImpl<ImportedModule> &Loaded,
Guy Benyei11169dd2012-12-18 14:30:41 +00002076 unsigned ClientLoadCapabilities) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002077 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002078
2079 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2080 Error("malformed block record in AST file");
2081 return Failure;
2082 }
2083
2084 // Read all of the records and blocks in the control block.
2085 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002086 while (1) {
2087 llvm::BitstreamEntry Entry = Stream.advance();
2088
2089 switch (Entry.Kind) {
2090 case llvm::BitstreamEntry::Error:
2091 Error("malformed block record in AST file");
2092 return Failure;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002093 case llvm::BitstreamEntry::EndBlock: {
2094 // Validate input files.
2095 const HeaderSearchOptions &HSOpts =
2096 PP.getHeaderSearchInfo().getHeaderSearchOpts();
Ben Langmuircb69b572014-03-07 06:40:32 +00002097
2098 // All user input files reside at the index range [0, Record[1]), and
2099 // system input files reside at [Record[1], Record[0]).
2100 // Record is the one from INPUT_FILE_OFFSETS.
2101 unsigned NumInputs = Record[0];
2102 unsigned NumUserInputs = Record[1];
2103
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002104 if (!DisableValidation &&
2105 (!HSOpts.ModulesValidateOncePerBuildSession ||
2106 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002107 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Ben Langmuircb69b572014-03-07 06:40:32 +00002108
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002109 // If we are reading a module, we will create a verification timestamp,
2110 // so we verify all input files. Otherwise, verify only user input
2111 // files.
Ben Langmuircb69b572014-03-07 06:40:32 +00002112
2113 unsigned N = NumUserInputs;
2114 if (ValidateSystemInputs ||
Ben Langmuircb69b572014-03-07 06:40:32 +00002115 (HSOpts.ModulesValidateOncePerBuildSession && F.Kind == MK_Module))
2116 N = NumInputs;
2117
Ben Langmuir3d4417c2014-02-07 17:31:11 +00002118 for (unsigned I = 0; I < N; ++I) {
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002119 InputFile IF = getInputFile(F, I+1, Complain);
2120 if (!IF.getFile() || IF.isOutOfDate())
Guy Benyei11169dd2012-12-18 14:30:41 +00002121 return OutOfDate;
Argyrios Kyrtzidis61c3d872013-03-01 03:26:04 +00002122 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002123 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002124
Argyrios Kyrtzidis6d0753d2014-03-14 03:07:38 +00002125 if (Listener)
2126 Listener->visitModuleFile(F.FileName);
2127
Ben Langmuircb69b572014-03-07 06:40:32 +00002128 if (Listener && Listener->needsInputFileVisitation()) {
2129 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2130 : NumUserInputs;
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00002131 for (unsigned I = 0; I < N; ++I) {
2132 bool IsSystem = I >= NumUserInputs;
2133 InputFileInfo FI = readInputFileInfo(F, I+1);
2134 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden);
2135 }
Ben Langmuircb69b572014-03-07 06:40:32 +00002136 }
2137
Guy Benyei11169dd2012-12-18 14:30:41 +00002138 return Success;
Dmitri Gribenkof430da42014-02-12 10:33:14 +00002139 }
2140
Chris Lattnere7b154b2013-01-19 21:39:22 +00002141 case llvm::BitstreamEntry::SubBlock:
2142 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002143 case INPUT_FILES_BLOCK_ID:
2144 F.InputFilesCursor = Stream;
2145 if (Stream.SkipBlock() || // Skip with the main cursor
2146 // Read the abbreviations
2147 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2148 Error("malformed block record in AST file");
2149 return Failure;
2150 }
2151 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002152
Guy Benyei11169dd2012-12-18 14:30:41 +00002153 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002154 if (Stream.SkipBlock()) {
2155 Error("malformed block record in AST file");
2156 return Failure;
2157 }
2158 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00002159 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002160
2161 case llvm::BitstreamEntry::Record:
2162 // The interesting case.
2163 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002164 }
2165
2166 // Read and process a record.
2167 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002168 StringRef Blob;
2169 switch ((ControlRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002170 case METADATA: {
2171 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2172 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002173 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2174 : diag::err_pch_version_too_new);
Guy Benyei11169dd2012-12-18 14:30:41 +00002175 return VersionMismatch;
2176 }
2177
2178 bool hasErrors = Record[5];
2179 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2180 Diag(diag::err_pch_with_compiler_errors);
2181 return HadErrors;
2182 }
2183
2184 F.RelocatablePCH = Record[4];
2185
2186 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002187 StringRef ASTBranch = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002188 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2189 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00002190 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
Guy Benyei11169dd2012-12-18 14:30:41 +00002191 return VersionMismatch;
2192 }
2193 break;
2194 }
2195
2196 case IMPORTS: {
2197 // Load each of the imported PCH files.
2198 unsigned Idx = 0, N = Record.size();
2199 while (Idx < N) {
2200 // Read information about the AST file.
2201 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2202 // The import location will be the local one for now; we will adjust
2203 // all import locations of module imports after the global source
2204 // location info are setup.
2205 SourceLocation ImportLoc =
2206 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor7029ce12013-03-19 00:28:20 +00002207 off_t StoredSize = (off_t)Record[Idx++];
2208 time_t StoredModTime = (time_t)Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00002209 unsigned Length = Record[Idx++];
2210 SmallString<128> ImportedFile(Record.begin() + Idx,
2211 Record.begin() + Idx + Length);
2212 Idx += Length;
2213
2214 // Load the AST file.
2215 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00002216 StoredSize, StoredModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00002217 ClientLoadCapabilities)) {
2218 case Failure: return Failure;
2219 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor2f1806e2013-03-19 00:38:50 +00002220 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00002221 case OutOfDate: return OutOfDate;
2222 case VersionMismatch: return VersionMismatch;
2223 case ConfigurationMismatch: return ConfigurationMismatch;
2224 case HadErrors: return HadErrors;
2225 case Success: break;
2226 }
2227 }
2228 break;
2229 }
2230
2231 case LANGUAGE_OPTIONS: {
2232 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2233 if (Listener && &F == *ModuleMgr.begin() &&
2234 ParseLanguageOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002235 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002236 return ConfigurationMismatch;
2237 break;
2238 }
2239
2240 case TARGET_OPTIONS: {
2241 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2242 if (Listener && &F == *ModuleMgr.begin() &&
2243 ParseTargetOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002244 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002245 return ConfigurationMismatch;
2246 break;
2247 }
2248
2249 case DIAGNOSTIC_OPTIONS: {
2250 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2251 if (Listener && &F == *ModuleMgr.begin() &&
2252 ParseDiagnosticOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002253 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002254 return ConfigurationMismatch;
2255 break;
2256 }
2257
2258 case FILE_SYSTEM_OPTIONS: {
2259 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2260 if (Listener && &F == *ModuleMgr.begin() &&
2261 ParseFileSystemOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002262 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002263 return ConfigurationMismatch;
2264 break;
2265 }
2266
2267 case HEADER_SEARCH_OPTIONS: {
2268 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2269 if (Listener && &F == *ModuleMgr.begin() &&
2270 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002271 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002272 return ConfigurationMismatch;
2273 break;
2274 }
2275
2276 case PREPROCESSOR_OPTIONS: {
2277 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
2278 if (Listener && &F == *ModuleMgr.begin() &&
2279 ParsePreprocessorOptions(Record, Complain, *Listener,
2280 SuggestedPredefines) &&
Ben Langmuir2cb4a782014-02-05 22:21:15 +00002281 !DisableValidation && !AllowConfigurationMismatch)
Guy Benyei11169dd2012-12-18 14:30:41 +00002282 return ConfigurationMismatch;
2283 break;
2284 }
2285
2286 case ORIGINAL_FILE:
2287 F.OriginalSourceFileID = FileID::get(Record[0]);
Chris Lattner0e6c9402013-01-20 02:38:54 +00002288 F.ActualOriginalSourceFileName = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002289 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2290 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
2291 break;
2292
2293 case ORIGINAL_FILE_ID:
2294 F.OriginalSourceFileID = FileID::get(Record[0]);
2295 break;
2296
2297 case ORIGINAL_PCH_DIR:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002298 F.OriginalDir = Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00002299 break;
2300
2301 case INPUT_FILE_OFFSETS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002302 F.InputFileOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002303 F.InputFilesLoaded.resize(Record[0]);
2304 break;
2305 }
2306 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002307}
2308
2309bool ASTReader::ReadASTBlock(ModuleFile &F) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002310 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002311
2312 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
2313 Error("malformed block record in AST file");
2314 return true;
2315 }
2316
2317 // Read all of the records and blocks for the AST file.
2318 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002319 while (1) {
2320 llvm::BitstreamEntry Entry = Stream.advance();
2321
2322 switch (Entry.Kind) {
2323 case llvm::BitstreamEntry::Error:
2324 Error("error at end of module block in AST file");
2325 return true;
2326 case llvm::BitstreamEntry::EndBlock: {
Richard Smithc0fbba72013-04-03 22:49:41 +00002327 // Outside of C++, we do not store a lookup map for the translation unit.
2328 // Instead, mark it as needing a lookup map to be built if this module
2329 // contains any declarations lexically within it (which it always does!).
2330 // This usually has no cost, since we very rarely need the lookup map for
2331 // the translation unit outside C++.
Guy Benyei11169dd2012-12-18 14:30:41 +00002332 DeclContext *DC = Context.getTranslationUnitDecl();
Richard Smithc0fbba72013-04-03 22:49:41 +00002333 if (DC->hasExternalLexicalStorage() &&
2334 !getContext().getLangOpts().CPlusPlus)
Guy Benyei11169dd2012-12-18 14:30:41 +00002335 DC->setMustBuildLookupTable();
Chris Lattnere7b154b2013-01-19 21:39:22 +00002336
Guy Benyei11169dd2012-12-18 14:30:41 +00002337 return false;
2338 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002339 case llvm::BitstreamEntry::SubBlock:
2340 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002341 case DECLTYPES_BLOCK_ID:
2342 // We lazily load the decls block, but we want to set up the
2343 // DeclsCursor cursor to point into it. Clone our current bitcode
2344 // cursor to it, enter the block and read the abbrevs in that block.
2345 // With the main cursor, we just skip over it.
2346 F.DeclsCursor = Stream;
2347 if (Stream.SkipBlock() || // Skip with the main cursor.
2348 // Read the abbrevs.
2349 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
2350 Error("malformed block record in AST file");
2351 return true;
2352 }
2353 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002354
Guy Benyei11169dd2012-12-18 14:30:41 +00002355 case DECL_UPDATES_BLOCK_ID:
2356 if (Stream.SkipBlock()) {
2357 Error("malformed block record in AST file");
2358 return true;
2359 }
2360 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002361
Guy Benyei11169dd2012-12-18 14:30:41 +00002362 case PREPROCESSOR_BLOCK_ID:
2363 F.MacroCursor = Stream;
2364 if (!PP.getExternalSource())
2365 PP.setExternalSource(this);
Chris Lattnere7b154b2013-01-19 21:39:22 +00002366
Guy Benyei11169dd2012-12-18 14:30:41 +00002367 if (Stream.SkipBlock() ||
2368 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2369 Error("malformed block record in AST file");
2370 return true;
2371 }
2372 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2373 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002374
Guy Benyei11169dd2012-12-18 14:30:41 +00002375 case PREPROCESSOR_DETAIL_BLOCK_ID:
2376 F.PreprocessorDetailCursor = Stream;
2377 if (Stream.SkipBlock() ||
Chris Lattnere7b154b2013-01-19 21:39:22 +00002378 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00002379 PREPROCESSOR_DETAIL_BLOCK_ID)) {
Chris Lattnere7b154b2013-01-19 21:39:22 +00002380 Error("malformed preprocessor detail record in AST file");
2381 return true;
2382 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002383 F.PreprocessorDetailStartOffset
Chris Lattnere7b154b2013-01-19 21:39:22 +00002384 = F.PreprocessorDetailCursor.GetCurrentBitNo();
2385
Guy Benyei11169dd2012-12-18 14:30:41 +00002386 if (!PP.getPreprocessingRecord())
2387 PP.createPreprocessingRecord();
2388 if (!PP.getPreprocessingRecord()->getExternalSource())
2389 PP.getPreprocessingRecord()->SetExternalSource(*this);
2390 break;
2391
2392 case SOURCE_MANAGER_BLOCK_ID:
2393 if (ReadSourceManagerBlock(F))
2394 return true;
2395 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002396
Guy Benyei11169dd2012-12-18 14:30:41 +00002397 case SUBMODULE_BLOCK_ID:
2398 if (ReadSubmoduleBlock(F))
2399 return true;
2400 break;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002401
Guy Benyei11169dd2012-12-18 14:30:41 +00002402 case COMMENTS_BLOCK_ID: {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00002403 BitstreamCursor C = Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00002404 if (Stream.SkipBlock() ||
2405 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2406 Error("malformed comments block in AST file");
2407 return true;
2408 }
2409 CommentsCursors.push_back(std::make_pair(C, &F));
2410 break;
2411 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00002412
Guy Benyei11169dd2012-12-18 14:30:41 +00002413 default:
Chris Lattnere7b154b2013-01-19 21:39:22 +00002414 if (Stream.SkipBlock()) {
2415 Error("malformed block record in AST file");
2416 return true;
2417 }
2418 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002419 }
2420 continue;
Chris Lattnere7b154b2013-01-19 21:39:22 +00002421
2422 case llvm::BitstreamEntry::Record:
2423 // The interesting case.
2424 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00002425 }
2426
2427 // Read and process a record.
2428 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00002429 StringRef Blob;
2430 switch ((ASTRecordTypes)Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00002431 default: // Default behavior: ignore.
2432 break;
2433
2434 case TYPE_OFFSET: {
2435 if (F.LocalNumTypes != 0) {
2436 Error("duplicate TYPE_OFFSET record in AST file");
2437 return true;
2438 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002439 F.TypeOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002440 F.LocalNumTypes = Record[0];
2441 unsigned LocalBaseTypeIndex = Record[1];
2442 F.BaseTypeIndex = getTotalNumTypes();
2443
2444 if (F.LocalNumTypes > 0) {
2445 // Introduce the global -> local mapping for types within this module.
2446 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2447
2448 // Introduce the local -> global mapping for types within this module.
2449 F.TypeRemap.insertOrReplace(
2450 std::make_pair(LocalBaseTypeIndex,
2451 F.BaseTypeIndex - LocalBaseTypeIndex));
2452
2453 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2454 }
2455 break;
2456 }
2457
2458 case DECL_OFFSET: {
2459 if (F.LocalNumDecls != 0) {
2460 Error("duplicate DECL_OFFSET record in AST file");
2461 return true;
2462 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002463 F.DeclOffsets = (const DeclOffset *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002464 F.LocalNumDecls = Record[0];
2465 unsigned LocalBaseDeclID = Record[1];
2466 F.BaseDeclID = getTotalNumDecls();
2467
2468 if (F.LocalNumDecls > 0) {
2469 // Introduce the global -> local mapping for declarations within this
2470 // module.
2471 GlobalDeclMap.insert(
2472 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
2473
2474 // Introduce the local -> global mapping for declarations within this
2475 // module.
2476 F.DeclRemap.insertOrReplace(
2477 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
2478
2479 // Introduce the global -> local mapping for declarations within this
2480 // module.
2481 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2482
2483 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2484 }
2485 break;
2486 }
2487
2488 case TU_UPDATE_LEXICAL: {
2489 DeclContext *TU = Context.getTranslationUnitDecl();
2490 DeclContextInfo &Info = F.DeclContextInfos[TU];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002491 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(Blob.data());
Guy Benyei11169dd2012-12-18 14:30:41 +00002492 Info.NumLexicalDecls
Chris Lattner0e6c9402013-01-20 02:38:54 +00002493 = static_cast<unsigned int>(Blob.size() / sizeof(KindDeclIDPair));
Guy Benyei11169dd2012-12-18 14:30:41 +00002494 TU->setHasExternalLexicalStorage(true);
2495 break;
2496 }
2497
2498 case UPDATE_VISIBLE: {
2499 unsigned Idx = 0;
2500 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
2501 ASTDeclContextNameLookupTable *Table =
2502 ASTDeclContextNameLookupTable::Create(
Chris Lattner0e6c9402013-01-20 02:38:54 +00002503 (const unsigned char *)Blob.data() + Record[Idx++],
2504 (const unsigned char *)Blob.data(),
Guy Benyei11169dd2012-12-18 14:30:41 +00002505 ASTDeclContextNameLookupTrait(*this, F));
2506 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2507 DeclContext *TU = Context.getTranslationUnitDecl();
Richard Smith52e3fba2014-03-11 07:17:35 +00002508 F.DeclContextInfos[TU].NameLookupTableData = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002509 TU->setHasExternalVisibleStorage(true);
Richard Smithd9174792014-03-11 03:10:46 +00002510 } else if (Decl *D = DeclsLoaded[ID - NUM_PREDEF_DECL_IDS]) {
2511 auto *DC = cast<DeclContext>(D);
2512 DC->getPrimaryContext()->setHasExternalVisibleStorage(true);
Richard Smith52e3fba2014-03-11 07:17:35 +00002513 auto *&LookupTable = F.DeclContextInfos[DC].NameLookupTableData;
2514 delete LookupTable;
2515 LookupTable = Table;
Guy Benyei11169dd2012-12-18 14:30:41 +00002516 } else
2517 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
2518 break;
2519 }
2520
2521 case IDENTIFIER_TABLE:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002522 F.IdentifierTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002523 if (Record[0]) {
2524 F.IdentifierLookupTable
2525 = ASTIdentifierLookupTable::Create(
2526 (const unsigned char *)F.IdentifierTableData + Record[0],
2527 (const unsigned char *)F.IdentifierTableData,
2528 ASTIdentifierLookupTrait(*this, F));
2529
2530 PP.getIdentifierTable().setExternalIdentifierLookup(this);
2531 }
2532 break;
2533
2534 case IDENTIFIER_OFFSET: {
2535 if (F.LocalNumIdentifiers != 0) {
2536 Error("duplicate IDENTIFIER_OFFSET record in AST file");
2537 return true;
2538 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00002539 F.IdentifierOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002540 F.LocalNumIdentifiers = Record[0];
2541 unsigned LocalBaseIdentifierID = Record[1];
2542 F.BaseIdentifierID = getTotalNumIdentifiers();
2543
2544 if (F.LocalNumIdentifiers > 0) {
2545 // Introduce the global -> local mapping for identifiers within this
2546 // module.
2547 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2548 &F));
2549
2550 // Introduce the local -> global mapping for identifiers within this
2551 // module.
2552 F.IdentifierRemap.insertOrReplace(
2553 std::make_pair(LocalBaseIdentifierID,
2554 F.BaseIdentifierID - LocalBaseIdentifierID));
2555
2556 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2557 + F.LocalNumIdentifiers);
2558 }
2559 break;
2560 }
2561
Ben Langmuir332aafe2014-01-31 01:06:56 +00002562 case EAGERLY_DESERIALIZED_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002563 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Ben Langmuir332aafe2014-01-31 01:06:56 +00002564 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002565 break;
2566
2567 case SPECIAL_TYPES:
Douglas Gregor44180f82013-02-01 23:45:03 +00002568 if (SpecialTypes.empty()) {
2569 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2570 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
2571 break;
2572 }
2573
2574 if (SpecialTypes.size() != Record.size()) {
2575 Error("invalid special-types record");
2576 return true;
2577 }
2578
2579 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2580 serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
2581 if (!SpecialTypes[I])
2582 SpecialTypes[I] = ID;
2583 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
2584 // merge step?
2585 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002586 break;
2587
2588 case STATISTICS:
2589 TotalNumStatements += Record[0];
2590 TotalNumMacros += Record[1];
2591 TotalLexicalDeclContexts += Record[2];
2592 TotalVisibleDeclContexts += Record[3];
2593 break;
2594
2595 case UNUSED_FILESCOPED_DECLS:
2596 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2597 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
2598 break;
2599
2600 case DELEGATING_CTORS:
2601 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2602 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
2603 break;
2604
2605 case WEAK_UNDECLARED_IDENTIFIERS:
2606 if (Record.size() % 4 != 0) {
2607 Error("invalid weak identifiers record");
2608 return true;
2609 }
2610
2611 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2612 // files. This isn't the way to do it :)
2613 WeakUndeclaredIdentifiers.clear();
2614
2615 // Translate the weak, undeclared identifiers into global IDs.
2616 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2617 WeakUndeclaredIdentifiers.push_back(
2618 getGlobalIdentifierID(F, Record[I++]));
2619 WeakUndeclaredIdentifiers.push_back(
2620 getGlobalIdentifierID(F, Record[I++]));
2621 WeakUndeclaredIdentifiers.push_back(
2622 ReadSourceLocation(F, Record, I).getRawEncoding());
2623 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2624 }
2625 break;
2626
Richard Smith78165b52013-01-10 23:43:47 +00002627 case LOCALLY_SCOPED_EXTERN_C_DECLS:
Guy Benyei11169dd2012-12-18 14:30:41 +00002628 for (unsigned I = 0, N = Record.size(); I != N; ++I)
Richard Smith78165b52013-01-10 23:43:47 +00002629 LocallyScopedExternCDecls.push_back(getGlobalDeclID(F, Record[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002630 break;
2631
2632 case SELECTOR_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002633 F.SelectorOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002634 F.LocalNumSelectors = Record[0];
2635 unsigned LocalBaseSelectorID = Record[1];
2636 F.BaseSelectorID = getTotalNumSelectors();
2637
2638 if (F.LocalNumSelectors > 0) {
2639 // Introduce the global -> local mapping for selectors within this
2640 // module.
2641 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2642
2643 // Introduce the local -> global mapping for selectors within this
2644 // module.
2645 F.SelectorRemap.insertOrReplace(
2646 std::make_pair(LocalBaseSelectorID,
2647 F.BaseSelectorID - LocalBaseSelectorID));
2648
2649 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2650 }
2651 break;
2652 }
2653
2654 case METHOD_POOL:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002655 F.SelectorLookupTableData = (const unsigned char *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002656 if (Record[0])
2657 F.SelectorLookupTable
2658 = ASTSelectorLookupTable::Create(
2659 F.SelectorLookupTableData + Record[0],
2660 F.SelectorLookupTableData,
2661 ASTSelectorLookupTrait(*this, F));
2662 TotalNumMethodPoolEntries += Record[1];
2663 break;
2664
2665 case REFERENCED_SELECTOR_POOL:
2666 if (!Record.empty()) {
2667 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2668 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2669 Record[Idx++]));
2670 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2671 getRawEncoding());
2672 }
2673 }
2674 break;
2675
2676 case PP_COUNTER_VALUE:
2677 if (!Record.empty() && Listener)
2678 Listener->ReadCounter(F, Record[0]);
2679 break;
2680
2681 case FILE_SORTED_DECLS:
Chris Lattner0e6c9402013-01-20 02:38:54 +00002682 F.FileSortedDecls = (const DeclID *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002683 F.NumFileSortedDecls = Record[0];
2684 break;
2685
2686 case SOURCE_LOCATION_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002687 F.SLocEntryOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002688 F.LocalNumSLocEntries = Record[0];
2689 unsigned SLocSpaceSize = Record[1];
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002690 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Guy Benyei11169dd2012-12-18 14:30:41 +00002691 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2692 SLocSpaceSize);
2693 // Make our entry in the range map. BaseID is negative and growing, so
2694 // we invert it. Because we invert it, though, we need the other end of
2695 // the range.
2696 unsigned RangeStart =
2697 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2698 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2699 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2700
2701 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2702 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2703 GlobalSLocOffsetMap.insert(
2704 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2705 - SLocSpaceSize,&F));
2706
2707 // Initialize the remapping table.
2708 // Invalid stays invalid.
2709 F.SLocRemap.insert(std::make_pair(0U, 0));
2710 // This module. Base was 2 when being compiled.
2711 F.SLocRemap.insert(std::make_pair(2U,
2712 static_cast<int>(F.SLocEntryBaseOffset - 2)));
2713
2714 TotalNumSLocEntries += F.LocalNumSLocEntries;
2715 break;
2716 }
2717
2718 case MODULE_OFFSET_MAP: {
2719 // Additional remapping information.
Chris Lattner0e6c9402013-01-20 02:38:54 +00002720 const unsigned char *Data = (const unsigned char*)Blob.data();
2721 const unsigned char *DataEnd = Data + Blob.size();
Guy Benyei11169dd2012-12-18 14:30:41 +00002722
2723 // Continuous range maps we may be updating in our module.
2724 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
2725 ContinuousRangeMap<uint32_t, int, 2>::Builder
2726 IdentifierRemap(F.IdentifierRemap);
2727 ContinuousRangeMap<uint32_t, int, 2>::Builder
2728 MacroRemap(F.MacroRemap);
2729 ContinuousRangeMap<uint32_t, int, 2>::Builder
2730 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2731 ContinuousRangeMap<uint32_t, int, 2>::Builder
2732 SubmoduleRemap(F.SubmoduleRemap);
2733 ContinuousRangeMap<uint32_t, int, 2>::Builder
2734 SelectorRemap(F.SelectorRemap);
2735 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
2736 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2737
2738 while(Data < DataEnd) {
2739 uint16_t Len = io::ReadUnalignedLE16(Data);
2740 StringRef Name = StringRef((const char*)Data, Len);
2741 Data += Len;
2742 ModuleFile *OM = ModuleMgr.lookup(Name);
2743 if (!OM) {
2744 Error("SourceLocation remap refers to unknown module");
2745 return true;
2746 }
2747
2748 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2749 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2750 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
2751 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
2752 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
2753 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2754 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
2755 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
2756
2757 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2758 SLocRemap.insert(std::make_pair(SLocOffset,
2759 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
2760 IdentifierRemap.insert(
2761 std::make_pair(IdentifierIDOffset,
2762 OM->BaseIdentifierID - IdentifierIDOffset));
2763 MacroRemap.insert(std::make_pair(MacroIDOffset,
2764 OM->BaseMacroID - MacroIDOffset));
2765 PreprocessedEntityRemap.insert(
2766 std::make_pair(PreprocessedEntityIDOffset,
2767 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
2768 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2769 OM->BaseSubmoduleID - SubmoduleIDOffset));
2770 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2771 OM->BaseSelectorID - SelectorIDOffset));
2772 DeclRemap.insert(std::make_pair(DeclIDOffset,
2773 OM->BaseDeclID - DeclIDOffset));
2774
2775 TypeRemap.insert(std::make_pair(TypeIndexOffset,
2776 OM->BaseTypeIndex - TypeIndexOffset));
2777
2778 // Global -> local mappings.
2779 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
2780 }
2781 break;
2782 }
2783
2784 case SOURCE_MANAGER_LINE_TABLE:
2785 if (ParseLineTable(F, Record))
2786 return true;
2787 break;
2788
2789 case SOURCE_LOCATION_PRELOADS: {
2790 // Need to transform from the local view (1-based IDs) to the global view,
2791 // which is based off F.SLocEntryBaseID.
2792 if (!F.PreloadSLocEntries.empty()) {
2793 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2794 return true;
2795 }
2796
2797 F.PreloadSLocEntries.swap(Record);
2798 break;
2799 }
2800
2801 case EXT_VECTOR_DECLS:
2802 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2803 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
2804 break;
2805
2806 case VTABLE_USES:
2807 if (Record.size() % 3 != 0) {
2808 Error("Invalid VTABLE_USES record");
2809 return true;
2810 }
2811
2812 // Later tables overwrite earlier ones.
2813 // FIXME: Modules will have some trouble with this. This is clearly not
2814 // the right way to do this.
2815 VTableUses.clear();
2816
2817 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2818 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2819 VTableUses.push_back(
2820 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2821 VTableUses.push_back(Record[Idx++]);
2822 }
2823 break;
2824
2825 case DYNAMIC_CLASSES:
2826 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2827 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
2828 break;
2829
2830 case PENDING_IMPLICIT_INSTANTIATIONS:
2831 if (PendingInstantiations.size() % 2 != 0) {
2832 Error("Invalid existing PendingInstantiations");
2833 return true;
2834 }
2835
2836 if (Record.size() % 2 != 0) {
2837 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2838 return true;
2839 }
2840
2841 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2842 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2843 PendingInstantiations.push_back(
2844 ReadSourceLocation(F, Record, I).getRawEncoding());
2845 }
2846 break;
2847
2848 case SEMA_DECL_REFS:
Richard Smith3d8e97e2013-10-18 06:54:39 +00002849 if (Record.size() != 2) {
2850 Error("Invalid SEMA_DECL_REFS block");
2851 return true;
2852 }
Guy Benyei11169dd2012-12-18 14:30:41 +00002853 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2854 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2855 break;
2856
2857 case PPD_ENTITIES_OFFSETS: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002858 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
2859 assert(Blob.size() % sizeof(PPEntityOffset) == 0);
2860 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
Guy Benyei11169dd2012-12-18 14:30:41 +00002861
2862 unsigned LocalBasePreprocessedEntityID = Record[0];
2863
2864 unsigned StartingID;
2865 if (!PP.getPreprocessingRecord())
2866 PP.createPreprocessingRecord();
2867 if (!PP.getPreprocessingRecord()->getExternalSource())
2868 PP.getPreprocessingRecord()->SetExternalSource(*this);
2869 StartingID
2870 = PP.getPreprocessingRecord()
2871 ->allocateLoadedEntities(F.NumPreprocessedEntities);
2872 F.BasePreprocessedEntityID = StartingID;
2873
2874 if (F.NumPreprocessedEntities > 0) {
2875 // Introduce the global -> local mapping for preprocessed entities in
2876 // this module.
2877 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2878
2879 // Introduce the local -> global mapping for preprocessed entities in
2880 // this module.
2881 F.PreprocessedEntityRemap.insertOrReplace(
2882 std::make_pair(LocalBasePreprocessedEntityID,
2883 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2884 }
2885
2886 break;
2887 }
2888
2889 case DECL_UPDATE_OFFSETS: {
2890 if (Record.size() % 2 != 0) {
2891 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2892 return true;
2893 }
2894 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
2895 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2896 .push_back(std::make_pair(&F, Record[I+1]));
2897 break;
2898 }
2899
2900 case DECL_REPLACEMENTS: {
2901 if (Record.size() % 3 != 0) {
2902 Error("invalid DECL_REPLACEMENTS block in AST file");
2903 return true;
2904 }
2905 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
2906 ReplacedDecls[getGlobalDeclID(F, Record[I])]
2907 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
2908 break;
2909 }
2910
2911 case OBJC_CATEGORIES_MAP: {
2912 if (F.LocalNumObjCCategoriesInMap != 0) {
2913 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
2914 return true;
2915 }
2916
2917 F.LocalNumObjCCategoriesInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002918 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002919 break;
2920 }
2921
2922 case OBJC_CATEGORIES:
2923 F.ObjCCategories.swap(Record);
2924 break;
2925
2926 case CXX_BASE_SPECIFIER_OFFSETS: {
2927 if (F.LocalNumCXXBaseSpecifiers != 0) {
2928 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2929 return true;
2930 }
2931
2932 F.LocalNumCXXBaseSpecifiers = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00002933 F.CXXBaseSpecifiersOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002934 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
2935 break;
2936 }
2937
2938 case DIAG_PRAGMA_MAPPINGS:
2939 if (F.PragmaDiagMappings.empty())
2940 F.PragmaDiagMappings.swap(Record);
2941 else
2942 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2943 Record.begin(), Record.end());
2944 break;
2945
2946 case CUDA_SPECIAL_DECL_REFS:
2947 // Later tables overwrite earlier ones.
2948 // FIXME: Modules will have trouble with this.
2949 CUDASpecialDeclRefs.clear();
2950 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2951 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
2952 break;
2953
2954 case HEADER_SEARCH_TABLE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00002955 F.HeaderFileInfoTableData = Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00002956 F.LocalNumHeaderFileInfos = Record[1];
Guy Benyei11169dd2012-12-18 14:30:41 +00002957 if (Record[0]) {
2958 F.HeaderFileInfoTable
2959 = HeaderFileInfoLookupTable::Create(
2960 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
2961 (const unsigned char *)F.HeaderFileInfoTableData,
2962 HeaderFileInfoTrait(*this, F,
2963 &PP.getHeaderSearchInfo(),
Chris Lattner0e6c9402013-01-20 02:38:54 +00002964 Blob.data() + Record[2]));
Guy Benyei11169dd2012-12-18 14:30:41 +00002965
2966 PP.getHeaderSearchInfo().SetExternalSource(this);
2967 if (!PP.getHeaderSearchInfo().getExternalLookup())
2968 PP.getHeaderSearchInfo().SetExternalLookup(this);
2969 }
2970 break;
2971 }
2972
2973 case FP_PRAGMA_OPTIONS:
2974 // Later tables overwrite earlier ones.
2975 FPPragmaOptions.swap(Record);
2976 break;
2977
2978 case OPENCL_EXTENSIONS:
2979 // Later tables overwrite earlier ones.
2980 OpenCLExtensions.swap(Record);
2981 break;
2982
2983 case TENTATIVE_DEFINITIONS:
2984 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2985 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
2986 break;
2987
2988 case KNOWN_NAMESPACES:
2989 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2990 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
2991 break;
Nick Lewycky8334af82013-01-26 00:35:08 +00002992
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00002993 case UNDEFINED_BUT_USED:
2994 if (UndefinedButUsed.size() % 2 != 0) {
2995 Error("Invalid existing UndefinedButUsed");
Nick Lewycky8334af82013-01-26 00:35:08 +00002996 return true;
2997 }
2998
2999 if (Record.size() % 2 != 0) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003000 Error("invalid undefined-but-used record");
Nick Lewycky8334af82013-01-26 00:35:08 +00003001 return true;
3002 }
3003 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00003004 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3005 UndefinedButUsed.push_back(
Nick Lewycky8334af82013-01-26 00:35:08 +00003006 ReadSourceLocation(F, Record, I).getRawEncoding());
3007 }
3008 break;
3009
Guy Benyei11169dd2012-12-18 14:30:41 +00003010 case IMPORTED_MODULES: {
3011 if (F.Kind != MK_Module) {
3012 // If we aren't loading a module (which has its own exports), make
3013 // all of the imported modules visible.
3014 // FIXME: Deal with macros-only imports.
3015 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3016 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
3017 ImportedModules.push_back(GlobalID);
3018 }
3019 }
3020 break;
3021 }
3022
3023 case LOCAL_REDECLARATIONS: {
3024 F.RedeclarationChains.swap(Record);
3025 break;
3026 }
3027
3028 case LOCAL_REDECLARATIONS_MAP: {
3029 if (F.LocalNumRedeclarationsInMap != 0) {
3030 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
3031 return true;
3032 }
3033
3034 F.LocalNumRedeclarationsInMap = Record[0];
Chris Lattner0e6c9402013-01-20 02:38:54 +00003035 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003036 break;
3037 }
3038
3039 case MERGED_DECLARATIONS: {
3040 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
3041 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
3042 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
3043 for (unsigned N = Record[Idx++]; N > 0; --N)
3044 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
3045 }
3046 break;
3047 }
3048
3049 case MACRO_OFFSET: {
3050 if (F.LocalNumMacros != 0) {
3051 Error("duplicate MACRO_OFFSET record in AST file");
3052 return true;
3053 }
Chris Lattner0e6c9402013-01-20 02:38:54 +00003054 F.MacroOffsets = (const uint32_t *)Blob.data();
Guy Benyei11169dd2012-12-18 14:30:41 +00003055 F.LocalNumMacros = Record[0];
3056 unsigned LocalBaseMacroID = Record[1];
3057 F.BaseMacroID = getTotalNumMacros();
3058
3059 if (F.LocalNumMacros > 0) {
3060 // Introduce the global -> local mapping for macros within this module.
3061 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3062
3063 // Introduce the local -> global mapping for macros within this module.
3064 F.MacroRemap.insertOrReplace(
3065 std::make_pair(LocalBaseMacroID,
3066 F.BaseMacroID - LocalBaseMacroID));
3067
3068 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3069 }
3070 break;
3071 }
3072
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003073 case MACRO_TABLE: {
3074 // FIXME: Not used yet.
Guy Benyei11169dd2012-12-18 14:30:41 +00003075 break;
3076 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00003077
3078 case LATE_PARSED_TEMPLATE: {
3079 LateParsedTemplates.append(Record.begin(), Record.end());
3080 break;
3081 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003082 }
3083 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003084}
3085
Douglas Gregorc1489562013-02-12 23:36:21 +00003086/// \brief Move the given method to the back of the global list of methods.
3087static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
3088 // Find the entry for this selector in the method pool.
3089 Sema::GlobalMethodPool::iterator Known
3090 = S.MethodPool.find(Method->getSelector());
3091 if (Known == S.MethodPool.end())
3092 return;
3093
3094 // Retrieve the appropriate method list.
3095 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
3096 : Known->second.second;
3097 bool Found = false;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003098 for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
Douglas Gregorc1489562013-02-12 23:36:21 +00003099 if (!Found) {
3100 if (List->Method == Method) {
3101 Found = true;
3102 } else {
3103 // Keep searching.
3104 continue;
3105 }
3106 }
3107
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003108 if (List->getNext())
3109 List->Method = List->getNext()->Method;
Douglas Gregorc1489562013-02-12 23:36:21 +00003110 else
3111 List->Method = Method;
3112 }
3113}
3114
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003115void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
Richard Smith49f906a2014-03-01 00:08:04 +00003116 for (unsigned I = 0, N = Names.HiddenDecls.size(); I != N; ++I) {
3117 Decl *D = Names.HiddenDecls[I];
3118 bool wasHidden = D->Hidden;
3119 D->Hidden = false;
Guy Benyei11169dd2012-12-18 14:30:41 +00003120
Richard Smith49f906a2014-03-01 00:08:04 +00003121 if (wasHidden && SemaObj) {
3122 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
3123 moveMethodToBackOfGlobalList(*SemaObj, Method);
Douglas Gregorc1489562013-02-12 23:36:21 +00003124 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003125 }
3126 }
Richard Smith49f906a2014-03-01 00:08:04 +00003127
3128 for (HiddenMacrosMap::const_iterator I = Names.HiddenMacros.begin(),
3129 E = Names.HiddenMacros.end();
3130 I != E; ++I)
3131 installImportedMacro(I->first, I->second, Owner);
Guy Benyei11169dd2012-12-18 14:30:41 +00003132}
3133
Richard Smith49f906a2014-03-01 00:08:04 +00003134void ASTReader::makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003135 Module::NameVisibilityKind NameVisibility,
Douglas Gregorfb912652013-03-20 21:10:35 +00003136 SourceLocation ImportLoc,
3137 bool Complain) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003138 llvm::SmallPtrSet<Module *, 4> Visited;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003139 SmallVector<Module *, 4> Stack;
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003140 Stack.push_back(Mod);
Guy Benyei11169dd2012-12-18 14:30:41 +00003141 while (!Stack.empty()) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003142 Mod = Stack.pop_back_val();
Guy Benyei11169dd2012-12-18 14:30:41 +00003143
3144 if (NameVisibility <= Mod->NameVisibility) {
Robert Wilhelm25284cc2013-08-23 16:11:15 +00003145 // This module already has this level of visibility (or greater), so
Guy Benyei11169dd2012-12-18 14:30:41 +00003146 // there is nothing more to do.
3147 continue;
3148 }
Richard Smith49f906a2014-03-01 00:08:04 +00003149
Guy Benyei11169dd2012-12-18 14:30:41 +00003150 if (!Mod->isAvailable()) {
3151 // Modules that aren't available cannot be made visible.
3152 continue;
3153 }
3154
3155 // Update the module's name visibility.
Richard Smith49f906a2014-03-01 00:08:04 +00003156 if (NameVisibility >= Module::MacrosVisible &&
3157 Mod->NameVisibility < Module::MacrosVisible)
3158 Mod->MacroVisibilityLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003159 Mod->NameVisibility = NameVisibility;
Richard Smith49f906a2014-03-01 00:08:04 +00003160
Guy Benyei11169dd2012-12-18 14:30:41 +00003161 // If we've already deserialized any names from this module,
3162 // mark them as visible.
3163 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
3164 if (Hidden != HiddenNamesMap.end()) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003165 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003166 HiddenNamesMap.erase(Hidden);
3167 }
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +00003168
Guy Benyei11169dd2012-12-18 14:30:41 +00003169 // Push any exported modules onto the stack to be marked as visible.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +00003170 SmallVector<Module *, 16> Exports;
3171 Mod->getExportedModules(Exports);
3172 for (SmallVectorImpl<Module *>::iterator
3173 I = Exports.begin(), E = Exports.end(); I != E; ++I) {
3174 Module *Exported = *I;
3175 if (Visited.insert(Exported))
3176 Stack.push_back(Exported);
Guy Benyei11169dd2012-12-18 14:30:41 +00003177 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003178
3179 // Detect any conflicts.
3180 if (Complain) {
3181 assert(ImportLoc.isValid() && "Missing import location");
3182 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
3183 if (Mod->Conflicts[I].Other->NameVisibility >= NameVisibility) {
3184 Diag(ImportLoc, diag::warn_module_conflict)
3185 << Mod->getFullModuleName()
3186 << Mod->Conflicts[I].Other->getFullModuleName()
3187 << Mod->Conflicts[I].Message;
3188 // FIXME: Need note where the other module was imported.
3189 }
3190 }
3191 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003192 }
3193}
3194
Douglas Gregore060e572013-01-25 01:03:03 +00003195bool ASTReader::loadGlobalIndex() {
3196 if (GlobalIndex)
3197 return false;
3198
3199 if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
3200 !Context.getLangOpts().Modules)
3201 return true;
3202
3203 // Try to load the global index.
3204 TriedLoadingGlobalIndex = true;
3205 StringRef ModuleCachePath
3206 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
3207 std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> Result
Douglas Gregor7029ce12013-03-19 00:28:20 +00003208 = GlobalModuleIndex::readIndex(ModuleCachePath);
Douglas Gregore060e572013-01-25 01:03:03 +00003209 if (!Result.first)
3210 return true;
3211
3212 GlobalIndex.reset(Result.first);
Douglas Gregor7211ac12013-01-25 23:32:03 +00003213 ModuleMgr.setGlobalIndex(GlobalIndex.get());
Douglas Gregore060e572013-01-25 01:03:03 +00003214 return false;
3215}
3216
3217bool ASTReader::isGlobalIndexUnavailable() const {
3218 return Context.getLangOpts().Modules && UseGlobalIndex &&
3219 !hasGlobalIndex() && TriedLoadingGlobalIndex;
3220}
3221
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003222static void updateModuleTimestamp(ModuleFile &MF) {
3223 // Overwrite the timestamp file contents so that file's mtime changes.
3224 std::string TimestampFilename = MF.getTimestampFilename();
3225 std::string ErrorInfo;
Rafael Espindola04a13be2014-02-24 15:06:52 +00003226 llvm::raw_fd_ostream OS(TimestampFilename.c_str(), ErrorInfo,
Rafael Espindola4fbd3732014-02-24 18:20:21 +00003227 llvm::sys::fs::F_Text);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003228 if (!ErrorInfo.empty())
3229 return;
3230 OS << "Timestamp file\n";
3231}
3232
Guy Benyei11169dd2012-12-18 14:30:41 +00003233ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
3234 ModuleKind Type,
3235 SourceLocation ImportLoc,
3236 unsigned ClientLoadCapabilities) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00003237 llvm::SaveAndRestore<SourceLocation>
3238 SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
3239
Guy Benyei11169dd2012-12-18 14:30:41 +00003240 // Bump the generation number.
3241 unsigned PreviousGeneration = CurrentGeneration++;
3242
3243 unsigned NumModules = ModuleMgr.size();
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003244 SmallVector<ImportedModule, 4> Loaded;
Guy Benyei11169dd2012-12-18 14:30:41 +00003245 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
3246 /*ImportedBy=*/0, Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003247 0, 0,
Guy Benyei11169dd2012-12-18 14:30:41 +00003248 ClientLoadCapabilities)) {
3249 case Failure:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003250 case Missing:
Guy Benyei11169dd2012-12-18 14:30:41 +00003251 case OutOfDate:
3252 case VersionMismatch:
3253 case ConfigurationMismatch:
3254 case HadErrors:
Douglas Gregor7029ce12013-03-19 00:28:20 +00003255 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end(),
3256 Context.getLangOpts().Modules
3257 ? &PP.getHeaderSearchInfo().getModuleMap()
3258 : 0);
Douglas Gregore060e572013-01-25 01:03:03 +00003259
3260 // If we find that any modules are unusable, the global index is going
3261 // to be out-of-date. Just remove it.
3262 GlobalIndex.reset();
Douglas Gregor7211ac12013-01-25 23:32:03 +00003263 ModuleMgr.setGlobalIndex(0);
Guy Benyei11169dd2012-12-18 14:30:41 +00003264 return ReadResult;
3265
3266 case Success:
3267 break;
3268 }
3269
3270 // Here comes stuff that we only do once the entire chain is loaded.
3271
3272 // Load the AST blocks of all of the modules that we loaded.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003273 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3274 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003275 M != MEnd; ++M) {
3276 ModuleFile &F = *M->Mod;
3277
3278 // Read the AST block.
3279 if (ReadASTBlock(F))
3280 return Failure;
3281
3282 // Once read, set the ModuleFile bit base offset and update the size in
3283 // bits of all files we've seen.
3284 F.GlobalBitOffset = TotalModulesSizeInBits;
3285 TotalModulesSizeInBits += F.SizeInBits;
3286 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
3287
3288 // Preload SLocEntries.
3289 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
3290 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
3291 // Load it through the SourceManager and don't call ReadSLocEntry()
3292 // directly because the entry may have already been loaded in which case
3293 // calling ReadSLocEntry() directly would trigger an assertion in
3294 // SourceManager.
3295 SourceMgr.getLoadedSLocEntryByID(Index);
3296 }
3297 }
3298
Douglas Gregor603cd862013-03-22 18:50:14 +00003299 // Setup the import locations and notify the module manager that we've
3300 // committed to these module files.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003301 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
3302 MEnd = Loaded.end();
Guy Benyei11169dd2012-12-18 14:30:41 +00003303 M != MEnd; ++M) {
3304 ModuleFile &F = *M->Mod;
Douglas Gregor603cd862013-03-22 18:50:14 +00003305
3306 ModuleMgr.moduleFileAccepted(&F);
3307
3308 // Set the import location.
Argyrios Kyrtzidis71c1af82013-02-01 16:36:14 +00003309 F.DirectImportLoc = ImportLoc;
Guy Benyei11169dd2012-12-18 14:30:41 +00003310 if (!M->ImportedBy)
3311 F.ImportLoc = M->ImportLoc;
3312 else
3313 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
3314 M->ImportLoc.getRawEncoding());
3315 }
3316
3317 // Mark all of the identifiers in the identifier table as being out of date,
3318 // so that various accessors know to check the loaded modules when the
3319 // identifier is used.
3320 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
3321 IdEnd = PP.getIdentifierTable().end();
3322 Id != IdEnd; ++Id)
3323 Id->second->setOutOfDate(true);
3324
3325 // Resolve any unresolved module exports.
Douglas Gregorfb912652013-03-20 21:10:35 +00003326 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
3327 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
Guy Benyei11169dd2012-12-18 14:30:41 +00003328 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
3329 Module *ResolvedMod = getSubmodule(GlobalID);
Douglas Gregorfb912652013-03-20 21:10:35 +00003330
3331 switch (Unresolved.Kind) {
3332 case UnresolvedModuleRef::Conflict:
3333 if (ResolvedMod) {
3334 Module::Conflict Conflict;
3335 Conflict.Other = ResolvedMod;
3336 Conflict.Message = Unresolved.String.str();
3337 Unresolved.Mod->Conflicts.push_back(Conflict);
3338 }
3339 continue;
3340
3341 case UnresolvedModuleRef::Import:
Guy Benyei11169dd2012-12-18 14:30:41 +00003342 if (ResolvedMod)
3343 Unresolved.Mod->Imports.push_back(ResolvedMod);
3344 continue;
Guy Benyei11169dd2012-12-18 14:30:41 +00003345
Douglas Gregorfb912652013-03-20 21:10:35 +00003346 case UnresolvedModuleRef::Export:
3347 if (ResolvedMod || Unresolved.IsWildcard)
3348 Unresolved.Mod->Exports.push_back(
3349 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
3350 continue;
3351 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003352 }
Douglas Gregorfb912652013-03-20 21:10:35 +00003353 UnresolvedModuleRefs.clear();
Daniel Jasperba7f2f72013-09-24 09:14:14 +00003354
3355 // FIXME: How do we load the 'use'd modules? They may not be submodules.
3356 // Might be unnecessary as use declarations are only used to build the
3357 // module itself.
Guy Benyei11169dd2012-12-18 14:30:41 +00003358
3359 InitializeContext();
3360
Richard Smith3d8e97e2013-10-18 06:54:39 +00003361 if (SemaObj)
3362 UpdateSema();
3363
Guy Benyei11169dd2012-12-18 14:30:41 +00003364 if (DeserializationListener)
3365 DeserializationListener->ReaderInitialized(this);
3366
3367 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
3368 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3369 PrimaryModule.OriginalSourceFileID
3370 = FileID::get(PrimaryModule.SLocEntryBaseID
3371 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
3372
3373 // If this AST file is a precompiled preamble, then set the
3374 // preamble file ID of the source manager to the file source file
3375 // from which the preamble was built.
3376 if (Type == MK_Preamble) {
3377 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
3378 } else if (Type == MK_MainFile) {
3379 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
3380 }
3381 }
3382
3383 // For any Objective-C class definitions we have already loaded, make sure
3384 // that we load any additional categories.
3385 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3386 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3387 ObjCClassesLoaded[I],
3388 PreviousGeneration);
3389 }
Douglas Gregore060e572013-01-25 01:03:03 +00003390
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003391 if (PP.getHeaderSearchInfo()
3392 .getHeaderSearchOpts()
3393 .ModulesValidateOncePerBuildSession) {
3394 // Now we are certain that the module and all modules it depends on are
3395 // up to date. Create or update timestamp files for modules that are
3396 // located in the module cache (not for PCH files that could be anywhere
3397 // in the filesystem).
3398 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
3399 ImportedModule &M = Loaded[I];
3400 if (M.Mod->Kind == MK_Module) {
3401 updateModuleTimestamp(*M.Mod);
3402 }
3403 }
3404 }
3405
Guy Benyei11169dd2012-12-18 14:30:41 +00003406 return Success;
3407}
3408
3409ASTReader::ASTReadResult
3410ASTReader::ReadASTCore(StringRef FileName,
3411 ModuleKind Type,
3412 SourceLocation ImportLoc,
3413 ModuleFile *ImportedBy,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003414 SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor7029ce12013-03-19 00:28:20 +00003415 off_t ExpectedSize, time_t ExpectedModTime,
Guy Benyei11169dd2012-12-18 14:30:41 +00003416 unsigned ClientLoadCapabilities) {
3417 ModuleFile *M;
Guy Benyei11169dd2012-12-18 14:30:41 +00003418 std::string ErrorStr;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003419 ModuleManager::AddModuleResult AddResult
3420 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
3421 CurrentGeneration, ExpectedSize, ExpectedModTime,
3422 M, ErrorStr);
Guy Benyei11169dd2012-12-18 14:30:41 +00003423
Douglas Gregor7029ce12013-03-19 00:28:20 +00003424 switch (AddResult) {
3425 case ModuleManager::AlreadyLoaded:
3426 return Success;
3427
3428 case ModuleManager::NewlyLoaded:
3429 // Load module file below.
3430 break;
3431
3432 case ModuleManager::Missing:
3433 // The module file was missing; if the client handle handle, that, return
3434 // it.
3435 if (ClientLoadCapabilities & ARR_Missing)
3436 return Missing;
3437
3438 // Otherwise, return an error.
3439 {
3440 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3441 + ErrorStr;
3442 Error(Msg);
3443 }
3444 return Failure;
3445
3446 case ModuleManager::OutOfDate:
3447 // We couldn't load the module file because it is out-of-date. If the
3448 // client can handle out-of-date, return it.
3449 if (ClientLoadCapabilities & ARR_OutOfDate)
3450 return OutOfDate;
3451
3452 // Otherwise, return an error.
3453 {
3454 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3455 + ErrorStr;
3456 Error(Msg);
3457 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003458 return Failure;
3459 }
3460
Douglas Gregor7029ce12013-03-19 00:28:20 +00003461 assert(M && "Missing module file");
Guy Benyei11169dd2012-12-18 14:30:41 +00003462
3463 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3464 // module?
3465 if (FileName != "-") {
3466 CurrentDir = llvm::sys::path::parent_path(FileName);
3467 if (CurrentDir.empty()) CurrentDir = ".";
3468 }
3469
3470 ModuleFile &F = *M;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003471 BitstreamCursor &Stream = F.Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003472 Stream.init(F.StreamFile);
3473 F.SizeInBits = F.Buffer->getBufferSize() * 8;
3474
3475 // Sniff for the signature.
3476 if (Stream.Read(8) != 'C' ||
3477 Stream.Read(8) != 'P' ||
3478 Stream.Read(8) != 'C' ||
3479 Stream.Read(8) != 'H') {
3480 Diag(diag::err_not_a_pch_file) << FileName;
3481 return Failure;
3482 }
3483
3484 // This is used for compatibility with older PCH formats.
3485 bool HaveReadControlBlock = false;
3486
Chris Lattnerefa77172013-01-20 00:00:22 +00003487 while (1) {
3488 llvm::BitstreamEntry Entry = Stream.advance();
3489
3490 switch (Entry.Kind) {
3491 case llvm::BitstreamEntry::Error:
3492 case llvm::BitstreamEntry::EndBlock:
3493 case llvm::BitstreamEntry::Record:
Guy Benyei11169dd2012-12-18 14:30:41 +00003494 Error("invalid record at top-level of AST file");
3495 return Failure;
Chris Lattnerefa77172013-01-20 00:00:22 +00003496
3497 case llvm::BitstreamEntry::SubBlock:
3498 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003499 }
3500
Guy Benyei11169dd2012-12-18 14:30:41 +00003501 // We only know the control subblock ID.
Chris Lattnerefa77172013-01-20 00:00:22 +00003502 switch (Entry.ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00003503 case llvm::bitc::BLOCKINFO_BLOCK_ID:
3504 if (Stream.ReadBlockInfoBlock()) {
3505 Error("malformed BlockInfoBlock in AST file");
3506 return Failure;
3507 }
3508 break;
3509 case CONTROL_BLOCK_ID:
3510 HaveReadControlBlock = true;
3511 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
3512 case Success:
3513 break;
3514
3515 case Failure: return Failure;
Douglas Gregor7029ce12013-03-19 00:28:20 +00003516 case Missing: return Missing;
Guy Benyei11169dd2012-12-18 14:30:41 +00003517 case OutOfDate: return OutOfDate;
3518 case VersionMismatch: return VersionMismatch;
3519 case ConfigurationMismatch: return ConfigurationMismatch;
3520 case HadErrors: return HadErrors;
3521 }
3522 break;
3523 case AST_BLOCK_ID:
3524 if (!HaveReadControlBlock) {
3525 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
Dmitri Gribenko2228cd32014-02-11 15:40:09 +00003526 Diag(diag::err_pch_version_too_old);
Guy Benyei11169dd2012-12-18 14:30:41 +00003527 return VersionMismatch;
3528 }
3529
3530 // Record that we've loaded this module.
3531 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
3532 return Success;
3533
3534 default:
3535 if (Stream.SkipBlock()) {
3536 Error("malformed block record in AST file");
3537 return Failure;
3538 }
3539 break;
3540 }
3541 }
3542
3543 return Success;
3544}
3545
3546void ASTReader::InitializeContext() {
3547 // If there's a listener, notify them that we "read" the translation unit.
3548 if (DeserializationListener)
3549 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3550 Context.getTranslationUnitDecl());
3551
3552 // Make sure we load the declaration update records for the translation unit,
3553 // if there are any.
3554 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3555 Context.getTranslationUnitDecl());
3556
3557 // FIXME: Find a better way to deal with collisions between these
3558 // built-in types. Right now, we just ignore the problem.
3559
3560 // Load the special types.
3561 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
3562 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3563 if (!Context.CFConstantStringTypeDecl)
3564 Context.setCFConstantStringType(GetType(String));
3565 }
3566
3567 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3568 QualType FileType = GetType(File);
3569 if (FileType.isNull()) {
3570 Error("FILE type is NULL");
3571 return;
3572 }
3573
3574 if (!Context.FILEDecl) {
3575 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3576 Context.setFILEDecl(Typedef->getDecl());
3577 else {
3578 const TagType *Tag = FileType->getAs<TagType>();
3579 if (!Tag) {
3580 Error("Invalid FILE type in AST file");
3581 return;
3582 }
3583 Context.setFILEDecl(Tag->getDecl());
3584 }
3585 }
3586 }
3587
3588 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
3589 QualType Jmp_bufType = GetType(Jmp_buf);
3590 if (Jmp_bufType.isNull()) {
3591 Error("jmp_buf type is NULL");
3592 return;
3593 }
3594
3595 if (!Context.jmp_bufDecl) {
3596 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3597 Context.setjmp_bufDecl(Typedef->getDecl());
3598 else {
3599 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3600 if (!Tag) {
3601 Error("Invalid jmp_buf type in AST file");
3602 return;
3603 }
3604 Context.setjmp_bufDecl(Tag->getDecl());
3605 }
3606 }
3607 }
3608
3609 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
3610 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3611 if (Sigjmp_bufType.isNull()) {
3612 Error("sigjmp_buf type is NULL");
3613 return;
3614 }
3615
3616 if (!Context.sigjmp_bufDecl) {
3617 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3618 Context.setsigjmp_bufDecl(Typedef->getDecl());
3619 else {
3620 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3621 assert(Tag && "Invalid sigjmp_buf type in AST file");
3622 Context.setsigjmp_bufDecl(Tag->getDecl());
3623 }
3624 }
3625 }
3626
3627 if (unsigned ObjCIdRedef
3628 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3629 if (Context.ObjCIdRedefinitionType.isNull())
3630 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3631 }
3632
3633 if (unsigned ObjCClassRedef
3634 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3635 if (Context.ObjCClassRedefinitionType.isNull())
3636 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3637 }
3638
3639 if (unsigned ObjCSelRedef
3640 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3641 if (Context.ObjCSelRedefinitionType.isNull())
3642 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3643 }
3644
3645 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3646 QualType Ucontext_tType = GetType(Ucontext_t);
3647 if (Ucontext_tType.isNull()) {
3648 Error("ucontext_t type is NULL");
3649 return;
3650 }
3651
3652 if (!Context.ucontext_tDecl) {
3653 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3654 Context.setucontext_tDecl(Typedef->getDecl());
3655 else {
3656 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3657 assert(Tag && "Invalid ucontext_t type in AST file");
3658 Context.setucontext_tDecl(Tag->getDecl());
3659 }
3660 }
3661 }
3662 }
3663
3664 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
3665
3666 // If there were any CUDA special declarations, deserialize them.
3667 if (!CUDASpecialDeclRefs.empty()) {
3668 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
3669 Context.setcudaConfigureCallDecl(
3670 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3671 }
3672
3673 // Re-export any modules that were imported by a non-module AST file.
3674 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3675 if (Module *Imported = getSubmodule(ImportedModules[I]))
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +00003676 makeModuleVisible(Imported, Module::AllVisible,
Douglas Gregorfb912652013-03-20 21:10:35 +00003677 /*ImportLoc=*/SourceLocation(),
3678 /*Complain=*/false);
Guy Benyei11169dd2012-12-18 14:30:41 +00003679 }
3680 ImportedModules.clear();
3681}
3682
3683void ASTReader::finalizeForWriting() {
3684 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3685 HiddenEnd = HiddenNamesMap.end();
3686 Hidden != HiddenEnd; ++Hidden) {
Argyrios Kyrtzidis3a9c42c2013-03-27 01:25:34 +00003687 makeNamesVisible(Hidden->second, Hidden->first);
Guy Benyei11169dd2012-12-18 14:30:41 +00003688 }
3689 HiddenNamesMap.clear();
3690}
3691
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003692/// \brief Given a cursor at the start of an AST file, scan ahead and drop the
3693/// cursor into the start of the given block ID, returning false on success and
3694/// true on failure.
3695static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003696 while (1) {
3697 llvm::BitstreamEntry Entry = Cursor.advance();
3698 switch (Entry.Kind) {
3699 case llvm::BitstreamEntry::Error:
3700 case llvm::BitstreamEntry::EndBlock:
3701 return true;
3702
3703 case llvm::BitstreamEntry::Record:
3704 // Ignore top-level records.
3705 Cursor.skipRecord(Entry.ID);
3706 break;
3707
3708 case llvm::BitstreamEntry::SubBlock:
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003709 if (Entry.ID == BlockID) {
3710 if (Cursor.EnterSubBlock(BlockID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003711 return true;
3712 // Found it!
3713 return false;
3714 }
3715
3716 if (Cursor.SkipBlock())
3717 return true;
3718 }
3719 }
3720}
3721
Guy Benyei11169dd2012-12-18 14:30:41 +00003722/// \brief Retrieve the name of the original source file name
3723/// directly from the AST file, without actually loading the AST
3724/// file.
3725std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
3726 FileManager &FileMgr,
3727 DiagnosticsEngine &Diags) {
3728 // Open the AST file.
3729 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00003730 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003731 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
3732 if (!Buffer) {
3733 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
3734 return std::string();
3735 }
3736
3737 // Initialize the stream
3738 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003739 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003740 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3741 (const unsigned char *)Buffer->getBufferEnd());
3742 Stream.init(StreamFile);
3743
3744 // Sniff for the signature.
3745 if (Stream.Read(8) != 'C' ||
3746 Stream.Read(8) != 'P' ||
3747 Stream.Read(8) != 'C' ||
3748 Stream.Read(8) != 'H') {
3749 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
3750 return std::string();
3751 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003752
Chris Lattnere7b154b2013-01-19 21:39:22 +00003753 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003754 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003755 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3756 return std::string();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003757 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003758
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003759 // Scan for ORIGINAL_FILE inside the control block.
3760 RecordData Record;
Chris Lattnere7b154b2013-01-19 21:39:22 +00003761 while (1) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003762 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
Chris Lattnere7b154b2013-01-19 21:39:22 +00003763 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3764 return std::string();
3765
3766 if (Entry.Kind != llvm::BitstreamEntry::Record) {
3767 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
3768 return std::string();
Guy Benyei11169dd2012-12-18 14:30:41 +00003769 }
Chris Lattnere7b154b2013-01-19 21:39:22 +00003770
Guy Benyei11169dd2012-12-18 14:30:41 +00003771 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003772 StringRef Blob;
3773 if (Stream.readRecord(Entry.ID, Record, &Blob) == ORIGINAL_FILE)
3774 return Blob.str();
Guy Benyei11169dd2012-12-18 14:30:41 +00003775 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003776}
3777
3778namespace {
3779 class SimplePCHValidator : public ASTReaderListener {
3780 const LangOptions &ExistingLangOpts;
3781 const TargetOptions &ExistingTargetOpts;
3782 const PreprocessorOptions &ExistingPPOpts;
3783 FileManager &FileMgr;
3784
3785 public:
3786 SimplePCHValidator(const LangOptions &ExistingLangOpts,
3787 const TargetOptions &ExistingTargetOpts,
3788 const PreprocessorOptions &ExistingPPOpts,
3789 FileManager &FileMgr)
3790 : ExistingLangOpts(ExistingLangOpts),
3791 ExistingTargetOpts(ExistingTargetOpts),
3792 ExistingPPOpts(ExistingPPOpts),
3793 FileMgr(FileMgr)
3794 {
3795 }
3796
Craig Topper3e89dfe2014-03-13 02:13:41 +00003797 bool ReadLanguageOptions(const LangOptions &LangOpts,
3798 bool Complain) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003799 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3800 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00003801 bool ReadTargetOptions(const TargetOptions &TargetOpts,
3802 bool Complain) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003803 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3804 }
Craig Topper3e89dfe2014-03-13 02:13:41 +00003805 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
3806 bool Complain,
3807 std::string &SuggestedPredefines) override {
Guy Benyei11169dd2012-12-18 14:30:41 +00003808 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00003809 SuggestedPredefines, ExistingLangOpts);
Guy Benyei11169dd2012-12-18 14:30:41 +00003810 }
3811 };
3812}
3813
3814bool ASTReader::readASTFileControlBlock(StringRef Filename,
3815 FileManager &FileMgr,
3816 ASTReaderListener &Listener) {
3817 // Open the AST file.
3818 std::string ErrStr;
Ahmed Charlesb8984322014-03-07 20:03:18 +00003819 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Guy Benyei11169dd2012-12-18 14:30:41 +00003820 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3821 if (!Buffer) {
3822 return true;
3823 }
3824
3825 // Initialize the stream
3826 llvm::BitstreamReader StreamFile;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003827 BitstreamCursor Stream;
Guy Benyei11169dd2012-12-18 14:30:41 +00003828 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3829 (const unsigned char *)Buffer->getBufferEnd());
3830 Stream.init(StreamFile);
3831
3832 // Sniff for the signature.
3833 if (Stream.Read(8) != 'C' ||
3834 Stream.Read(8) != 'P' ||
3835 Stream.Read(8) != 'C' ||
3836 Stream.Read(8) != 'H') {
3837 return true;
3838 }
3839
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003840 // Scan for the CONTROL_BLOCK_ID block.
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003841 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003842 return true;
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003843
3844 bool NeedsInputFiles = Listener.needsInputFileVisitation();
Ben Langmuircb69b572014-03-07 06:40:32 +00003845 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003846 BitstreamCursor InputFilesCursor;
3847 if (NeedsInputFiles) {
3848 InputFilesCursor = Stream;
3849 if (SkipCursorToBlock(InputFilesCursor, INPUT_FILES_BLOCK_ID))
3850 return true;
3851
3852 // Read the abbreviations
3853 while (true) {
3854 uint64_t Offset = InputFilesCursor.GetCurrentBitNo();
3855 unsigned Code = InputFilesCursor.ReadCode();
3856
3857 // We expect all abbrevs to be at the start of the block.
3858 if (Code != llvm::bitc::DEFINE_ABBREV) {
3859 InputFilesCursor.JumpToBit(Offset);
3860 break;
3861 }
3862 InputFilesCursor.ReadAbbrevRecord();
3863 }
3864 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003865
3866 // Scan for ORIGINAL_FILE inside the control block.
Guy Benyei11169dd2012-12-18 14:30:41 +00003867 RecordData Record;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003868 while (1) {
3869 llvm::BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
3870 if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
3871 return false;
3872
3873 if (Entry.Kind != llvm::BitstreamEntry::Record)
3874 return true;
3875
Guy Benyei11169dd2012-12-18 14:30:41 +00003876 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00003877 StringRef Blob;
3878 unsigned RecCode = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003879 switch ((ControlRecordTypes)RecCode) {
3880 case METADATA: {
3881 if (Record[0] != VERSION_MAJOR)
3882 return true;
Guy Benyei11169dd2012-12-18 14:30:41 +00003883
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003884 if (Listener.ReadFullVersionInformation(Blob))
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003885 return true;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +00003886
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003887 break;
3888 }
3889 case LANGUAGE_OPTIONS:
3890 if (ParseLanguageOptions(Record, false, Listener))
3891 return true;
3892 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003893
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003894 case TARGET_OPTIONS:
3895 if (ParseTargetOptions(Record, false, Listener))
3896 return true;
3897 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003898
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003899 case DIAGNOSTIC_OPTIONS:
3900 if (ParseDiagnosticOptions(Record, false, Listener))
3901 return true;
3902 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003903
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003904 case FILE_SYSTEM_OPTIONS:
3905 if (ParseFileSystemOptions(Record, false, Listener))
3906 return true;
3907 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003908
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003909 case HEADER_SEARCH_OPTIONS:
3910 if (ParseHeaderSearchOptions(Record, false, Listener))
3911 return true;
3912 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003913
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003914 case PREPROCESSOR_OPTIONS: {
3915 std::string IgnoredSuggestedPredefines;
3916 if (ParsePreprocessorOptions(Record, false, Listener,
3917 IgnoredSuggestedPredefines))
3918 return true;
3919 break;
3920 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003921
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003922 case INPUT_FILE_OFFSETS: {
3923 if (!NeedsInputFiles)
3924 break;
3925
3926 unsigned NumInputFiles = Record[0];
3927 unsigned NumUserFiles = Record[1];
3928 const uint32_t *InputFileOffs = (const uint32_t *)Blob.data();
3929 for (unsigned I = 0; I != NumInputFiles; ++I) {
3930 // Go find this input file.
3931 bool isSystemFile = I >= NumUserFiles;
Ben Langmuircb69b572014-03-07 06:40:32 +00003932
3933 if (isSystemFile && !NeedsSystemInputFiles)
3934 break; // the rest are system input files
3935
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003936 BitstreamCursor &Cursor = InputFilesCursor;
3937 SavedStreamPosition SavedPosition(Cursor);
3938 Cursor.JumpToBit(InputFileOffs[I]);
3939
3940 unsigned Code = Cursor.ReadCode();
3941 RecordData Record;
3942 StringRef Blob;
3943 bool shouldContinue = false;
3944 switch ((InputFileRecordTypes)Cursor.readRecord(Code, Record, &Blob)) {
3945 case INPUT_FILE:
Argyrios Kyrtzidis68ccbe02014-03-14 02:26:31 +00003946 bool Overridden = static_cast<bool>(Record[3]);
3947 shouldContinue = Listener.visitInputFile(Blob, isSystemFile, Overridden);
Argyrios Kyrtzidisc4cd2c42013-05-06 19:23:40 +00003948 break;
3949 }
3950 if (!shouldContinue)
3951 break;
3952 }
3953 break;
3954 }
3955
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003956 default:
3957 // No other validation to perform.
3958 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003959 }
3960 }
Guy Benyei11169dd2012-12-18 14:30:41 +00003961}
3962
3963
3964bool ASTReader::isAcceptableASTFile(StringRef Filename,
3965 FileManager &FileMgr,
3966 const LangOptions &LangOpts,
3967 const TargetOptions &TargetOpts,
3968 const PreprocessorOptions &PPOpts) {
3969 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3970 return !readASTFileControlBlock(Filename, FileMgr, validator);
3971}
3972
3973bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
3974 // Enter the submodule block.
3975 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3976 Error("malformed submodule block record in AST file");
3977 return true;
3978 }
3979
3980 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
3981 bool First = true;
3982 Module *CurrentModule = 0;
3983 RecordData Record;
3984 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003985 llvm::BitstreamEntry Entry = F.Stream.advanceSkippingSubblocks();
3986
3987 switch (Entry.Kind) {
3988 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
3989 case llvm::BitstreamEntry::Error:
3990 Error("malformed block record in AST file");
3991 return true;
3992 case llvm::BitstreamEntry::EndBlock:
Guy Benyei11169dd2012-12-18 14:30:41 +00003993 return false;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003994 case llvm::BitstreamEntry::Record:
3995 // The interesting case.
3996 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00003997 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00003998
Guy Benyei11169dd2012-12-18 14:30:41 +00003999 // Read a record.
Chris Lattner0e6c9402013-01-20 02:38:54 +00004000 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004001 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004002 switch (F.Stream.readRecord(Entry.ID, Record, &Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004003 default: // Default behavior: ignore.
4004 break;
4005
4006 case SUBMODULE_DEFINITION: {
4007 if (First) {
4008 Error("missing submodule metadata record at beginning of block");
4009 return true;
4010 }
4011
Douglas Gregor8d932422013-03-20 03:59:18 +00004012 if (Record.size() < 8) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004013 Error("malformed module definition");
4014 return true;
4015 }
4016
Chris Lattner0e6c9402013-01-20 02:38:54 +00004017 StringRef Name = Blob;
Richard Smith9bca2982014-03-08 00:03:56 +00004018 unsigned Idx = 0;
4019 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
4020 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
4021 bool IsFramework = Record[Idx++];
4022 bool IsExplicit = Record[Idx++];
4023 bool IsSystem = Record[Idx++];
4024 bool IsExternC = Record[Idx++];
4025 bool InferSubmodules = Record[Idx++];
4026 bool InferExplicitSubmodules = Record[Idx++];
4027 bool InferExportWildcard = Record[Idx++];
4028 bool ConfigMacrosExhaustive = Record[Idx++];
Douglas Gregor8d932422013-03-20 03:59:18 +00004029
Guy Benyei11169dd2012-12-18 14:30:41 +00004030 Module *ParentModule = 0;
4031 if (Parent)
4032 ParentModule = getSubmodule(Parent);
4033
4034 // Retrieve this (sub)module from the module map, creating it if
4035 // necessary.
4036 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
4037 IsFramework,
4038 IsExplicit).first;
4039 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
4040 if (GlobalIndex >= SubmodulesLoaded.size() ||
4041 SubmodulesLoaded[GlobalIndex]) {
4042 Error("too many submodules");
4043 return true;
4044 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004045
Douglas Gregor7029ce12013-03-19 00:28:20 +00004046 if (!ParentModule) {
4047 if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
4048 if (CurFile != F.File) {
4049 if (!Diags.isDiagnosticInFlight()) {
4050 Diag(diag::err_module_file_conflict)
4051 << CurrentModule->getTopLevelModuleName()
4052 << CurFile->getName()
4053 << F.File->getName();
4054 }
4055 return true;
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004056 }
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004057 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004058
4059 CurrentModule->setASTFile(F.File);
Douglas Gregor8a114ab2013-02-06 22:40:31 +00004060 }
Douglas Gregor7029ce12013-03-19 00:28:20 +00004061
Guy Benyei11169dd2012-12-18 14:30:41 +00004062 CurrentModule->IsFromModuleFile = true;
4063 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Richard Smith9bca2982014-03-08 00:03:56 +00004064 CurrentModule->IsExternC = IsExternC;
Guy Benyei11169dd2012-12-18 14:30:41 +00004065 CurrentModule->InferSubmodules = InferSubmodules;
4066 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
4067 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregor8d932422013-03-20 03:59:18 +00004068 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
Guy Benyei11169dd2012-12-18 14:30:41 +00004069 if (DeserializationListener)
4070 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
4071
4072 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004073
Douglas Gregorfb912652013-03-20 21:10:35 +00004074 // Clear out data that will be replaced by what is the module file.
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004075 CurrentModule->LinkLibraries.clear();
Douglas Gregor8d932422013-03-20 03:59:18 +00004076 CurrentModule->ConfigMacros.clear();
Douglas Gregorfb912652013-03-20 21:10:35 +00004077 CurrentModule->UnresolvedConflicts.clear();
4078 CurrentModule->Conflicts.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00004079 break;
4080 }
4081
4082 case SUBMODULE_UMBRELLA_HEADER: {
4083 if (First) {
4084 Error("missing submodule metadata record at beginning of block");
4085 return true;
4086 }
4087
4088 if (!CurrentModule)
4089 break;
4090
Chris Lattner0e6c9402013-01-20 02:38:54 +00004091 if (const FileEntry *Umbrella = PP.getFileManager().getFile(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004092 if (!CurrentModule->getUmbrellaHeader())
4093 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
4094 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
4095 Error("mismatched umbrella headers in submodule");
4096 return true;
4097 }
4098 }
4099 break;
4100 }
4101
4102 case SUBMODULE_HEADER: {
4103 if (First) {
4104 Error("missing submodule metadata record at beginning of block");
4105 return true;
4106 }
4107
4108 if (!CurrentModule)
4109 break;
4110
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004111 // We lazily associate headers with their modules via the HeaderInfoTable.
4112 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4113 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004114 break;
4115 }
4116
4117 case SUBMODULE_EXCLUDED_HEADER: {
4118 if (First) {
4119 Error("missing submodule metadata record at beginning of block");
4120 return true;
4121 }
4122
4123 if (!CurrentModule)
4124 break;
4125
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00004126 // We lazily associate headers with their modules via the HeaderInfoTable.
4127 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4128 // of complete filenames or remove it entirely.
Guy Benyei11169dd2012-12-18 14:30:41 +00004129 break;
4130 }
4131
Lawrence Crowlb53e5482013-06-20 21:14:14 +00004132 case SUBMODULE_PRIVATE_HEADER: {
4133 if (First) {
4134 Error("missing submodule metadata record at beginning of block");
4135 return true;
4136 }
4137
4138 if (!CurrentModule)
4139 break;
4140
4141 // We lazily associate headers with their modules via the HeaderInfoTable.
4142 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
4143 // of complete filenames or remove it entirely.
4144 break;
4145 }
4146
Guy Benyei11169dd2012-12-18 14:30:41 +00004147 case SUBMODULE_TOPHEADER: {
4148 if (First) {
4149 Error("missing submodule metadata record at beginning of block");
4150 return true;
4151 }
4152
4153 if (!CurrentModule)
4154 break;
4155
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00004156 CurrentModule->addTopHeaderFilename(Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004157 break;
4158 }
4159
4160 case SUBMODULE_UMBRELLA_DIR: {
4161 if (First) {
4162 Error("missing submodule metadata record at beginning of block");
4163 return true;
4164 }
4165
4166 if (!CurrentModule)
4167 break;
4168
Guy Benyei11169dd2012-12-18 14:30:41 +00004169 if (const DirectoryEntry *Umbrella
Chris Lattner0e6c9402013-01-20 02:38:54 +00004170 = PP.getFileManager().getDirectory(Blob)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004171 if (!CurrentModule->getUmbrellaDir())
4172 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
4173 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
4174 Error("mismatched umbrella directories in submodule");
4175 return true;
4176 }
4177 }
4178 break;
4179 }
4180
4181 case SUBMODULE_METADATA: {
4182 if (!First) {
4183 Error("submodule metadata record not at beginning of block");
4184 return true;
4185 }
4186 First = false;
4187
4188 F.BaseSubmoduleID = getTotalNumSubmodules();
4189 F.LocalNumSubmodules = Record[0];
4190 unsigned LocalBaseSubmoduleID = Record[1];
4191 if (F.LocalNumSubmodules > 0) {
4192 // Introduce the global -> local mapping for submodules within this
4193 // module.
4194 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
4195
4196 // Introduce the local -> global mapping for submodules within this
4197 // module.
4198 F.SubmoduleRemap.insertOrReplace(
4199 std::make_pair(LocalBaseSubmoduleID,
4200 F.BaseSubmoduleID - LocalBaseSubmoduleID));
4201
4202 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
4203 }
4204 break;
4205 }
4206
4207 case SUBMODULE_IMPORTS: {
4208 if (First) {
4209 Error("missing submodule metadata record at beginning of block");
4210 return true;
4211 }
4212
4213 if (!CurrentModule)
4214 break;
4215
4216 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004217 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004218 Unresolved.File = &F;
4219 Unresolved.Mod = CurrentModule;
4220 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004221 Unresolved.Kind = UnresolvedModuleRef::Import;
Guy Benyei11169dd2012-12-18 14:30:41 +00004222 Unresolved.IsWildcard = false;
Douglas Gregorfb912652013-03-20 21:10:35 +00004223 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004224 }
4225 break;
4226 }
4227
4228 case SUBMODULE_EXPORTS: {
4229 if (First) {
4230 Error("missing submodule metadata record at beginning of block");
4231 return true;
4232 }
4233
4234 if (!CurrentModule)
4235 break;
4236
4237 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregorfb912652013-03-20 21:10:35 +00004238 UnresolvedModuleRef Unresolved;
Guy Benyei11169dd2012-12-18 14:30:41 +00004239 Unresolved.File = &F;
4240 Unresolved.Mod = CurrentModule;
4241 Unresolved.ID = Record[Idx];
Douglas Gregorfb912652013-03-20 21:10:35 +00004242 Unresolved.Kind = UnresolvedModuleRef::Export;
Guy Benyei11169dd2012-12-18 14:30:41 +00004243 Unresolved.IsWildcard = Record[Idx + 1];
Douglas Gregorfb912652013-03-20 21:10:35 +00004244 UnresolvedModuleRefs.push_back(Unresolved);
Guy Benyei11169dd2012-12-18 14:30:41 +00004245 }
4246
4247 // Once we've loaded the set of exports, there's no reason to keep
4248 // the parsed, unresolved exports around.
4249 CurrentModule->UnresolvedExports.clear();
4250 break;
4251 }
4252 case SUBMODULE_REQUIRES: {
4253 if (First) {
4254 Error("missing submodule metadata record at beginning of block");
4255 return true;
4256 }
4257
4258 if (!CurrentModule)
4259 break;
4260
Richard Smitha3feee22013-10-28 22:18:19 +00004261 CurrentModule->addRequirement(Blob, Record[0], Context.getLangOpts(),
Guy Benyei11169dd2012-12-18 14:30:41 +00004262 Context.getTargetInfo());
4263 break;
4264 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004265
4266 case SUBMODULE_LINK_LIBRARY:
4267 if (First) {
4268 Error("missing submodule metadata record at beginning of block");
4269 return true;
4270 }
4271
4272 if (!CurrentModule)
4273 break;
4274
4275 CurrentModule->LinkLibraries.push_back(
Chris Lattner0e6c9402013-01-20 02:38:54 +00004276 Module::LinkLibrary(Blob, Record[0]));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00004277 break;
Douglas Gregor35b13ec2013-03-20 00:22:05 +00004278
4279 case SUBMODULE_CONFIG_MACRO:
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 CurrentModule->ConfigMacros.push_back(Blob.str());
4289 break;
Douglas Gregorfb912652013-03-20 21:10:35 +00004290
4291 case SUBMODULE_CONFLICT: {
4292 if (First) {
4293 Error("missing submodule metadata record at beginning of block");
4294 return true;
4295 }
4296
4297 if (!CurrentModule)
4298 break;
4299
4300 UnresolvedModuleRef Unresolved;
4301 Unresolved.File = &F;
4302 Unresolved.Mod = CurrentModule;
4303 Unresolved.ID = Record[0];
4304 Unresolved.Kind = UnresolvedModuleRef::Conflict;
4305 Unresolved.IsWildcard = false;
4306 Unresolved.String = Blob;
4307 UnresolvedModuleRefs.push_back(Unresolved);
4308 break;
4309 }
Guy Benyei11169dd2012-12-18 14:30:41 +00004310 }
4311 }
4312}
4313
4314/// \brief Parse the record that corresponds to a LangOptions data
4315/// structure.
4316///
4317/// This routine parses the language options from the AST file and then gives
4318/// them to the AST listener if one is set.
4319///
4320/// \returns true if the listener deems the file unacceptable, false otherwise.
4321bool ASTReader::ParseLanguageOptions(const RecordData &Record,
4322 bool Complain,
4323 ASTReaderListener &Listener) {
4324 LangOptions LangOpts;
4325 unsigned Idx = 0;
4326#define LANGOPT(Name, Bits, Default, Description) \
4327 LangOpts.Name = Record[Idx++];
4328#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
4329 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
4330#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00004331#define SANITIZER(NAME, ID) LangOpts.Sanitize.ID = Record[Idx++];
4332#include "clang/Basic/Sanitizers.def"
Guy Benyei11169dd2012-12-18 14:30:41 +00004333
4334 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
4335 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
4336 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
4337
4338 unsigned Length = Record[Idx++];
4339 LangOpts.CurrentModule.assign(Record.begin() + Idx,
4340 Record.begin() + Idx + Length);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004341
4342 Idx += Length;
4343
4344 // Comment options.
4345 for (unsigned N = Record[Idx++]; N; --N) {
4346 LangOpts.CommentOpts.BlockCommandNames.push_back(
4347 ReadString(Record, Idx));
4348 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00004349 LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00004350
Guy Benyei11169dd2012-12-18 14:30:41 +00004351 return Listener.ReadLanguageOptions(LangOpts, Complain);
4352}
4353
4354bool ASTReader::ParseTargetOptions(const RecordData &Record,
4355 bool Complain,
4356 ASTReaderListener &Listener) {
4357 unsigned Idx = 0;
4358 TargetOptions TargetOpts;
4359 TargetOpts.Triple = ReadString(Record, Idx);
4360 TargetOpts.CPU = ReadString(Record, Idx);
4361 TargetOpts.ABI = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004362 TargetOpts.LinkerVersion = ReadString(Record, Idx);
4363 for (unsigned N = Record[Idx++]; N; --N) {
4364 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
4365 }
4366 for (unsigned N = Record[Idx++]; N; --N) {
4367 TargetOpts.Features.push_back(ReadString(Record, Idx));
4368 }
4369
4370 return Listener.ReadTargetOptions(TargetOpts, Complain);
4371}
4372
4373bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
4374 ASTReaderListener &Listener) {
4375 DiagnosticOptions DiagOpts;
4376 unsigned Idx = 0;
4377#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
4378#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
4379 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
4380#include "clang/Basic/DiagnosticOptions.def"
4381
4382 for (unsigned N = Record[Idx++]; N; --N) {
4383 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
4384 }
4385
4386 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
4387}
4388
4389bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
4390 ASTReaderListener &Listener) {
4391 FileSystemOptions FSOpts;
4392 unsigned Idx = 0;
4393 FSOpts.WorkingDir = ReadString(Record, Idx);
4394 return Listener.ReadFileSystemOptions(FSOpts, Complain);
4395}
4396
4397bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
4398 bool Complain,
4399 ASTReaderListener &Listener) {
4400 HeaderSearchOptions HSOpts;
4401 unsigned Idx = 0;
4402 HSOpts.Sysroot = ReadString(Record, Idx);
4403
4404 // Include entries.
4405 for (unsigned N = Record[Idx++]; N; --N) {
4406 std::string Path = ReadString(Record, Idx);
4407 frontend::IncludeDirGroup Group
4408 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004409 bool IsFramework = Record[Idx++];
4410 bool IgnoreSysRoot = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004411 HSOpts.UserEntries.push_back(
Daniel Dunbar53681732013-01-30 00:34:26 +00004412 HeaderSearchOptions::Entry(Path, Group, IsFramework, IgnoreSysRoot));
Guy Benyei11169dd2012-12-18 14:30:41 +00004413 }
4414
4415 // System header prefixes.
4416 for (unsigned N = Record[Idx++]; N; --N) {
4417 std::string Prefix = ReadString(Record, Idx);
4418 bool IsSystemHeader = Record[Idx++];
4419 HSOpts.SystemHeaderPrefixes.push_back(
4420 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
4421 }
4422
4423 HSOpts.ResourceDir = ReadString(Record, Idx);
4424 HSOpts.ModuleCachePath = ReadString(Record, Idx);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00004425 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00004426 HSOpts.DisableModuleHash = Record[Idx++];
4427 HSOpts.UseBuiltinIncludes = Record[Idx++];
4428 HSOpts.UseStandardSystemIncludes = Record[Idx++];
4429 HSOpts.UseStandardCXXIncludes = Record[Idx++];
4430 HSOpts.UseLibcxx = Record[Idx++];
4431
4432 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
4433}
4434
4435bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
4436 bool Complain,
4437 ASTReaderListener &Listener,
4438 std::string &SuggestedPredefines) {
4439 PreprocessorOptions PPOpts;
4440 unsigned Idx = 0;
4441
4442 // Macro definitions/undefs
4443 for (unsigned N = Record[Idx++]; N; --N) {
4444 std::string Macro = ReadString(Record, Idx);
4445 bool IsUndef = Record[Idx++];
4446 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
4447 }
4448
4449 // Includes
4450 for (unsigned N = Record[Idx++]; N; --N) {
4451 PPOpts.Includes.push_back(ReadString(Record, Idx));
4452 }
4453
4454 // Macro Includes
4455 for (unsigned N = Record[Idx++]; N; --N) {
4456 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
4457 }
4458
4459 PPOpts.UsePredefines = Record[Idx++];
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00004460 PPOpts.DetailedRecord = Record[Idx++];
Guy Benyei11169dd2012-12-18 14:30:41 +00004461 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
4462 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
4463 PPOpts.ObjCXXARCStandardLibrary =
4464 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
4465 SuggestedPredefines.clear();
4466 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
4467 SuggestedPredefines);
4468}
4469
4470std::pair<ModuleFile *, unsigned>
4471ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
4472 GlobalPreprocessedEntityMapType::iterator
4473 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
4474 assert(I != GlobalPreprocessedEntityMap.end() &&
4475 "Corrupted global preprocessed entity map");
4476 ModuleFile *M = I->second;
4477 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
4478 return std::make_pair(M, LocalIndex);
4479}
4480
4481std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
4482ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
4483 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
4484 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
4485 Mod.NumPreprocessedEntities);
4486
4487 return std::make_pair(PreprocessingRecord::iterator(),
4488 PreprocessingRecord::iterator());
4489}
4490
4491std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
4492ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
4493 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
4494 ModuleDeclIterator(this, &Mod,
4495 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
4496}
4497
4498PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
4499 PreprocessedEntityID PPID = Index+1;
4500 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4501 ModuleFile &M = *PPInfo.first;
4502 unsigned LocalIndex = PPInfo.second;
4503 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4504
Guy Benyei11169dd2012-12-18 14:30:41 +00004505 if (!PP.getPreprocessingRecord()) {
4506 Error("no preprocessing record");
4507 return 0;
4508 }
4509
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004510 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
4511 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
4512
4513 llvm::BitstreamEntry Entry =
4514 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
4515 if (Entry.Kind != llvm::BitstreamEntry::Record)
4516 return 0;
4517
Guy Benyei11169dd2012-12-18 14:30:41 +00004518 // Read the record.
4519 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
4520 ReadSourceLocation(M, PPOffs.End));
4521 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004522 StringRef Blob;
Guy Benyei11169dd2012-12-18 14:30:41 +00004523 RecordData Record;
4524 PreprocessorDetailRecordTypes RecType =
Chris Lattner0e6c9402013-01-20 02:38:54 +00004525 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.readRecord(
4526 Entry.ID, Record, &Blob);
Guy Benyei11169dd2012-12-18 14:30:41 +00004527 switch (RecType) {
4528 case PPD_MACRO_EXPANSION: {
4529 bool isBuiltin = Record[0];
4530 IdentifierInfo *Name = 0;
4531 MacroDefinition *Def = 0;
4532 if (isBuiltin)
4533 Name = getLocalIdentifier(M, Record[1]);
4534 else {
4535 PreprocessedEntityID
4536 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
4537 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
4538 }
4539
4540 MacroExpansion *ME;
4541 if (isBuiltin)
4542 ME = new (PPRec) MacroExpansion(Name, Range);
4543 else
4544 ME = new (PPRec) MacroExpansion(Def, Range);
4545
4546 return ME;
4547 }
4548
4549 case PPD_MACRO_DEFINITION: {
4550 // Decode the identifier info and then check again; if the macro is
4551 // still defined and associated with the identifier,
4552 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
4553 MacroDefinition *MD
4554 = new (PPRec) MacroDefinition(II, Range);
4555
4556 if (DeserializationListener)
4557 DeserializationListener->MacroDefinitionRead(PPID, MD);
4558
4559 return MD;
4560 }
4561
4562 case PPD_INCLUSION_DIRECTIVE: {
Chris Lattner0e6c9402013-01-20 02:38:54 +00004563 const char *FullFileNameStart = Blob.data() + Record[0];
4564 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
Guy Benyei11169dd2012-12-18 14:30:41 +00004565 const FileEntry *File = 0;
4566 if (!FullFileName.empty())
4567 File = PP.getFileManager().getFile(FullFileName);
4568
4569 // FIXME: Stable encoding
4570 InclusionDirective::InclusionKind Kind
4571 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
4572 InclusionDirective *ID
4573 = new (PPRec) InclusionDirective(PPRec, Kind,
Chris Lattner0e6c9402013-01-20 02:38:54 +00004574 StringRef(Blob.data(), Record[0]),
Guy Benyei11169dd2012-12-18 14:30:41 +00004575 Record[1], Record[3],
4576 File,
4577 Range);
4578 return ID;
4579 }
4580 }
4581
4582 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
4583}
4584
4585/// \brief \arg SLocMapI points at a chunk of a module that contains no
4586/// preprocessed entities or the entities it contains are not the ones we are
4587/// looking for. Find the next module that contains entities and return the ID
4588/// of the first entry.
4589PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
4590 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
4591 ++SLocMapI;
4592 for (GlobalSLocOffsetMapType::const_iterator
4593 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
4594 ModuleFile &M = *SLocMapI->second;
4595 if (M.NumPreprocessedEntities)
4596 return M.BasePreprocessedEntityID;
4597 }
4598
4599 return getTotalNumPreprocessedEntities();
4600}
4601
4602namespace {
4603
4604template <unsigned PPEntityOffset::*PPLoc>
4605struct PPEntityComp {
4606 const ASTReader &Reader;
4607 ModuleFile &M;
4608
4609 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
4610
4611 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
4612 SourceLocation LHS = getLoc(L);
4613 SourceLocation RHS = getLoc(R);
4614 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4615 }
4616
4617 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
4618 SourceLocation LHS = getLoc(L);
4619 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4620 }
4621
4622 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
4623 SourceLocation RHS = getLoc(R);
4624 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4625 }
4626
4627 SourceLocation getLoc(const PPEntityOffset &PPE) const {
4628 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
4629 }
4630};
4631
4632}
4633
4634/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
4635PreprocessedEntityID
4636ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
4637 if (SourceMgr.isLocalSourceLocation(BLoc))
4638 return getTotalNumPreprocessedEntities();
4639
4640 GlobalSLocOffsetMapType::const_iterator
4641 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004642 BLoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004643 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4644 "Corrupted global sloc offset map");
4645
4646 if (SLocMapI->second->NumPreprocessedEntities == 0)
4647 return findNextPreprocessedEntity(SLocMapI);
4648
4649 ModuleFile &M = *SLocMapI->second;
4650 typedef const PPEntityOffset *pp_iterator;
4651 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4652 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4653
4654 size_t Count = M.NumPreprocessedEntities;
4655 size_t Half;
4656 pp_iterator First = pp_begin;
4657 pp_iterator PPI;
4658
4659 // Do a binary search manually instead of using std::lower_bound because
4660 // The end locations of entities may be unordered (when a macro expansion
4661 // is inside another macro argument), but for this case it is not important
4662 // whether we get the first macro expansion or its containing macro.
4663 while (Count > 0) {
4664 Half = Count/2;
4665 PPI = First;
4666 std::advance(PPI, Half);
4667 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4668 BLoc)){
4669 First = PPI;
4670 ++First;
4671 Count = Count - Half - 1;
4672 } else
4673 Count = Half;
4674 }
4675
4676 if (PPI == pp_end)
4677 return findNextPreprocessedEntity(SLocMapI);
4678
4679 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4680}
4681
4682/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4683PreprocessedEntityID
4684ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4685 if (SourceMgr.isLocalSourceLocation(ELoc))
4686 return getTotalNumPreprocessedEntities();
4687
4688 GlobalSLocOffsetMapType::const_iterator
4689 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004690 ELoc.getOffset() - 1);
Guy Benyei11169dd2012-12-18 14:30:41 +00004691 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4692 "Corrupted global sloc offset map");
4693
4694 if (SLocMapI->second->NumPreprocessedEntities == 0)
4695 return findNextPreprocessedEntity(SLocMapI);
4696
4697 ModuleFile &M = *SLocMapI->second;
4698 typedef const PPEntityOffset *pp_iterator;
4699 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4700 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4701 pp_iterator PPI =
4702 std::upper_bound(pp_begin, pp_end, ELoc,
4703 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4704
4705 if (PPI == pp_end)
4706 return findNextPreprocessedEntity(SLocMapI);
4707
4708 return M.BasePreprocessedEntityID + (PPI - pp_begin);
4709}
4710
4711/// \brief Returns a pair of [Begin, End) indices of preallocated
4712/// preprocessed entities that \arg Range encompasses.
4713std::pair<unsigned, unsigned>
4714 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4715 if (Range.isInvalid())
4716 return std::make_pair(0,0);
4717 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4718
4719 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4720 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4721 return std::make_pair(BeginID, EndID);
4722}
4723
4724/// \brief Optionally returns true or false if the preallocated preprocessed
4725/// entity with index \arg Index came from file \arg FID.
David Blaikie05785d12013-02-20 22:23:23 +00004726Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
Guy Benyei11169dd2012-12-18 14:30:41 +00004727 FileID FID) {
4728 if (FID.isInvalid())
4729 return false;
4730
4731 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4732 ModuleFile &M = *PPInfo.first;
4733 unsigned LocalIndex = PPInfo.second;
4734 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4735
4736 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4737 if (Loc.isInvalid())
4738 return false;
4739
4740 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4741 return true;
4742 else
4743 return false;
4744}
4745
4746namespace {
4747 /// \brief Visitor used to search for information about a header file.
4748 class HeaderFileInfoVisitor {
Guy Benyei11169dd2012-12-18 14:30:41 +00004749 const FileEntry *FE;
4750
David Blaikie05785d12013-02-20 22:23:23 +00004751 Optional<HeaderFileInfo> HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004752
4753 public:
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004754 explicit HeaderFileInfoVisitor(const FileEntry *FE)
4755 : FE(FE) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00004756
4757 static bool visit(ModuleFile &M, void *UserData) {
4758 HeaderFileInfoVisitor *This
4759 = static_cast<HeaderFileInfoVisitor *>(UserData);
4760
Guy Benyei11169dd2012-12-18 14:30:41 +00004761 HeaderFileInfoLookupTable *Table
4762 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4763 if (!Table)
4764 return false;
4765
4766 // Look in the on-disk hash table for an entry for this file name.
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00004767 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004768 if (Pos == Table->end())
4769 return false;
4770
4771 This->HFI = *Pos;
4772 return true;
4773 }
4774
David Blaikie05785d12013-02-20 22:23:23 +00004775 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
Guy Benyei11169dd2012-12-18 14:30:41 +00004776 };
4777}
4778
4779HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Argyrios Kyrtzidis61a38962013-03-06 18:12:44 +00004780 HeaderFileInfoVisitor Visitor(FE);
Guy Benyei11169dd2012-12-18 14:30:41 +00004781 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
Argyrios Kyrtzidis1054bbf2013-05-08 23:46:55 +00004782 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
Guy Benyei11169dd2012-12-18 14:30:41 +00004783 return *HFI;
Guy Benyei11169dd2012-12-18 14:30:41 +00004784
4785 return HeaderFileInfo();
4786}
4787
4788void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
4789 // FIXME: Make it work properly with modules.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004790 SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Guy Benyei11169dd2012-12-18 14:30:41 +00004791 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
4792 ModuleFile &F = *(*I);
4793 unsigned Idx = 0;
4794 DiagStates.clear();
4795 assert(!Diag.DiagStates.empty());
4796 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
4797 while (Idx < F.PragmaDiagMappings.size()) {
4798 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
4799 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4800 if (DiagStateID != 0) {
4801 Diag.DiagStatePoints.push_back(
4802 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4803 FullSourceLoc(Loc, SourceMgr)));
4804 continue;
4805 }
4806
4807 assert(DiagStateID == 0);
4808 // A new DiagState was created here.
4809 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4810 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4811 DiagStates.push_back(NewState);
4812 Diag.DiagStatePoints.push_back(
4813 DiagnosticsEngine::DiagStatePoint(NewState,
4814 FullSourceLoc(Loc, SourceMgr)));
4815 while (1) {
4816 assert(Idx < F.PragmaDiagMappings.size() &&
4817 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4818 if (Idx >= F.PragmaDiagMappings.size()) {
4819 break; // Something is messed up but at least avoid infinite loop in
4820 // release build.
4821 }
4822 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4823 if (DiagID == (unsigned)-1) {
4824 break; // no more diag/map pairs for this location.
4825 }
4826 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
4827 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4828 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
4829 }
4830 }
4831 }
4832}
4833
4834/// \brief Get the correct cursor and offset for loading a type.
4835ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
4836 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
4837 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
4838 ModuleFile *M = I->second;
4839 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
4840}
4841
4842/// \brief Read and return the type with the given index..
4843///
4844/// The index is the type ID, shifted and minus the number of predefs. This
4845/// routine actually reads the record corresponding to the type at the given
4846/// location. It is a helper routine for GetType, which deals with reading type
4847/// IDs.
4848QualType ASTReader::readTypeRecord(unsigned Index) {
4849 RecordLocation Loc = TypeCursorForIndex(Index);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00004850 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00004851
4852 // Keep track of where we are in the stream, then jump back there
4853 // after reading this type.
4854 SavedStreamPosition SavedPosition(DeclsCursor);
4855
4856 ReadingKindTracker ReadingKind(Read_Type, *this);
4857
4858 // Note that we are loading a type record.
4859 Deserializing AType(this);
4860
4861 unsigned Idx = 0;
4862 DeclsCursor.JumpToBit(Loc.Offset);
4863 RecordData Record;
4864 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00004865 switch ((TypeCode)DeclsCursor.readRecord(Code, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00004866 case TYPE_EXT_QUAL: {
4867 if (Record.size() != 2) {
4868 Error("Incorrect encoding of extended qualifier type");
4869 return QualType();
4870 }
4871 QualType Base = readType(*Loc.F, Record, Idx);
4872 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
4873 return Context.getQualifiedType(Base, Quals);
4874 }
4875
4876 case TYPE_COMPLEX: {
4877 if (Record.size() != 1) {
4878 Error("Incorrect encoding of complex type");
4879 return QualType();
4880 }
4881 QualType ElemType = readType(*Loc.F, Record, Idx);
4882 return Context.getComplexType(ElemType);
4883 }
4884
4885 case TYPE_POINTER: {
4886 if (Record.size() != 1) {
4887 Error("Incorrect encoding of pointer type");
4888 return QualType();
4889 }
4890 QualType PointeeType = readType(*Loc.F, Record, Idx);
4891 return Context.getPointerType(PointeeType);
4892 }
4893
Reid Kleckner8a365022013-06-24 17:51:48 +00004894 case TYPE_DECAYED: {
4895 if (Record.size() != 1) {
4896 Error("Incorrect encoding of decayed type");
4897 return QualType();
4898 }
4899 QualType OriginalType = readType(*Loc.F, Record, Idx);
4900 QualType DT = Context.getAdjustedParameterType(OriginalType);
4901 if (!isa<DecayedType>(DT))
4902 Error("Decayed type does not decay");
4903 return DT;
4904 }
4905
Reid Kleckner0503a872013-12-05 01:23:43 +00004906 case TYPE_ADJUSTED: {
4907 if (Record.size() != 2) {
4908 Error("Incorrect encoding of adjusted type");
4909 return QualType();
4910 }
4911 QualType OriginalTy = readType(*Loc.F, Record, Idx);
4912 QualType AdjustedTy = readType(*Loc.F, Record, Idx);
4913 return Context.getAdjustedType(OriginalTy, AdjustedTy);
4914 }
4915
Guy Benyei11169dd2012-12-18 14:30:41 +00004916 case TYPE_BLOCK_POINTER: {
4917 if (Record.size() != 1) {
4918 Error("Incorrect encoding of block pointer type");
4919 return QualType();
4920 }
4921 QualType PointeeType = readType(*Loc.F, Record, Idx);
4922 return Context.getBlockPointerType(PointeeType);
4923 }
4924
4925 case TYPE_LVALUE_REFERENCE: {
4926 if (Record.size() != 2) {
4927 Error("Incorrect encoding of lvalue reference type");
4928 return QualType();
4929 }
4930 QualType PointeeType = readType(*Loc.F, Record, Idx);
4931 return Context.getLValueReferenceType(PointeeType, Record[1]);
4932 }
4933
4934 case TYPE_RVALUE_REFERENCE: {
4935 if (Record.size() != 1) {
4936 Error("Incorrect encoding of rvalue reference type");
4937 return QualType();
4938 }
4939 QualType PointeeType = readType(*Loc.F, Record, Idx);
4940 return Context.getRValueReferenceType(PointeeType);
4941 }
4942
4943 case TYPE_MEMBER_POINTER: {
4944 if (Record.size() != 2) {
4945 Error("Incorrect encoding of member pointer type");
4946 return QualType();
4947 }
4948 QualType PointeeType = readType(*Loc.F, Record, Idx);
4949 QualType ClassType = readType(*Loc.F, Record, Idx);
4950 if (PointeeType.isNull() || ClassType.isNull())
4951 return QualType();
4952
4953 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
4954 }
4955
4956 case TYPE_CONSTANT_ARRAY: {
4957 QualType ElementType = readType(*Loc.F, Record, Idx);
4958 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4959 unsigned IndexTypeQuals = Record[2];
4960 unsigned Idx = 3;
4961 llvm::APInt Size = ReadAPInt(Record, Idx);
4962 return Context.getConstantArrayType(ElementType, Size,
4963 ASM, IndexTypeQuals);
4964 }
4965
4966 case TYPE_INCOMPLETE_ARRAY: {
4967 QualType ElementType = readType(*Loc.F, Record, Idx);
4968 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4969 unsigned IndexTypeQuals = Record[2];
4970 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
4971 }
4972
4973 case TYPE_VARIABLE_ARRAY: {
4974 QualType ElementType = readType(*Loc.F, Record, Idx);
4975 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4976 unsigned IndexTypeQuals = Record[2];
4977 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4978 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
4979 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
4980 ASM, IndexTypeQuals,
4981 SourceRange(LBLoc, RBLoc));
4982 }
4983
4984 case TYPE_VECTOR: {
4985 if (Record.size() != 3) {
4986 Error("incorrect encoding of vector type in AST file");
4987 return QualType();
4988 }
4989
4990 QualType ElementType = readType(*Loc.F, Record, Idx);
4991 unsigned NumElements = Record[1];
4992 unsigned VecKind = Record[2];
4993 return Context.getVectorType(ElementType, NumElements,
4994 (VectorType::VectorKind)VecKind);
4995 }
4996
4997 case TYPE_EXT_VECTOR: {
4998 if (Record.size() != 3) {
4999 Error("incorrect encoding of extended vector type in AST file");
5000 return QualType();
5001 }
5002
5003 QualType ElementType = readType(*Loc.F, Record, Idx);
5004 unsigned NumElements = Record[1];
5005 return Context.getExtVectorType(ElementType, NumElements);
5006 }
5007
5008 case TYPE_FUNCTION_NO_PROTO: {
5009 if (Record.size() != 6) {
5010 Error("incorrect encoding of no-proto function type");
5011 return QualType();
5012 }
5013 QualType ResultType = readType(*Loc.F, Record, Idx);
5014 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
5015 (CallingConv)Record[4], Record[5]);
5016 return Context.getFunctionNoProtoType(ResultType, Info);
5017 }
5018
5019 case TYPE_FUNCTION_PROTO: {
5020 QualType ResultType = readType(*Loc.F, Record, Idx);
5021
5022 FunctionProtoType::ExtProtoInfo EPI;
5023 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
5024 /*hasregparm*/ Record[2],
5025 /*regparm*/ Record[3],
5026 static_cast<CallingConv>(Record[4]),
5027 /*produces*/ Record[5]);
5028
5029 unsigned Idx = 6;
5030 unsigned NumParams = Record[Idx++];
5031 SmallVector<QualType, 16> ParamTypes;
5032 for (unsigned I = 0; I != NumParams; ++I)
5033 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
5034
5035 EPI.Variadic = Record[Idx++];
5036 EPI.HasTrailingReturn = Record[Idx++];
5037 EPI.TypeQuals = Record[Idx++];
5038 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
5039 ExceptionSpecificationType EST =
5040 static_cast<ExceptionSpecificationType>(Record[Idx++]);
5041 EPI.ExceptionSpecType = EST;
5042 SmallVector<QualType, 2> Exceptions;
5043 if (EST == EST_Dynamic) {
5044 EPI.NumExceptions = Record[Idx++];
5045 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
5046 Exceptions.push_back(readType(*Loc.F, Record, Idx));
5047 EPI.Exceptions = Exceptions.data();
5048 } else if (EST == EST_ComputedNoexcept) {
5049 EPI.NoexceptExpr = ReadExpr(*Loc.F);
5050 } else if (EST == EST_Uninstantiated) {
5051 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
5052 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
5053 } else if (EST == EST_Unevaluated) {
5054 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
5055 }
Jordan Rose5c382722013-03-08 21:51:21 +00005056 return Context.getFunctionType(ResultType, ParamTypes, EPI);
Guy Benyei11169dd2012-12-18 14:30:41 +00005057 }
5058
5059 case TYPE_UNRESOLVED_USING: {
5060 unsigned Idx = 0;
5061 return Context.getTypeDeclType(
5062 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
5063 }
5064
5065 case TYPE_TYPEDEF: {
5066 if (Record.size() != 2) {
5067 Error("incorrect encoding of typedef type");
5068 return QualType();
5069 }
5070 unsigned Idx = 0;
5071 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
5072 QualType Canonical = readType(*Loc.F, Record, Idx);
5073 if (!Canonical.isNull())
5074 Canonical = Context.getCanonicalType(Canonical);
5075 return Context.getTypedefType(Decl, Canonical);
5076 }
5077
5078 case TYPE_TYPEOF_EXPR:
5079 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
5080
5081 case TYPE_TYPEOF: {
5082 if (Record.size() != 1) {
5083 Error("incorrect encoding of typeof(type) in AST file");
5084 return QualType();
5085 }
5086 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5087 return Context.getTypeOfType(UnderlyingType);
5088 }
5089
5090 case TYPE_DECLTYPE: {
5091 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5092 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
5093 }
5094
5095 case TYPE_UNARY_TRANSFORM: {
5096 QualType BaseType = readType(*Loc.F, Record, Idx);
5097 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
5098 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
5099 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
5100 }
5101
Richard Smith74aeef52013-04-26 16:15:35 +00005102 case TYPE_AUTO: {
5103 QualType Deduced = readType(*Loc.F, Record, Idx);
5104 bool IsDecltypeAuto = Record[Idx++];
Richard Smith27d807c2013-04-30 13:56:41 +00005105 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false;
Manuel Klimek2fdbea22013-08-22 12:12:24 +00005106 return Context.getAutoType(Deduced, IsDecltypeAuto, IsDependent);
Richard Smith74aeef52013-04-26 16:15:35 +00005107 }
Guy Benyei11169dd2012-12-18 14:30:41 +00005108
5109 case TYPE_RECORD: {
5110 if (Record.size() != 2) {
5111 Error("incorrect encoding of record type");
5112 return QualType();
5113 }
5114 unsigned Idx = 0;
5115 bool IsDependent = Record[Idx++];
5116 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
5117 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
5118 QualType T = Context.getRecordType(RD);
5119 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5120 return T;
5121 }
5122
5123 case TYPE_ENUM: {
5124 if (Record.size() != 2) {
5125 Error("incorrect encoding of enum type");
5126 return QualType();
5127 }
5128 unsigned Idx = 0;
5129 bool IsDependent = Record[Idx++];
5130 QualType T
5131 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
5132 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5133 return T;
5134 }
5135
5136 case TYPE_ATTRIBUTED: {
5137 if (Record.size() != 3) {
5138 Error("incorrect encoding of attributed type");
5139 return QualType();
5140 }
5141 QualType modifiedType = readType(*Loc.F, Record, Idx);
5142 QualType equivalentType = readType(*Loc.F, Record, Idx);
5143 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
5144 return Context.getAttributedType(kind, modifiedType, equivalentType);
5145 }
5146
5147 case TYPE_PAREN: {
5148 if (Record.size() != 1) {
5149 Error("incorrect encoding of paren type");
5150 return QualType();
5151 }
5152 QualType InnerType = readType(*Loc.F, Record, Idx);
5153 return Context.getParenType(InnerType);
5154 }
5155
5156 case TYPE_PACK_EXPANSION: {
5157 if (Record.size() != 2) {
5158 Error("incorrect encoding of pack expansion type");
5159 return QualType();
5160 }
5161 QualType Pattern = readType(*Loc.F, Record, Idx);
5162 if (Pattern.isNull())
5163 return QualType();
David Blaikie05785d12013-02-20 22:23:23 +00005164 Optional<unsigned> NumExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00005165 if (Record[1])
5166 NumExpansions = Record[1] - 1;
5167 return Context.getPackExpansionType(Pattern, NumExpansions);
5168 }
5169
5170 case TYPE_ELABORATED: {
5171 unsigned Idx = 0;
5172 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5173 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5174 QualType NamedType = readType(*Loc.F, Record, Idx);
5175 return Context.getElaboratedType(Keyword, NNS, NamedType);
5176 }
5177
5178 case TYPE_OBJC_INTERFACE: {
5179 unsigned Idx = 0;
5180 ObjCInterfaceDecl *ItfD
5181 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
5182 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
5183 }
5184
5185 case TYPE_OBJC_OBJECT: {
5186 unsigned Idx = 0;
5187 QualType Base = readType(*Loc.F, Record, Idx);
5188 unsigned NumProtos = Record[Idx++];
5189 SmallVector<ObjCProtocolDecl*, 4> Protos;
5190 for (unsigned I = 0; I != NumProtos; ++I)
5191 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
5192 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
5193 }
5194
5195 case TYPE_OBJC_OBJECT_POINTER: {
5196 unsigned Idx = 0;
5197 QualType Pointee = readType(*Loc.F, Record, Idx);
5198 return Context.getObjCObjectPointerType(Pointee);
5199 }
5200
5201 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
5202 unsigned Idx = 0;
5203 QualType Parm = readType(*Loc.F, Record, Idx);
5204 QualType Replacement = readType(*Loc.F, Record, Idx);
5205 return
5206 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
5207 Replacement);
5208 }
5209
5210 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
5211 unsigned Idx = 0;
5212 QualType Parm = readType(*Loc.F, Record, Idx);
5213 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
5214 return Context.getSubstTemplateTypeParmPackType(
5215 cast<TemplateTypeParmType>(Parm),
5216 ArgPack);
5217 }
5218
5219 case TYPE_INJECTED_CLASS_NAME: {
5220 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
5221 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
5222 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
5223 // for AST reading, too much interdependencies.
5224 return
5225 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
5226 }
5227
5228 case TYPE_TEMPLATE_TYPE_PARM: {
5229 unsigned Idx = 0;
5230 unsigned Depth = Record[Idx++];
5231 unsigned Index = Record[Idx++];
5232 bool Pack = Record[Idx++];
5233 TemplateTypeParmDecl *D
5234 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
5235 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
5236 }
5237
5238 case TYPE_DEPENDENT_NAME: {
5239 unsigned Idx = 0;
5240 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5241 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5242 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5243 QualType Canon = readType(*Loc.F, Record, Idx);
5244 if (!Canon.isNull())
5245 Canon = Context.getCanonicalType(Canon);
5246 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
5247 }
5248
5249 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
5250 unsigned Idx = 0;
5251 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
5252 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
5253 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
5254 unsigned NumArgs = Record[Idx++];
5255 SmallVector<TemplateArgument, 8> Args;
5256 Args.reserve(NumArgs);
5257 while (NumArgs--)
5258 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
5259 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
5260 Args.size(), Args.data());
5261 }
5262
5263 case TYPE_DEPENDENT_SIZED_ARRAY: {
5264 unsigned Idx = 0;
5265
5266 // ArrayType
5267 QualType ElementType = readType(*Loc.F, Record, Idx);
5268 ArrayType::ArraySizeModifier ASM
5269 = (ArrayType::ArraySizeModifier)Record[Idx++];
5270 unsigned IndexTypeQuals = Record[Idx++];
5271
5272 // DependentSizedArrayType
5273 Expr *NumElts = ReadExpr(*Loc.F);
5274 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
5275
5276 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
5277 IndexTypeQuals, Brackets);
5278 }
5279
5280 case TYPE_TEMPLATE_SPECIALIZATION: {
5281 unsigned Idx = 0;
5282 bool IsDependent = Record[Idx++];
5283 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
5284 SmallVector<TemplateArgument, 8> Args;
5285 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
5286 QualType Underlying = readType(*Loc.F, Record, Idx);
5287 QualType T;
5288 if (Underlying.isNull())
5289 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
5290 Args.size());
5291 else
5292 T = Context.getTemplateSpecializationType(Name, Args.data(),
5293 Args.size(), Underlying);
5294 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
5295 return T;
5296 }
5297
5298 case TYPE_ATOMIC: {
5299 if (Record.size() != 1) {
5300 Error("Incorrect encoding of atomic type");
5301 return QualType();
5302 }
5303 QualType ValueType = readType(*Loc.F, Record, Idx);
5304 return Context.getAtomicType(ValueType);
5305 }
5306 }
5307 llvm_unreachable("Invalid TypeCode!");
5308}
5309
5310class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
5311 ASTReader &Reader;
5312 ModuleFile &F;
5313 const ASTReader::RecordData &Record;
5314 unsigned &Idx;
5315
5316 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
5317 unsigned &I) {
5318 return Reader.ReadSourceLocation(F, R, I);
5319 }
5320
5321 template<typename T>
5322 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
5323 return Reader.ReadDeclAs<T>(F, Record, Idx);
5324 }
5325
5326public:
5327 TypeLocReader(ASTReader &Reader, ModuleFile &F,
5328 const ASTReader::RecordData &Record, unsigned &Idx)
5329 : Reader(Reader), F(F), Record(Record), Idx(Idx)
5330 { }
5331
5332 // We want compile-time assurance that we've enumerated all of
5333 // these, so unfortunately we have to declare them first, then
5334 // define them out-of-line.
5335#define ABSTRACT_TYPELOC(CLASS, PARENT)
5336#define TYPELOC(CLASS, PARENT) \
5337 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
5338#include "clang/AST/TypeLocNodes.def"
5339
5340 void VisitFunctionTypeLoc(FunctionTypeLoc);
5341 void VisitArrayTypeLoc(ArrayTypeLoc);
5342};
5343
5344void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
5345 // nothing to do
5346}
5347void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
5348 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
5349 if (TL.needsExtraLocalData()) {
5350 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
5351 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
5352 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
5353 TL.setModeAttr(Record[Idx++]);
5354 }
5355}
5356void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
5357 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5358}
5359void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
5360 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5361}
Reid Kleckner8a365022013-06-24 17:51:48 +00005362void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
5363 // nothing to do
5364}
Reid Kleckner0503a872013-12-05 01:23:43 +00005365void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
5366 // nothing to do
5367}
Guy Benyei11169dd2012-12-18 14:30:41 +00005368void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
5369 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
5370}
5371void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
5372 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
5373}
5374void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
5375 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
5376}
5377void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
5378 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5379 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5380}
5381void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
5382 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
5383 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
5384 if (Record[Idx++])
5385 TL.setSizeExpr(Reader.ReadExpr(F));
5386 else
5387 TL.setSizeExpr(0);
5388}
5389void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
5390 VisitArrayTypeLoc(TL);
5391}
5392void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
5393 VisitArrayTypeLoc(TL);
5394}
5395void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
5396 VisitArrayTypeLoc(TL);
5397}
5398void TypeLocReader::VisitDependentSizedArrayTypeLoc(
5399 DependentSizedArrayTypeLoc TL) {
5400 VisitArrayTypeLoc(TL);
5401}
5402void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
5403 DependentSizedExtVectorTypeLoc TL) {
5404 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5405}
5406void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
5407 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5408}
5409void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
5410 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5411}
5412void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
5413 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
5414 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5415 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5416 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00005417 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
5418 TL.setParam(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00005419 }
5420}
5421void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
5422 VisitFunctionTypeLoc(TL);
5423}
5424void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
5425 VisitFunctionTypeLoc(TL);
5426}
5427void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
5428 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5429}
5430void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
5431 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5432}
5433void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
5434 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5435 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5436 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5437}
5438void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
5439 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
5440 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5441 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5442 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5443}
5444void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
5445 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5446}
5447void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
5448 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5449 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5450 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5451 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
5452}
5453void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
5454 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5455}
5456void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
5457 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5458}
5459void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
5460 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5461}
5462void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
5463 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
5464 if (TL.hasAttrOperand()) {
5465 SourceRange range;
5466 range.setBegin(ReadSourceLocation(Record, Idx));
5467 range.setEnd(ReadSourceLocation(Record, Idx));
5468 TL.setAttrOperandParensRange(range);
5469 }
5470 if (TL.hasAttrExprOperand()) {
5471 if (Record[Idx++])
5472 TL.setAttrExprOperand(Reader.ReadExpr(F));
5473 else
5474 TL.setAttrExprOperand(0);
5475 } else if (TL.hasAttrEnumOperand())
5476 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
5477}
5478void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
5479 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5480}
5481void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
5482 SubstTemplateTypeParmTypeLoc TL) {
5483 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5484}
5485void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
5486 SubstTemplateTypeParmPackTypeLoc TL) {
5487 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5488}
5489void TypeLocReader::VisitTemplateSpecializationTypeLoc(
5490 TemplateSpecializationTypeLoc TL) {
5491 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5492 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5493 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5494 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5495 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
5496 TL.setArgLocInfo(i,
5497 Reader.GetTemplateArgumentLocInfo(F,
5498 TL.getTypePtr()->getArg(i).getKind(),
5499 Record, Idx));
5500}
5501void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
5502 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5503 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5504}
5505void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
5506 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5507 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5508}
5509void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
5510 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5511}
5512void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
5513 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5514 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5515 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5516}
5517void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
5518 DependentTemplateSpecializationTypeLoc TL) {
5519 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
5520 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
5521 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
5522 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
5523 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5524 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5525 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
5526 TL.setArgLocInfo(I,
5527 Reader.GetTemplateArgumentLocInfo(F,
5528 TL.getTypePtr()->getArg(I).getKind(),
5529 Record, Idx));
5530}
5531void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
5532 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
5533}
5534void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
5535 TL.setNameLoc(ReadSourceLocation(Record, Idx));
5536}
5537void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
5538 TL.setHasBaseTypeAsWritten(Record[Idx++]);
5539 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
5540 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
5541 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
5542 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
5543}
5544void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
5545 TL.setStarLoc(ReadSourceLocation(Record, Idx));
5546}
5547void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
5548 TL.setKWLoc(ReadSourceLocation(Record, Idx));
5549 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
5550 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
5551}
5552
5553TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
5554 const RecordData &Record,
5555 unsigned &Idx) {
5556 QualType InfoTy = readType(F, Record, Idx);
5557 if (InfoTy.isNull())
5558 return 0;
5559
5560 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
5561 TypeLocReader TLR(*this, F, Record, Idx);
5562 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
5563 TLR.Visit(TL);
5564 return TInfo;
5565}
5566
5567QualType ASTReader::GetType(TypeID ID) {
5568 unsigned FastQuals = ID & Qualifiers::FastMask;
5569 unsigned Index = ID >> Qualifiers::FastWidth;
5570
5571 if (Index < NUM_PREDEF_TYPE_IDS) {
5572 QualType T;
5573 switch ((PredefinedTypeIDs)Index) {
5574 case PREDEF_TYPE_NULL_ID: return QualType();
5575 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
5576 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
5577
5578 case PREDEF_TYPE_CHAR_U_ID:
5579 case PREDEF_TYPE_CHAR_S_ID:
5580 // FIXME: Check that the signedness of CharTy is correct!
5581 T = Context.CharTy;
5582 break;
5583
5584 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
5585 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
5586 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
5587 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
5588 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
5589 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
5590 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
5591 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
5592 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
5593 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
5594 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
5595 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
5596 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
5597 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
5598 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
5599 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
5600 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
5601 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
5602 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
5603 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
5604 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
5605 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
5606 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
5607 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
5608 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
5609 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
5610 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
5611 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00005612 case PREDEF_TYPE_IMAGE1D_ID: T = Context.OCLImage1dTy; break;
5613 case PREDEF_TYPE_IMAGE1D_ARR_ID: T = Context.OCLImage1dArrayTy; break;
5614 case PREDEF_TYPE_IMAGE1D_BUFF_ID: T = Context.OCLImage1dBufferTy; break;
5615 case PREDEF_TYPE_IMAGE2D_ID: T = Context.OCLImage2dTy; break;
5616 case PREDEF_TYPE_IMAGE2D_ARR_ID: T = Context.OCLImage2dArrayTy; break;
5617 case PREDEF_TYPE_IMAGE3D_ID: T = Context.OCLImage3dTy; break;
Guy Benyei61054192013-02-07 10:55:47 +00005618 case PREDEF_TYPE_SAMPLER_ID: T = Context.OCLSamplerTy; break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00005619 case PREDEF_TYPE_EVENT_ID: T = Context.OCLEventTy; break;
Guy Benyei11169dd2012-12-18 14:30:41 +00005620 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
5621
5622 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
5623 T = Context.getAutoRRefDeductType();
5624 break;
5625
5626 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
5627 T = Context.ARCUnbridgedCastTy;
5628 break;
5629
5630 case PREDEF_TYPE_VA_LIST_TAG:
5631 T = Context.getVaListTagType();
5632 break;
5633
5634 case PREDEF_TYPE_BUILTIN_FN:
5635 T = Context.BuiltinFnTy;
5636 break;
5637 }
5638
5639 assert(!T.isNull() && "Unknown predefined type");
5640 return T.withFastQualifiers(FastQuals);
5641 }
5642
5643 Index -= NUM_PREDEF_TYPE_IDS;
5644 assert(Index < TypesLoaded.size() && "Type index out-of-range");
5645 if (TypesLoaded[Index].isNull()) {
5646 TypesLoaded[Index] = readTypeRecord(Index);
5647 if (TypesLoaded[Index].isNull())
5648 return QualType();
5649
5650 TypesLoaded[Index]->setFromAST();
5651 if (DeserializationListener)
5652 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
5653 TypesLoaded[Index]);
5654 }
5655
5656 return TypesLoaded[Index].withFastQualifiers(FastQuals);
5657}
5658
5659QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
5660 return GetType(getGlobalTypeID(F, LocalID));
5661}
5662
5663serialization::TypeID
5664ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
5665 unsigned FastQuals = LocalID & Qualifiers::FastMask;
5666 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
5667
5668 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
5669 return LocalID;
5670
5671 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5672 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
5673 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
5674
5675 unsigned GlobalIndex = LocalIndex + I->second;
5676 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
5677}
5678
5679TemplateArgumentLocInfo
5680ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
5681 TemplateArgument::ArgKind Kind,
5682 const RecordData &Record,
5683 unsigned &Index) {
5684 switch (Kind) {
5685 case TemplateArgument::Expression:
5686 return ReadExpr(F);
5687 case TemplateArgument::Type:
5688 return GetTypeSourceInfo(F, Record, Index);
5689 case TemplateArgument::Template: {
5690 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5691 Index);
5692 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5693 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5694 SourceLocation());
5695 }
5696 case TemplateArgument::TemplateExpansion: {
5697 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5698 Index);
5699 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
5700 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
5701 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
5702 EllipsisLoc);
5703 }
5704 case TemplateArgument::Null:
5705 case TemplateArgument::Integral:
5706 case TemplateArgument::Declaration:
5707 case TemplateArgument::NullPtr:
5708 case TemplateArgument::Pack:
5709 // FIXME: Is this right?
5710 return TemplateArgumentLocInfo();
5711 }
5712 llvm_unreachable("unexpected template argument loc");
5713}
5714
5715TemplateArgumentLoc
5716ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
5717 const RecordData &Record, unsigned &Index) {
5718 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
5719
5720 if (Arg.getKind() == TemplateArgument::Expression) {
5721 if (Record[Index++]) // bool InfoHasSameExpr.
5722 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5723 }
5724 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
5725 Record, Index));
5726}
5727
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005728const ASTTemplateArgumentListInfo*
5729ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F,
5730 const RecordData &Record,
5731 unsigned &Index) {
5732 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index);
5733 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index);
5734 unsigned NumArgsAsWritten = Record[Index++];
5735 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
5736 for (unsigned i = 0; i != NumArgsAsWritten; ++i)
5737 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index));
5738 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
5739}
5740
Guy Benyei11169dd2012-12-18 14:30:41 +00005741Decl *ASTReader::GetExternalDecl(uint32_t ID) {
5742 return GetDecl(ID);
5743}
5744
5745uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
5746 unsigned &Idx){
5747 if (Idx >= Record.size())
5748 return 0;
5749
5750 unsigned LocalID = Record[Idx++];
5751 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
5752}
5753
5754CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
5755 RecordLocation Loc = getLocalBitOffset(Offset);
Chris Lattner7fb3bef2013-01-20 00:56:42 +00005756 BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Guy Benyei11169dd2012-12-18 14:30:41 +00005757 SavedStreamPosition SavedPosition(Cursor);
5758 Cursor.JumpToBit(Loc.Offset);
5759 ReadingKindTracker ReadingKind(Read_Decl, *this);
5760 RecordData Record;
5761 unsigned Code = Cursor.ReadCode();
Chris Lattner0e6c9402013-01-20 02:38:54 +00005762 unsigned RecCode = Cursor.readRecord(Code, Record);
Guy Benyei11169dd2012-12-18 14:30:41 +00005763 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5764 Error("Malformed AST file: missing C++ base specifiers");
5765 return 0;
5766 }
5767
5768 unsigned Idx = 0;
5769 unsigned NumBases = Record[Idx++];
5770 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
5771 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5772 for (unsigned I = 0; I != NumBases; ++I)
5773 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
5774 return Bases;
5775}
5776
5777serialization::DeclID
5778ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
5779 if (LocalID < NUM_PREDEF_DECL_IDS)
5780 return LocalID;
5781
5782 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5783 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
5784 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5785
5786 return LocalID + I->second;
5787}
5788
5789bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
5790 ModuleFile &M) const {
5791 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5792 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5793 return &M == I->second;
5794}
5795
Douglas Gregor9f782892013-01-21 15:25:38 +00005796ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
Guy Benyei11169dd2012-12-18 14:30:41 +00005797 if (!D->isFromASTFile())
5798 return 0;
5799 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5800 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5801 return I->second;
5802}
5803
5804SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5805 if (ID < NUM_PREDEF_DECL_IDS)
5806 return SourceLocation();
5807
5808 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5809
5810 if (Index > DeclsLoaded.size()) {
5811 Error("declaration ID out-of-range for AST file");
5812 return SourceLocation();
5813 }
5814
5815 if (Decl *D = DeclsLoaded[Index])
5816 return D->getLocation();
5817
5818 unsigned RawLocation = 0;
5819 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5820 return ReadSourceLocation(*Rec.F, RawLocation);
5821}
5822
5823Decl *ASTReader::GetDecl(DeclID ID) {
5824 if (ID < NUM_PREDEF_DECL_IDS) {
5825 switch ((PredefinedDeclIDs)ID) {
5826 case PREDEF_DECL_NULL_ID:
5827 return 0;
5828
5829 case PREDEF_DECL_TRANSLATION_UNIT_ID:
5830 return Context.getTranslationUnitDecl();
5831
5832 case PREDEF_DECL_OBJC_ID_ID:
5833 return Context.getObjCIdDecl();
5834
5835 case PREDEF_DECL_OBJC_SEL_ID:
5836 return Context.getObjCSelDecl();
5837
5838 case PREDEF_DECL_OBJC_CLASS_ID:
5839 return Context.getObjCClassDecl();
5840
5841 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5842 return Context.getObjCProtocolDecl();
5843
5844 case PREDEF_DECL_INT_128_ID:
5845 return Context.getInt128Decl();
5846
5847 case PREDEF_DECL_UNSIGNED_INT_128_ID:
5848 return Context.getUInt128Decl();
5849
5850 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
5851 return Context.getObjCInstanceTypeDecl();
5852
5853 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5854 return Context.getBuiltinVaListDecl();
5855 }
5856 }
5857
5858 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5859
5860 if (Index >= DeclsLoaded.size()) {
5861 assert(0 && "declaration ID out-of-range for AST file");
5862 Error("declaration ID out-of-range for AST file");
5863 return 0;
5864 }
5865
5866 if (!DeclsLoaded[Index]) {
5867 ReadDeclRecord(ID);
5868 if (DeserializationListener)
5869 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5870 }
5871
5872 return DeclsLoaded[Index];
5873}
5874
5875DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5876 DeclID GlobalID) {
5877 if (GlobalID < NUM_PREDEF_DECL_IDS)
5878 return GlobalID;
5879
5880 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5881 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5882 ModuleFile *Owner = I->second;
5883
5884 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5885 = M.GlobalToLocalDeclIDs.find(Owner);
5886 if (Pos == M.GlobalToLocalDeclIDs.end())
5887 return 0;
5888
5889 return GlobalID - Owner->BaseDeclID + Pos->second;
5890}
5891
5892serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
5893 const RecordData &Record,
5894 unsigned &Idx) {
5895 if (Idx >= Record.size()) {
5896 Error("Corrupted AST file");
5897 return 0;
5898 }
5899
5900 return getGlobalDeclID(F, Record[Idx++]);
5901}
5902
5903/// \brief Resolve the offset of a statement into a statement.
5904///
5905/// This operation will read a new statement from the external
5906/// source each time it is called, and is meant to be used via a
5907/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
5908Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
5909 // Switch case IDs are per Decl.
5910 ClearSwitchCaseIDs();
5911
5912 // Offset here is a global offset across the entire chain.
5913 RecordLocation Loc = getLocalBitOffset(Offset);
5914 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5915 return ReadStmtFromStream(*Loc.F);
5916}
5917
5918namespace {
5919 class FindExternalLexicalDeclsVisitor {
5920 ASTReader &Reader;
5921 const DeclContext *DC;
5922 bool (*isKindWeWant)(Decl::Kind);
5923
5924 SmallVectorImpl<Decl*> &Decls;
5925 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5926
5927 public:
5928 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5929 bool (*isKindWeWant)(Decl::Kind),
5930 SmallVectorImpl<Decl*> &Decls)
5931 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5932 {
5933 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5934 PredefsVisited[I] = false;
5935 }
5936
5937 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
5938 if (Preorder)
5939 return false;
5940
5941 FindExternalLexicalDeclsVisitor *This
5942 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5943
5944 ModuleFile::DeclContextInfosMap::iterator Info
5945 = M.DeclContextInfos.find(This->DC);
5946 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5947 return false;
5948
5949 // Load all of the declaration IDs
5950 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5951 *IDE = ID + Info->second.NumLexicalDecls;
5952 ID != IDE; ++ID) {
5953 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5954 continue;
5955
5956 // Don't add predefined declarations to the lexical context more
5957 // than once.
5958 if (ID->second < NUM_PREDEF_DECL_IDS) {
5959 if (This->PredefsVisited[ID->second])
5960 continue;
5961
5962 This->PredefsVisited[ID->second] = true;
5963 }
5964
5965 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5966 if (!This->DC->isDeclInLexicalTraversal(D))
5967 This->Decls.push_back(D);
5968 }
5969 }
5970
5971 return false;
5972 }
5973 };
5974}
5975
5976ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
5977 bool (*isKindWeWant)(Decl::Kind),
5978 SmallVectorImpl<Decl*> &Decls) {
5979 // There might be lexical decls in multiple modules, for the TU at
5980 // least. Walk all of the modules in the order they were loaded.
5981 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5982 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
5983 ++NumLexicalDeclContextsRead;
5984 return ELR_Success;
5985}
5986
5987namespace {
5988
5989class DeclIDComp {
5990 ASTReader &Reader;
5991 ModuleFile &Mod;
5992
5993public:
5994 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
5995
5996 bool operator()(LocalDeclID L, LocalDeclID R) const {
5997 SourceLocation LHS = getLocation(L);
5998 SourceLocation RHS = getLocation(R);
5999 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6000 }
6001
6002 bool operator()(SourceLocation LHS, LocalDeclID R) const {
6003 SourceLocation RHS = getLocation(R);
6004 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6005 }
6006
6007 bool operator()(LocalDeclID L, SourceLocation RHS) const {
6008 SourceLocation LHS = getLocation(L);
6009 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6010 }
6011
6012 SourceLocation getLocation(LocalDeclID ID) const {
6013 return Reader.getSourceManager().getFileLoc(
6014 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
6015 }
6016};
6017
6018}
6019
6020void ASTReader::FindFileRegionDecls(FileID File,
6021 unsigned Offset, unsigned Length,
6022 SmallVectorImpl<Decl *> &Decls) {
6023 SourceManager &SM = getSourceManager();
6024
6025 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
6026 if (I == FileDeclIDs.end())
6027 return;
6028
6029 FileDeclsInfo &DInfo = I->second;
6030 if (DInfo.Decls.empty())
6031 return;
6032
6033 SourceLocation
6034 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
6035 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
6036
6037 DeclIDComp DIDComp(*this, *DInfo.Mod);
6038 ArrayRef<serialization::LocalDeclID>::iterator
6039 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6040 BeginLoc, DIDComp);
6041 if (BeginIt != DInfo.Decls.begin())
6042 --BeginIt;
6043
6044 // If we are pointing at a top-level decl inside an objc container, we need
6045 // to backtrack until we find it otherwise we will fail to report that the
6046 // region overlaps with an objc container.
6047 while (BeginIt != DInfo.Decls.begin() &&
6048 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
6049 ->isTopLevelDeclInObjCContainer())
6050 --BeginIt;
6051
6052 ArrayRef<serialization::LocalDeclID>::iterator
6053 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
6054 EndLoc, DIDComp);
6055 if (EndIt != DInfo.Decls.end())
6056 ++EndIt;
6057
6058 for (ArrayRef<serialization::LocalDeclID>::iterator
6059 DIt = BeginIt; DIt != EndIt; ++DIt)
6060 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
6061}
6062
6063namespace {
6064 /// \brief ModuleFile visitor used to perform name lookup into a
6065 /// declaration context.
6066 class DeclContextNameLookupVisitor {
6067 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006068 SmallVectorImpl<const DeclContext *> &Contexts;
Guy Benyei11169dd2012-12-18 14:30:41 +00006069 DeclarationName Name;
6070 SmallVectorImpl<NamedDecl *> &Decls;
6071
6072 public:
6073 DeclContextNameLookupVisitor(ASTReader &Reader,
6074 SmallVectorImpl<const DeclContext *> &Contexts,
6075 DeclarationName Name,
6076 SmallVectorImpl<NamedDecl *> &Decls)
6077 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
6078
6079 static bool visit(ModuleFile &M, void *UserData) {
6080 DeclContextNameLookupVisitor *This
6081 = static_cast<DeclContextNameLookupVisitor *>(UserData);
6082
6083 // Check whether we have any visible declaration information for
6084 // this context in this module.
6085 ModuleFile::DeclContextInfosMap::iterator Info;
6086 bool FoundInfo = false;
6087 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6088 Info = M.DeclContextInfos.find(This->Contexts[I]);
6089 if (Info != M.DeclContextInfos.end() &&
6090 Info->second.NameLookupTableData) {
6091 FoundInfo = true;
6092 break;
6093 }
6094 }
6095
6096 if (!FoundInfo)
6097 return false;
6098
6099 // Look for this name within this module.
Richard Smith52e3fba2014-03-11 07:17:35 +00006100 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006101 Info->second.NameLookupTableData;
6102 ASTDeclContextNameLookupTable::iterator Pos
6103 = LookupTable->find(This->Name);
6104 if (Pos == LookupTable->end())
6105 return false;
6106
6107 bool FoundAnything = false;
6108 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
6109 for (; Data.first != Data.second; ++Data.first) {
6110 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
6111 if (!ND)
6112 continue;
6113
6114 if (ND->getDeclName() != This->Name) {
6115 // A name might be null because the decl's redeclarable part is
6116 // currently read before reading its name. The lookup is triggered by
6117 // building that decl (likely indirectly), and so it is later in the
6118 // sense of "already existing" and can be ignored here.
6119 continue;
6120 }
6121
6122 // Record this declaration.
6123 FoundAnything = true;
6124 This->Decls.push_back(ND);
6125 }
6126
6127 return FoundAnything;
6128 }
6129 };
6130}
6131
Douglas Gregor9f782892013-01-21 15:25:38 +00006132/// \brief Retrieve the "definitive" module file for the definition of the
6133/// given declaration context, if there is one.
6134///
6135/// The "definitive" module file is the only place where we need to look to
6136/// find information about the declarations within the given declaration
6137/// context. For example, C++ and Objective-C classes, C structs/unions, and
6138/// Objective-C protocols, categories, and extensions are all defined in a
6139/// single place in the source code, so they have definitive module files
6140/// associated with them. C++ namespaces, on the other hand, can have
6141/// definitions in multiple different module files.
6142///
6143/// Note: this needs to be kept in sync with ASTWriter::AddedVisibleDecl's
6144/// NDEBUG checking.
6145static ModuleFile *getDefinitiveModuleFileFor(const DeclContext *DC,
6146 ASTReader &Reader) {
Douglas Gregor7a6e2002013-01-22 17:08:30 +00006147 if (const DeclContext *DefDC = getDefinitiveDeclContext(DC))
6148 return Reader.getOwningModuleFile(cast<Decl>(DefDC));
Douglas Gregor9f782892013-01-21 15:25:38 +00006149
6150 return 0;
6151}
6152
Richard Smith9ce12e32013-02-07 03:30:24 +00006153bool
Guy Benyei11169dd2012-12-18 14:30:41 +00006154ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
6155 DeclarationName Name) {
6156 assert(DC->hasExternalVisibleStorage() &&
6157 "DeclContext has no visible decls in storage");
6158 if (!Name)
Richard Smith9ce12e32013-02-07 03:30:24 +00006159 return false;
Guy Benyei11169dd2012-12-18 14:30:41 +00006160
6161 SmallVector<NamedDecl *, 64> Decls;
6162
6163 // Compute the declaration contexts we need to look into. Multiple such
6164 // declaration contexts occur when two declaration contexts from disjoint
6165 // modules get merged, e.g., when two namespaces with the same name are
6166 // independently defined in separate modules.
6167 SmallVector<const DeclContext *, 2> Contexts;
6168 Contexts.push_back(DC);
6169
6170 if (DC->isNamespace()) {
6171 MergedDeclsMap::iterator Merged
6172 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6173 if (Merged != MergedDecls.end()) {
6174 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6175 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6176 }
6177 }
6178
6179 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor9f782892013-01-21 15:25:38 +00006180
6181 // If we can definitively determine which module file to look into,
6182 // only look there. Otherwise, look in all module files.
6183 ModuleFile *Definitive;
6184 if (Contexts.size() == 1 &&
6185 (Definitive = getDefinitiveModuleFileFor(DC, *this))) {
6186 DeclContextNameLookupVisitor::visit(*Definitive, &Visitor);
6187 } else {
6188 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
6189 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006190 ++NumVisibleDeclContextsRead;
6191 SetExternalVisibleDeclsForName(DC, Name, Decls);
Richard Smith9ce12e32013-02-07 03:30:24 +00006192 return !Decls.empty();
Guy Benyei11169dd2012-12-18 14:30:41 +00006193}
6194
6195namespace {
6196 /// \brief ModuleFile visitor used to retrieve all visible names in a
6197 /// declaration context.
6198 class DeclContextAllNamesVisitor {
6199 ASTReader &Reader;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006200 SmallVectorImpl<const DeclContext *> &Contexts;
Craig Topper3598eb72013-07-05 04:43:31 +00006201 DeclsMap &Decls;
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006202 bool VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006203
6204 public:
6205 DeclContextAllNamesVisitor(ASTReader &Reader,
6206 SmallVectorImpl<const DeclContext *> &Contexts,
Craig Topper3598eb72013-07-05 04:43:31 +00006207 DeclsMap &Decls, bool VisitAll)
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006208 : Reader(Reader), Contexts(Contexts), Decls(Decls), VisitAll(VisitAll) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006209
6210 static bool visit(ModuleFile &M, void *UserData) {
6211 DeclContextAllNamesVisitor *This
6212 = static_cast<DeclContextAllNamesVisitor *>(UserData);
6213
6214 // Check whether we have any visible declaration information for
6215 // this context in this module.
6216 ModuleFile::DeclContextInfosMap::iterator Info;
6217 bool FoundInfo = false;
6218 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
6219 Info = M.DeclContextInfos.find(This->Contexts[I]);
6220 if (Info != M.DeclContextInfos.end() &&
6221 Info->second.NameLookupTableData) {
6222 FoundInfo = true;
6223 break;
6224 }
6225 }
6226
6227 if (!FoundInfo)
6228 return false;
6229
Richard Smith52e3fba2014-03-11 07:17:35 +00006230 ASTDeclContextNameLookupTable *LookupTable =
Guy Benyei11169dd2012-12-18 14:30:41 +00006231 Info->second.NameLookupTableData;
6232 bool FoundAnything = false;
6233 for (ASTDeclContextNameLookupTable::data_iterator
Douglas Gregor5e306b12013-01-23 22:38:11 +00006234 I = LookupTable->data_begin(), E = LookupTable->data_end();
6235 I != E;
6236 ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006237 ASTDeclContextNameLookupTrait::data_type Data = *I;
6238 for (; Data.first != Data.second; ++Data.first) {
6239 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
6240 *Data.first);
6241 if (!ND)
6242 continue;
6243
6244 // Record this declaration.
6245 FoundAnything = true;
6246 This->Decls[ND->getDeclName()].push_back(ND);
6247 }
6248 }
6249
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006250 return FoundAnything && !This->VisitAll;
Guy Benyei11169dd2012-12-18 14:30:41 +00006251 }
6252 };
6253}
6254
6255void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
6256 if (!DC->hasExternalVisibleStorage())
6257 return;
Craig Topper79be4cd2013-07-05 04:33:53 +00006258 DeclsMap Decls;
Guy Benyei11169dd2012-12-18 14:30:41 +00006259
6260 // Compute the declaration contexts we need to look into. Multiple such
6261 // declaration contexts occur when two declaration contexts from disjoint
6262 // modules get merged, e.g., when two namespaces with the same name are
6263 // independently defined in separate modules.
6264 SmallVector<const DeclContext *, 2> Contexts;
6265 Contexts.push_back(DC);
6266
6267 if (DC->isNamespace()) {
6268 MergedDeclsMap::iterator Merged
6269 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
6270 if (Merged != MergedDecls.end()) {
6271 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
6272 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
6273 }
6274 }
6275
Argyrios Kyrtzidis2810e9d2012-12-19 22:21:18 +00006276 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls,
6277 /*VisitAll=*/DC->isFileContext());
Guy Benyei11169dd2012-12-18 14:30:41 +00006278 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
6279 ++NumVisibleDeclContextsRead;
6280
Craig Topper79be4cd2013-07-05 04:33:53 +00006281 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006282 SetExternalVisibleDeclsForName(DC, I->first, I->second);
6283 }
6284 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
6285}
6286
6287/// \brief Under non-PCH compilation the consumer receives the objc methods
6288/// before receiving the implementation, and codegen depends on this.
6289/// We simulate this by deserializing and passing to consumer the methods of the
6290/// implementation before passing the deserialized implementation decl.
6291static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
6292 ASTConsumer *Consumer) {
6293 assert(ImplD && Consumer);
6294
Aaron Ballmanaff18c02014-03-13 19:03:34 +00006295 for (auto *I : ImplD->methods())
6296 Consumer->HandleInterestingDecl(DeclGroupRef(I));
Guy Benyei11169dd2012-12-18 14:30:41 +00006297
6298 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
6299}
6300
6301void ASTReader::PassInterestingDeclsToConsumer() {
6302 assert(Consumer);
6303 while (!InterestingDecls.empty()) {
6304 Decl *D = InterestingDecls.front();
6305 InterestingDecls.pop_front();
6306
6307 PassInterestingDeclToConsumer(D);
6308 }
6309}
6310
6311void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
6312 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
6313 PassObjCImplDeclToConsumer(ImplD, Consumer);
6314 else
6315 Consumer->HandleInterestingDecl(DeclGroupRef(D));
6316}
6317
6318void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
6319 this->Consumer = Consumer;
6320
6321 if (!Consumer)
6322 return;
6323
Ben Langmuir332aafe2014-01-31 01:06:56 +00006324 for (unsigned I = 0, N = EagerlyDeserializedDecls.size(); I != N; ++I) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006325 // Force deserialization of this decl, which will cause it to be queued for
6326 // passing to the consumer.
Ben Langmuir332aafe2014-01-31 01:06:56 +00006327 GetDecl(EagerlyDeserializedDecls[I]);
Guy Benyei11169dd2012-12-18 14:30:41 +00006328 }
Ben Langmuir332aafe2014-01-31 01:06:56 +00006329 EagerlyDeserializedDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006330
6331 PassInterestingDeclsToConsumer();
6332}
6333
6334void ASTReader::PrintStats() {
6335 std::fprintf(stderr, "*** AST File Statistics:\n");
6336
6337 unsigned NumTypesLoaded
6338 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
6339 QualType());
6340 unsigned NumDeclsLoaded
6341 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
6342 (Decl *)0);
6343 unsigned NumIdentifiersLoaded
6344 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
6345 IdentifiersLoaded.end(),
6346 (IdentifierInfo *)0);
6347 unsigned NumMacrosLoaded
6348 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
6349 MacrosLoaded.end(),
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006350 (MacroInfo *)0);
Guy Benyei11169dd2012-12-18 14:30:41 +00006351 unsigned NumSelectorsLoaded
6352 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
6353 SelectorsLoaded.end(),
6354 Selector());
6355
6356 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
6357 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
6358 NumSLocEntriesRead, TotalNumSLocEntries,
6359 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
6360 if (!TypesLoaded.empty())
6361 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
6362 NumTypesLoaded, (unsigned)TypesLoaded.size(),
6363 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
6364 if (!DeclsLoaded.empty())
6365 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
6366 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
6367 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
6368 if (!IdentifiersLoaded.empty())
6369 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
6370 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
6371 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
6372 if (!MacrosLoaded.empty())
6373 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6374 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
6375 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
6376 if (!SelectorsLoaded.empty())
6377 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
6378 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
6379 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
6380 if (TotalNumStatements)
6381 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
6382 NumStatementsRead, TotalNumStatements,
6383 ((float)NumStatementsRead/TotalNumStatements * 100));
6384 if (TotalNumMacros)
6385 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
6386 NumMacrosRead, TotalNumMacros,
6387 ((float)NumMacrosRead/TotalNumMacros * 100));
6388 if (TotalLexicalDeclContexts)
6389 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
6390 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
6391 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
6392 * 100));
6393 if (TotalVisibleDeclContexts)
6394 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
6395 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
6396 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
6397 * 100));
6398 if (TotalNumMethodPoolEntries) {
6399 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
6400 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
6401 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
6402 * 100));
Guy Benyei11169dd2012-12-18 14:30:41 +00006403 }
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006404 if (NumMethodPoolLookups) {
6405 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n",
6406 NumMethodPoolHits, NumMethodPoolLookups,
6407 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
6408 }
6409 if (NumMethodPoolTableLookups) {
6410 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n",
6411 NumMethodPoolTableHits, NumMethodPoolTableLookups,
6412 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
6413 * 100.0));
6414 }
6415
Douglas Gregor00a50f72013-01-25 00:38:33 +00006416 if (NumIdentifierLookupHits) {
6417 std::fprintf(stderr,
6418 " %u / %u identifier table lookups succeeded (%f%%)\n",
6419 NumIdentifierLookupHits, NumIdentifierLookups,
6420 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
6421 }
6422
Douglas Gregore060e572013-01-25 01:03:03 +00006423 if (GlobalIndex) {
6424 std::fprintf(stderr, "\n");
6425 GlobalIndex->printStats();
6426 }
6427
Guy Benyei11169dd2012-12-18 14:30:41 +00006428 std::fprintf(stderr, "\n");
6429 dump();
6430 std::fprintf(stderr, "\n");
6431}
6432
6433template<typename Key, typename ModuleFile, unsigned InitialCapacity>
6434static void
6435dumpModuleIDMap(StringRef Name,
6436 const ContinuousRangeMap<Key, ModuleFile *,
6437 InitialCapacity> &Map) {
6438 if (Map.begin() == Map.end())
6439 return;
6440
6441 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
6442 llvm::errs() << Name << ":\n";
6443 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
6444 I != IEnd; ++I) {
6445 llvm::errs() << " " << I->first << " -> " << I->second->FileName
6446 << "\n";
6447 }
6448}
6449
6450void ASTReader::dump() {
6451 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
6452 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
6453 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
6454 dumpModuleIDMap("Global type map", GlobalTypeMap);
6455 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
6456 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
6457 dumpModuleIDMap("Global macro map", GlobalMacroMap);
6458 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
6459 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
6460 dumpModuleIDMap("Global preprocessed entity map",
6461 GlobalPreprocessedEntityMap);
6462
6463 llvm::errs() << "\n*** PCH/Modules Loaded:";
6464 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
6465 MEnd = ModuleMgr.end();
6466 M != MEnd; ++M)
6467 (*M)->dump();
6468}
6469
6470/// Return the amount of memory used by memory buffers, breaking down
6471/// by heap-backed versus mmap'ed memory.
6472void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
6473 for (ModuleConstIterator I = ModuleMgr.begin(),
6474 E = ModuleMgr.end(); I != E; ++I) {
6475 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
6476 size_t bytes = buf->getBufferSize();
6477 switch (buf->getBufferKind()) {
6478 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
6479 sizes.malloc_bytes += bytes;
6480 break;
6481 case llvm::MemoryBuffer::MemoryBuffer_MMap:
6482 sizes.mmap_bytes += bytes;
6483 break;
6484 }
6485 }
6486 }
6487}
6488
6489void ASTReader::InitializeSema(Sema &S) {
6490 SemaObj = &S;
6491 S.addExternalSource(this);
6492
6493 // Makes sure any declarations that were deserialized "too early"
6494 // still get added to the identifier's declaration chains.
6495 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00006496 pushExternalDeclIntoScope(PreloadedDecls[I],
6497 PreloadedDecls[I]->getDeclName());
Guy Benyei11169dd2012-12-18 14:30:41 +00006498 }
6499 PreloadedDecls.clear();
6500
Richard Smith3d8e97e2013-10-18 06:54:39 +00006501 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006502 if (!FPPragmaOptions.empty()) {
6503 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
6504 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
6505 }
6506
Richard Smith3d8e97e2013-10-18 06:54:39 +00006507 // FIXME: What happens if these are changed by a module import?
Guy Benyei11169dd2012-12-18 14:30:41 +00006508 if (!OpenCLExtensions.empty()) {
6509 unsigned I = 0;
6510#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
6511#include "clang/Basic/OpenCLExtensions.def"
6512
6513 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
6514 }
Richard Smith3d8e97e2013-10-18 06:54:39 +00006515
6516 UpdateSema();
6517}
6518
6519void ASTReader::UpdateSema() {
6520 assert(SemaObj && "no Sema to update");
6521
6522 // Load the offsets of the declarations that Sema references.
6523 // They will be lazily deserialized when needed.
6524 if (!SemaDeclRefs.empty()) {
6525 assert(SemaDeclRefs.size() % 2 == 0);
6526 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 2) {
6527 if (!SemaObj->StdNamespace)
6528 SemaObj->StdNamespace = SemaDeclRefs[I];
6529 if (!SemaObj->StdBadAlloc)
6530 SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
6531 }
6532 SemaDeclRefs.clear();
6533 }
Guy Benyei11169dd2012-12-18 14:30:41 +00006534}
6535
6536IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
6537 // Note that we are loading an identifier.
6538 Deserializing AnIdentifier(this);
Douglas Gregore060e572013-01-25 01:03:03 +00006539 StringRef Name(NameStart, NameEnd - NameStart);
6540
6541 // If there is a global index, look there first to determine which modules
6542 // provably do not have any results for this identifier.
Douglas Gregor7211ac12013-01-25 23:32:03 +00006543 GlobalModuleIndex::HitSet Hits;
6544 GlobalModuleIndex::HitSet *HitsPtr = 0;
Douglas Gregore060e572013-01-25 01:03:03 +00006545 if (!loadGlobalIndex()) {
Douglas Gregor7211ac12013-01-25 23:32:03 +00006546 if (GlobalIndex->lookupIdentifier(Name, Hits)) {
6547 HitsPtr = &Hits;
Douglas Gregore060e572013-01-25 01:03:03 +00006548 }
6549 }
Douglas Gregor7211ac12013-01-25 23:32:03 +00006550 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
Douglas Gregor00a50f72013-01-25 00:38:33 +00006551 NumIdentifierLookups,
6552 NumIdentifierLookupHits);
Douglas Gregor7211ac12013-01-25 23:32:03 +00006553 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor, HitsPtr);
Guy Benyei11169dd2012-12-18 14:30:41 +00006554 IdentifierInfo *II = Visitor.getIdentifierInfo();
6555 markIdentifierUpToDate(II);
6556 return II;
6557}
6558
6559namespace clang {
6560 /// \brief An identifier-lookup iterator that enumerates all of the
6561 /// identifiers stored within a set of AST files.
6562 class ASTIdentifierIterator : public IdentifierIterator {
6563 /// \brief The AST reader whose identifiers are being enumerated.
6564 const ASTReader &Reader;
6565
6566 /// \brief The current index into the chain of AST files stored in
6567 /// the AST reader.
6568 unsigned Index;
6569
6570 /// \brief The current position within the identifier lookup table
6571 /// of the current AST file.
6572 ASTIdentifierLookupTable::key_iterator Current;
6573
6574 /// \brief The end position within the identifier lookup table of
6575 /// the current AST file.
6576 ASTIdentifierLookupTable::key_iterator End;
6577
6578 public:
6579 explicit ASTIdentifierIterator(const ASTReader &Reader);
6580
Craig Topper3e89dfe2014-03-13 02:13:41 +00006581 StringRef Next() override;
Guy Benyei11169dd2012-12-18 14:30:41 +00006582 };
6583}
6584
6585ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
6586 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
6587 ASTIdentifierLookupTable *IdTable
6588 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
6589 Current = IdTable->key_begin();
6590 End = IdTable->key_end();
6591}
6592
6593StringRef ASTIdentifierIterator::Next() {
6594 while (Current == End) {
6595 // If we have exhausted all of our AST files, we're done.
6596 if (Index == 0)
6597 return StringRef();
6598
6599 --Index;
6600 ASTIdentifierLookupTable *IdTable
6601 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
6602 IdentifierLookupTable;
6603 Current = IdTable->key_begin();
6604 End = IdTable->key_end();
6605 }
6606
6607 // We have any identifiers remaining in the current AST file; return
6608 // the next one.
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006609 StringRef Result = *Current;
Guy Benyei11169dd2012-12-18 14:30:41 +00006610 ++Current;
Douglas Gregorbfd73d72013-01-23 18:53:14 +00006611 return Result;
Guy Benyei11169dd2012-12-18 14:30:41 +00006612}
6613
Argyrios Kyrtzidis9aca3c62013-04-17 22:10:55 +00006614IdentifierIterator *ASTReader::getIdentifiers() {
6615 if (!loadGlobalIndex())
6616 return GlobalIndex->createIdentifierIterator();
6617
Guy Benyei11169dd2012-12-18 14:30:41 +00006618 return new ASTIdentifierIterator(*this);
6619}
6620
6621namespace clang { namespace serialization {
6622 class ReadMethodPoolVisitor {
6623 ASTReader &Reader;
6624 Selector Sel;
6625 unsigned PriorGeneration;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006626 unsigned InstanceBits;
6627 unsigned FactoryBits;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00006628 SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
6629 SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Guy Benyei11169dd2012-12-18 14:30:41 +00006630
6631 public:
6632 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
6633 unsigned PriorGeneration)
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006634 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration),
6635 InstanceBits(0), FactoryBits(0) { }
Guy Benyei11169dd2012-12-18 14:30:41 +00006636
6637 static bool visit(ModuleFile &M, void *UserData) {
6638 ReadMethodPoolVisitor *This
6639 = static_cast<ReadMethodPoolVisitor *>(UserData);
6640
6641 if (!M.SelectorLookupTable)
6642 return false;
6643
6644 // If we've already searched this module file, skip it now.
6645 if (M.Generation <= This->PriorGeneration)
6646 return true;
6647
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006648 ++This->Reader.NumMethodPoolTableLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006649 ASTSelectorLookupTable *PoolTable
6650 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
6651 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
6652 if (Pos == PoolTable->end())
6653 return false;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006654
6655 ++This->Reader.NumMethodPoolTableHits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006656 ++This->Reader.NumSelectorsRead;
6657 // FIXME: Not quite happy with the statistics here. We probably should
6658 // disable this tracking when called via LoadSelector.
6659 // Also, should entries without methods count as misses?
6660 ++This->Reader.NumMethodPoolEntriesRead;
6661 ASTSelectorLookupTrait::data_type Data = *Pos;
6662 if (This->Reader.DeserializationListener)
6663 This->Reader.DeserializationListener->SelectorRead(Data.ID,
6664 This->Sel);
6665
6666 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
6667 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006668 This->InstanceBits = Data.InstanceBits;
6669 This->FactoryBits = Data.FactoryBits;
Guy Benyei11169dd2012-12-18 14:30:41 +00006670 return true;
6671 }
6672
6673 /// \brief Retrieve the instance methods found by this visitor.
6674 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
6675 return InstanceMethods;
6676 }
6677
6678 /// \brief Retrieve the instance methods found by this visitor.
6679 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
6680 return FactoryMethods;
6681 }
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006682
6683 unsigned getInstanceBits() const { return InstanceBits; }
6684 unsigned getFactoryBits() const { return FactoryBits; }
Guy Benyei11169dd2012-12-18 14:30:41 +00006685 };
6686} } // end namespace clang::serialization
6687
6688/// \brief Add the given set of methods to the method list.
6689static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
6690 ObjCMethodList &List) {
6691 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
6692 S.addMethodToGlobalList(&List, Methods[I]);
6693 }
6694}
6695
6696void ASTReader::ReadMethodPool(Selector Sel) {
6697 // Get the selector generation and update it to the current generation.
6698 unsigned &Generation = SelectorGeneration[Sel];
6699 unsigned PriorGeneration = Generation;
6700 Generation = CurrentGeneration;
6701
6702 // Search for methods defined with this selector.
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006703 ++NumMethodPoolLookups;
Guy Benyei11169dd2012-12-18 14:30:41 +00006704 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
6705 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
6706
6707 if (Visitor.getInstanceMethods().empty() &&
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006708 Visitor.getFactoryMethods().empty())
Guy Benyei11169dd2012-12-18 14:30:41 +00006709 return;
Douglas Gregorad2f7a52013-01-28 17:54:36 +00006710
6711 ++NumMethodPoolHits;
6712
Guy Benyei11169dd2012-12-18 14:30:41 +00006713 if (!getSema())
6714 return;
6715
6716 Sema &S = *getSema();
6717 Sema::GlobalMethodPool::iterator Pos
6718 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
6719
6720 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
6721 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00006722 Pos->second.first.setBits(Visitor.getInstanceBits());
6723 Pos->second.second.setBits(Visitor.getFactoryBits());
Guy Benyei11169dd2012-12-18 14:30:41 +00006724}
6725
6726void ASTReader::ReadKnownNamespaces(
6727 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
6728 Namespaces.clear();
6729
6730 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
6731 if (NamespaceDecl *Namespace
6732 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
6733 Namespaces.push_back(Namespace);
6734 }
6735}
6736
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006737void ASTReader::ReadUndefinedButUsed(
Nick Lewyckyf0f56162013-01-31 03:23:57 +00006738 llvm::DenseMap<NamedDecl*, SourceLocation> &Undefined) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006739 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
6740 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
Nick Lewycky8334af82013-01-26 00:35:08 +00006741 SourceLocation Loc =
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00006742 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
Nick Lewycky8334af82013-01-26 00:35:08 +00006743 Undefined.insert(std::make_pair(D, Loc));
6744 }
6745}
Nick Lewycky8334af82013-01-26 00:35:08 +00006746
Guy Benyei11169dd2012-12-18 14:30:41 +00006747void ASTReader::ReadTentativeDefinitions(
6748 SmallVectorImpl<VarDecl *> &TentativeDefs) {
6749 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
6750 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
6751 if (Var)
6752 TentativeDefs.push_back(Var);
6753 }
6754 TentativeDefinitions.clear();
6755}
6756
6757void ASTReader::ReadUnusedFileScopedDecls(
6758 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
6759 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
6760 DeclaratorDecl *D
6761 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
6762 if (D)
6763 Decls.push_back(D);
6764 }
6765 UnusedFileScopedDecls.clear();
6766}
6767
6768void ASTReader::ReadDelegatingConstructors(
6769 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
6770 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
6771 CXXConstructorDecl *D
6772 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
6773 if (D)
6774 Decls.push_back(D);
6775 }
6776 DelegatingCtorDecls.clear();
6777}
6778
6779void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
6780 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
6781 TypedefNameDecl *D
6782 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
6783 if (D)
6784 Decls.push_back(D);
6785 }
6786 ExtVectorDecls.clear();
6787}
6788
6789void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
6790 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
6791 CXXRecordDecl *D
6792 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
6793 if (D)
6794 Decls.push_back(D);
6795 }
6796 DynamicClasses.clear();
6797}
6798
6799void
Richard Smith78165b52013-01-10 23:43:47 +00006800ASTReader::ReadLocallyScopedExternCDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6801 for (unsigned I = 0, N = LocallyScopedExternCDecls.size(); I != N; ++I) {
6802 NamedDecl *D
6803 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternCDecls[I]));
Guy Benyei11169dd2012-12-18 14:30:41 +00006804 if (D)
6805 Decls.push_back(D);
6806 }
Richard Smith78165b52013-01-10 23:43:47 +00006807 LocallyScopedExternCDecls.clear();
Guy Benyei11169dd2012-12-18 14:30:41 +00006808}
6809
6810void ASTReader::ReadReferencedSelectors(
6811 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6812 if (ReferencedSelectorsData.empty())
6813 return;
6814
6815 // If there are @selector references added them to its pool. This is for
6816 // implementation of -Wselector.
6817 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6818 unsigned I = 0;
6819 while (I < DataSize) {
6820 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6821 SourceLocation SelLoc
6822 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6823 Sels.push_back(std::make_pair(Sel, SelLoc));
6824 }
6825 ReferencedSelectorsData.clear();
6826}
6827
6828void ASTReader::ReadWeakUndeclaredIdentifiers(
6829 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6830 if (WeakUndeclaredIdentifiers.empty())
6831 return;
6832
6833 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6834 IdentifierInfo *WeakId
6835 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6836 IdentifierInfo *AliasId
6837 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6838 SourceLocation Loc
6839 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6840 bool Used = WeakUndeclaredIdentifiers[I++];
6841 WeakInfo WI(AliasId, Loc);
6842 WI.setUsed(Used);
6843 WeakIDs.push_back(std::make_pair(WeakId, WI));
6844 }
6845 WeakUndeclaredIdentifiers.clear();
6846}
6847
6848void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6849 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6850 ExternalVTableUse VT;
6851 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6852 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6853 VT.DefinitionRequired = VTableUses[Idx++];
6854 VTables.push_back(VT);
6855 }
6856
6857 VTableUses.clear();
6858}
6859
6860void ASTReader::ReadPendingInstantiations(
6861 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6862 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6863 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6864 SourceLocation Loc
6865 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
6866
6867 Pending.push_back(std::make_pair(D, Loc));
6868 }
6869 PendingInstantiations.clear();
6870}
6871
Richard Smithe40f2ba2013-08-07 21:41:30 +00006872void ASTReader::ReadLateParsedTemplates(
6873 llvm::DenseMap<const FunctionDecl *, LateParsedTemplate *> &LPTMap) {
6874 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
6875 /* In loop */) {
6876 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
6877
6878 LateParsedTemplate *LT = new LateParsedTemplate;
6879 LT->D = GetDecl(LateParsedTemplates[Idx++]);
6880
6881 ModuleFile *F = getOwningModuleFile(LT->D);
6882 assert(F && "No module");
6883
6884 unsigned TokN = LateParsedTemplates[Idx++];
6885 LT->Toks.reserve(TokN);
6886 for (unsigned T = 0; T < TokN; ++T)
6887 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
6888
6889 LPTMap[FD] = LT;
6890 }
6891
6892 LateParsedTemplates.clear();
6893}
6894
Guy Benyei11169dd2012-12-18 14:30:41 +00006895void ASTReader::LoadSelector(Selector Sel) {
6896 // It would be complicated to avoid reading the methods anyway. So don't.
6897 ReadMethodPool(Sel);
6898}
6899
6900void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
6901 assert(ID && "Non-zero identifier ID required");
6902 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
6903 IdentifiersLoaded[ID - 1] = II;
6904 if (DeserializationListener)
6905 DeserializationListener->IdentifierRead(ID, II);
6906}
6907
6908/// \brief Set the globally-visible declarations associated with the given
6909/// identifier.
6910///
6911/// If the AST reader is currently in a state where the given declaration IDs
6912/// cannot safely be resolved, they are queued until it is safe to resolve
6913/// them.
6914///
6915/// \param II an IdentifierInfo that refers to one or more globally-visible
6916/// declarations.
6917///
6918/// \param DeclIDs the set of declaration IDs with the name @p II that are
6919/// visible at global scope.
6920///
Douglas Gregor6168bd22013-02-18 15:53:43 +00006921/// \param Decls if non-null, this vector will be populated with the set of
6922/// deserialized declarations. These declarations will not be pushed into
6923/// scope.
Guy Benyei11169dd2012-12-18 14:30:41 +00006924void
6925ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
6926 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor6168bd22013-02-18 15:53:43 +00006927 SmallVectorImpl<Decl *> *Decls) {
6928 if (NumCurrentElementsDeserializing && !Decls) {
6929 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
Guy Benyei11169dd2012-12-18 14:30:41 +00006930 return;
6931 }
6932
6933 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6934 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6935 if (SemaObj) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00006936 // If we're simply supposed to record the declarations, do so now.
6937 if (Decls) {
6938 Decls->push_back(D);
6939 continue;
6940 }
6941
Guy Benyei11169dd2012-12-18 14:30:41 +00006942 // Introduce this declaration into the translation-unit scope
6943 // and add it to the declaration chain for this identifier, so
6944 // that (unqualified) name lookup will find it.
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00006945 pushExternalDeclIntoScope(D, II);
Guy Benyei11169dd2012-12-18 14:30:41 +00006946 } else {
6947 // Queue this declaration so that it will be added to the
6948 // translation unit scope and identifier's declaration chain
6949 // once a Sema object is known.
6950 PreloadedDecls.push_back(D);
6951 }
6952 }
6953}
6954
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006955IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00006956 if (ID == 0)
6957 return 0;
6958
6959 if (IdentifiersLoaded.empty()) {
6960 Error("no identifier table in AST file");
6961 return 0;
6962 }
6963
6964 ID -= 1;
6965 if (!IdentifiersLoaded[ID]) {
6966 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6967 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
6968 ModuleFile *M = I->second;
6969 unsigned Index = ID - M->BaseIdentifierID;
6970 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
6971
6972 // All of the strings in the AST file are preceded by a 16-bit length.
6973 // Extract that 16-bit length to avoid having to execute strlen().
6974 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6975 // unsigned integers. This is important to avoid integer overflow when
6976 // we cast them to 'unsigned'.
6977 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
6978 unsigned StrLen = (((unsigned) StrLenPtr[0])
6979 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006980 IdentifiersLoaded[ID]
6981 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Guy Benyei11169dd2012-12-18 14:30:41 +00006982 if (DeserializationListener)
6983 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
6984 }
6985
6986 return IdentifiersLoaded[ID];
6987}
6988
Douglas Gregorc8a992f2013-01-21 16:52:34 +00006989IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
6990 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
Guy Benyei11169dd2012-12-18 14:30:41 +00006991}
6992
6993IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
6994 if (LocalID < NUM_PREDEF_IDENT_IDS)
6995 return LocalID;
6996
6997 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6998 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6999 assert(I != M.IdentifierRemap.end()
7000 && "Invalid index into identifier index remap");
7001
7002 return LocalID + I->second;
7003}
7004
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007005MacroInfo *ASTReader::getMacro(MacroID ID) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007006 if (ID == 0)
7007 return 0;
7008
7009 if (MacrosLoaded.empty()) {
7010 Error("no macro table in AST file");
7011 return 0;
7012 }
7013
7014 ID -= NUM_PREDEF_MACRO_IDS;
7015 if (!MacrosLoaded[ID]) {
7016 GlobalMacroMapType::iterator I
7017 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
7018 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
7019 ModuleFile *M = I->second;
7020 unsigned Index = ID - M->BaseMacroID;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007021 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]);
7022
7023 if (DeserializationListener)
7024 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
7025 MacrosLoaded[ID]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007026 }
7027
7028 return MacrosLoaded[ID];
7029}
7030
7031MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
7032 if (LocalID < NUM_PREDEF_MACRO_IDS)
7033 return LocalID;
7034
7035 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7036 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
7037 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
7038
7039 return LocalID + I->second;
7040}
7041
7042serialization::SubmoduleID
7043ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
7044 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
7045 return LocalID;
7046
7047 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7048 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
7049 assert(I != M.SubmoduleRemap.end()
7050 && "Invalid index into submodule index remap");
7051
7052 return LocalID + I->second;
7053}
7054
7055Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
7056 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
7057 assert(GlobalID == 0 && "Unhandled global submodule ID");
7058 return 0;
7059 }
7060
7061 if (GlobalID > SubmodulesLoaded.size()) {
7062 Error("submodule ID out of range in AST file");
7063 return 0;
7064 }
7065
7066 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
7067}
Douglas Gregorc147b0b2013-01-12 01:29:50 +00007068
7069Module *ASTReader::getModule(unsigned ID) {
7070 return getSubmodule(ID);
7071}
7072
Guy Benyei11169dd2012-12-18 14:30:41 +00007073Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
7074 return DecodeSelector(getGlobalSelectorID(M, LocalID));
7075}
7076
7077Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
7078 if (ID == 0)
7079 return Selector();
7080
7081 if (ID > SelectorsLoaded.size()) {
7082 Error("selector ID out of range in AST file");
7083 return Selector();
7084 }
7085
7086 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
7087 // Load this selector from the selector table.
7088 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
7089 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
7090 ModuleFile &M = *I->second;
7091 ASTSelectorLookupTrait Trait(*this, M);
7092 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
7093 SelectorsLoaded[ID - 1] =
7094 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
7095 if (DeserializationListener)
7096 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
7097 }
7098
7099 return SelectorsLoaded[ID - 1];
7100}
7101
7102Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
7103 return DecodeSelector(ID);
7104}
7105
7106uint32_t ASTReader::GetNumExternalSelectors() {
7107 // ID 0 (the null selector) is considered an external selector.
7108 return getTotalNumSelectors() + 1;
7109}
7110
7111serialization::SelectorID
7112ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
7113 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
7114 return LocalID;
7115
7116 ContinuousRangeMap<uint32_t, int, 2>::iterator I
7117 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
7118 assert(I != M.SelectorRemap.end()
7119 && "Invalid index into selector index remap");
7120
7121 return LocalID + I->second;
7122}
7123
7124DeclarationName
7125ASTReader::ReadDeclarationName(ModuleFile &F,
7126 const RecordData &Record, unsigned &Idx) {
7127 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
7128 switch (Kind) {
7129 case DeclarationName::Identifier:
Douglas Gregorc8a992f2013-01-21 16:52:34 +00007130 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007131
7132 case DeclarationName::ObjCZeroArgSelector:
7133 case DeclarationName::ObjCOneArgSelector:
7134 case DeclarationName::ObjCMultiArgSelector:
7135 return DeclarationName(ReadSelector(F, Record, Idx));
7136
7137 case DeclarationName::CXXConstructorName:
7138 return Context.DeclarationNames.getCXXConstructorName(
7139 Context.getCanonicalType(readType(F, Record, Idx)));
7140
7141 case DeclarationName::CXXDestructorName:
7142 return Context.DeclarationNames.getCXXDestructorName(
7143 Context.getCanonicalType(readType(F, Record, Idx)));
7144
7145 case DeclarationName::CXXConversionFunctionName:
7146 return Context.DeclarationNames.getCXXConversionFunctionName(
7147 Context.getCanonicalType(readType(F, Record, Idx)));
7148
7149 case DeclarationName::CXXOperatorName:
7150 return Context.DeclarationNames.getCXXOperatorName(
7151 (OverloadedOperatorKind)Record[Idx++]);
7152
7153 case DeclarationName::CXXLiteralOperatorName:
7154 return Context.DeclarationNames.getCXXLiteralOperatorName(
7155 GetIdentifierInfo(F, Record, Idx));
7156
7157 case DeclarationName::CXXUsingDirective:
7158 return DeclarationName::getUsingDirectiveName();
7159 }
7160
7161 llvm_unreachable("Invalid NameKind!");
7162}
7163
7164void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
7165 DeclarationNameLoc &DNLoc,
7166 DeclarationName Name,
7167 const RecordData &Record, unsigned &Idx) {
7168 switch (Name.getNameKind()) {
7169 case DeclarationName::CXXConstructorName:
7170 case DeclarationName::CXXDestructorName:
7171 case DeclarationName::CXXConversionFunctionName:
7172 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
7173 break;
7174
7175 case DeclarationName::CXXOperatorName:
7176 DNLoc.CXXOperatorName.BeginOpNameLoc
7177 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7178 DNLoc.CXXOperatorName.EndOpNameLoc
7179 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7180 break;
7181
7182 case DeclarationName::CXXLiteralOperatorName:
7183 DNLoc.CXXLiteralOperatorName.OpNameLoc
7184 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
7185 break;
7186
7187 case DeclarationName::Identifier:
7188 case DeclarationName::ObjCZeroArgSelector:
7189 case DeclarationName::ObjCOneArgSelector:
7190 case DeclarationName::ObjCMultiArgSelector:
7191 case DeclarationName::CXXUsingDirective:
7192 break;
7193 }
7194}
7195
7196void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
7197 DeclarationNameInfo &NameInfo,
7198 const RecordData &Record, unsigned &Idx) {
7199 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
7200 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
7201 DeclarationNameLoc DNLoc;
7202 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
7203 NameInfo.setInfo(DNLoc);
7204}
7205
7206void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
7207 const RecordData &Record, unsigned &Idx) {
7208 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
7209 unsigned NumTPLists = Record[Idx++];
7210 Info.NumTemplParamLists = NumTPLists;
7211 if (NumTPLists) {
7212 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
7213 for (unsigned i=0; i != NumTPLists; ++i)
7214 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
7215 }
7216}
7217
7218TemplateName
7219ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
7220 unsigned &Idx) {
7221 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
7222 switch (Kind) {
7223 case TemplateName::Template:
7224 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
7225
7226 case TemplateName::OverloadedTemplate: {
7227 unsigned size = Record[Idx++];
7228 UnresolvedSet<8> Decls;
7229 while (size--)
7230 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
7231
7232 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
7233 }
7234
7235 case TemplateName::QualifiedTemplate: {
7236 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7237 bool hasTemplKeyword = Record[Idx++];
7238 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
7239 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
7240 }
7241
7242 case TemplateName::DependentTemplate: {
7243 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
7244 if (Record[Idx++]) // isIdentifier
7245 return Context.getDependentTemplateName(NNS,
7246 GetIdentifierInfo(F, Record,
7247 Idx));
7248 return Context.getDependentTemplateName(NNS,
7249 (OverloadedOperatorKind)Record[Idx++]);
7250 }
7251
7252 case TemplateName::SubstTemplateTemplateParm: {
7253 TemplateTemplateParmDecl *param
7254 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7255 if (!param) return TemplateName();
7256 TemplateName replacement = ReadTemplateName(F, Record, Idx);
7257 return Context.getSubstTemplateTemplateParm(param, replacement);
7258 }
7259
7260 case TemplateName::SubstTemplateTemplateParmPack: {
7261 TemplateTemplateParmDecl *Param
7262 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
7263 if (!Param)
7264 return TemplateName();
7265
7266 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
7267 if (ArgPack.getKind() != TemplateArgument::Pack)
7268 return TemplateName();
7269
7270 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
7271 }
7272 }
7273
7274 llvm_unreachable("Unhandled template name kind!");
7275}
7276
7277TemplateArgument
7278ASTReader::ReadTemplateArgument(ModuleFile &F,
7279 const RecordData &Record, unsigned &Idx) {
7280 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
7281 switch (Kind) {
7282 case TemplateArgument::Null:
7283 return TemplateArgument();
7284 case TemplateArgument::Type:
7285 return TemplateArgument(readType(F, Record, Idx));
7286 case TemplateArgument::Declaration: {
7287 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
7288 bool ForReferenceParam = Record[Idx++];
7289 return TemplateArgument(D, ForReferenceParam);
7290 }
7291 case TemplateArgument::NullPtr:
7292 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
7293 case TemplateArgument::Integral: {
7294 llvm::APSInt Value = ReadAPSInt(Record, Idx);
7295 QualType T = readType(F, Record, Idx);
7296 return TemplateArgument(Context, Value, T);
7297 }
7298 case TemplateArgument::Template:
7299 return TemplateArgument(ReadTemplateName(F, Record, Idx));
7300 case TemplateArgument::TemplateExpansion: {
7301 TemplateName Name = ReadTemplateName(F, Record, Idx);
David Blaikie05785d12013-02-20 22:23:23 +00007302 Optional<unsigned> NumTemplateExpansions;
Guy Benyei11169dd2012-12-18 14:30:41 +00007303 if (unsigned NumExpansions = Record[Idx++])
7304 NumTemplateExpansions = NumExpansions - 1;
7305 return TemplateArgument(Name, NumTemplateExpansions);
7306 }
7307 case TemplateArgument::Expression:
7308 return TemplateArgument(ReadExpr(F));
7309 case TemplateArgument::Pack: {
7310 unsigned NumArgs = Record[Idx++];
7311 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
7312 for (unsigned I = 0; I != NumArgs; ++I)
7313 Args[I] = ReadTemplateArgument(F, Record, Idx);
7314 return TemplateArgument(Args, NumArgs);
7315 }
7316 }
7317
7318 llvm_unreachable("Unhandled template argument kind!");
7319}
7320
7321TemplateParameterList *
7322ASTReader::ReadTemplateParameterList(ModuleFile &F,
7323 const RecordData &Record, unsigned &Idx) {
7324 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
7325 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
7326 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
7327
7328 unsigned NumParams = Record[Idx++];
7329 SmallVector<NamedDecl *, 16> Params;
7330 Params.reserve(NumParams);
7331 while (NumParams--)
7332 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
7333
7334 TemplateParameterList* TemplateParams =
7335 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
7336 Params.data(), Params.size(), RAngleLoc);
7337 return TemplateParams;
7338}
7339
7340void
7341ASTReader::
Craig Topper5603df42013-07-05 19:34:19 +00007342ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs,
Guy Benyei11169dd2012-12-18 14:30:41 +00007343 ModuleFile &F, const RecordData &Record,
7344 unsigned &Idx) {
7345 unsigned NumTemplateArgs = Record[Idx++];
7346 TemplArgs.reserve(NumTemplateArgs);
7347 while (NumTemplateArgs--)
7348 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
7349}
7350
7351/// \brief Read a UnresolvedSet structure.
Richard Smitha4ba74c2013-08-30 04:46:40 +00007352void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set,
Guy Benyei11169dd2012-12-18 14:30:41 +00007353 const RecordData &Record, unsigned &Idx) {
7354 unsigned NumDecls = Record[Idx++];
7355 Set.reserve(Context, NumDecls);
7356 while (NumDecls--) {
Richard Smitha4ba74c2013-08-30 04:46:40 +00007357 DeclID ID = ReadDeclID(F, Record, Idx);
Guy Benyei11169dd2012-12-18 14:30:41 +00007358 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Richard Smitha4ba74c2013-08-30 04:46:40 +00007359 Set.addLazyDecl(Context, ID, AS);
Guy Benyei11169dd2012-12-18 14:30:41 +00007360 }
7361}
7362
7363CXXBaseSpecifier
7364ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
7365 const RecordData &Record, unsigned &Idx) {
7366 bool isVirtual = static_cast<bool>(Record[Idx++]);
7367 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
7368 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
7369 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
7370 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
7371 SourceRange Range = ReadSourceRange(F, Record, Idx);
7372 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
7373 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
7374 EllipsisLoc);
7375 Result.setInheritConstructors(inheritConstructors);
7376 return Result;
7377}
7378
7379std::pair<CXXCtorInitializer **, unsigned>
7380ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
7381 unsigned &Idx) {
7382 CXXCtorInitializer **CtorInitializers = 0;
7383 unsigned NumInitializers = Record[Idx++];
7384 if (NumInitializers) {
7385 CtorInitializers
7386 = new (Context) CXXCtorInitializer*[NumInitializers];
7387 for (unsigned i=0; i != NumInitializers; ++i) {
7388 TypeSourceInfo *TInfo = 0;
7389 bool IsBaseVirtual = false;
7390 FieldDecl *Member = 0;
7391 IndirectFieldDecl *IndirectMember = 0;
7392
7393 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
7394 switch (Type) {
7395 case CTOR_INITIALIZER_BASE:
7396 TInfo = GetTypeSourceInfo(F, Record, Idx);
7397 IsBaseVirtual = Record[Idx++];
7398 break;
7399
7400 case CTOR_INITIALIZER_DELEGATING:
7401 TInfo = GetTypeSourceInfo(F, Record, Idx);
7402 break;
7403
7404 case CTOR_INITIALIZER_MEMBER:
7405 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
7406 break;
7407
7408 case CTOR_INITIALIZER_INDIRECT_MEMBER:
7409 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
7410 break;
7411 }
7412
7413 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
7414 Expr *Init = ReadExpr(F);
7415 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
7416 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
7417 bool IsWritten = Record[Idx++];
7418 unsigned SourceOrderOrNumArrayIndices;
7419 SmallVector<VarDecl *, 8> Indices;
7420 if (IsWritten) {
7421 SourceOrderOrNumArrayIndices = Record[Idx++];
7422 } else {
7423 SourceOrderOrNumArrayIndices = Record[Idx++];
7424 Indices.reserve(SourceOrderOrNumArrayIndices);
7425 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
7426 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
7427 }
7428
7429 CXXCtorInitializer *BOMInit;
7430 if (Type == CTOR_INITIALIZER_BASE) {
7431 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
7432 LParenLoc, Init, RParenLoc,
7433 MemberOrEllipsisLoc);
7434 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
7435 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
7436 Init, RParenLoc);
7437 } else if (IsWritten) {
7438 if (Member)
7439 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
7440 LParenLoc, Init, RParenLoc);
7441 else
7442 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7443 MemberOrEllipsisLoc, LParenLoc,
7444 Init, RParenLoc);
7445 } else {
Argyrios Kyrtzidis794671d2013-05-30 23:59:46 +00007446 if (IndirectMember) {
7447 assert(Indices.empty() && "Indirect field improperly initialized");
7448 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
7449 MemberOrEllipsisLoc, LParenLoc,
7450 Init, RParenLoc);
7451 } else {
7452 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
7453 LParenLoc, Init, RParenLoc,
7454 Indices.data(), Indices.size());
7455 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007456 }
7457
7458 if (IsWritten)
7459 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
7460 CtorInitializers[i] = BOMInit;
7461 }
7462 }
7463
7464 return std::make_pair(CtorInitializers, NumInitializers);
7465}
7466
7467NestedNameSpecifier *
7468ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
7469 const RecordData &Record, unsigned &Idx) {
7470 unsigned N = Record[Idx++];
7471 NestedNameSpecifier *NNS = 0, *Prev = 0;
7472 for (unsigned I = 0; I != N; ++I) {
7473 NestedNameSpecifier::SpecifierKind Kind
7474 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7475 switch (Kind) {
7476 case NestedNameSpecifier::Identifier: {
7477 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7478 NNS = NestedNameSpecifier::Create(Context, Prev, II);
7479 break;
7480 }
7481
7482 case NestedNameSpecifier::Namespace: {
7483 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7484 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
7485 break;
7486 }
7487
7488 case NestedNameSpecifier::NamespaceAlias: {
7489 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7490 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
7491 break;
7492 }
7493
7494 case NestedNameSpecifier::TypeSpec:
7495 case NestedNameSpecifier::TypeSpecWithTemplate: {
7496 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
7497 if (!T)
7498 return 0;
7499
7500 bool Template = Record[Idx++];
7501 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
7502 break;
7503 }
7504
7505 case NestedNameSpecifier::Global: {
7506 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
7507 // No associated value, and there can't be a prefix.
7508 break;
7509 }
7510 }
7511 Prev = NNS;
7512 }
7513 return NNS;
7514}
7515
7516NestedNameSpecifierLoc
7517ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
7518 unsigned &Idx) {
7519 unsigned N = Record[Idx++];
7520 NestedNameSpecifierLocBuilder Builder;
7521 for (unsigned I = 0; I != N; ++I) {
7522 NestedNameSpecifier::SpecifierKind Kind
7523 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
7524 switch (Kind) {
7525 case NestedNameSpecifier::Identifier: {
7526 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
7527 SourceRange Range = ReadSourceRange(F, Record, Idx);
7528 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
7529 break;
7530 }
7531
7532 case NestedNameSpecifier::Namespace: {
7533 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
7534 SourceRange Range = ReadSourceRange(F, Record, Idx);
7535 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
7536 break;
7537 }
7538
7539 case NestedNameSpecifier::NamespaceAlias: {
7540 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
7541 SourceRange Range = ReadSourceRange(F, Record, Idx);
7542 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
7543 break;
7544 }
7545
7546 case NestedNameSpecifier::TypeSpec:
7547 case NestedNameSpecifier::TypeSpecWithTemplate: {
7548 bool Template = Record[Idx++];
7549 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
7550 if (!T)
7551 return NestedNameSpecifierLoc();
7552 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7553
7554 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
7555 Builder.Extend(Context,
7556 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
7557 T->getTypeLoc(), ColonColonLoc);
7558 break;
7559 }
7560
7561 case NestedNameSpecifier::Global: {
7562 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
7563 Builder.MakeGlobal(Context, ColonColonLoc);
7564 break;
7565 }
7566 }
7567 }
7568
7569 return Builder.getWithLocInContext(Context);
7570}
7571
7572SourceRange
7573ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
7574 unsigned &Idx) {
7575 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
7576 SourceLocation end = ReadSourceLocation(F, Record, Idx);
7577 return SourceRange(beg, end);
7578}
7579
7580/// \brief Read an integral value
7581llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
7582 unsigned BitWidth = Record[Idx++];
7583 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
7584 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
7585 Idx += NumWords;
7586 return Result;
7587}
7588
7589/// \brief Read a signed integral value
7590llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
7591 bool isUnsigned = Record[Idx++];
7592 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
7593}
7594
7595/// \brief Read a floating-point value
Tim Northover178723a2013-01-22 09:46:51 +00007596llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record,
7597 const llvm::fltSemantics &Sem,
7598 unsigned &Idx) {
7599 return llvm::APFloat(Sem, ReadAPInt(Record, Idx));
Guy Benyei11169dd2012-12-18 14:30:41 +00007600}
7601
7602// \brief Read a string
7603std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
7604 unsigned Len = Record[Idx++];
7605 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
7606 Idx += Len;
7607 return Result;
7608}
7609
7610VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
7611 unsigned &Idx) {
7612 unsigned Major = Record[Idx++];
7613 unsigned Minor = Record[Idx++];
7614 unsigned Subminor = Record[Idx++];
7615 if (Minor == 0)
7616 return VersionTuple(Major);
7617 if (Subminor == 0)
7618 return VersionTuple(Major, Minor - 1);
7619 return VersionTuple(Major, Minor - 1, Subminor - 1);
7620}
7621
7622CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
7623 const RecordData &Record,
7624 unsigned &Idx) {
7625 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
7626 return CXXTemporary::Create(Context, Decl);
7627}
7628
7629DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Argyrios Kyrtzidisdc9fdaf2013-05-24 05:44:08 +00007630 return Diag(CurrentImportLoc, DiagID);
Guy Benyei11169dd2012-12-18 14:30:41 +00007631}
7632
7633DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
7634 return Diags.Report(Loc, DiagID);
7635}
7636
7637/// \brief Retrieve the identifier table associated with the
7638/// preprocessor.
7639IdentifierTable &ASTReader::getIdentifierTable() {
7640 return PP.getIdentifierTable();
7641}
7642
7643/// \brief Record that the given ID maps to the given switch-case
7644/// statement.
7645void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
7646 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
7647 "Already have a SwitchCase with this ID");
7648 (*CurrSwitchCaseStmts)[ID] = SC;
7649}
7650
7651/// \brief Retrieve the switch-case statement with the given ID.
7652SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
7653 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
7654 return (*CurrSwitchCaseStmts)[ID];
7655}
7656
7657void ASTReader::ClearSwitchCaseIDs() {
7658 CurrSwitchCaseStmts->clear();
7659}
7660
7661void ASTReader::ReadComments() {
7662 std::vector<RawComment *> Comments;
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007663 for (SmallVectorImpl<std::pair<BitstreamCursor,
Guy Benyei11169dd2012-12-18 14:30:41 +00007664 serialization::ModuleFile *> >::iterator
7665 I = CommentsCursors.begin(),
7666 E = CommentsCursors.end();
7667 I != E; ++I) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007668 BitstreamCursor &Cursor = I->first;
Guy Benyei11169dd2012-12-18 14:30:41 +00007669 serialization::ModuleFile &F = *I->second;
7670 SavedStreamPosition SavedPosition(Cursor);
7671
7672 RecordData Record;
7673 while (true) {
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007674 llvm::BitstreamEntry Entry =
7675 Cursor.advanceSkippingSubblocks(BitstreamCursor::AF_DontPopBlockAtEnd);
7676
7677 switch (Entry.Kind) {
7678 case llvm::BitstreamEntry::SubBlock: // Handled for us already.
7679 case llvm::BitstreamEntry::Error:
7680 Error("malformed block record in AST file");
7681 return;
7682 case llvm::BitstreamEntry::EndBlock:
7683 goto NextCursor;
7684 case llvm::BitstreamEntry::Record:
7685 // The interesting case.
Guy Benyei11169dd2012-12-18 14:30:41 +00007686 break;
Guy Benyei11169dd2012-12-18 14:30:41 +00007687 }
7688
7689 // Read a record.
7690 Record.clear();
Chris Lattner0e6c9402013-01-20 02:38:54 +00007691 switch ((CommentRecordTypes)Cursor.readRecord(Entry.ID, Record)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007692 case COMMENTS_RAW_COMMENT: {
7693 unsigned Idx = 0;
7694 SourceRange SR = ReadSourceRange(F, Record, Idx);
7695 RawComment::CommentKind Kind =
7696 (RawComment::CommentKind) Record[Idx++];
7697 bool IsTrailingComment = Record[Idx++];
7698 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00007699 Comments.push_back(new (Context) RawComment(
7700 SR, Kind, IsTrailingComment, IsAlmostTrailingComment,
7701 Context.getLangOpts().CommentOpts.ParseAllComments));
Guy Benyei11169dd2012-12-18 14:30:41 +00007702 break;
7703 }
7704 }
7705 }
Chris Lattner7fb3bef2013-01-20 00:56:42 +00007706 NextCursor:;
Guy Benyei11169dd2012-12-18 14:30:41 +00007707 }
7708 Context.Comments.addCommentsToFront(Comments);
7709}
7710
7711void ASTReader::finishPendingActions() {
7712 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
Richard Smith2b9e3e32013-10-18 06:05:18 +00007713 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
7714 !PendingOdrMergeChecks.empty()) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007715 // If any identifiers with corresponding top-level declarations have
7716 // been loaded, load those declarations now.
Craig Topper79be4cd2013-07-05 04:33:53 +00007717 typedef llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2> >
7718 TopLevelDeclsMap;
7719 TopLevelDeclsMap TopLevelDecls;
7720
Guy Benyei11169dd2012-12-18 14:30:41 +00007721 while (!PendingIdentifierInfos.empty()) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007722 // FIXME: std::move
7723 IdentifierInfo *II = PendingIdentifierInfos.back().first;
7724 SmallVector<uint32_t, 4> DeclIDs = PendingIdentifierInfos.back().second;
Douglas Gregorcb15f082013-02-19 18:26:28 +00007725 PendingIdentifierInfos.pop_back();
Douglas Gregor6168bd22013-02-18 15:53:43 +00007726
7727 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
Guy Benyei11169dd2012-12-18 14:30:41 +00007728 }
7729
7730 // Load pending declaration chains.
7731 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
7732 loadPendingDeclChain(PendingDeclChains[I]);
7733 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
7734 }
7735 PendingDeclChains.clear();
7736
Douglas Gregor6168bd22013-02-18 15:53:43 +00007737 // Make the most recent of the top-level declarations visible.
Craig Topper79be4cd2013-07-05 04:33:53 +00007738 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
7739 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
Douglas Gregor6168bd22013-02-18 15:53:43 +00007740 IdentifierInfo *II = TLD->first;
7741 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007742 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
Douglas Gregor6168bd22013-02-18 15:53:43 +00007743 }
7744 }
7745
Guy Benyei11169dd2012-12-18 14:30:41 +00007746 // Load any pending macro definitions.
7747 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007748 IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
7749 SmallVector<PendingMacroInfo, 2> GlobalIDs;
7750 GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
7751 // Initialize the macro history from chained-PCHs ahead of module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00007752 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00007753 ++IDIdx) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007754 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7755 if (Info.M->Kind != MK_Module)
7756 resolvePendingMacro(II, Info);
7757 }
7758 // Handle module imports.
Richard Smith49f906a2014-03-01 00:08:04 +00007759 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00007760 ++IDIdx) {
7761 const PendingMacroInfo &Info = GlobalIDs[IDIdx];
7762 if (Info.M->Kind == MK_Module)
7763 resolvePendingMacro(II, Info);
Guy Benyei11169dd2012-12-18 14:30:41 +00007764 }
7765 }
7766 PendingMacroIDs.clear();
Argyrios Kyrtzidis83a6e3b2013-02-16 00:48:59 +00007767
7768 // Wire up the DeclContexts for Decls that we delayed setting until
7769 // recursive loading is completed.
7770 while (!PendingDeclContextInfos.empty()) {
7771 PendingDeclContextInfo Info = PendingDeclContextInfos.front();
7772 PendingDeclContextInfos.pop_front();
7773 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
7774 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
7775 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
7776 }
Richard Smith2b9e3e32013-10-18 06:05:18 +00007777
7778 // For each declaration from a merged context, check that the canonical
7779 // definition of that context also contains a declaration of the same
7780 // entity.
7781 while (!PendingOdrMergeChecks.empty()) {
7782 NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
7783
7784 // FIXME: Skip over implicit declarations for now. This matters for things
7785 // like implicitly-declared special member functions. This isn't entirely
7786 // correct; we can end up with multiple unmerged declarations of the same
7787 // implicit entity.
7788 if (D->isImplicit())
7789 continue;
7790
7791 DeclContext *CanonDef = D->getDeclContext();
7792 DeclContext::lookup_result R = CanonDef->lookup(D->getDeclName());
7793
7794 bool Found = false;
7795 const Decl *DCanon = D->getCanonicalDecl();
7796
7797 llvm::SmallVector<const NamedDecl*, 4> Candidates;
7798 for (DeclContext::lookup_iterator I = R.begin(), E = R.end();
7799 !Found && I != E; ++I) {
Aaron Ballman86c93902014-03-06 23:45:36 +00007800 for (auto RI : (*I)->redecls()) {
7801 if (RI->getLexicalDeclContext() == CanonDef) {
Richard Smith2b9e3e32013-10-18 06:05:18 +00007802 // This declaration is present in the canonical definition. If it's
7803 // in the same redecl chain, it's the one we're looking for.
Aaron Ballman86c93902014-03-06 23:45:36 +00007804 if (RI->getCanonicalDecl() == DCanon)
Richard Smith2b9e3e32013-10-18 06:05:18 +00007805 Found = true;
7806 else
Aaron Ballman86c93902014-03-06 23:45:36 +00007807 Candidates.push_back(cast<NamedDecl>(RI));
Richard Smith2b9e3e32013-10-18 06:05:18 +00007808 break;
7809 }
7810 }
7811 }
7812
7813 if (!Found) {
7814 D->setInvalidDecl();
7815
7816 Module *CanonDefModule = cast<Decl>(CanonDef)->getOwningModule();
7817 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
7818 << D << D->getOwningModule()->getFullModuleName()
7819 << CanonDef << !CanonDefModule
7820 << (CanonDefModule ? CanonDefModule->getFullModuleName() : "");
7821
7822 if (Candidates.empty())
7823 Diag(cast<Decl>(CanonDef)->getLocation(),
7824 diag::note_module_odr_violation_no_possible_decls) << D;
7825 else {
7826 for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
7827 Diag(Candidates[I]->getLocation(),
7828 diag::note_module_odr_violation_possible_decl)
7829 << Candidates[I];
7830 }
7831 }
7832 }
Guy Benyei11169dd2012-12-18 14:30:41 +00007833 }
7834
7835 // If we deserialized any C++ or Objective-C class definitions, any
7836 // Objective-C protocol definitions, or any redeclarable templates, make sure
7837 // that all redeclarations point to the definitions. Note that this can only
7838 // happen now, after the redeclaration chains have been fully wired.
7839 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
7840 DEnd = PendingDefinitions.end();
7841 D != DEnd; ++D) {
7842 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
7843 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
7844 // Make sure that the TagType points at the definition.
7845 const_cast<TagType*>(TagT)->decl = TD;
7846 }
7847
Aaron Ballman86c93902014-03-06 23:45:36 +00007848 if (auto RD = dyn_cast<CXXRecordDecl>(*D)) {
7849 for (auto R : RD->redecls())
7850 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
Guy Benyei11169dd2012-12-18 14:30:41 +00007851
7852 }
7853
7854 continue;
7855 }
7856
Aaron Ballman86c93902014-03-06 23:45:36 +00007857 if (auto ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Guy Benyei11169dd2012-12-18 14:30:41 +00007858 // Make sure that the ObjCInterfaceType points at the definition.
7859 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
7860 ->Decl = ID;
7861
Aaron Ballman86c93902014-03-06 23:45:36 +00007862 for (auto R : ID->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007863 R->Data = ID->Data;
7864
7865 continue;
7866 }
7867
Aaron Ballman86c93902014-03-06 23:45:36 +00007868 if (auto PD = dyn_cast<ObjCProtocolDecl>(*D)) {
7869 for (auto R : PD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007870 R->Data = PD->Data;
7871
7872 continue;
7873 }
7874
Aaron Ballman86c93902014-03-06 23:45:36 +00007875 auto RTD = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
7876 for (auto R : RTD->redecls())
Guy Benyei11169dd2012-12-18 14:30:41 +00007877 R->Common = RTD->Common;
7878 }
7879 PendingDefinitions.clear();
7880
7881 // Load the bodies of any functions or methods we've encountered. We do
7882 // this now (delayed) so that we can be sure that the declaration chains
7883 // have been fully wired up.
7884 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
7885 PBEnd = PendingBodies.end();
7886 PB != PBEnd; ++PB) {
7887 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
7888 // FIXME: Check for =delete/=default?
7889 // FIXME: Complain about ODR violations here?
7890 if (!getContext().getLangOpts().Modules || !FD->hasBody())
7891 FD->setLazyBody(PB->second);
7892 continue;
7893 }
7894
7895 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
7896 if (!getContext().getLangOpts().Modules || !MD->hasBody())
7897 MD->setLazyBody(PB->second);
7898 }
7899 PendingBodies.clear();
7900}
7901
7902void ASTReader::FinishedDeserializing() {
7903 assert(NumCurrentElementsDeserializing &&
7904 "FinishedDeserializing not paired with StartedDeserializing");
7905 if (NumCurrentElementsDeserializing == 1) {
7906 // We decrease NumCurrentElementsDeserializing only after pending actions
7907 // are finished, to avoid recursively re-calling finishPendingActions().
7908 finishPendingActions();
7909 }
7910 --NumCurrentElementsDeserializing;
7911
7912 if (NumCurrentElementsDeserializing == 0 &&
7913 Consumer && !PassingDeclsToConsumer) {
7914 // Guard variable to avoid recursively redoing the process of passing
7915 // decls to consumer.
7916 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
7917 true);
7918
7919 while (!InterestingDecls.empty()) {
7920 // We are not in recursive loading, so it's safe to pass the "interesting"
7921 // decls to the consumer.
7922 Decl *D = InterestingDecls.front();
7923 InterestingDecls.pop_front();
7924 PassInterestingDeclToConsumer(D);
7925 }
7926 }
7927}
7928
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007929void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
Rafael Espindola7b56f6c2013-10-19 16:55:03 +00007930 D = D->getMostRecentDecl();
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00007931
7932 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
7933 SemaObj->TUScope->AddDecl(D);
7934 } else if (SemaObj->TUScope) {
7935 // Adding the decl to IdResolver may have failed because it was already in
7936 // (even though it was not added in scope). If it is already in, make sure
7937 // it gets in the scope as well.
7938 if (std::find(SemaObj->IdResolver.begin(Name),
7939 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
7940 SemaObj->TUScope->AddDecl(D);
7941 }
7942}
7943
Guy Benyei11169dd2012-12-18 14:30:41 +00007944ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
7945 StringRef isysroot, bool DisableValidation,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007946 bool AllowASTWithCompilerErrors,
7947 bool AllowConfigurationMismatch,
Ben Langmuir3d4417c2014-02-07 17:31:11 +00007948 bool ValidateSystemInputs,
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007949 bool UseGlobalIndex)
Guy Benyei11169dd2012-12-18 14:30:41 +00007950 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7951 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
7952 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
7953 Consumer(0), ModuleMgr(PP.getFileManager()),
7954 isysroot(isysroot), DisableValidation(DisableValidation),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007955 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Ben Langmuir2cb4a782014-02-05 22:21:15 +00007956 AllowConfigurationMismatch(AllowConfigurationMismatch),
Ben Langmuir3d4417c2014-02-07 17:31:11 +00007957 ValidateSystemInputs(ValidateSystemInputs),
Douglas Gregorc1bbec82013-01-25 00:45:27 +00007958 UseGlobalIndex(UseGlobalIndex), TriedLoadingGlobalIndex(false),
Guy Benyei11169dd2012-12-18 14:30:41 +00007959 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7960 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor00a50f72013-01-25 00:38:33 +00007961 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7962 TotalNumMacros(0), NumIdentifierLookups(0), NumIdentifierLookupHits(0),
7963 NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
Douglas Gregorad2f7a52013-01-28 17:54:36 +00007964 NumMethodPoolLookups(0), NumMethodPoolHits(0),
7965 NumMethodPoolTableLookups(0), NumMethodPoolTableHits(0),
7966 TotalNumMethodPoolEntries(0),
Guy Benyei11169dd2012-12-18 14:30:41 +00007967 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
7968 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7969 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
7970 PassingDeclsToConsumer(false),
Richard Smith629ff362013-07-31 00:26:46 +00007971 NumCXXBaseSpecifiersLoaded(0), ReadingKind(Read_None)
Guy Benyei11169dd2012-12-18 14:30:41 +00007972{
7973 SourceMgr.setExternalSLocEntrySource(this);
7974}
7975
7976ASTReader::~ASTReader() {
7977 for (DeclContextVisibleUpdatesPending::iterator
7978 I = PendingVisibleUpdates.begin(),
7979 E = PendingVisibleUpdates.end();
7980 I != E; ++I) {
7981 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7982 F = I->second.end();
7983 J != F; ++J)
7984 delete J->first;
7985 }
7986}