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