blob: f64962e341c5efcd0c1052f986ff796d38383bf1 [file] [log] [blame]
Sebastian Redl3b3c8742010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +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//
Sebastian Redl2c499f62010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner92ba5ff2009-04-27 05:14:47 +000013
Sebastian Redlf5b13462010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Chandler Carruth22a11b72011-12-09 00:02:23 +000017#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000018#include "ASTCommon.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000019#include "ASTReaderInternals.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000020#include "clang/Sema/Sema.h"
John McCallcc14d1f2010-08-24 08:50:51 +000021#include "clang/Sema/Scope.h"
Douglas Gregor1a0d0b92009-04-14 00:24:19 +000022#include "clang/AST/ASTConsumer.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include "clang/AST/ASTContext.h"
John McCall19c1bfd2010-08-25 05:32:35 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Douglas Gregor9b272512011-02-28 23:58:31 +000027#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000028#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000029#include "clang/AST/TypeLocVisitor.h"
Chris Lattner34321bc2009-04-10 21:41:48 +000030#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000031#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Lex/Preprocessor.h"
Douglas Gregorb6af6c22012-10-24 20:05:57 +000033#include "clang/Lex/PreprocessorOptions.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000034#include "clang/Lex/HeaderSearch.h"
Douglas Gregor2d302362012-10-24 16:50:34 +000035#include "clang/Lex/HeaderSearchOptions.h"
Douglas Gregora868bbd2009-04-21 22:25:48 +000036#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000037#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000038#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000039#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000040#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000041#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000042#include "clang/Basic/TargetOptions.h"
Douglas Gregord54f3a12009-10-05 21:07:28 +000043#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000044#include "clang/Basic/VersionTuple.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000045#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000046#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047#include "llvm/Support/MemoryBuffer.h"
John McCall0ad16662009-10-29 08:12:44 +000048#include "llvm/Support/ErrorHandling.h"
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Nick Lewycky2bd0ab22012-04-16 02:51:46 +000051#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencerf25faaa2010-12-09 17:36:38 +000052#include "llvm/Support/system_error.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053#include <algorithm>
Douglas Gregorc379c072009-04-28 18:58:38 +000054#include <iterator>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055#include <cstdio>
Douglas Gregorc5046832009-04-27 18:38:38 +000056#include <sys/stat.h>
Douglas Gregor09b69892011-02-10 17:09:37 +000057
Douglas Gregoref84c4b2009-04-09 22:27:44 +000058using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000059using namespace clang::serialization;
Douglas Gregord44252e2011-08-25 20:47:51 +000060using namespace clang::serialization::reader;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061
62//===----------------------------------------------------------------------===//
Sebastian Redld44cd6a2010-08-18 23:57:06 +000063// PCH validator implementation
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000064//===----------------------------------------------------------------------===//
65
Sebastian Redl3e31c722010-08-18 23:56:56 +000066ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000067
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000068/// \brief Compare the given set of language options against an existing set of
69/// language options.
70///
71/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
72///
73/// \returns true if the languagae options mis-match, false otherwise.
74static bool checkLanguageOptions(const LangOptions &LangOpts,
75 const LangOptions &ExistingLangOpts,
76 DiagnosticsEngine *Diags) {
77#define LANGOPT(Name, Bits, Default, Description) \
78 if (ExistingLangOpts.Name != LangOpts.Name) { \
79 if (Diags) \
80 Diags->Report(diag::err_pch_langopt_mismatch) \
81 << Description << LangOpts.Name << ExistingLangOpts.Name; \
82 return true; \
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000083 }
84
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000085#define VALUE_LANGOPT(Name, Bits, Default, Description) \
86 if (ExistingLangOpts.Name != LangOpts.Name) { \
87 if (Diags) \
88 Diags->Report(diag::err_pch_langopt_value_mismatch) \
89 << Description; \
90 return true; \
Douglas Gregor4b29c162012-10-22 23:51:00 +000091 }
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +000092
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000093#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
94 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \
95 if (Diags) \
96 Diags->Report(diag::err_pch_langopt_value_mismatch) \
97 << Description; \
98 return true; \
Douglas Gregorc2ae8802011-09-13 18:26:39 +000099 }
100
101#define BENIGN_LANGOPT(Name, Bits, Default, Description)
102#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
103#include "clang/Basic/LangOptions.def"
John McCall5fb5df92012-06-20 06:18:46 +0000104
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000105 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
106 if (Diags)
107 Diags->Report(diag::err_pch_langopt_value_mismatch)
108 << "target Objective-C runtime";
John McCall5fb5df92012-06-20 06:18:46 +0000109 return true;
110 }
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000111
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000112 return false;
113}
114
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000115/// \brief Compare the given set of target options against an existing set of
116/// target options.
117///
118/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
119///
120/// \returns true if the target options mis-match, false otherwise.
121static bool checkTargetOptions(const TargetOptions &TargetOpts,
122 const TargetOptions &ExistingTargetOpts,
123 DiagnosticsEngine *Diags) {
Douglas Gregor4b29c162012-10-22 23:51:00 +0000124#define CHECK_TARGET_OPT(Field, Name) \
125 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000126 if (Diags) \
127 Diags->Report(diag::err_pch_targetopt_mismatch) \
Douglas Gregor4b29c162012-10-22 23:51:00 +0000128 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
129 return true; \
Douglas Gregorcb177f12012-10-16 23:40:58 +0000130 }
131
132 CHECK_TARGET_OPT(Triple, "target");
133 CHECK_TARGET_OPT(CPU, "target CPU");
134 CHECK_TARGET_OPT(ABI, "target ABI");
135 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
136 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
137#undef CHECK_TARGET_OPT
138
139 // Compare feature sets.
140 SmallVector<StringRef, 4> ExistingFeatures(
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000141 ExistingTargetOpts.FeaturesAsWritten.begin(),
142 ExistingTargetOpts.FeaturesAsWritten.end());
Douglas Gregorcb177f12012-10-16 23:40:58 +0000143 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
144 TargetOpts.FeaturesAsWritten.end());
145 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
146 std::sort(ReadFeatures.begin(), ReadFeatures.end());
147
148 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
149 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
150 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
151 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
152 ++ExistingIdx;
153 ++ReadIdx;
154 continue;
155 }
156
157 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000158 if (Diags)
159 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor4b29c162012-10-22 23:51:00 +0000160 << false << ReadFeatures[ReadIdx];
Douglas Gregorcb177f12012-10-16 23:40:58 +0000161 return true;
162 }
163
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000164 if (Diags)
165 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor4b29c162012-10-22 23:51:00 +0000166 << true << ExistingFeatures[ExistingIdx];
Douglas Gregorcb177f12012-10-16 23:40:58 +0000167 return true;
168 }
169
170 if (ExistingIdx < ExistingN) {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000171 if (Diags)
172 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor4b29c162012-10-22 23:51:00 +0000173 << true << ExistingFeatures[ExistingIdx];
Douglas Gregorcb177f12012-10-16 23:40:58 +0000174 return true;
175 }
176
177 if (ReadIdx < ReadN) {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000178 if (Diags)
179 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor4b29c162012-10-22 23:51:00 +0000180 << false << ReadFeatures[ReadIdx];
Douglas Gregorcb177f12012-10-16 23:40:58 +0000181 return true;
182 }
183
184 return false;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000185}
186
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000187bool
188PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
189 bool Complain) {
Douglas Gregorb6368752012-10-24 23:41:50 +0000190 const LangOptions &ExistingLangOpts = PP.getLangOpts();
191 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000192 Complain? &Reader.Diags : 0);
193}
194
195bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
196 bool Complain) {
197 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
198 return checkTargetOptions(TargetOpts, ExistingTargetOpts,
199 Complain? &Reader.Diags : 0);
200}
201
Benjamin Kramer90b5b682010-11-25 18:29:30 +0000202namespace {
Douglas Gregorb6368752012-10-24 23:41:50 +0000203 typedef llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >
204 MacroDefinitionsMap;
205}
206
207/// \brief Collect the macro definitions provided by the given preprocessor
208/// options.
209static void collectMacroDefinitions(const PreprocessorOptions &PPOpts,
210 MacroDefinitionsMap &Macros,
211 SmallVectorImpl<StringRef> *MacroNames = 0){
212 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
213 StringRef Macro = PPOpts.Macros[I].first;
214 bool IsUndef = PPOpts.Macros[I].second;
215
216 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
217 StringRef MacroName = MacroPair.first;
218 StringRef MacroBody = MacroPair.second;
219
220 // For an #undef'd macro, we only care about the name.
221 if (IsUndef) {
222 if (MacroNames && !Macros.count(MacroName))
223 MacroNames->push_back(MacroName);
224
225 Macros[MacroName] = std::make_pair("", true);
226 continue;
227 }
228
229 // For a #define'd macro, figure out the actual definition.
230 if (MacroName.size() == Macro.size())
231 MacroBody = "1";
232 else {
233 // Note: GCC drops anything following an end-of-line character.
234 StringRef::size_type End = MacroBody.find_first_of("\n\r");
235 MacroBody = MacroBody.substr(0, End);
236 }
237
238 if (MacroNames && !Macros.count(MacroName))
239 MacroNames->push_back(MacroName);
240 Macros[MacroName] = std::make_pair(MacroBody, false);
241 }
242}
243
244/// \brief Check the preprocessor options deserialized from the control block
245/// against the preprocessor options in an existing preprocessor.
246///
247/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
248static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
249 const PreprocessorOptions &ExistingPPOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +0000250 DiagnosticsEngine *Diags,
251 FileManager &FileMgr,
252 std::string &SuggestedPredefines) {
Douglas Gregorb6368752012-10-24 23:41:50 +0000253 // Check macro definitions.
254 MacroDefinitionsMap ASTFileMacros;
255 collectMacroDefinitions(PPOpts, ASTFileMacros);
256 MacroDefinitionsMap ExistingMacros;
257 SmallVector<StringRef, 4> ExistingMacroNames;
258 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
259
260 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
261 // Dig out the macro definition in the existing preprocessor options.
262 StringRef MacroName = ExistingMacroNames[I];
263 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
264
265 // Check whether we know anything about this macro name or not.
266 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/> >::iterator Known
267 = ASTFileMacros.find(MacroName);
268 if (Known == ASTFileMacros.end()) {
269 // FIXME: Check whether this identifier was referenced anywhere in the
270 // AST file. If so, we should reject the AST file. Unfortunately, this
271 // information isn't in the control block. What shall we do about it?
Douglas Gregor55358ed2012-10-25 00:07:54 +0000272
273 if (Existing.second) {
274 SuggestedPredefines += "#undef ";
275 SuggestedPredefines += MacroName.str();
276 SuggestedPredefines += '\n';
277 } else {
278 SuggestedPredefines += "#define ";
279 SuggestedPredefines += MacroName.str();
Douglas Gregor471c1172012-10-25 00:25:27 +0000280 SuggestedPredefines += ' ';
Douglas Gregor55358ed2012-10-25 00:07:54 +0000281 SuggestedPredefines += Existing.first.str();
282 SuggestedPredefines += '\n';
283 }
Douglas Gregorb6368752012-10-24 23:41:50 +0000284 continue;
285 }
286
287 // If the macro was defined in one but undef'd in the other, we have a
288 // conflict.
289 if (Existing.second != Known->second.second) {
290 if (Diags) {
291 Diags->Report(diag::err_pch_macro_def_undef)
292 << MacroName << Known->second.second;
293 }
294 return true;
295 }
296
297 // If the macro was #undef'd in both, or if the macro bodies are identical,
298 // it's fine.
299 if (Existing.second || Existing.first == Known->second.first)
300 continue;
301
302 // The macro bodies differ; complain.
303 if (Diags) {
304 Diags->Report(diag::err_pch_macro_def_conflict)
305 << MacroName << Known->second.first << Existing.first;
306 }
307 return true;
308 }
309
310 // Check whether we're using predefines.
311 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines) {
312 if (Diags) {
313 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
314 }
315 return true;
316 }
317
Douglas Gregor55358ed2012-10-25 00:07:54 +0000318 // Compute the #include and #include_macros lines we need.
319 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
320 StringRef File = ExistingPPOpts.Includes[I];
321 if (File == ExistingPPOpts.ImplicitPCHInclude)
322 continue;
323
324 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
325 != PPOpts.Includes.end())
326 continue;
327
328 SuggestedPredefines += "#include \"";
329 SuggestedPredefines +=
330 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
331 SuggestedPredefines += "\"\n";
332 }
333
334 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
335 StringRef File = ExistingPPOpts.MacroIncludes[I];
336 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
337 File)
338 != PPOpts.MacroIncludes.end())
339 continue;
340
341 SuggestedPredefines += "#__include_macros \"";
342 SuggestedPredefines +=
343 HeaderSearch::NormalizeDashIncludePath(File, FileMgr);
344 SuggestedPredefines += "\"\n##\n";
345 }
346
Douglas Gregorb6368752012-10-24 23:41:50 +0000347 return false;
348}
349
350bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +0000351 bool Complain,
352 std::string &SuggestedPredefines) {
Douglas Gregorb6368752012-10-24 23:41:50 +0000353 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
354
355 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +0000356 Complain? &Reader.Diags : 0,
357 PP.getFileManager(),
358 SuggestedPredefines);
Douglas Gregorb6368752012-10-24 23:41:50 +0000359}
360
Douglas Gregor5712ebc2010-03-16 16:35:32 +0000361void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
362 unsigned ID) {
363 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
364 ++NumHeaderInfos;
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000365}
366
Argyrios Kyrtzidise445c722012-10-10 02:12:47 +0000367void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000368 PP.setCounterValue(Value);
369}
370
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +0000371//===----------------------------------------------------------------------===//
Sebastian Redl2c499f62010-08-18 23:56:43 +0000372// AST reader implementation
Douglas Gregora868bbd2009-04-21 22:25:48 +0000373//===----------------------------------------------------------------------===//
374
Sebastian Redl07a89a82010-07-30 00:29:29 +0000375void
Sebastian Redl3e31c722010-08-18 23:56:56 +0000376ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redl07a89a82010-07-30 00:29:29 +0000377 DeserializationListener = Listener;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000378}
379
Chris Lattner92ba5ff2009-04-27 05:14:47 +0000380
Douglas Gregorc78d3462009-04-24 21:10:55 +0000381
Douglas Gregord44252e2011-08-25 20:47:51 +0000382unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
383 return serialization::ComputeHash(Sel);
384}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000385
Mike Stump11289f42009-09-09 15:08:12 +0000386
Douglas Gregord44252e2011-08-25 20:47:51 +0000387std::pair<unsigned, unsigned>
388ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
389 using namespace clang::io;
390 unsigned KeyLen = ReadUnalignedLE16(d);
391 unsigned DataLen = ReadUnalignedLE16(d);
392 return std::make_pair(KeyLen, DataLen);
393}
394
395ASTSelectorLookupTrait::internal_key_type
396ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
397 using namespace clang::io;
Douglas Gregor4163aca2011-09-09 21:34:22 +0000398 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregord44252e2011-08-25 20:47:51 +0000399 unsigned N = ReadUnalignedLE16(d);
400 IdentifierInfo *FirstII
401 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
402 if (N == 0)
403 return SelTable.getNullarySelector(FirstII);
404 else if (N == 1)
405 return SelTable.getUnarySelector(FirstII);
406
407 SmallVector<IdentifierInfo *, 16> Args;
408 Args.push_back(FirstII);
409 for (unsigned I = 1; I != N; ++I)
410 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
411
412 return SelTable.getSelector(N, Args.data());
413}
414
415ASTSelectorLookupTrait::data_type
416ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
417 unsigned DataLen) {
418 using namespace clang::io;
419
420 data_type Result;
421
422 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
423 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
424 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
425
426 // Load instance methods
Douglas Gregord44252e2011-08-25 20:47:51 +0000427 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
428 if (ObjCMethodDecl *Method
429 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
430 Result.Instance.push_back(Method);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000431 }
Mike Stump11289f42009-09-09 15:08:12 +0000432
Douglas Gregord44252e2011-08-25 20:47:51 +0000433 // Load factory methods
Douglas Gregord44252e2011-08-25 20:47:51 +0000434 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
435 if (ObjCMethodDecl *Method
436 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
437 Result.Factory.push_back(Method);
Douglas Gregorc78d3462009-04-24 21:10:55 +0000438 }
Mike Stump11289f42009-09-09 15:08:12 +0000439
Douglas Gregord44252e2011-08-25 20:47:51 +0000440 return Result;
441}
Mike Stump11289f42009-09-09 15:08:12 +0000442
Douglas Gregord44252e2011-08-25 20:47:51 +0000443unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
444 return llvm::HashString(StringRef(a.first, a.second));
445}
Mike Stump11289f42009-09-09 15:08:12 +0000446
Douglas Gregord44252e2011-08-25 20:47:51 +0000447std::pair<unsigned, unsigned>
448ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
449 using namespace clang::io;
450 unsigned DataLen = ReadUnalignedLE16(d);
451 unsigned KeyLen = ReadUnalignedLE16(d);
452 return std::make_pair(KeyLen, DataLen);
453}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000454
Douglas Gregord44252e2011-08-25 20:47:51 +0000455std::pair<const char*, unsigned>
456ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
457 assert(n >= 2 && d[n-1] == '\0');
458 return std::make_pair((const char*) d, n-1);
459}
Douglas Gregorc78d3462009-04-24 21:10:55 +0000460
Douglas Gregord44252e2011-08-25 20:47:51 +0000461IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
462 const unsigned char* d,
463 unsigned DataLen) {
464 using namespace clang::io;
465 unsigned RawID = ReadUnalignedLE32(d);
466 bool IsInteresting = RawID & 0x01;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Douglas Gregord44252e2011-08-25 20:47:51 +0000468 // Wipe out the "is interesting" bit.
469 RawID = RawID >> 1;
Douglas Gregorc78d3462009-04-24 21:10:55 +0000470
Douglas Gregord44252e2011-08-25 20:47:51 +0000471 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
472 if (!IsInteresting) {
473 // For uninteresting identifiers, just build the IdentifierInfo
474 // and associate it with the persistent ID.
Douglas Gregora868bbd2009-04-21 22:25:48 +0000475 IdentifierInfo *II = KnownII;
Douglas Gregor247afcc2012-01-24 15:24:38 +0000476 if (!II) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +0000477 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor247afcc2012-01-24 15:24:38 +0000478 KnownII = II;
479 }
Douglas Gregora868bbd2009-04-21 22:25:48 +0000480 Reader.SetIdentifierInfo(ID, II);
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000481 II->setIsFromAST();
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +0000482 Reader.markIdentifierUpToDate(II);
Douglas Gregora868bbd2009-04-21 22:25:48 +0000483 return II;
484 }
Mike Stump11289f42009-09-09 15:08:12 +0000485
Alexander Kornienko1d26c022012-09-25 17:18:14 +0000486 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregord44252e2011-08-25 20:47:51 +0000487 unsigned Bits = ReadUnalignedLE16(d);
488 bool CPlusPlusOperatorKeyword = Bits & 0x01;
489 Bits >>= 1;
490 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
491 Bits >>= 1;
492 bool Poisoned = Bits & 0x01;
493 Bits >>= 1;
494 bool ExtensionToken = Bits & 0x01;
495 Bits >>= 1;
Alexander Kornienko1d26c022012-09-25 17:18:14 +0000496 bool hadMacroDefinition = Bits & 0x01;
497 Bits >>= 1;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000498
Douglas Gregord44252e2011-08-25 20:47:51 +0000499 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko1d26c022012-09-25 17:18:14 +0000500 DataLen -= 8;
Douglas Gregora868bbd2009-04-21 22:25:48 +0000501
Douglas Gregord44252e2011-08-25 20:47:51 +0000502 // Build the IdentifierInfo itself and link the identifier ID with
503 // the new IdentifierInfo.
504 IdentifierInfo *II = KnownII;
Douglas Gregor247afcc2012-01-24 15:24:38 +0000505 if (!II) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000506 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor247afcc2012-01-24 15:24:38 +0000507 KnownII = II;
508 }
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +0000509 Reader.markIdentifierUpToDate(II);
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000510 II->setIsFromAST();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000511
Douglas Gregord44252e2011-08-25 20:47:51 +0000512 // Set or check the various bits in the IdentifierInfo structure.
513 // Token IDs are read-only.
514 if (HasRevertedTokenIDToIdentifier)
515 II->RevertTokenIDToIdentifier();
516 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
517 assert(II->isExtensionToken() == ExtensionToken &&
518 "Incorrect extension token flag");
519 (void)ExtensionToken;
520 if (Poisoned)
521 II->setIsPoisoned(true);
522 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
523 "Incorrect C++ operator keyword flag");
524 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000525
Douglas Gregord44252e2011-08-25 20:47:51 +0000526 // If this identifier is a macro, deserialize the macro
527 // definition.
Alexander Kornienko1d26c022012-09-25 17:18:14 +0000528 if (hadMacroDefinition) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +0000529 SmallVector<MacroID, 4> MacroIDs;
530 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
531 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
532 DataLen -= 4;
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +0000533 }
Douglas Gregor5a4649b2012-10-11 00:46:49 +0000534 DataLen -= 4;
535 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000536 }
537
Douglas Gregor935bc7a22011-10-27 09:33:13 +0000538 Reader.SetIdentifierInfo(ID, II);
539
Douglas Gregord44252e2011-08-25 20:47:51 +0000540 // Read all of the declarations visible at global scope with this
541 // name.
Douglas Gregord44252e2011-08-25 20:47:51 +0000542 if (DataLen > 0) {
543 SmallVector<uint32_t, 4> DeclIDs;
544 for (; DataLen > 0; DataLen -= 4)
545 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
546 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000547 }
548
Douglas Gregord44252e2011-08-25 20:47:51 +0000549 return II;
550}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000551
Douglas Gregord44252e2011-08-25 20:47:51 +0000552unsigned
553ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
554 llvm::FoldingSetNodeID ID;
555 ID.AddInteger(Key.Kind);
556
557 switch (Key.Kind) {
558 case DeclarationName::Identifier:
559 case DeclarationName::CXXLiteralOperatorName:
560 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
561 break;
562 case DeclarationName::ObjCZeroArgSelector:
563 case DeclarationName::ObjCOneArgSelector:
564 case DeclarationName::ObjCMultiArgSelector:
565 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
566 break;
567 case DeclarationName::CXXOperatorName:
568 ID.AddInteger((OverloadedOperatorKind)Key.Data);
569 break;
570 case DeclarationName::CXXConstructorName:
571 case DeclarationName::CXXDestructorName:
572 case DeclarationName::CXXConversionFunctionName:
573 case DeclarationName::CXXUsingDirective:
574 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000575 }
576
Douglas Gregord44252e2011-08-25 20:47:51 +0000577 return ID.ComputeHash();
578}
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000579
Douglas Gregord44252e2011-08-25 20:47:51 +0000580ASTDeclContextNameLookupTrait::internal_key_type
581ASTDeclContextNameLookupTrait::GetInternalKey(
582 const external_key_type& Name) const {
583 DeclNameKey Key;
584 Key.Kind = Name.getNameKind();
585 switch (Name.getNameKind()) {
586 case DeclarationName::Identifier:
587 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
588 break;
589 case DeclarationName::ObjCZeroArgSelector:
590 case DeclarationName::ObjCOneArgSelector:
591 case DeclarationName::ObjCMultiArgSelector:
592 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
593 break;
594 case DeclarationName::CXXOperatorName:
595 Key.Data = Name.getCXXOverloadedOperator();
596 break;
597 case DeclarationName::CXXLiteralOperatorName:
598 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
599 break;
600 case DeclarationName::CXXConstructorName:
601 case DeclarationName::CXXDestructorName:
602 case DeclarationName::CXXConversionFunctionName:
603 case DeclarationName::CXXUsingDirective:
604 Key.Data = 0;
605 break;
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +0000606 }
607
Douglas Gregord44252e2011-08-25 20:47:51 +0000608 return Key;
609}
610
Douglas Gregord44252e2011-08-25 20:47:51 +0000611std::pair<unsigned, unsigned>
612ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
613 using namespace clang::io;
614 unsigned KeyLen = ReadUnalignedLE16(d);
615 unsigned DataLen = ReadUnalignedLE16(d);
616 return std::make_pair(KeyLen, DataLen);
617}
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000618
Douglas Gregord44252e2011-08-25 20:47:51 +0000619ASTDeclContextNameLookupTrait::internal_key_type
620ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
621 using namespace clang::io;
622
623 DeclNameKey Key;
624 Key.Kind = (DeclarationName::NameKind)*d++;
625 switch (Key.Kind) {
626 case DeclarationName::Identifier:
627 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
628 break;
629 case DeclarationName::ObjCZeroArgSelector:
630 case DeclarationName::ObjCOneArgSelector:
631 case DeclarationName::ObjCMultiArgSelector:
632 Key.Data =
633 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
634 .getAsOpaquePtr();
635 break;
636 case DeclarationName::CXXOperatorName:
637 Key.Data = *d++; // OverloadedOperatorKind
638 break;
639 case DeclarationName::CXXLiteralOperatorName:
640 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
641 break;
642 case DeclarationName::CXXConstructorName:
643 case DeclarationName::CXXDestructorName:
644 case DeclarationName::CXXConversionFunctionName:
645 case DeclarationName::CXXUsingDirective:
646 Key.Data = 0;
647 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000648 }
649
Douglas Gregord44252e2011-08-25 20:47:51 +0000650 return Key;
651}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000652
Douglas Gregord44252e2011-08-25 20:47:51 +0000653ASTDeclContextNameLookupTrait::data_type
654ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
655 const unsigned char* d,
Nick Lewycky2bd0ab22012-04-16 02:51:46 +0000656 unsigned DataLen) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000657 using namespace clang::io;
658 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregorde95ead2012-01-06 16:09:53 +0000659 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregord44252e2011-08-25 20:47:51 +0000660 return std::make_pair(Start, Start + NumDecls);
661}
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +0000662
Douglas Gregorde3ef502011-11-30 23:21:26 +0000663bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor94619c82011-08-24 19:03:07 +0000664 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000665 const std::pair<uint64_t, uint64_t> &Offsets,
666 DeclContextInfo &Info) {
667 SavedStreamPosition SavedPosition(Cursor);
668 // First the lexical decls.
669 if (Offsets.first != 0) {
670 Cursor.JumpToBit(Offsets.first);
671
672 RecordData Record;
673 const char *Blob;
674 unsigned BlobLen;
675 unsigned Code = Cursor.ReadCode();
676 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
677 if (RecCode != DECL_CONTEXT_LEXICAL) {
678 Error("Expected lexical block");
679 return true;
680 }
681
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +0000682 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
683 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000684 }
685
686 // Now the lookup table.
687 if (Offsets.second != 0) {
688 Cursor.JumpToBit(Offsets.second);
689
690 RecordData Record;
691 const char *Blob;
692 unsigned BlobLen;
693 unsigned Code = Cursor.ReadCode();
694 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
695 if (RecCode != DECL_CONTEXT_VISIBLE) {
696 Error("Expected visible lookup table block");
697 return true;
698 }
699 Info.NameLookupTableData
700 = ASTDeclContextNameLookupTable::Create(
701 (const unsigned char *)Blob + Record[0],
702 (const unsigned char *)Blob,
Douglas Gregor94619c82011-08-24 19:03:07 +0000703 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +0000704 }
705
706 return false;
707}
708
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000709void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000710 Error(diag::err_fe_pch_malformed, Msg);
711}
712
713void ASTReader::Error(unsigned DiagID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000714 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidisdaa41f52011-04-25 22:23:56 +0000715 if (Diags.isDiagnosticInFlight())
716 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
717 else
718 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000719}
720
Douglas Gregorc5046832009-04-27 18:38:38 +0000721//===----------------------------------------------------------------------===//
722// Source Manager Deserialization
723//===----------------------------------------------------------------------===//
724
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000725/// \brief Read the line table in the source manager block.
Sebastian Redl2c373b92010-10-05 15:59:54 +0000726/// \returns true if there was an error.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000727bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000728 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000729 unsigned Idx = 0;
730 LineTableInfo &LineTable = SourceMgr.getLineTable();
731
732 // Parse the file names
Douglas Gregora8854652009-04-13 17:12:42 +0000733 std::map<int, int> FileIDs;
734 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000735 // Extract the file name
736 unsigned FilenameLen = Record[Idx++];
737 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
738 Idx += FilenameLen;
Douglas Gregor6bdae4b2012-10-18 21:31:35 +0000739 MaybeAddSystemRootToFilename(F, Filename);
Jay Foad9a6b0982011-06-21 15:13:30 +0000740 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000741 }
742
743 // Parse the line entries
744 std::vector<LineEntry> Entries;
745 while (Idx < Record.size()) {
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000746 int FID = Record[Idx++];
Douglas Gregor925296b2011-07-19 16:10:42 +0000747 assert(FID >= 0 && "Serialized line entries for non-local file.");
748 // Remap FileID from 1-based old view.
749 FID += F.SLocEntryBaseID - 1;
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000750
751 // Extract the line entries
752 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000753 assert(NumEntries && "Numentries is 00000");
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000754 Entries.clear();
755 Entries.reserve(NumEntries);
756 for (unsigned I = 0; I != NumEntries; ++I) {
757 unsigned FileOffset = Record[Idx++];
758 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidise3029a72010-07-02 11:55:05 +0000759 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump11289f42009-09-09 15:08:12 +0000760 SrcMgr::CharacteristicKind FileKind
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000761 = (SrcMgr::CharacteristicKind)Record[Idx++];
762 unsigned IncludeOffset = Record[Idx++];
763 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
764 FileKind, IncludeOffset));
765 }
Douglas Gregor02c2dbf2012-06-08 16:40:28 +0000766 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregor4c7626e2009-04-13 16:31:14 +0000767 }
768
769 return false;
770}
771
Sebastian Redl393f8b72010-07-19 20:52:06 +0000772/// \brief Read a source manager block
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000773bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000774 using namespace SrcMgr;
Douglas Gregor258ae542009-04-27 06:38:32 +0000775
Sebastian Redl393f8b72010-07-19 20:52:06 +0000776 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl34522812010-07-16 17:50:48 +0000777
Douglas Gregor258ae542009-04-27 06:38:32 +0000778 // Set the source-location entry cursor to the current position in
779 // the stream. This cursor will be used to read the contents of the
780 // source manager block initially, and then lazily read
781 // source-location entries as needed.
Sebastian Redl393f8b72010-07-19 20:52:06 +0000782 SLocEntryCursor = F.Stream;
Douglas Gregor258ae542009-04-27 06:38:32 +0000783
784 // The stream itself is going to skip over the source manager block.
Sebastian Redl393f8b72010-07-19 20:52:06 +0000785 if (F.Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000786 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000787 return true;
Douglas Gregor258ae542009-04-27 06:38:32 +0000788 }
789
790 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +0000791 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000792 Error("malformed source manager block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000793 return true;
Douglas Gregor92863e42009-04-10 23:10:45 +0000794 }
Douglas Gregora7f71a92009-04-10 03:52:48 +0000795
Douglas Gregora7f71a92009-04-10 03:52:48 +0000796 RecordData Record;
797 while (true) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000798 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregora7f71a92009-04-10 03:52:48 +0000799 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000800 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000801 Error("error at end of Source Manager block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000802 return true;
Douglas Gregor92863e42009-04-10 23:10:45 +0000803 }
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000804 return false;
Douglas Gregora7f71a92009-04-10 03:52:48 +0000805 }
Mike Stump11289f42009-09-09 15:08:12 +0000806
Douglas Gregora7f71a92009-04-10 03:52:48 +0000807 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
808 // No known subblocks, always skip them.
Douglas Gregor258ae542009-04-27 06:38:32 +0000809 SLocEntryCursor.ReadSubBlockID();
810 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000811 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000812 return true;
Douglas Gregor92863e42009-04-10 23:10:45 +0000813 }
Douglas Gregora7f71a92009-04-10 03:52:48 +0000814 continue;
815 }
Mike Stump11289f42009-09-09 15:08:12 +0000816
Douglas Gregora7f71a92009-04-10 03:52:48 +0000817 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000818 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregora7f71a92009-04-10 03:52:48 +0000819 continue;
820 }
Mike Stump11289f42009-09-09 15:08:12 +0000821
Douglas Gregora7f71a92009-04-10 03:52:48 +0000822 // Read a record.
823 const char *BlobStart;
824 unsigned BlobLen;
825 Record.clear();
Douglas Gregor258ae542009-04-27 06:38:32 +0000826 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregora7f71a92009-04-10 03:52:48 +0000827 default: // Default behavior: ignore.
828 break;
829
Sebastian Redl539c5062010-08-18 23:57:32 +0000830 case SM_SLOC_FILE_ENTRY:
831 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000832 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor258ae542009-04-27 06:38:32 +0000833 // Once we hit one of the source location entries, we're done.
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000834 return false;
Douglas Gregora7f71a92009-04-10 03:52:48 +0000835 }
836 }
837}
838
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000839/// \brief If a header file is not found at the path that we expect it to be
840/// and the PCH file was moved from its original location, try to resolve the
841/// file by assuming that header+PCH were moved together and the header is in
842/// the same place relative to the PCH.
843static std::string
844resolveFileRelativeToOriginalDir(const std::string &Filename,
845 const std::string &OriginalDir,
846 const std::string &CurrDir) {
847 assert(OriginalDir != CurrDir &&
848 "No point trying to resolve the file if the PCH dir didn't change");
849 using namespace llvm::sys;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000850 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000851 fs::make_absolute(filePath);
852 assert(path::is_absolute(OriginalDir));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000853 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000854
855 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
856 fileDirE = path::end(path::parent_path(filePath));
857 path::const_iterator origDirI = path::begin(OriginalDir),
858 origDirE = path::end(OriginalDir);
859 // Skip the common path components from filePath and OriginalDir.
860 while (fileDirI != fileDirE && origDirI != origDirE &&
861 *fileDirI == *origDirI) {
862 ++fileDirI;
863 ++origDirI;
864 }
865 for (; origDirI != origDirE; ++origDirI)
866 path::append(currPCHPath, "..");
867 path::append(currPCHPath, fileDirI, fileDirE);
868 path::append(currPCHPath, path::filename(Filename));
869 return currPCHPath.str();
870}
871
Douglas Gregor4750b772012-10-22 22:53:10 +0000872bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor258ae542009-04-27 06:38:32 +0000873 if (ID == 0)
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000874 return false;
Douglas Gregor258ae542009-04-27 06:38:32 +0000875
Douglas Gregor49bf76b2011-07-21 18:46:38 +0000876 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000877 Error("source location entry ID out-of-range for AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000878 return true;
Douglas Gregor258ae542009-04-27 06:38:32 +0000879 }
880
Douglas Gregorde3ef502011-11-30 23:21:26 +0000881 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregor925296b2011-07-19 16:10:42 +0000882 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redl2c373b92010-10-05 15:59:54 +0000883 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregor925296b2011-07-19 16:10:42 +0000884 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl34522812010-07-16 17:50:48 +0000885
Douglas Gregor258ae542009-04-27 06:38:32 +0000886 ++NumSLocEntriesRead;
Douglas Gregor258ae542009-04-27 06:38:32 +0000887 unsigned Code = SLocEntryCursor.ReadCode();
888 if (Code == llvm::bitc::END_BLOCK ||
889 Code == llvm::bitc::ENTER_SUBBLOCK ||
890 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000891 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000892 return true;
Douglas Gregor258ae542009-04-27 06:38:32 +0000893 }
894
Douglas Gregor258ae542009-04-27 06:38:32 +0000895 RecordData Record;
896 const char *BlobStart;
897 unsigned BlobLen;
898 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
899 default:
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000900 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000901 return true;
Douglas Gregor258ae542009-04-27 06:38:32 +0000902
Sebastian Redl539c5062010-08-18 23:57:32 +0000903 case SM_SLOC_FILE_ENTRY: {
Argyrios Kyrtzidis969fdfd2012-02-20 23:58:07 +0000904 // We will detect whether a file changed and return 'Failure' for it, but
905 // we will also try to fail gracefully by setting up the SLocEntry.
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000906 unsigned InputID = Record[4];
907 InputFile IF = getInputFile(*F, InputID);
908 const FileEntry *File = IF.getPointer();
909 bool OverriddenBuffer = IF.getInt();
Argyrios Kyrtzidis969fdfd2012-02-20 23:58:07 +0000910
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000911 if (!IF.getPointer())
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000912 return true;
Douglas Gregorb41ca8f2010-03-21 22:49:54 +0000913
Douglas Gregor925296b2011-07-19 16:10:42 +0000914 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregora6895d82011-07-22 16:00:58 +0000915 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregor925296b2011-07-19 16:10:42 +0000916 // This is the module's main file.
917 IncludeLoc = getImportLocation(F);
918 }
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000919 SrcMgr::CharacteristicKind
920 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
921 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregor925296b2011-07-19 16:10:42 +0000922 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +0000923 SrcMgr::FileInfo &FileInfo =
924 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000925 FileInfo.NumCreatedFIDs = Record[5];
Douglas Gregor258ae542009-04-27 06:38:32 +0000926 if (Record[3])
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +0000927 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +0000928
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000929 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
930 unsigned NumFileDecls = Record[7];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +0000931 if (NumFileDecls) {
932 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis6c798be2011-10-31 07:20:08 +0000933 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
934 NumFileDecls));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +0000935 }
Douglas Gregor09b69892011-02-10 17:09:37 +0000936
Douglas Gregor66797172011-11-17 01:44:33 +0000937 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidis6d7833f2012-07-11 20:59:04 +0000938 = SourceMgr.getOrCreateContentCache(File,
939 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor66797172011-11-17 01:44:33 +0000940 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
941 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregor9dc32122011-11-16 20:05:18 +0000942 unsigned Code = SLocEntryCursor.ReadCode();
943 Record.clear();
944 unsigned RecCode
945 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
946
947 if (RecCode != SM_SLOC_BUFFER_BLOB) {
948 Error("AST record has invalid code");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000949 return true;
Douglas Gregor9dc32122011-11-16 20:05:18 +0000950 }
951
952 llvm::MemoryBuffer *Buffer
953 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000954 File->getName());
Douglas Gregor9dc32122011-11-16 20:05:18 +0000955 SourceMgr.overrideFileContents(File, Buffer);
956 }
Argyrios Kyrtzidis969fdfd2012-02-20 23:58:07 +0000957
Douglas Gregor258ae542009-04-27 06:38:32 +0000958 break;
959 }
960
Sebastian Redl539c5062010-08-18 23:57:32 +0000961 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor258ae542009-04-27 06:38:32 +0000962 const char *Name = BlobStart;
963 unsigned Offset = Record[0];
Argyrios Kyrtzidis6566e232012-11-09 19:40:45 +0000964 SrcMgr::CharacteristicKind
965 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
Argyrios Kyrtzidis2969e122012-11-06 00:35:04 +0000966 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor258ae542009-04-27 06:38:32 +0000967 unsigned Code = SLocEntryCursor.ReadCode();
968 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000969 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +0000970 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000971
Sebastian Redl539c5062010-08-18 23:57:32 +0000972 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000973 Error("AST record has invalid code");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000974 return true;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000975 }
976
Douglas Gregor258ae542009-04-27 06:38:32 +0000977 llvm::MemoryBuffer *Buffer
Douglas Gregor9dc32122011-11-16 20:05:18 +0000978 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
979 Name);
Argyrios Kyrtzidis6566e232012-11-09 19:40:45 +0000980 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
981 BaseOffset + Offset, IncludeLoc);
Douglas Gregor258ae542009-04-27 06:38:32 +0000982 break;
983 }
984
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000985 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +0000986 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruth115b0772011-07-26 03:03:05 +0000987 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +0000988 ReadSourceLocation(*F, Record[2]),
989 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +0000990 Record[4],
991 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +0000992 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +0000993 break;
Mike Stump11289f42009-09-09 15:08:12 +0000994 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000995 }
996
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000997 return false;
Douglas Gregor258ae542009-04-27 06:38:32 +0000998}
999
Douglas Gregor925296b2011-07-19 16:10:42 +00001000/// \brief Find the location where the module F is imported.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001001SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001002 if (F->ImportLoc.isValid())
1003 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001004
Douglas Gregor925296b2011-07-19 16:10:42 +00001005 // Otherwise we have a PCH. It's considered to be "imported" at the first
1006 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001007 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001008 // Main file is the importer. We assume that it is the first entry in the
1009 // entry table. We can't ask the manager, because at the time of PCH loading
1010 // the main file entry doesn't exist yet.
1011 // The very first entry is the invalid instantiation loc, which takes up
1012 // offsets 0 and 1.
1013 return SourceLocation::getFromRawEncoding(2U);
1014 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001015 //return F->Loaders[0]->FirstLoc;
1016 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001017}
1018
Chris Lattnere78a6be2009-04-27 01:05:14 +00001019/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1020/// specified cursor. Read the abbreviations that are at the top of the block
1021/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001022bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001023 unsigned BlockID) {
1024 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001025 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001026 return Failure;
1027 }
Mike Stump11289f42009-09-09 15:08:12 +00001028
Chris Lattnere78a6be2009-04-27 01:05:14 +00001029 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001030 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001031 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001032
Chris Lattnere78a6be2009-04-27 01:05:14 +00001033 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001034 if (Code != llvm::bitc::DEFINE_ABBREV) {
1035 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001036 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001037 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001038 Cursor.ReadAbbrevRecord();
1039 }
1040}
1041
Douglas Gregore7400892012-10-11 17:41:54 +00001042void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1043 MacroInfo *Hint) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001044 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001045
Douglas Gregorc3366a52009-04-21 23:56:24 +00001046 // Keep track of where we are in the stream, then jump back there
1047 // after reading this macro.
1048 SavedStreamPosition SavedPosition(Stream);
1049
1050 Stream.JumpToBit(Offset);
1051 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001052 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001053 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001054
Douglas Gregor5968b1b2012-10-11 21:07:39 +00001055 // RAII object to add the loaded macro information once we're done
1056 // adding tokens.
1057 struct AddLoadedMacroInfoRAII {
1058 Preprocessor &PP;
1059 MacroInfo *Hint;
1060 MacroInfo *MI;
1061 IdentifierInfo *II;
1062
1063 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1064 : PP(PP), Hint(Hint), MI(), II() { }
1065 ~AddLoadedMacroInfoRAII( ) {
1066 if (MI) {
1067 // Finally, install the macro.
1068 PP.addLoadedMacroInfo(II, MI, Hint);
1069 }
1070 }
1071 } AddLoadedMacroInfo(PP, Hint);
1072
Douglas Gregorc3366a52009-04-21 23:56:24 +00001073 while (true) {
1074 unsigned Code = Stream.ReadCode();
1075 switch (Code) {
1076 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001077 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001078
1079 case llvm::bitc::ENTER_SUBBLOCK:
1080 // No known subblocks, always skip them.
1081 Stream.ReadSubBlockID();
1082 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001083 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001084 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001085 }
1086 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001087
Douglas Gregorc3366a52009-04-21 23:56:24 +00001088 case llvm::bitc::DEFINE_ABBREV:
1089 Stream.ReadAbbrevRecord();
1090 continue;
1091 default: break;
1092 }
1093
1094 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001095 const char *BlobStart = 0;
1096 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001097 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001098 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001099 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001100 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001101 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001102 case PP_MACRO_OBJECT_LIKE:
1103 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001104 // If we already have a macro, that means that we've hit the end
1105 // of the definition of the macro we were looking for. We're
1106 // done.
1107 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001108 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001109
Douglas Gregora3e41532011-07-28 20:55:49 +00001110 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001111 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001112 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001113 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001116 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1117
1118 // If this macro has already been loaded, don't do so again.
1119 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1120 return;
1121
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001122 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1123 unsigned NextIndex = 3;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001124 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor51825b42011-09-09 22:02:16 +00001125 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001126
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001127 // Record this macro.
1128 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1129
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001130 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1131 if (UndefLoc.isValid())
1132 MI->setUndefLoc(UndefLoc);
1133
1134 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001135 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001136
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001137 bool IsPublic = Record[NextIndex++];
Douglas Gregorebf00492011-10-17 15:32:29 +00001138 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001139
Sebastian Redl539c5062010-08-18 23:57:32 +00001140 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001141 // Decode function-like macro info.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001142 bool isC99VarArgs = Record[NextIndex++];
1143 bool isGNUVarArgs = Record[NextIndex++];
Eli Friedman14d3c792012-11-14 02:18:46 +00001144 bool hasCommaPasting = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001145 MacroArgs.clear();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001146 unsigned NumArgs = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001147 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001148 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001149
1150 // Install function-like macro info.
1151 MI->setIsFunctionLike();
1152 if (isC99VarArgs) MI->setIsC99Varargs();
1153 if (isGNUVarArgs) MI->setIsGNUVarargs();
Eli Friedman14d3c792012-11-14 02:18:46 +00001154 if (hasCommaPasting) MI->setHasCommaPasting();
Douglas Gregor038c3382009-05-22 22:45:36 +00001155 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor51825b42011-09-09 22:02:16 +00001156 PP.getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001157 }
1158
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001159 if (DeserializationListener)
1160 DeserializationListener->MacroRead(GlobalID, MI);
1161
1162 // If an update record marked this as undefined, do so now.
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001163 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001164 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1165 if (Update != MacroUpdates.end()) {
1166 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregorcfa46a82012-10-12 00:16:50 +00001167 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1168 bool Hidden = false;
1169 if (unsigned SubmoduleID = Update->second[I].first) {
1170 if (Module *Owner = getSubmodule(SubmoduleID)) {
1171 if (Owner->NameVisibility == Module::Hidden) {
1172 // Note that this #undef is hidden.
1173 Hidden = true;
1174
1175 // Record this hiding for later.
1176 HiddenNamesMap[Owner].push_back(
1177 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1178 }
1179 }
1180 }
1181
1182 if (!Hidden) {
1183 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1184 if (PPMutationListener *Listener = PP.getPPMutationListener())
1185 Listener->UndefinedMacro(MI);
1186 break;
1187 }
1188 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001189 }
1190 MacroUpdates.erase(Update);
1191 }
1192
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001193 // Determine whether this macro definition is visible.
1194 bool Hidden = !MI->isPublic();
1195 if (!Hidden && GlobalSubmoduleID) {
1196 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1197 if (Owner->NameVisibility == Module::Hidden) {
1198 // The owning module is not visible, and this macro definition
1199 // should not be, either.
1200 Hidden = true;
1201
1202 // Note that this macro definition was hidden because its owning
1203 // module is not yet visible.
1204 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1205 }
1206 }
1207 }
1208 MI->setHidden(Hidden);
1209
Douglas Gregor5968b1b2012-10-11 21:07:39 +00001210 // Make sure we install the macro once we're done.
1211 AddLoadedMacroInfo.MI = MI;
1212 AddLoadedMacroInfo.II = II;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001213
1214 // Remember that we saw this macro last so that we add the tokens that
1215 // form its body to it.
1216 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001217
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001218 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1219 Record[NextIndex]) {
1220 // We have a macro definition. Register the association
1221 PreprocessedEntityID
1222 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1223 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1224 PPRec.RegisterMacroDefinition(Macro,
1225 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregoraae92242010-03-19 21:51:54 +00001226 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001227
Douglas Gregorc3366a52009-04-21 23:56:24 +00001228 ++NumMacrosRead;
1229 break;
1230 }
Mike Stump11289f42009-09-09 15:08:12 +00001231
Sebastian Redl539c5062010-08-18 23:57:32 +00001232 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001233 // If we see a TOKEN before a PP_MACRO_*, then the file is
1234 // erroneous, just pretend we didn't see this.
1235 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001236
Douglas Gregorc3366a52009-04-21 23:56:24 +00001237 Token Tok;
1238 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001239 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001240 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001241 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001242 Tok.setIdentifierInfo(II);
1243 Tok.setKind((tok::TokenKind)Record[3]);
1244 Tok.setFlag((Token::TokenFlags)Record[4]);
1245 Macro->AddTokenToBody(Tok);
1246 break;
1247 }
David Blaikie8a40f702012-01-17 06:56:22 +00001248 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001249 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001250}
1251
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001252PreprocessedEntityID
Douglas Gregorde3ef502011-11-30 23:21:26 +00001253ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidisd67164e2011-09-19 20:40:02 +00001254 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001255 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1256 assert(I != M.PreprocessedEntityRemap.end()
1257 && "Invalid index into preprocessed entity index remap");
1258
1259 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001260}
1261
Douglas Gregord44252e2011-08-25 20:47:51 +00001262unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1263 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregor09b69892011-02-10 17:09:37 +00001264}
Douglas Gregord44252e2011-08-25 20:47:51 +00001265
1266HeaderFileInfoTrait::internal_key_type
1267HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1268
1269bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1270 if (strcmp(a, b) == 0)
1271 return true;
1272
1273 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1274 return false;
Douglas Gregore32e0542011-12-09 16:22:07 +00001275
1276 // Determine whether the actual files are equivalent.
1277 bool Result = false;
1278 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregord44252e2011-08-25 20:47:51 +00001279 return false;
1280
Douglas Gregore32e0542011-12-09 16:22:07 +00001281 return Result;
Douglas Gregord44252e2011-08-25 20:47:51 +00001282}
1283
1284std::pair<unsigned, unsigned>
1285HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1286 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1287 unsigned DataLen = (unsigned) *d++;
1288 return std::make_pair(KeyLen + 1, DataLen);
1289}
1290
1291HeaderFileInfoTrait::data_type
1292HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1293 unsigned DataLen) {
1294 const unsigned char *End = d + DataLen;
1295 using namespace clang::io;
1296 HeaderFileInfo HFI;
1297 unsigned Flags = *d++;
1298 HFI.isImport = (Flags >> 5) & 0x01;
1299 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1300 HFI.DirInfo = (Flags >> 2) & 0x03;
1301 HFI.Resolved = (Flags >> 1) & 0x01;
1302 HFI.IndexHeaderMapHeader = Flags & 0x01;
1303 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor7d75bf62011-10-17 18:53:12 +00001304 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1305 ReadUnalignedLE32(d));
Douglas Gregord44252e2011-08-25 20:47:51 +00001306 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1307 // The framework offset is 1 greater than the actual offset,
1308 // since 0 is used as an indicator for "no framework name".
1309 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1310 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1311 }
1312
1313 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1314 (void)End;
1315
1316 // This HeaderFileInfo was externally loaded.
1317 HFI.External = true;
1318 return HFI;
1319}
Douglas Gregor09b69892011-02-10 17:09:37 +00001320
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001321void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1322 II->setHadMacroDefinition(true);
1323 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1324 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001325}
1326
Sebastian Redl2c499f62010-08-18 23:56:43 +00001327void ASTReader::ReadDefinedMacros() {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001328 // Note that we are loading defined macros.
1329 Deserializing Macros(this);
1330
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001331 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1332 E = ModuleMgr.rend(); I != E; ++I) {
1333 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001334
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001335 // If there was no preprocessor block, skip this file.
1336 if (!MacroCursor.getBitStreamReader())
1337 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001338
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001339 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001340 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001341
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001342 RecordData Record;
1343 while (true) {
1344 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001345 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001346 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001347
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001348 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1349 // No known subblocks, always skip them.
1350 Cursor.ReadSubBlockID();
1351 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001352 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001353 return;
1354 }
1355 continue;
1356 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001357
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001358 if (Code == llvm::bitc::DEFINE_ABBREV) {
1359 Cursor.ReadAbbrevRecord();
1360 continue;
1361 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001362
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001363 // Read a record.
1364 const char *BlobStart;
1365 unsigned BlobLen;
1366 Record.clear();
1367 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1368 default: // Default behavior: ignore.
1369 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001370
Sebastian Redl539c5062010-08-18 23:57:32 +00001371 case PP_MACRO_OBJECT_LIKE:
1372 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001373 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001374 break;
1375
Sebastian Redl539c5062010-08-18 23:57:32 +00001376 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001377 // Ignore tokens.
1378 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001379 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001380 }
1381 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001382}
1383
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001384namespace {
1385 /// \brief Visitor class used to look up identifirs in an AST file.
1386 class IdentifierLookupVisitor {
1387 StringRef Name;
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001388 unsigned PriorGeneration;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001389 IdentifierInfo *Found;
1390 public:
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001391 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1392 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001393
Douglas Gregorde3ef502011-11-30 23:21:26 +00001394 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001395 IdentifierLookupVisitor *This
1396 = static_cast<IdentifierLookupVisitor *>(UserData);
1397
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001398 // If we've already searched this module file, skip it now.
1399 if (M.Generation <= This->PriorGeneration)
1400 return true;
1401
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001402 ASTIdentifierLookupTable *IdTable
1403 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1404 if (!IdTable)
1405 return false;
1406
Douglas Gregor247afcc2012-01-24 15:24:38 +00001407 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1408 M, This->Found);
1409
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001410 std::pair<const char*, unsigned> Key(This->Name.begin(),
1411 This->Name.size());
Douglas Gregor247afcc2012-01-24 15:24:38 +00001412 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001413 if (Pos == IdTable->end())
1414 return false;
1415
1416 // Dereferencing the iterator has the effect of building the
1417 // IdentifierInfo node and populating it with the various
1418 // declarations it needs.
1419 This->Found = *Pos;
1420 return true;
1421 }
1422
1423 // \brief Retrieve the identifier info found within the module
1424 // files.
1425 IdentifierInfo *getIdentifierInfo() const { return Found; }
1426 };
1427}
1428
1429void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001430 // Note that we are loading an identifier.
1431 Deserializing AnIdentifier(this);
1432
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001433 unsigned PriorGeneration = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001434 if (getContext().getLangOpts().Modules)
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001435 PriorGeneration = IdentifierGeneration[&II];
1436
1437 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1438 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1439 markIdentifierUpToDate(&II);
1440}
1441
1442void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1443 if (!II)
1444 return;
1445
1446 II->setOutOfDate(false);
1447
1448 // Update the generation for this identifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001449 if (getContext().getLangOpts().Modules)
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001450 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001451}
1452
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001453llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor4b29c162012-10-22 23:51:00 +00001454ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001455 // If this ID is bogus, just return an empty input file.
1456 if (ID == 0 || ID > F.InputFilesLoaded.size())
1457 return InputFile();
1458
1459 // If we've already loaded this input file, return it.
1460 if (F.InputFilesLoaded[ID-1].getPointer())
1461 return F.InputFilesLoaded[ID-1];
1462
1463 // Go find this input file.
1464 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1465 SavedStreamPosition SavedPosition(Cursor);
1466 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1467
1468 unsigned Code = Cursor.ReadCode();
1469 RecordData Record;
1470 const char *BlobStart = 0;
1471 unsigned BlobLen = 0;
1472 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1473 &BlobStart, &BlobLen)) {
1474 case INPUT_FILE: {
1475 unsigned StoredID = Record[0];
1476 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi395a5742012-10-22 21:50:39 +00001477 (void)StoredID;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001478 off_t StoredSize = (off_t)Record[1];
1479 time_t StoredTime = (time_t)Record[2];
1480 bool Overridden = (bool)Record[3];
1481
1482 // Get the file entry for this input file.
1483 StringRef OrigFilename(BlobStart, BlobLen);
1484 std::string Filename = OrigFilename;
1485 MaybeAddSystemRootToFilename(F, Filename);
1486 const FileEntry *File
1487 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1488 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1489
1490 // If we didn't find the file, resolve it relative to the
1491 // original directory from which this AST file was created.
1492 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1493 F.OriginalDir != CurrentDir) {
1494 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1495 F.OriginalDir,
1496 CurrentDir);
1497 if (!Resolved.empty())
1498 File = FileMgr.getFile(Resolved);
1499 }
1500
1501 // For an overridden file, create a virtual file with the stored
1502 // size/timestamp.
1503 if (Overridden && File == 0) {
1504 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1505 }
1506
1507 if (File == 0) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001508 if (Complain) {
1509 std::string ErrorStr = "could not find file '";
1510 ErrorStr += Filename;
1511 ErrorStr += "' referenced by AST file";
1512 Error(ErrorStr.c_str());
1513 }
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001514 return InputFile();
1515 }
1516
1517 // Note that we've loaded this input file.
1518 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1519
1520 // Check if there was a request to override the contents of the file
1521 // that was part of the precompiled header. Overridding such a file
1522 // can lead to problems when lexing using the source locations from the
1523 // PCH.
1524 SourceManager &SM = getSourceManager();
1525 if (!Overridden && SM.isFileOverridden(File)) {
1526 Error(diag::err_fe_pch_file_overridden, Filename);
1527 // After emitting the diagnostic, recover by disabling the override so
1528 // that the original file will be used.
1529 SM.disableFileContentsOverride(File);
1530 // The FileEntry is a virtual file entry with the size of the contents
1531 // that would override the original contents. Set it to the original's
1532 // size/time.
1533 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1534 StoredSize, StoredTime);
1535 }
1536
1537 // For an overridden file, there is nothing to validate.
1538 if (Overridden)
1539 return InputFile(File, Overridden);
1540
1541 // The stat info from the FileEntry came from the cached stat
1542 // info of the PCH, so we cannot trust it.
1543 struct stat StatBuf;
1544 if (::stat(File->getName(), &StatBuf) != 0) {
1545 StatBuf.st_size = File->getSize();
1546 StatBuf.st_mtime = File->getModificationTime();
1547 }
1548
1549 if ((StoredSize != StatBuf.st_size
1550#if !defined(LLVM_ON_WIN32)
1551 // In our regression testing, the Windows file system seems to
1552 // have inconsistent modification times that sometimes
1553 // erroneously trigger this error-handling path.
1554 || StoredTime != StatBuf.st_mtime
1555#endif
1556 )) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001557 if (Complain)
1558 Error(diag::err_fe_pch_file_modified, Filename);
1559
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001560 return InputFile();
1561 }
1562
1563 return InputFile(File, Overridden);
1564 }
1565 }
1566
1567 return InputFile();
1568}
1569
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001570const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor451dffa2012-10-18 21:47:16 +00001571 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001572 std::string Filename = filenameStrRef;
Douglas Gregor451dffa2012-10-18 21:47:16 +00001573 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001574 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor451dffa2012-10-18 21:47:16 +00001575 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1576 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001577 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor451dffa2012-10-18 21:47:16 +00001578 M.OriginalDir,
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001579 CurrentDir);
1580 if (!resolved.empty())
1581 File = FileMgr.getFile(resolved);
1582 }
1583
1584 return File;
1585}
1586
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001587/// \brief If we are loading a relocatable PCH file, and the filename is
1588/// not an absolute path, add the system root to the beginning of the file
1589/// name.
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001590void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1591 std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001592 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00001593 if (!M.RelocatablePCH)
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001594 return;
Mike Stump11289f42009-09-09 15:08:12 +00001595
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001596 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001597 return;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001598
Douglas Gregorc567ba22011-07-22 16:35:34 +00001599 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001600 // If no system root was given, default to '/'
1601 Filename.insert(Filename.begin(), '/');
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001602 return;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001603 }
Mike Stump11289f42009-09-09 15:08:12 +00001604
Douglas Gregorc567ba22011-07-22 16:35:34 +00001605 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001606 if (isysroot[Length - 1] != '/')
1607 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001608
Douglas Gregorc567ba22011-07-22 16:35:34 +00001609 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001610}
1611
Douglas Gregor4b29c162012-10-22 23:51:00 +00001612ASTReader::ASTReadResult
1613ASTReader::ReadControlBlock(ModuleFile &F,
1614 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
1615 unsigned ClientLoadCapabilities) {
Douglas Gregor112b9072012-10-18 05:31:06 +00001616 llvm::BitstreamCursor &Stream = F.Stream;
1617
1618 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1619 Error("malformed block record in AST file");
1620 return Failure;
1621 }
1622
1623 // Read all of the records and blocks in the control block.
1624 RecordData Record;
1625 while (!Stream.AtEndOfStream()) {
1626 unsigned Code = Stream.ReadCode();
1627 if (Code == llvm::bitc::END_BLOCK) {
1628 if (Stream.ReadBlockEnd()) {
1629 Error("error at end of control block in AST file");
1630 return Failure;
1631 }
1632
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001633 // Validate all of the input files.
1634 if (!DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001635 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001636 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor4b29c162012-10-22 23:51:00 +00001637 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001638 return OutOfDate;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001639 }
1640
Douglas Gregor112b9072012-10-18 05:31:06 +00001641 return Success;
1642 }
1643
1644 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001645 switch (Stream.ReadSubBlockID()) {
1646 case INPUT_FILES_BLOCK_ID:
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001647 F.InputFilesCursor = Stream;
1648 if (Stream.SkipBlock() || // Skip with the main cursor
1649 // Read the abbreviations
1650 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1651 Error("malformed block record in AST file");
Douglas Gregor72be3902012-10-19 00:38:02 +00001652 return Failure;
Douglas Gregor72be3902012-10-19 00:38:02 +00001653 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001654 continue;
Douglas Gregor72be3902012-10-19 00:38:02 +00001655
1656 default:
1657 if (!Stream.SkipBlock())
1658 continue;
1659 break;
1660 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001661
1662 Error("malformed block record in AST file");
1663 return Failure;
1664 }
1665
1666 if (Code == llvm::bitc::DEFINE_ABBREV) {
1667 Stream.ReadAbbrevRecord();
1668 continue;
1669 }
1670
1671 // Read and process a record.
1672 Record.clear();
1673 const char *BlobStart = 0;
1674 unsigned BlobLen = 0;
1675 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1676 &BlobStart, &BlobLen)) {
1677 case METADATA: {
1678 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001679 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1680 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1681 : diag::warn_pch_version_too_new);
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001682 return VersionMismatch;
Douglas Gregor112b9072012-10-18 05:31:06 +00001683 }
1684
1685 bool hasErrors = Record[5];
1686 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1687 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001688 return HadErrors;
Douglas Gregor112b9072012-10-18 05:31:06 +00001689 }
1690
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00001691 F.RelocatablePCH = Record[4];
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001692
1693 const std::string &CurBranch = getClangFullRepositoryVersion();
1694 StringRef ASTBranch(BlobStart, BlobLen);
1695 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001696 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1697 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001698 return VersionMismatch;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001699 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001700 break;
1701 }
1702
1703 case IMPORTS: {
1704 // Load each of the imported PCH files.
1705 unsigned Idx = 0, N = Record.size();
1706 while (Idx < N) {
1707 // Read information about the AST file.
1708 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1709 unsigned Length = Record[Idx++];
1710 SmallString<128> ImportedFile(Record.begin() + Idx,
1711 Record.begin() + Idx + Length);
1712 Idx += Length;
1713
1714 // Load the AST file.
Douglas Gregor4b29c162012-10-22 23:51:00 +00001715 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
1716 ClientLoadCapabilities)) {
Douglas Gregor112b9072012-10-18 05:31:06 +00001717 case Failure: return Failure;
1718 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001719 case OutOfDate: return OutOfDate;
1720 case VersionMismatch: return VersionMismatch;
1721 case ConfigurationMismatch: return ConfigurationMismatch;
1722 case HadErrors: return HadErrors;
Douglas Gregor112b9072012-10-18 05:31:06 +00001723 case Success: break;
1724 }
1725 }
1726 break;
1727 }
1728
Douglas Gregor4b29c162012-10-22 23:51:00 +00001729 case LANGUAGE_OPTIONS: {
1730 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1731 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00001732 ParseLanguageOptions(Record, Complain, *Listener) &&
1733 !DisableValidation)
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001734 return ConfigurationMismatch;
Douglas Gregor112b9072012-10-18 05:31:06 +00001735 break;
Douglas Gregor4b29c162012-10-22 23:51:00 +00001736 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001737
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001738 case TARGET_OPTIONS: {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00001739 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1740 if (Listener && &F == *ModuleMgr.begin() &&
1741 ParseTargetOptions(Record, Complain, *Listener) &&
1742 !DisableValidation)
1743 return ConfigurationMismatch;
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001744 break;
1745 }
1746
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001747 case DIAGNOSTIC_OPTIONS: {
1748 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1749 if (Listener && &F == *ModuleMgr.begin() &&
1750 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1751 !DisableValidation)
1752 return ConfigurationMismatch;
1753 break;
1754 }
Douglas Gregorc6317db2012-10-24 15:49:58 +00001755
1756 case FILE_SYSTEM_OPTIONS: {
1757 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1758 if (Listener && &F == *ModuleMgr.begin() &&
1759 ParseFileSystemOptions(Record, Complain, *Listener) &&
1760 !DisableValidation)
1761 return ConfigurationMismatch;
1762 break;
1763 }
1764
Douglas Gregor2d302362012-10-24 16:50:34 +00001765 case HEADER_SEARCH_OPTIONS: {
1766 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1767 if (Listener && &F == *ModuleMgr.begin() &&
1768 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1769 !DisableValidation)
1770 return ConfigurationMismatch;
1771 break;
1772 }
1773
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001774 case PREPROCESSOR_OPTIONS: {
1775 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1776 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor55358ed2012-10-25 00:07:54 +00001777 ParsePreprocessorOptions(Record, Complain, *Listener,
1778 SuggestedPredefines) &&
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001779 !DisableValidation)
1780 return ConfigurationMismatch;
1781 break;
1782 }
1783
Douglas Gregorfad10d82012-10-18 18:36:53 +00001784 case ORIGINAL_FILE:
Douglas Gregor451dffa2012-10-18 21:47:16 +00001785 F.OriginalSourceFileID = FileID::get(Record[0]);
1786 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
1787 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1788 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor112b9072012-10-18 05:31:06 +00001789 break;
1790
Douglas Gregor112b9072012-10-18 05:31:06 +00001791 case ORIGINAL_PCH_DIR:
Douglas Gregor451dffa2012-10-18 21:47:16 +00001792 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor112b9072012-10-18 05:31:06 +00001793 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00001794
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001795 case INPUT_FILE_OFFSETS:
1796 F.InputFileOffsets = (const uint32_t *)BlobStart;
1797 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor72be3902012-10-19 00:38:02 +00001798 break;
1799 }
Douglas Gregor72be3902012-10-19 00:38:02 +00001800 }
1801
1802 Error("premature end of bitstream in AST file");
1803 return Failure;
1804}
1805
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001806bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001807 llvm::BitstreamCursor &Stream = F.Stream;
1808
Sebastian Redl539c5062010-08-18 23:57:32 +00001809 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001810 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001811 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001812 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001813
Douglas Gregor112b9072012-10-18 05:31:06 +00001814 // Read all of the records and blocks for the AST file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001815 RecordData Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001816 while (!Stream.AtEndOfStream()) {
1817 unsigned Code = Stream.ReadCode();
1818 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001819 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001820 Error("error at end of module block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001821 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001822 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001823
Argyrios Kyrtzidis6fa16822012-09-21 01:30:00 +00001824 DeclContext *DC = Context.getTranslationUnitDecl();
1825 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1826 DC->setMustBuildLookupTable();
1827
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001828 return false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001829 }
1830
1831 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1832 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001833 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001834 // We lazily load the decls block, but we want to set up the
1835 // DeclsCursor cursor to point into it. Clone our current bitcode
1836 // cursor to it, enter the block and read the abbrevs in that block.
1837 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001838 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001839 if (Stream.SkipBlock() || // Skip with the main cursor.
1840 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001841 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001842 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001843 return true;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001844 }
1845 break;
Mike Stump11289f42009-09-09 15:08:12 +00001846
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001847 case DECL_UPDATES_BLOCK_ID:
1848 if (Stream.SkipBlock()) {
1849 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001850 return true;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001851 }
1852 break;
1853
Sebastian Redl539c5062010-08-18 23:57:32 +00001854 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001855 F.MacroCursor = Stream;
Douglas Gregor51825b42011-09-09 22:02:16 +00001856 if (!PP.getExternalSource())
1857 PP.setExternalSource(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001858
Douglas Gregor796d76a2010-10-20 22:00:55 +00001859 if (Stream.SkipBlock() ||
1860 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001861 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001862 return true;
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001863 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001864 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001865 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001866
Douglas Gregor92a96f52011-02-08 21:58:10 +00001867 case PREPROCESSOR_DETAIL_BLOCK_ID:
1868 F.PreprocessorDetailCursor = Stream;
1869 if (Stream.SkipBlock() ||
1870 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1871 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1872 Error("malformed preprocessor detail record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001873 return true;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001874 }
1875 F.PreprocessorDetailStartOffset
1876 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor51825b42011-09-09 22:02:16 +00001877
1878 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +00001879 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor51825b42011-09-09 22:02:16 +00001880 if (!PP.getPreprocessingRecord()->getExternalSource())
1881 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001882 break;
1883
Sebastian Redl539c5062010-08-18 23:57:32 +00001884 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001885 if (ReadSourceManagerBlock(F))
1886 return true;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001887 break;
Douglas Gregor69021972011-11-30 17:33:56 +00001888
1889 case SUBMODULE_BLOCK_ID:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001890 if (ReadSubmoduleBlock(F))
1891 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00001892 break;
1893
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001894 case COMMENTS_BLOCK_ID: {
1895 llvm::BitstreamCursor C = Stream;
1896 if (Stream.SkipBlock() ||
1897 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1898 Error("malformed comments block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001899 return true;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001900 }
1901 CommentsCursors.push_back(std::make_pair(C, &F));
1902 break;
1903 }
1904
Douglas Gregor69021972011-11-30 17:33:56 +00001905 default:
1906 if (!Stream.SkipBlock())
1907 break;
1908 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001909 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001910 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001911 continue;
1912 }
1913
1914 if (Code == llvm::bitc::DEFINE_ABBREV) {
1915 Stream.ReadAbbrevRecord();
1916 continue;
1917 }
1918
1919 // Read and process a record.
1920 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001921 const char *BlobStart = 0;
1922 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001923 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001924 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001925 default: // Default behavior: ignore.
1926 break;
1927
Douglas Gregor5204bde2011-08-02 16:26:37 +00001928 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001929 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001930 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001931 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001932 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001933 F.TypeOffsets = (const uint32_t *)BlobStart;
1934 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001935 unsigned LocalBaseTypeIndex = Record[1];
1936 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00001937
Douglas Gregor5204bde2011-08-02 16:26:37 +00001938 if (F.LocalNumTypes > 0) {
1939 // Introduce the global -> local mapping for types within this module.
1940 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1941
1942 // Introduce the local -> global mapping for types within this module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00001943 F.TypeRemap.insertOrReplace(
1944 std::make_pair(LocalBaseTypeIndex,
1945 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00001946
1947 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1948 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001949 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00001950 }
1951
Douglas Gregorf7180622011-08-03 15:48:04 +00001952 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001953 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001954 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001955 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001956 }
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00001957 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl9e687992010-07-19 22:06:55 +00001958 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00001959 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001960 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00001961
Douglas Gregorf7180622011-08-03 15:48:04 +00001962 if (F.LocalNumDecls > 0) {
1963 // Introduce the global -> local mapping for declarations within this
1964 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00001965 GlobalDeclMap.insert(
1966 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00001967
1968 // Introduce the local -> global mapping for declarations within this
1969 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00001970 F.DeclRemap.insertOrReplace(
1971 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregorf7180622011-08-03 15:48:04 +00001972
Douglas Gregor05f10352011-12-17 23:38:30 +00001973 // Introduce the global -> local mapping for declarations within this
1974 // module.
1975 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1976
Douglas Gregorf7180622011-08-03 15:48:04 +00001977 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1978 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001979 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00001980 }
1981
Sebastian Redl539c5062010-08-18 23:57:32 +00001982 case TU_UPDATE_LEXICAL: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00001983 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00001984 DeclContextInfo &Info = F.DeclContextInfos[TU];
1985 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1986 Info.NumLexicalDecls
1987 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor4163aca2011-09-09 21:34:22 +00001988 TU->setHasExternalLexicalStorage(true);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001989 break;
1990 }
1991
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001992 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00001993 unsigned Idx = 0;
1994 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00001995 ASTDeclContextNameLookupTable *Table =
1996 ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00001997 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00001998 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00001999 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor4163aca2011-09-09 21:34:22 +00002000 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2001 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002002 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregordab42432011-08-12 00:15:20 +00002003 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002004 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00002005 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002006 break;
2007 }
2008
Sebastian Redl539c5062010-08-18 23:57:32 +00002009 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002010 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002011 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002012 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002013 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002014 (const unsigned char *)F.IdentifierTableData + Record[0],
2015 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002016 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor51825b42011-09-09 22:02:16 +00002017
2018 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002019 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002020 break;
2021
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002022 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002023 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002024 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002025 return true;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002026 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002027 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2028 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002029 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002030 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00002031
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002032 if (F.LocalNumIdentifiers > 0) {
2033 // Introduce the global -> local mapping for identifiers within this
2034 // module.
2035 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2036 &F));
2037
2038 // Introduce the local -> global mapping for identifiers within this
2039 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002040 F.IdentifierRemap.insertOrReplace(
2041 std::make_pair(LocalBaseIdentifierID,
2042 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002043
2044 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2045 + F.LocalNumIdentifiers);
2046 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002047 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002048 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002049
Sebastian Redl539c5062010-08-18 23:57:32 +00002050 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002051 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2052 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002053 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002054
Sebastian Redl539c5062010-08-18 23:57:32 +00002055 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002056 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2057 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002058 break;
2059
Sebastian Redl539c5062010-08-18 23:57:32 +00002060 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002061 TotalNumStatements += Record[0];
2062 TotalNumMacros += Record[1];
2063 TotalLexicalDeclContexts += Record[2];
2064 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002065 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002066
Sebastian Redl539c5062010-08-18 23:57:32 +00002067 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002068 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2069 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002070 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002071
Alexis Hunt27a761d2011-05-04 23:29:54 +00002072 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002073 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2074 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002075 break;
2076
Sebastian Redl539c5062010-08-18 23:57:32 +00002077 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002078 if (Record.size() % 4 != 0) {
2079 Error("invalid weak identifiers record");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002080 return true;
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002081 }
2082
2083 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2084 // files. This isn't the way to do it :)
2085 WeakUndeclaredIdentifiers.clear();
2086
2087 // Translate the weak, undeclared identifiers into global IDs.
2088 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2089 WeakUndeclaredIdentifiers.push_back(
2090 getGlobalIdentifierID(F, Record[I++]));
2091 WeakUndeclaredIdentifiers.push_back(
2092 getGlobalIdentifierID(F, Record[I++]));
2093 WeakUndeclaredIdentifiers.push_back(
2094 ReadSourceLocation(F, Record, I).getRawEncoding());
2095 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2096 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002097 break;
2098
Sebastian Redl539c5062010-08-18 23:57:32 +00002099 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002100 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2101 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002102 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002103
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002104 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002105 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002106 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002107 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002108 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002109
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002110 if (F.LocalNumSelectors > 0) {
2111 // Introduce the global -> local mapping for selectors within this
2112 // module.
2113 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2114
2115 // Introduce the local -> global mapping for selectors within this
2116 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002117 F.SelectorRemap.insertOrReplace(
2118 std::make_pair(LocalBaseSelectorID,
2119 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002120
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002121 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2122 }
2123 break;
2124 }
2125
Sebastian Redl539c5062010-08-18 23:57:32 +00002126 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002127 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002128 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002129 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002130 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002131 F.SelectorLookupTableData + Record[0],
2132 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002133 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002134 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002135 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002136
Sebastian Redl96371b42010-09-22 00:42:30 +00002137 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002138 if (!Record.empty()) {
2139 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2140 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2141 Record[Idx++]));
2142 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2143 getRawEncoding());
2144 }
2145 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002146 break;
2147
Sebastian Redl539c5062010-08-18 23:57:32 +00002148 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002149 if (!Record.empty() && Listener)
Argyrios Kyrtzidise445c722012-10-10 02:12:47 +00002150 Listener->ReadCounter(F, Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002151 break;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002152
2153 case FILE_SORTED_DECLS:
2154 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002155 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002156 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002157
Douglas Gregor925296b2011-07-19 16:10:42 +00002158 case SOURCE_LOCATION_OFFSETS: {
2159 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002160 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002161 unsigned SLocSpaceSize = Record[1];
Douglas Gregor925296b2011-07-19 16:10:42 +00002162 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002163 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2164 SLocSpaceSize);
Douglas Gregor925296b2011-07-19 16:10:42 +00002165 // Make our entry in the range map. BaseID is negative and growing, so
2166 // we invert it. Because we invert it, though, we need the other end of
2167 // the range.
2168 unsigned RangeStart =
2169 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2170 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2171 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2172
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002173 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2174 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2175 GlobalSLocOffsetMap.insert(
2176 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2177 - SLocSpaceSize,&F));
2178
Douglas Gregor925296b2011-07-19 16:10:42 +00002179 // Initialize the remapping table.
2180 // Invalid stays invalid.
2181 F.SLocRemap.insert(std::make_pair(0U, 0));
2182 // This module. Base was 2 when being compiled.
2183 F.SLocRemap.insert(std::make_pair(2U,
2184 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002185
2186 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002187 break;
2188 }
2189
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002190 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002191 // Additional remapping information.
2192 const unsigned char *Data = (const unsigned char*)BlobStart;
2193 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002194
2195 // Continuous range maps we may be updating in our module.
2196 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002197 ContinuousRangeMap<uint32_t, int, 2>::Builder
2198 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002199 ContinuousRangeMap<uint32_t, int, 2>::Builder
2200 MacroRemap(F.MacroRemap);
2201 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002202 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2203 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor253eefe2011-12-01 00:59:36 +00002204 SubmoduleRemap(F.SubmoduleRemap);
2205 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002206 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002207 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002208 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2209
Douglas Gregor925296b2011-07-19 16:10:42 +00002210 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002211 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002212 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002213 Data += Len;
Douglas Gregorde3ef502011-11-30 23:21:26 +00002214 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002215 if (!OM) {
2216 Error("SourceLocation remap refers to unknown module");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002217 return true;
Douglas Gregor925296b2011-07-19 16:10:42 +00002218 }
Douglas Gregor00659902011-08-02 10:56:51 +00002219
2220 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2221 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002222 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002223 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002224 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002225 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2226 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002227 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002228
2229 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2230 SLocRemap.insert(std::make_pair(SLocOffset,
2231 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002232 IdentifierRemap.insert(
2233 std::make_pair(IdentifierIDOffset,
2234 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002235 MacroRemap.insert(std::make_pair(MacroIDOffset,
2236 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002237 PreprocessedEntityRemap.insert(
2238 std::make_pair(PreprocessedEntityIDOffset,
2239 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor253eefe2011-12-01 00:59:36 +00002240 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2241 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002242 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2243 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002244 DeclRemap.insert(std::make_pair(DeclIDOffset,
2245 OM->BaseDeclID - DeclIDOffset));
2246
Douglas Gregor5204bde2011-08-02 16:26:37 +00002247 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002248 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor05f10352011-12-17 23:38:30 +00002249
2250 // Global -> local mappings.
2251 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregor925296b2011-07-19 16:10:42 +00002252 }
2253 break;
2254 }
2255
Douglas Gregor925296b2011-07-19 16:10:42 +00002256 case SOURCE_MANAGER_LINE_TABLE:
2257 if (ParseLineTable(F, Record))
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002258 return true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002259 break;
2260
Douglas Gregor925296b2011-07-19 16:10:42 +00002261 case SOURCE_LOCATION_PRELOADS: {
2262 // Need to transform from the local view (1-based IDs) to the global view,
2263 // which is based off F.SLocEntryBaseID.
Douglas Gregora918bab2011-08-25 21:09:44 +00002264 if (!F.PreloadSLocEntries.empty()) {
2265 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002266 return true;
Douglas Gregora918bab2011-08-25 21:09:44 +00002267 }
2268
2269 F.PreloadSLocEntries.swap(Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002270 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002271 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002272
Sebastian Redl539c5062010-08-18 23:57:32 +00002273 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002274 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2275 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002276 break;
2277
Sebastian Redl539c5062010-08-18 23:57:32 +00002278 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002279 if (Record.size() % 3 != 0) {
2280 Error("Invalid VTABLE_USES record");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002281 return true;
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002282 }
2283
Sebastian Redl08aca90252010-08-05 18:21:25 +00002284 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002285 // FIXME: Modules will have some trouble with this. This is clearly not
2286 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002287 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002288
2289 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2290 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2291 VTableUses.push_back(
2292 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2293 VTableUses.push_back(Record[Idx++]);
2294 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002295 break;
2296
Sebastian Redl539c5062010-08-18 23:57:32 +00002297 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002298 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2299 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002300 break;
2301
Sebastian Redl539c5062010-08-18 23:57:32 +00002302 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002303 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann63469422c2012-10-02 09:09:43 +00002304 Error("Invalid existing PendingInstantiations");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002305 return true;
Axel Naumann63469422c2012-10-02 09:09:43 +00002306 }
2307
2308 if (Record.size() % 2 != 0) {
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002309 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002310 return true;
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002311 }
Axel Naumann63469422c2012-10-02 09:09:43 +00002312
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002313 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2314 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2315 PendingInstantiations.push_back(
2316 ReadSourceLocation(F, Record, I).getRawEncoding());
2317 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002318 break;
2319
Sebastian Redl539c5062010-08-18 23:57:32 +00002320 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002321 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002322 // FIXME: Modules will have some trouble with this.
2323 SemaDeclRefs.clear();
2324 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2325 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002326 break;
2327
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002328 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002329 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2330 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2331 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002332
2333 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002334
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002335 unsigned StartingID;
Douglas Gregor51825b42011-09-09 22:02:16 +00002336 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +00002337 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor51825b42011-09-09 22:02:16 +00002338 if (!PP.getPreprocessingRecord()->getExternalSource())
2339 PP.getPreprocessingRecord()->SetExternalSource(*this);
2340 StartingID
2341 = PP.getPreprocessingRecord()
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002342 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002343 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002344
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002345 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002346 // Introduce the global -> local mapping for preprocessed entities in
2347 // this module.
2348 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2349
2350 // Introduce the local -> global mapping for preprocessed entities in
2351 // this module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002352 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002353 std::make_pair(LocalBasePreprocessedEntityID,
2354 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2355 }
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002356
Douglas Gregoraae92242010-03-19 21:51:54 +00002357 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002358 }
2359
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002360 case DECL_UPDATE_OFFSETS: {
2361 if (Record.size() % 2 != 0) {
2362 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002363 return true;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002364 }
2365 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002366 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2367 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002368 break;
2369 }
2370
Sebastian Redl539c5062010-08-18 23:57:32 +00002371 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002372 if (Record.size() % 3 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002373 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002374 return true;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002375 }
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002376 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregorf7180622011-08-03 15:48:04 +00002377 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002378 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002379 break;
2380 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002381
Douglas Gregor404cdde2012-01-27 01:47:08 +00002382 case OBJC_CATEGORIES_MAP: {
2383 if (F.LocalNumObjCCategoriesInMap != 0) {
2384 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002385 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002386 }
Douglas Gregor404cdde2012-01-27 01:47:08 +00002387
2388 F.LocalNumObjCCategoriesInMap = Record[0];
2389 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002390 break;
2391 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002392
Douglas Gregor404cdde2012-01-27 01:47:08 +00002393 case OBJC_CATEGORIES:
2394 F.ObjCCategories.swap(Record);
2395 break;
2396
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002397 case CXX_BASE_SPECIFIER_OFFSETS: {
2398 if (F.LocalNumCXXBaseSpecifiers != 0) {
2399 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002400 return true;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002401 }
2402
2403 F.LocalNumCXXBaseSpecifiers = Record[0];
2404 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002405 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002406 break;
2407 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002408
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002409 case DIAG_PRAGMA_MAPPINGS:
Douglas Gregor925296b2011-07-19 16:10:42 +00002410 if (F.PragmaDiagMappings.empty())
2411 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002412 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002413 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2414 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002415 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002416
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002417 case CUDA_SPECIAL_DECL_REFS:
2418 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002419 // FIXME: Modules will have trouble with this.
2420 CUDASpecialDeclRefs.clear();
2421 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2422 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002423 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002424
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002425 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002426 F.HeaderFileInfoTableData = BlobStart;
2427 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002428 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002429 if (Record[0]) {
2430 F.HeaderFileInfoTable
2431 = HeaderFileInfoLookupTable::Create(
2432 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002433 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002434 HeaderFileInfoTrait(*this, F,
Douglas Gregor51825b42011-09-09 22:02:16 +00002435 &PP.getHeaderSearchInfo(),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002436 BlobStart + Record[2]));
Douglas Gregor51825b42011-09-09 22:02:16 +00002437
2438 PP.getHeaderSearchInfo().SetExternalSource(this);
2439 if (!PP.getHeaderSearchInfo().getExternalLookup())
2440 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002441 }
2442 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002443 }
2444
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002445 case FP_PRAGMA_OPTIONS:
2446 // Later tables overwrite earlier ones.
2447 FPPragmaOptions.swap(Record);
2448 break;
2449
2450 case OPENCL_EXTENSIONS:
2451 // Later tables overwrite earlier ones.
2452 OpenCLExtensions.swap(Record);
2453 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002454
2455 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002456 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2457 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002458 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002459
2460 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002461 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2462 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002463 break;
Douglas Gregor0a839132011-12-03 00:59:55 +00002464
2465 case IMPORTED_MODULES: {
2466 if (F.Kind != MK_Module) {
2467 // If we aren't loading a module (which has its own exports), make
2468 // all of the imported modules visible.
2469 // FIXME: Deal with macros-only imports.
2470 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2471 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2472 ImportedModules.push_back(GlobalID);
2473 }
2474 }
2475 break;
Douglas Gregor05f10352011-12-17 23:38:30 +00002476 }
Douglas Gregor358cd442012-01-15 16:58:34 +00002477
Douglas Gregor05f10352011-12-17 23:38:30 +00002478 case LOCAL_REDECLARATIONS: {
Douglas Gregor358cd442012-01-15 16:58:34 +00002479 F.RedeclarationChains.swap(Record);
2480 break;
2481 }
2482
2483 case LOCAL_REDECLARATIONS_MAP: {
2484 if (F.LocalNumRedeclarationsInMap != 0) {
2485 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002486 return true;
Douglas Gregor05f10352011-12-17 23:38:30 +00002487 }
Douglas Gregor0a839132011-12-03 00:59:55 +00002488
Douglas Gregor358cd442012-01-15 16:58:34 +00002489 F.LocalNumRedeclarationsInMap = Record[0];
2490 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregor05f10352011-12-17 23:38:30 +00002491 break;
Douglas Gregor0a839132011-12-03 00:59:55 +00002492 }
Douglas Gregor464b0ca2011-12-22 21:40:42 +00002493
2494 case MERGED_DECLARATIONS: {
2495 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2496 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2497 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2498 for (unsigned N = Record[Idx++]; N > 0; --N)
2499 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2500 }
2501 break;
2502 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002503
2504 case MACRO_OFFSET: {
2505 if (F.LocalNumMacros != 0) {
2506 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002507 return true;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002508 }
2509 F.MacroOffsets = (const uint32_t *)BlobStart;
2510 F.LocalNumMacros = Record[0];
2511 unsigned LocalBaseMacroID = Record[1];
2512 F.BaseMacroID = getTotalNumMacros();
2513
2514 if (F.LocalNumMacros > 0) {
2515 // Introduce the global -> local mapping for macros within this module.
2516 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2517
2518 // Introduce the local -> global mapping for macros within this module.
2519 F.MacroRemap.insertOrReplace(
2520 std::make_pair(LocalBaseMacroID,
2521 F.BaseMacroID - LocalBaseMacroID));
2522
2523 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2524 }
2525 break;
2526 }
2527
2528 case MACRO_UPDATES: {
2529 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2530 MacroID ID = getGlobalMacroID(F, Record[I++]);
2531 if (I == N)
2532 break;
2533
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002534 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2535 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2536 MacroUpdate Update;
2537 Update.UndefLoc = UndefLoc;
2538 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002539 }
2540 break;
2541 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002542 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002543 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002544 Error("premature end of bitstream in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002545 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002546}
2547
Douglas Gregorcf68c582011-12-01 22:20:10 +00002548void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002549 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002550 switch (Names[I].getKind()) {
2551 case HiddenName::Declaration:
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002552 Names[I].getDecl()->Hidden = false;
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002553 break;
2554
2555 case HiddenName::MacroVisibility: {
2556 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2557 Macro.second->setHidden(!Macro.second->isPublic());
2558 if (Macro.second->isDefined()) {
2559 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2560 }
2561 break;
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002562 }
2563
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002564 case HiddenName::MacroUndef: {
2565 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2566 if (Macro.second->isDefined()) {
2567 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2568 if (PPMutationListener *Listener = PP.getPPMutationListener())
2569 Listener->UndefinedMacro(Macro.second);
2570 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2571 }
2572 break;
2573 }
Douglas Gregor0abc2622011-12-20 22:06:13 +00002574 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002575 }
Douglas Gregorcf68c582011-12-01 22:20:10 +00002576}
2577
Douglas Gregorff2be532011-12-01 17:11:21 +00002578void ASTReader::makeModuleVisible(Module *Mod,
2579 Module::NameVisibilityKind NameVisibility) {
2580 llvm::SmallPtrSet<Module *, 4> Visited;
2581 llvm::SmallVector<Module *, 4> Stack;
2582 Stack.push_back(Mod);
2583 while (!Stack.empty()) {
2584 Mod = Stack.back();
2585 Stack.pop_back();
2586
2587 if (NameVisibility <= Mod->NameVisibility) {
2588 // This module already has this level of visibility (or greater), so
2589 // there is nothing more to do.
2590 continue;
2591 }
2592
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002593 if (!Mod->isAvailable()) {
2594 // Modules that aren't available cannot be made visible.
2595 continue;
2596 }
2597
Douglas Gregorff2be532011-12-01 17:11:21 +00002598 // Update the module's name visibility.
2599 Mod->NameVisibility = NameVisibility;
2600
Douglas Gregorcf68c582011-12-01 22:20:10 +00002601 // If we've already deserialized any names from this module,
Douglas Gregorff2be532011-12-01 17:11:21 +00002602 // mark them as visible.
Douglas Gregorcf68c582011-12-01 22:20:10 +00002603 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2604 if (Hidden != HiddenNamesMap.end()) {
2605 makeNamesVisible(Hidden->second);
2606 HiddenNamesMap.erase(Hidden);
2607 }
Douglas Gregorff2be532011-12-01 17:11:21 +00002608
2609 // Push any non-explicit submodules onto the stack to be marked as
2610 // visible.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002611 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2612 SubEnd = Mod->submodule_end();
Douglas Gregorff2be532011-12-01 17:11:21 +00002613 Sub != SubEnd; ++Sub) {
Douglas Gregoreb90e832012-01-04 23:32:19 +00002614 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2615 Stack.push_back(*Sub);
Douglas Gregorff2be532011-12-01 17:11:21 +00002616 }
Douglas Gregor54139282011-12-02 19:11:09 +00002617
2618 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002619 bool AnyWildcard = false;
2620 bool UnrestrictedWildcard = false;
2621 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor54139282011-12-02 19:11:09 +00002622 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2623 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002624 if (!Mod->Exports[I].getInt()) {
2625 // Export a named module directly; no wildcards involved.
2626 if (Visited.insert(Exported))
Douglas Gregor54139282011-12-02 19:11:09 +00002627 Stack.push_back(Exported);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002628
2629 continue;
Douglas Gregor54139282011-12-02 19:11:09 +00002630 }
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002631
2632 // Wildcard export: export all of the imported modules that match
2633 // the given pattern.
2634 AnyWildcard = true;
2635 if (UnrestrictedWildcard)
2636 continue;
2637
2638 if (Module *Restriction = Mod->Exports[I].getPointer())
2639 WildcardRestrictions.push_back(Restriction);
2640 else {
2641 WildcardRestrictions.clear();
2642 UnrestrictedWildcard = true;
2643 }
2644 }
2645
2646 // If there were any wildcards, push any imported modules that were
2647 // re-exported by the wildcard restriction.
2648 if (!AnyWildcard)
2649 continue;
2650
2651 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2652 Module *Imported = Mod->Imports[I];
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00002653 if (!Visited.insert(Imported))
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002654 continue;
2655
2656 bool Acceptable = UnrestrictedWildcard;
2657 if (!Acceptable) {
2658 // Check whether this module meets one of the restrictions.
2659 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2660 Module *Restriction = WildcardRestrictions[R];
2661 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2662 Acceptable = true;
2663 break;
2664 }
2665 }
2666 }
2667
2668 if (!Acceptable)
2669 continue;
2670
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002671 Stack.push_back(Imported);
Douglas Gregor54139282011-12-02 19:11:09 +00002672 }
Douglas Gregorff2be532011-12-01 17:11:21 +00002673 }
2674}
2675
Sebastian Redl009e7f22010-10-05 16:15:19 +00002676ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002677 ModuleKind Type,
2678 unsigned ClientLoadCapabilities) {
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002679 // Bump the generation number.
Douglas Gregor404cdde2012-01-27 01:47:08 +00002680 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor112b9072012-10-18 05:31:06 +00002681
Douglas Gregor188dbef2012-11-07 17:46:15 +00002682 unsigned NumModules = ModuleMgr.size();
Douglas Gregor112b9072012-10-18 05:31:06 +00002683 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor188dbef2012-11-07 17:46:15 +00002684 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type,
2685 /*ImportedBy=*/0, Loaded,
2686 ClientLoadCapabilities)) {
2687 case Failure:
2688 case OutOfDate:
2689 case VersionMismatch:
2690 case ConfigurationMismatch:
2691 case HadErrors:
2692 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end());
2693 return ReadResult;
2694
2695 case Success:
2696 break;
Sebastian Redl2abc0382010-07-16 20:41:52 +00002697 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002698
2699 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002700
Douglas Gregor112b9072012-10-18 05:31:06 +00002701 // Load the AST blocks of all of the modules that we loaded.
2702 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
2703 MEnd = Loaded.end();
2704 M != MEnd; ++M) {
2705 ModuleFile &F = **M;
2706
2707 // Read the AST block.
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002708 if (ReadASTBlock(F))
2709 return Failure;
Douglas Gregor112b9072012-10-18 05:31:06 +00002710
2711 // Once read, set the ModuleFile bit base offset and update the size in
2712 // bits of all files we've seen.
2713 F.GlobalBitOffset = TotalModulesSizeInBits;
2714 TotalModulesSizeInBits += F.SizeInBits;
2715 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2716
Douglas Gregor112b9072012-10-18 05:31:06 +00002717 // Preload SLocEntries.
2718 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2719 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor4750b772012-10-22 22:53:10 +00002720 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor112b9072012-10-18 05:31:06 +00002721 // directly because the entry may have already been loaded in which case
Douglas Gregor4750b772012-10-22 22:53:10 +00002722 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor112b9072012-10-18 05:31:06 +00002723 // SourceManager.
2724 SourceMgr.getLoadedSLocEntryByID(Index);
2725 }
2726 }
2727
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002728 // Mark all of the identifiers in the identifier table as being out of date,
2729 // so that various accessors know to check the loaded modules when the
2730 // identifier is used.
Douglas Gregor51825b42011-09-09 22:02:16 +00002731 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2732 IdEnd = PP.getIdentifierTable().end();
2733 Id != IdEnd; ++Id)
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002734 Id->second->setOutOfDate(true);
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002735
Douglas Gregor24bb9232011-12-02 18:58:38 +00002736 // Resolve any unresolved module exports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002737 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2738 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2739 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002740 Module *ResolvedMod = getSubmodule(GlobalID);
2741
2742 if (Unresolved.IsImport) {
2743 if (ResolvedMod)
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002744 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002745 continue;
Douglas Gregor24bb9232011-12-02 18:58:38 +00002746 }
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002747
2748 if (ResolvedMod || Unresolved.IsWildcard)
2749 Unresolved.Mod->Exports.push_back(
2750 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregor24bb9232011-12-02 18:58:38 +00002751 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002752 UnresolvedModuleImportExports.clear();
Douglas Gregor24bb9232011-12-02 18:58:38 +00002753
Douglas Gregor4163aca2011-09-09 21:34:22 +00002754 InitializeContext();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002755
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002756 if (DeserializationListener)
2757 DeserializationListener->ReaderInitialized(this);
2758
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002759 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2760 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2761 PrimaryModule.OriginalSourceFileID
2762 = FileID::get(PrimaryModule.SLocEntryBaseID
2763 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002764
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002765 // If this AST file is a precompiled preamble, then set the
2766 // preamble file ID of the source manager to the file source file
2767 // from which the preamble was built.
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002768 if (Type == MK_Preamble) {
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002769 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002770 } else if (Type == MK_MainFile) {
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002771 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002772 }
Douglas Gregor936a5b42010-11-30 05:23:00 +00002773 }
2774
Douglas Gregor404cdde2012-01-27 01:47:08 +00002775 // For any Objective-C class definitions we have already loaded, make sure
2776 // that we load any additional categories.
2777 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2778 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2779 ObjCClassesLoaded[I],
2780 PreviousGeneration);
2781 }
2782
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002783 return Success;
2784}
2785
Douglas Gregor4b29c162012-10-22 23:51:00 +00002786ASTReader::ASTReadResult
2787ASTReader::ReadASTCore(StringRef FileName,
2788 ModuleKind Type,
2789 ModuleFile *ImportedBy,
2790 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
2791 unsigned ClientLoadCapabilities) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002792 ModuleFile *M;
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002793 bool NewModule;
2794 std::string ErrorStr;
2795 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002796 CurrentGeneration, ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002797
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002798 if (!M) {
2799 // We couldn't load the module.
2800 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2801 + ErrorStr;
2802 Error(Msg);
2803 return Failure;
2804 }
2805
2806 if (!NewModule) {
2807 // We've already loaded this module.
2808 return Success;
2809 }
2810
2811 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2812 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002813 if (FileName != "-") {
2814 CurrentDir = llvm::sys::path::parent_path(FileName);
2815 if (CurrentDir.empty()) CurrentDir = ".";
2816 }
2817
Douglas Gregorde3ef502011-11-30 23:21:26 +00002818 ModuleFile &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002819 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002820 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002821 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002822
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002823 // Sniff for the signature.
2824 if (Stream.Read(8) != 'C' ||
2825 Stream.Read(8) != 'P' ||
2826 Stream.Read(8) != 'C' ||
2827 Stream.Read(8) != 'H') {
2828 Diag(diag::err_not_a_pch_file) << FileName;
2829 return Failure;
2830 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002831
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002832 while (!Stream.AtEndOfStream()) {
2833 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002834
Douglas Gregor92863e42009-04-10 23:10:45 +00002835 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002836 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002837 return Failure;
2838 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002839
2840 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002841
Douglas Gregor0aa21c92012-10-18 18:27:37 +00002842 // We only know the control subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002843 switch (BlockID) {
2844 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002845 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002846 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002847 return Failure;
2848 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002849 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00002850 case CONTROL_BLOCK_ID:
Douglas Gregor4b29c162012-10-22 23:51:00 +00002851 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002852 case Success:
2853 break;
2854
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002855 case Failure: return Failure;
2856 case OutOfDate: return OutOfDate;
2857 case VersionMismatch: return VersionMismatch;
2858 case ConfigurationMismatch: return ConfigurationMismatch;
2859 case HadErrors: return HadErrors;
Douglas Gregor55abb232009-04-10 20:39:37 +00002860 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002861 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00002862 case AST_BLOCK_ID:
2863 // Record that we've loaded this module.
2864 Loaded.push_back(M);
2865 return Success;
2866
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002867 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002868 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002869 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002870 return Failure;
2871 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002872 break;
2873 }
Mike Stump11289f42009-09-09 15:08:12 +00002874 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002875
Sebastian Redl2abc0382010-07-16 20:41:52 +00002876 return Success;
2877}
2878
Douglas Gregor51825b42011-09-09 22:02:16 +00002879void ASTReader::InitializeContext() {
Douglas Gregordab42432011-08-12 00:15:20 +00002880 // If there's a listener, notify them that we "read" the translation unit.
2881 if (DeserializationListener)
Douglas Gregor4163aca2011-09-09 21:34:22 +00002882 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2883 Context.getTranslationUnitDecl());
Douglas Gregoraa433012010-10-01 01:18:02 +00002884
Douglas Gregordab42432011-08-12 00:15:20 +00002885 // Make sure we load the declaration update records for the translation unit,
2886 // if there are any.
Douglas Gregor4163aca2011-09-09 21:34:22 +00002887 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2888 Context.getTranslationUnitDecl());
Douglas Gregordab42432011-08-12 00:15:20 +00002889
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002890 // FIXME: Find a better way to deal with collisions between these
2891 // built-in types. Right now, we just ignore the problem.
2892
2893 // Load the special types.
Douglas Gregord53ae832012-01-17 18:09:05 +00002894 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002895 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2896 if (!Context.CFConstantStringTypeDecl)
2897 Context.setCFConstantStringType(GetType(String));
2898 }
2899
2900 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2901 QualType FileType = GetType(File);
2902 if (FileType.isNull()) {
2903 Error("FILE type is NULL");
2904 return;
2905 }
2906
2907 if (!Context.FILEDecl) {
2908 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2909 Context.setFILEDecl(Typedef->getDecl());
2910 else {
2911 const TagType *Tag = FileType->getAs<TagType>();
2912 if (!Tag) {
2913 Error("Invalid FILE type in AST file");
2914 return;
2915 }
2916 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002917 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002918 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002919 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002920
Douglas Gregor3c267f7a2011-11-11 19:13:12 +00002921 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002922 QualType Jmp_bufType = GetType(Jmp_buf);
2923 if (Jmp_bufType.isNull()) {
2924 Error("jmp_buf type is NULL");
2925 return;
2926 }
2927
2928 if (!Context.jmp_bufDecl) {
2929 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2930 Context.setjmp_bufDecl(Typedef->getDecl());
2931 else {
2932 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2933 if (!Tag) {
2934 Error("Invalid jmp_buf type in AST file");
2935 return;
2936 }
2937 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002938 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002939 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002940 }
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002941
Douglas Gregor3c267f7a2011-11-11 19:13:12 +00002942 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002943 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2944 if (Sigjmp_bufType.isNull()) {
2945 Error("sigjmp_buf type is NULL");
2946 return;
2947 }
2948
2949 if (!Context.sigjmp_bufDecl) {
2950 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2951 Context.setsigjmp_bufDecl(Typedef->getDecl());
2952 else {
2953 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2954 assert(Tag && "Invalid sigjmp_buf type in AST file");
2955 Context.setsigjmp_bufDecl(Tag->getDecl());
2956 }
2957 }
2958 }
2959
2960 if (unsigned ObjCIdRedef
2961 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2962 if (Context.ObjCIdRedefinitionType.isNull())
2963 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2964 }
2965
2966 if (unsigned ObjCClassRedef
2967 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2968 if (Context.ObjCClassRedefinitionType.isNull())
2969 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2970 }
2971
2972 if (unsigned ObjCSelRedef
2973 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2974 if (Context.ObjCSelRedefinitionType.isNull())
2975 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2976 }
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00002977
2978 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2979 QualType Ucontext_tType = GetType(Ucontext_t);
2980 if (Ucontext_tType.isNull()) {
2981 Error("ucontext_t type is NULL");
2982 return;
2983 }
2984
2985 if (!Context.ucontext_tDecl) {
2986 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2987 Context.setucontext_tDecl(Typedef->getDecl());
2988 else {
2989 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2990 assert(Tag && "Invalid ucontext_t type in AST file");
2991 Context.setucontext_tDecl(Tag->getDecl());
2992 }
2993 }
2994 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002995 }
2996
Douglas Gregor4163aca2011-09-09 21:34:22 +00002997 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002998
2999 // If there were any CUDA special declarations, deserialize them.
3000 if (!CUDASpecialDeclRefs.empty()) {
3001 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor4163aca2011-09-09 21:34:22 +00003002 Context.setcudaConfigureCallDecl(
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003003 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3004 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003005
3006 // Re-export any modules that were imported by a non-module AST file.
3007 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3008 if (Module *Imported = getSubmodule(ImportedModules[I]))
3009 makeModuleVisible(Imported, Module::AllVisible);
3010 }
3011 ImportedModules.clear();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003012}
3013
Douglas Gregorcf68c582011-12-01 22:20:10 +00003014void ASTReader::finalizeForWriting() {
3015 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3016 HiddenEnd = HiddenNamesMap.end();
3017 Hidden != HiddenEnd; ++Hidden) {
3018 makeNamesVisible(Hidden->second);
3019 }
3020 HiddenNamesMap.clear();
3021}
3022
Douglas Gregor45fe0362009-05-12 01:31:05 +00003023/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003024/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00003025/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003026std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00003027 FileManager &FileMgr,
David Blaikie9c902b52011-09-25 23:23:43 +00003028 DiagnosticsEngine &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003029 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003030 std::string ErrStr;
Dylan Noblesmithe2778992012-02-05 02:12:40 +00003031 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00003032 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00003033 if (!Buffer) {
Kaelyn Uhrain272d7182012-06-20 00:36:03 +00003034 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003035 return std::string();
3036 }
3037
3038 // Initialize the stream
3039 llvm::BitstreamReader StreamFile;
3040 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00003041 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00003042 (const unsigned char *)Buffer->getBufferEnd());
3043 Stream.init(StreamFile);
3044
3045 // Sniff for the signature.
3046 if (Stream.Read(8) != 'C' ||
3047 Stream.Read(8) != 'P' ||
3048 Stream.Read(8) != 'C' ||
3049 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003050 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003051 return std::string();
3052 }
3053
3054 RecordData Record;
3055 while (!Stream.AtEndOfStream()) {
3056 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00003057
Douglas Gregor45fe0362009-05-12 01:31:05 +00003058 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3059 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00003060
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003061 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003062 switch (BlockID) {
Douglas Gregor112b9072012-10-18 05:31:06 +00003063 case CONTROL_BLOCK_ID:
3064 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003065 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003066 return std::string();
3067 }
3068 break;
Mike Stump11289f42009-09-09 15:08:12 +00003069
Douglas Gregor45fe0362009-05-12 01:31:05 +00003070 default:
3071 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003072 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003073 return std::string();
3074 }
3075 break;
3076 }
3077 continue;
3078 }
3079
3080 if (Code == llvm::bitc::END_BLOCK) {
3081 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003082 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003083 return std::string();
3084 }
3085 continue;
3086 }
3087
3088 if (Code == llvm::bitc::DEFINE_ABBREV) {
3089 Stream.ReadAbbrevRecord();
3090 continue;
3091 }
3092
3093 Record.clear();
3094 const char *BlobStart = 0;
3095 unsigned BlobLen = 0;
Douglas Gregorfad10d82012-10-18 18:36:53 +00003096 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregor45fe0362009-05-12 01:31:05 +00003097 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00003098 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00003099
3100 return std::string();
3101}
3102
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003103namespace {
3104 class SimplePCHValidator : public ASTReaderListener {
3105 const LangOptions &ExistingLangOpts;
3106 const TargetOptions &ExistingTargetOpts;
Douglas Gregorb6368752012-10-24 23:41:50 +00003107 const PreprocessorOptions &ExistingPPOpts;
Douglas Gregor55358ed2012-10-25 00:07:54 +00003108 FileManager &FileMgr;
3109
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003110 public:
3111 SimplePCHValidator(const LangOptions &ExistingLangOpts,
Douglas Gregorb6368752012-10-24 23:41:50 +00003112 const TargetOptions &ExistingTargetOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003113 const PreprocessorOptions &ExistingPPOpts,
3114 FileManager &FileMgr)
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003115 : ExistingLangOpts(ExistingLangOpts),
Douglas Gregorb6368752012-10-24 23:41:50 +00003116 ExistingTargetOpts(ExistingTargetOpts),
Douglas Gregor55358ed2012-10-25 00:07:54 +00003117 ExistingPPOpts(ExistingPPOpts),
3118 FileMgr(FileMgr)
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003119 {
3120 }
3121
3122 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3123 bool Complain) {
3124 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3125 }
3126 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3127 bool Complain) {
3128 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3129 }
Douglas Gregorb6368752012-10-24 23:41:50 +00003130 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003131 bool Complain,
3132 std::string &SuggestedPredefines) {
3133 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3134 SuggestedPredefines);
Douglas Gregorb6368752012-10-24 23:41:50 +00003135 }
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003136 };
3137}
3138
Douglas Gregor52901822012-11-06 23:40:54 +00003139bool ASTReader::readASTFileControlBlock(StringRef Filename,
3140 FileManager &FileMgr,
3141 ASTReaderListener &Listener) {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003142 // Open the AST file.
3143 std::string ErrStr;
3144 OwningPtr<llvm::MemoryBuffer> Buffer;
3145 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3146 if (!Buffer) {
Douglas Gregor52901822012-11-06 23:40:54 +00003147 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003148 }
3149
3150 // Initialize the stream
3151 llvm::BitstreamReader StreamFile;
3152 llvm::BitstreamCursor Stream;
3153 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3154 (const unsigned char *)Buffer->getBufferEnd());
3155 Stream.init(StreamFile);
3156
3157 // Sniff for the signature.
3158 if (Stream.Read(8) != 'C' ||
3159 Stream.Read(8) != 'P' ||
3160 Stream.Read(8) != 'C' ||
3161 Stream.Read(8) != 'H') {
Douglas Gregor52901822012-11-06 23:40:54 +00003162 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003163 }
3164
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003165 RecordData Record;
3166 bool InControlBlock = false;
3167 while (!Stream.AtEndOfStream()) {
3168 unsigned Code = Stream.ReadCode();
3169
3170 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3171 unsigned BlockID = Stream.ReadSubBlockID();
3172
3173 // We only know the control subblock ID.
3174 switch (BlockID) {
3175 case CONTROL_BLOCK_ID:
3176 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Douglas Gregor52901822012-11-06 23:40:54 +00003177 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003178 } else {
3179 InControlBlock = true;
3180 }
3181 break;
3182
3183 default:
3184 if (Stream.SkipBlock())
Douglas Gregor52901822012-11-06 23:40:54 +00003185 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003186 break;
3187 }
3188 continue;
3189 }
3190
3191 if (Code == llvm::bitc::END_BLOCK) {
3192 if (Stream.ReadBlockEnd()) {
Douglas Gregor52901822012-11-06 23:40:54 +00003193 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003194 }
Douglas Gregor52901822012-11-06 23:40:54 +00003195
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003196 InControlBlock = false;
3197 continue;
3198 }
3199
3200 if (Code == llvm::bitc::DEFINE_ABBREV) {
3201 Stream.ReadAbbrevRecord();
3202 continue;
3203 }
3204
3205 Record.clear();
3206 const char *BlobStart = 0;
3207 unsigned BlobLen = 0;
3208 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3209 if (InControlBlock) {
3210 switch ((ControlRecordTypes)RecCode) {
3211 case METADATA: {
3212 if (Record[0] != VERSION_MAJOR) {
Douglas Gregor52901822012-11-06 23:40:54 +00003213 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003214 }
3215
3216 const std::string &CurBranch = getClangFullRepositoryVersion();
3217 StringRef ASTBranch(BlobStart, BlobLen);
3218 if (StringRef(CurBranch) != ASTBranch)
Douglas Gregor52901822012-11-06 23:40:54 +00003219 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003220
3221 break;
3222 }
3223 case LANGUAGE_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003224 if (ParseLanguageOptions(Record, false, Listener))
3225 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003226 break;
3227
3228 case TARGET_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003229 if (ParseTargetOptions(Record, false, Listener))
3230 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003231 break;
3232
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003233 case DIAGNOSTIC_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003234 if (ParseDiagnosticOptions(Record, false, Listener))
3235 return true;
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003236 break;
3237
Douglas Gregor2d302362012-10-24 16:50:34 +00003238 case FILE_SYSTEM_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003239 if (ParseFileSystemOptions(Record, false, Listener))
3240 return true;
Douglas Gregor2d302362012-10-24 16:50:34 +00003241 break;
3242
3243 case HEADER_SEARCH_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003244 if (ParseHeaderSearchOptions(Record, false, Listener))
3245 return true;
Douglas Gregor2d302362012-10-24 16:50:34 +00003246 break;
3247
Douglas Gregor55358ed2012-10-25 00:07:54 +00003248 case PREPROCESSOR_OPTIONS: {
3249 std::string IgnoredSuggestedPredefines;
Douglas Gregor52901822012-11-06 23:40:54 +00003250 if (ParsePreprocessorOptions(Record, false, Listener,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003251 IgnoredSuggestedPredefines))
Douglas Gregor52901822012-11-06 23:40:54 +00003252 return true;
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003253 break;
Douglas Gregor55358ed2012-10-25 00:07:54 +00003254 }
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003255
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003256 default:
3257 // No other validation to perform.
3258 break;
3259 }
3260 }
3261 }
3262
Douglas Gregor52901822012-11-06 23:40:54 +00003263 return false;
3264}
3265
3266
3267bool ASTReader::isAcceptableASTFile(StringRef Filename,
3268 FileManager &FileMgr,
3269 const LangOptions &LangOpts,
3270 const TargetOptions &TargetOpts,
3271 const PreprocessorOptions &PPOpts) {
3272 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3273 return !readASTFileControlBlock(Filename, FileMgr, validator);
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003274}
3275
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003276bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor69021972011-11-30 17:33:56 +00003277 // Enter the submodule block.
3278 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3279 Error("malformed submodule block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003280 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003281 }
3282
3283 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor253eefe2011-12-01 00:59:36 +00003284 bool First = true;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003285 Module *CurrentModule = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00003286 RecordData Record;
3287 while (true) {
3288 unsigned Code = F.Stream.ReadCode();
3289 if (Code == llvm::bitc::END_BLOCK) {
3290 if (F.Stream.ReadBlockEnd()) {
3291 Error("error at end of submodule block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003292 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003293 }
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003294 return false;
Douglas Gregor69021972011-11-30 17:33:56 +00003295 }
3296
3297 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3298 // No known subblocks, always skip them.
3299 F.Stream.ReadSubBlockID();
3300 if (F.Stream.SkipBlock()) {
3301 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003302 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003303 }
3304 continue;
3305 }
3306
3307 if (Code == llvm::bitc::DEFINE_ABBREV) {
3308 F.Stream.ReadAbbrevRecord();
3309 continue;
3310 }
3311
3312 // Read a record.
3313 const char *BlobStart;
3314 unsigned BlobLen;
3315 Record.clear();
3316 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3317 default: // Default behavior: ignore.
3318 break;
3319
3320 case SUBMODULE_DEFINITION: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003321 if (First) {
3322 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003323 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003324 }
3325
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003326 if (Record.size() < 7) {
Douglas Gregor73441092011-12-05 22:27:44 +00003327 Error("malformed module definition");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003328 return true;
Douglas Gregor73441092011-12-05 22:27:44 +00003329 }
3330
Douglas Gregor69021972011-11-30 17:33:56 +00003331 StringRef Name(BlobStart, BlobLen);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003332 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3333 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3334 bool IsFramework = Record[2];
3335 bool IsExplicit = Record[3];
Douglas Gregora686e1b2012-01-27 19:52:33 +00003336 bool IsSystem = Record[4];
3337 bool InferSubmodules = Record[5];
3338 bool InferExplicitSubmodules = Record[6];
3339 bool InferExportWildcard = Record[7];
Douglas Gregor73441092011-12-05 22:27:44 +00003340
Douglas Gregorde3ef502011-11-30 23:21:26 +00003341 Module *ParentModule = 0;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003342 if (Parent)
3343 ParentModule = getSubmodule(Parent);
Douglas Gregor69021972011-11-30 17:33:56 +00003344
3345 // Retrieve this (sub)module from the module map, creating it if
3346 // necessary.
3347 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3348 IsFramework,
3349 IsExplicit).first;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003350 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3351 if (GlobalIndex >= SubmodulesLoaded.size() ||
3352 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003353 Error("too many submodules");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003354 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003355 }
Douglas Gregore37a85a2011-12-02 17:30:13 +00003356
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +00003357 CurrentModule->setASTFile(F.File);
Douglas Gregor98a52db2011-12-20 00:28:52 +00003358 CurrentModule->IsFromModuleFile = true;
Douglas Gregora686e1b2012-01-27 19:52:33 +00003359 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor73441092011-12-05 22:27:44 +00003360 CurrentModule->InferSubmodules = InferSubmodules;
3361 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3362 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregore37a85a2011-12-02 17:30:13 +00003363 if (DeserializationListener)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003364 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregore37a85a2011-12-02 17:30:13 +00003365
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003366 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor69021972011-11-30 17:33:56 +00003367 break;
3368 }
3369
Douglas Gregor524e33e2011-12-08 19:11:24 +00003370 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003371 if (First) {
3372 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003373 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003374 }
3375
Douglas Gregor69021972011-11-30 17:33:56 +00003376 if (!CurrentModule)
3377 break;
3378
3379 StringRef FileName(BlobStart, BlobLen);
3380 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor73141fa2011-12-08 17:39:04 +00003381 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003382 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor73141fa2011-12-08 17:39:04 +00003383 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor69021972011-11-30 17:33:56 +00003384 Error("mismatched umbrella headers in submodule");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003385 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003386 }
3387 }
3388 break;
3389 }
3390
3391 case SUBMODULE_HEADER: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003392 if (First) {
3393 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003394 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003395 }
3396
Douglas Gregor69021972011-11-30 17:33:56 +00003397 if (!CurrentModule)
3398 break;
3399
3400 // FIXME: Be more lazy about this!
3401 StringRef FileName(BlobStart, BlobLen);
3402 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3403 if (std::find(CurrentModule->Headers.begin(),
3404 CurrentModule->Headers.end(),
3405 File) == CurrentModule->Headers.end())
Douglas Gregor59527662012-10-15 06:28:11 +00003406 ModMap.addHeader(CurrentModule, File, false);
3407 }
3408 break;
3409 }
3410
3411 case SUBMODULE_EXCLUDED_HEADER: {
3412 if (First) {
3413 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003414 return true;
Douglas Gregor59527662012-10-15 06:28:11 +00003415 }
3416
3417 if (!CurrentModule)
3418 break;
3419
3420 // FIXME: Be more lazy about this!
3421 StringRef FileName(BlobStart, BlobLen);
3422 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3423 if (std::find(CurrentModule->Headers.begin(),
3424 CurrentModule->Headers.end(),
3425 File) == CurrentModule->Headers.end())
3426 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor69021972011-11-30 17:33:56 +00003427 }
3428 break;
3429 }
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00003430
3431 case SUBMODULE_TOPHEADER: {
3432 if (First) {
3433 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003434 return true;
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00003435 }
3436
3437 if (!CurrentModule)
3438 break;
3439
3440 // FIXME: Be more lazy about this!
3441 StringRef FileName(BlobStart, BlobLen);
3442 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3443 CurrentModule->TopHeaders.insert(File);
3444 break;
3445 }
3446
Douglas Gregor524e33e2011-12-08 19:11:24 +00003447 case SUBMODULE_UMBRELLA_DIR: {
3448 if (First) {
3449 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003450 return true;
Douglas Gregor524e33e2011-12-08 19:11:24 +00003451 }
3452
3453 if (!CurrentModule)
3454 break;
3455
3456 StringRef DirName(BlobStart, BlobLen);
3457 if (const DirectoryEntry *Umbrella
3458 = PP.getFileManager().getDirectory(DirName)) {
3459 if (!CurrentModule->getUmbrellaDir())
3460 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3461 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3462 Error("mismatched umbrella directories in submodule");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003463 return true;
Douglas Gregor524e33e2011-12-08 19:11:24 +00003464 }
3465 }
3466 break;
3467 }
3468
Douglas Gregor253eefe2011-12-01 00:59:36 +00003469 case SUBMODULE_METADATA: {
3470 if (!First) {
3471 Error("submodule metadata record not at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003472 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003473 }
3474 First = false;
3475
3476 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor253eefe2011-12-01 00:59:36 +00003477 F.LocalNumSubmodules = Record[0];
3478 unsigned LocalBaseSubmoduleID = Record[1];
3479 if (F.LocalNumSubmodules > 0) {
3480 // Introduce the global -> local mapping for submodules within this
3481 // module.
3482 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3483
3484 // Introduce the local -> global mapping for submodules within this
3485 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00003486 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor253eefe2011-12-01 00:59:36 +00003487 std::make_pair(LocalBaseSubmoduleID,
3488 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3489
3490 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3491 }
3492 break;
3493 }
Douglas Gregor24bb9232011-12-02 18:58:38 +00003494
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003495 case SUBMODULE_IMPORTS: {
3496 if (First) {
3497 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003498 return true;
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003499 }
3500
3501 if (!CurrentModule)
3502 break;
3503
3504 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3505 UnresolvedModuleImportExport Unresolved;
3506 Unresolved.File = &F;
3507 Unresolved.Mod = CurrentModule;
3508 Unresolved.ID = Record[Idx];
3509 Unresolved.IsImport = true;
3510 Unresolved.IsWildcard = false;
3511 UnresolvedModuleImportExports.push_back(Unresolved);
3512 }
3513 break;
3514 }
3515
Douglas Gregor24bb9232011-12-02 18:58:38 +00003516 case SUBMODULE_EXPORTS: {
3517 if (First) {
3518 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003519 return true;
Douglas Gregor24bb9232011-12-02 18:58:38 +00003520 }
3521
3522 if (!CurrentModule)
3523 break;
3524
3525 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003526 UnresolvedModuleImportExport Unresolved;
Douglas Gregor24bb9232011-12-02 18:58:38 +00003527 Unresolved.File = &F;
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003528 Unresolved.Mod = CurrentModule;
3529 Unresolved.ID = Record[Idx];
3530 Unresolved.IsImport = false;
3531 Unresolved.IsWildcard = Record[Idx + 1];
3532 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregor24bb9232011-12-02 18:58:38 +00003533 }
3534
3535 // Once we've loaded the set of exports, there's no reason to keep
3536 // the parsed, unresolved exports around.
3537 CurrentModule->UnresolvedExports.clear();
3538 break;
3539 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003540 case SUBMODULE_REQUIRES: {
3541 if (First) {
3542 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003543 return true;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003544 }
3545
3546 if (!CurrentModule)
3547 break;
3548
3549 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikiebbafb8a2012-03-11 07:00:24 +00003550 Context.getLangOpts(),
Douglas Gregor89929282012-01-30 06:01:29 +00003551 Context.getTargetInfo());
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003552 break;
3553 }
Douglas Gregor69021972011-11-30 17:33:56 +00003554 }
3555 }
Douglas Gregor69021972011-11-30 17:33:56 +00003556}
3557
Douglas Gregor55abb232009-04-10 20:39:37 +00003558/// \brief Parse the record that corresponds to a LangOptions data
3559/// structure.
3560///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003561/// This routine parses the language options from the AST file and then gives
3562/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00003563///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003564/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003565bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3566 bool Complain,
3567 ASTReaderListener &Listener) {
3568 LangOptions LangOpts;
3569 unsigned Idx = 0;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00003570#define LANGOPT(Name, Bits, Default, Description) \
3571 LangOpts.Name = Record[Idx++];
3572#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3573 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3574#include "clang/Basic/LangOptions.def"
John McCall5fb5df92012-06-20 06:18:46 +00003575
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003576 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3577 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3578 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3579
3580 unsigned Length = Record[Idx++];
3581 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3582 Record.begin() + Idx + Length);
3583 return Listener.ReadLanguageOptions(LangOpts, Complain);
3584}
3585
3586bool ASTReader::ParseTargetOptions(const RecordData &Record,
3587 bool Complain,
3588 ASTReaderListener &Listener) {
3589 unsigned Idx = 0;
3590 TargetOptions TargetOpts;
3591 TargetOpts.Triple = ReadString(Record, Idx);
3592 TargetOpts.CPU = ReadString(Record, Idx);
3593 TargetOpts.ABI = ReadString(Record, Idx);
3594 TargetOpts.CXXABI = ReadString(Record, Idx);
3595 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3596 for (unsigned N = Record[Idx++]; N; --N) {
3597 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3598 }
3599 for (unsigned N = Record[Idx++]; N; --N) {
3600 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor55abb232009-04-10 20:39:37 +00003601 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003602
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003603 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor55abb232009-04-10 20:39:37 +00003604}
3605
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003606bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3607 ASTReaderListener &Listener) {
3608 DiagnosticOptions DiagOpts;
3609 unsigned Idx = 0;
3610#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3611#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3612 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3613#include "clang/Basic/DiagnosticOptions.def"
3614
3615 for (unsigned N = Record[Idx++]; N; --N) {
3616 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3617 }
3618
3619 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3620}
3621
Douglas Gregorc6317db2012-10-24 15:49:58 +00003622bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3623 ASTReaderListener &Listener) {
3624 FileSystemOptions FSOpts;
3625 unsigned Idx = 0;
3626 FSOpts.WorkingDir = ReadString(Record, Idx);
3627 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3628}
3629
Douglas Gregor2d302362012-10-24 16:50:34 +00003630bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3631 bool Complain,
3632 ASTReaderListener &Listener) {
3633 HeaderSearchOptions HSOpts;
3634 unsigned Idx = 0;
3635 HSOpts.Sysroot = ReadString(Record, Idx);
3636
3637 // Include entries.
3638 for (unsigned N = Record[Idx++]; N; --N) {
3639 std::string Path = ReadString(Record, Idx);
3640 frontend::IncludeDirGroup Group
3641 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3642 bool IsUserSupplied = Record[Idx++];
3643 bool IsFramework = Record[Idx++];
3644 bool IgnoreSysRoot = Record[Idx++];
3645 bool IsInternal = Record[Idx++];
3646 bool ImplicitExternC = Record[Idx++];
3647 HSOpts.UserEntries.push_back(
3648 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3649 IgnoreSysRoot, IsInternal, ImplicitExternC));
3650 }
3651
3652 // System header prefixes.
3653 for (unsigned N = Record[Idx++]; N; --N) {
3654 std::string Prefix = ReadString(Record, Idx);
3655 bool IsSystemHeader = Record[Idx++];
3656 HSOpts.SystemHeaderPrefixes.push_back(
3657 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3658 }
3659
3660 HSOpts.ResourceDir = ReadString(Record, Idx);
3661 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3662 HSOpts.DisableModuleHash = Record[Idx++];
3663 HSOpts.UseBuiltinIncludes = Record[Idx++];
3664 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3665 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3666 HSOpts.UseLibcxx = Record[Idx++];
3667
3668 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3669}
3670
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003671bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3672 bool Complain,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003673 ASTReaderListener &Listener,
3674 std::string &SuggestedPredefines) {
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003675 PreprocessorOptions PPOpts;
3676 unsigned Idx = 0;
3677
3678 // Macro definitions/undefs
3679 for (unsigned N = Record[Idx++]; N; --N) {
3680 std::string Macro = ReadString(Record, Idx);
3681 bool IsUndef = Record[Idx++];
3682 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3683 }
3684
3685 // Includes
3686 for (unsigned N = Record[Idx++]; N; --N) {
3687 PPOpts.Includes.push_back(ReadString(Record, Idx));
3688 }
3689
3690 // Macro Includes
3691 for (unsigned N = Record[Idx++]; N; --N) {
3692 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3693 }
3694
Douglas Gregorb6368752012-10-24 23:41:50 +00003695 PPOpts.UsePredefines = Record[Idx++];
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003696 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3697 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3698 PPOpts.ObjCXXARCStandardLibrary =
3699 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
Douglas Gregor55358ed2012-10-25 00:07:54 +00003700 SuggestedPredefines.clear();
3701 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3702 SuggestedPredefines);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003703}
3704
Douglas Gregorde3ef502011-11-30 23:21:26 +00003705std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003706ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003707 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003708 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003709 assert(I != GlobalPreprocessedEntityMap.end() &&
3710 "Corrupted global preprocessed entity map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00003711 ModuleFile *M = I->second;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003712 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3713 return std::make_pair(M, LocalIndex);
3714}
3715
Argyrios Kyrtzidisd4fcf5802012-10-02 16:10:51 +00003716std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3717ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3718 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3719 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3720 Mod.NumPreprocessedEntities);
3721
3722 return std::make_pair(PreprocessingRecord::iterator(),
3723 PreprocessingRecord::iterator());
3724}
3725
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003726std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3727ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3728 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3729 ModuleDeclIterator(this, &Mod,
3730 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3731}
3732
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003733PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3734 PreprocessedEntityID PPID = Index+1;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003735 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3736 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003737 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003738 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor92a96f52011-02-08 21:58:10 +00003739
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003740 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003741 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003742
3743 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3744 switch (Code) {
3745 case llvm::bitc::END_BLOCK:
3746 return 0;
3747
3748 case llvm::bitc::ENTER_SUBBLOCK:
3749 Error("unexpected subblock record in preprocessor detail block");
3750 return 0;
3751
3752 case llvm::bitc::DEFINE_ABBREV:
3753 Error("unexpected abbrevation record in preprocessor detail block");
3754 return 0;
3755
3756 default:
3757 break;
3758 }
3759
3760 if (!PP.getPreprocessingRecord()) {
3761 Error("no preprocessing record");
3762 return 0;
3763 }
3764
3765 // Read the record.
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003766 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3767 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003768 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3769 const char *BlobStart = 0;
3770 unsigned BlobLen = 0;
3771 RecordData Record;
3772 PreprocessorDetailRecordTypes RecType =
3773 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3774 Code, Record, BlobStart, BlobLen);
3775 switch (RecType) {
3776 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003777 bool isBuiltin = Record[0];
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003778 IdentifierInfo *Name = 0;
3779 MacroDefinition *Def = 0;
3780 if (isBuiltin)
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003781 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003782 else {
3783 PreprocessedEntityID
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003784 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003785 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3786 }
3787
3788 MacroExpansion *ME;
3789 if (isBuiltin)
3790 ME = new (PPRec) MacroExpansion(Name, Range);
3791 else
3792 ME = new (PPRec) MacroExpansion(Def, Range);
3793
3794 return ME;
3795 }
3796
3797 case PPD_MACRO_DEFINITION: {
3798 // Decode the identifier info and then check again; if the macro is
3799 // still defined and associated with the identifier,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003800 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003801 MacroDefinition *MD
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003802 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003803
3804 if (DeserializationListener)
3805 DeserializationListener->MacroDefinitionRead(PPID, MD);
3806
3807 return MD;
3808 }
3809
3810 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003811 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00003812 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3813 const FileEntry *File = 0;
3814 if (!FullFileName.empty())
3815 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003816
3817 // FIXME: Stable encoding
3818 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003819 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003820 InclusionDirective *ID
3821 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003822 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00003823 Record[1], Record[3],
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003824 File,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003825 Range);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003826 return ID;
3827 }
3828 }
David Blaikie8a40f702012-01-17 06:56:22 +00003829
3830 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregoraae92242010-03-19 21:51:54 +00003831}
3832
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003833/// \brief \arg SLocMapI points at a chunk of a module that contains no
3834/// preprocessed entities or the entities it contains are not the ones we are
3835/// looking for. Find the next module that contains entities and return the ID
3836/// of the first entry.
3837PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3838 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3839 ++SLocMapI;
3840 for (GlobalSLocOffsetMapType::const_iterator
3841 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00003842 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003843 if (M.NumPreprocessedEntities)
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003844 return M.BasePreprocessedEntityID;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003845 }
3846
3847 return getTotalNumPreprocessedEntities();
3848}
3849
3850namespace {
3851
3852template <unsigned PPEntityOffset::*PPLoc>
3853struct PPEntityComp {
3854 const ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003855 ModuleFile &M;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003856
Douglas Gregorde3ef502011-11-30 23:21:26 +00003857 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003858
Benjamin Kramer5ce7f102011-09-21 06:42:26 +00003859 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3860 SourceLocation LHS = getLoc(L);
3861 SourceLocation RHS = getLoc(R);
3862 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3863 }
3864
3865 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003866 SourceLocation LHS = getLoc(L);
3867 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3868 }
3869
Benjamin Kramer5ce7f102011-09-21 06:42:26 +00003870 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003871 SourceLocation RHS = getLoc(R);
3872 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3873 }
3874
3875 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3876 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3877 }
3878};
3879
3880}
3881
3882/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3883PreprocessedEntityID
3884ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3885 if (SourceMgr.isLocalSourceLocation(BLoc))
3886 return getTotalNumPreprocessedEntities();
3887
3888 GlobalSLocOffsetMapType::const_iterator
3889 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3890 BLoc.getOffset());
3891 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3892 "Corrupted global sloc offset map");
3893
3894 if (SLocMapI->second->NumPreprocessedEntities == 0)
3895 return findNextPreprocessedEntity(SLocMapI);
3896
Douglas Gregorde3ef502011-11-30 23:21:26 +00003897 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003898 typedef const PPEntityOffset *pp_iterator;
3899 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3900 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +00003901
3902 size_t Count = M.NumPreprocessedEntities;
3903 size_t Half;
3904 pp_iterator First = pp_begin;
3905 pp_iterator PPI;
3906
3907 // Do a binary search manually instead of using std::lower_bound because
3908 // The end locations of entities may be unordered (when a macro expansion
3909 // is inside another macro argument), but for this case it is not important
3910 // whether we get the first macro expansion or its containing macro.
3911 while (Count > 0) {
3912 Half = Count/2;
3913 PPI = First;
3914 std::advance(PPI, Half);
3915 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3916 BLoc)){
3917 First = PPI;
3918 ++First;
3919 Count = Count - Half - 1;
3920 } else
3921 Count = Half;
3922 }
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003923
3924 if (PPI == pp_end)
3925 return findNextPreprocessedEntity(SLocMapI);
3926
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003927 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003928}
3929
3930/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3931PreprocessedEntityID
3932ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3933 if (SourceMgr.isLocalSourceLocation(ELoc))
3934 return getTotalNumPreprocessedEntities();
3935
3936 GlobalSLocOffsetMapType::const_iterator
3937 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3938 ELoc.getOffset());
3939 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3940 "Corrupted global sloc offset map");
3941
3942 if (SLocMapI->second->NumPreprocessedEntities == 0)
3943 return findNextPreprocessedEntity(SLocMapI);
3944
Douglas Gregorde3ef502011-11-30 23:21:26 +00003945 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003946 typedef const PPEntityOffset *pp_iterator;
3947 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3948 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3949 pp_iterator PPI =
3950 std::upper_bound(pp_begin, pp_end, ELoc,
3951 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3952
3953 if (PPI == pp_end)
3954 return findNextPreprocessedEntity(SLocMapI);
3955
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003956 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003957}
3958
3959/// \brief Returns a pair of [Begin, End) indices of preallocated
3960/// preprocessed entities that \arg Range encompasses.
3961std::pair<unsigned, unsigned>
3962 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3963 if (Range.isInvalid())
3964 return std::make_pair(0,0);
3965 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3966
3967 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3968 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3969 return std::make_pair(BeginID, EndID);
3970}
3971
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003972/// \brief Optionally returns true or false if the preallocated preprocessed
3973/// entity with index \arg Index came from file \arg FID.
3974llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3975 FileID FID) {
3976 if (FID.isInvalid())
3977 return false;
3978
Douglas Gregorde3ef502011-11-30 23:21:26 +00003979 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3980 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003981 unsigned LocalIndex = PPInfo.second;
3982 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3983
3984 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3985 if (Loc.isInvalid())
3986 return false;
3987
3988 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3989 return true;
3990 else
3991 return false;
3992}
3993
Douglas Gregor69e94642011-08-25 18:14:34 +00003994namespace {
3995 /// \brief Visitor used to search for information about a header file.
3996 class HeaderFileInfoVisitor {
3997 ASTReader &Reader;
3998 const FileEntry *FE;
3999
4000 llvm::Optional<HeaderFileInfo> HFI;
4001
4002 public:
4003 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4004 : Reader(Reader), FE(FE) { }
4005
Douglas Gregorde3ef502011-11-30 23:21:26 +00004006 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor69e94642011-08-25 18:14:34 +00004007 HeaderFileInfoVisitor *This
4008 = static_cast<HeaderFileInfoVisitor *>(UserData);
4009
4010 HeaderFileInfoTrait Trait(This->Reader, M,
4011 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4012 M.HeaderFileFrameworkStrings,
4013 This->FE->getName());
4014
4015 HeaderFileInfoLookupTable *Table
4016 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4017 if (!Table)
4018 return false;
4019
4020 // Look in the on-disk hash table for an entry for this file name.
4021 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4022 &Trait);
4023 if (Pos == Table->end())
4024 return false;
4025
4026 This->HFI = *Pos;
4027 return true;
4028 }
4029
4030 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4031 };
4032}
4033
Douglas Gregor09b69892011-02-10 17:09:37 +00004034HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregor69e94642011-08-25 18:14:34 +00004035 HeaderFileInfoVisitor Visitor(*this, FE);
4036 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4037 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregor09b69892011-02-10 17:09:37 +00004038 if (Listener)
Douglas Gregor69e94642011-08-25 18:14:34 +00004039 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4040 return *HFI;
Douglas Gregor09b69892011-02-10 17:09:37 +00004041 }
4042
4043 return HeaderFileInfo();
4044}
4045
David Blaikie9c902b52011-09-25 23:23:43 +00004046void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004047 // FIXME: Make it work properly with modules.
4048 llvm::SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004049 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00004050 ModuleFile &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00004051 unsigned Idx = 0;
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004052 DiagStates.clear();
4053 assert(!Diag.DiagStates.empty());
4054 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
Douglas Gregor925296b2011-07-19 16:10:42 +00004055 while (Idx < F.PragmaDiagMappings.size()) {
4056 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004057 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4058 if (DiagStateID != 0) {
4059 Diag.DiagStatePoints.push_back(
4060 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4061 FullSourceLoc(Loc, SourceMgr)));
4062 continue;
4063 }
4064
4065 assert(DiagStateID == 0);
4066 // A new DiagState was created here.
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004067 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004068 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4069 DiagStates.push_back(NewState);
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004070 Diag.DiagStatePoints.push_back(
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004071 DiagnosticsEngine::DiagStatePoint(NewState,
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004072 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregor925296b2011-07-19 16:10:42 +00004073 while (1) {
4074 assert(Idx < F.PragmaDiagMappings.size() &&
4075 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4076 if (Idx >= F.PragmaDiagMappings.size()) {
4077 break; // Something is messed up but at least avoid infinite loop in
4078 // release build.
4079 }
4080 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4081 if (DiagID == (unsigned)-1) {
4082 break; // no more diag/map pairs for this location.
4083 }
4084 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004085 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4086 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregor925296b2011-07-19 16:10:42 +00004087 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00004088 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00004089 }
4090}
4091
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004092/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004093ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004094 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00004095 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00004096 ModuleFile *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00004097 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004098}
4099
4100/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004101///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004102/// The index is the type ID, shifted and minus the number of predefs. This
4103/// routine actually reads the record corresponding to the type at the given
4104/// location. It is a helper routine for GetType, which deals with reading type
4105/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00004106QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004107 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004108 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00004109
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004110 // Keep track of where we are in the stream, then jump back there
4111 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00004112 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004113
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004114 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004115
Douglas Gregor1342e842009-07-06 18:54:52 +00004116 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004117 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00004118
Douglas Gregor903b7e92011-07-22 00:38:23 +00004119 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00004120 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004121 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004122 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00004123 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4124 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004125 if (Record.size() != 2) {
4126 Error("Incorrect encoding of extended qualifier type");
4127 return QualType();
4128 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004129 QualType Base = readType(*Loc.F, Record, Idx);
4130 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004131 return Context.getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00004132 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004133
Sebastian Redl539c5062010-08-18 23:57:32 +00004134 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004135 if (Record.size() != 1) {
4136 Error("Incorrect encoding of complex type");
4137 return QualType();
4138 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004139 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004140 return Context.getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004141 }
4142
Sebastian Redl539c5062010-08-18 23:57:32 +00004143 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004144 if (Record.size() != 1) {
4145 Error("Incorrect encoding of pointer type");
4146 return QualType();
4147 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004148 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004149 return Context.getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004150 }
4151
Sebastian Redl539c5062010-08-18 23:57:32 +00004152 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004153 if (Record.size() != 1) {
4154 Error("Incorrect encoding of block pointer type");
4155 return QualType();
4156 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004157 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004158 return Context.getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004159 }
4160
Sebastian Redl539c5062010-08-18 23:57:32 +00004161 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00004162 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004163 Error("Incorrect encoding of lvalue reference type");
4164 return QualType();
4165 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004166 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004167 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004168 }
4169
Sebastian Redl539c5062010-08-18 23:57:32 +00004170 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004171 if (Record.size() != 1) {
4172 Error("Incorrect encoding of rvalue reference type");
4173 return QualType();
4174 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004175 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004176 return Context.getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004177 }
4178
Sebastian Redl539c5062010-08-18 23:57:32 +00004179 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00004180 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004181 Error("Incorrect encoding of member pointer type");
4182 return QualType();
4183 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004184 QualType PointeeType = readType(*Loc.F, Record, Idx);
4185 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004186 if (PointeeType.isNull() || ClassType.isNull())
4187 return QualType();
4188
Douglas Gregor4163aca2011-09-09 21:34:22 +00004189 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004190 }
4191
Sebastian Redl539c5062010-08-18 23:57:32 +00004192 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004193 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004194 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4195 unsigned IndexTypeQuals = Record[2];
4196 unsigned Idx = 3;
4197 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004198 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor04318252009-07-06 15:59:29 +00004199 ASM, IndexTypeQuals);
4200 }
4201
Sebastian Redl539c5062010-08-18 23:57:32 +00004202 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004203 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004204 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4205 unsigned IndexTypeQuals = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004206 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004207 }
4208
Sebastian Redl539c5062010-08-18 23:57:32 +00004209 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004210 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004211 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4212 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00004213 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4214 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004215 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00004216 ASM, IndexTypeQuals,
4217 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004218 }
4219
Sebastian Redl539c5062010-08-18 23:57:32 +00004220 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00004221 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004222 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004223 return QualType();
4224 }
4225
Douglas Gregor903b7e92011-07-22 00:38:23 +00004226 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004227 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00004228 unsigned VecKind = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004229 return Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00004230 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004231 }
4232
Sebastian Redl539c5062010-08-18 23:57:32 +00004233 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00004234 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004235 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004236 return QualType();
4237 }
4238
Douglas Gregor903b7e92011-07-22 00:38:23 +00004239 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004240 unsigned NumElements = Record[1];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004241 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004242 }
4243
Sebastian Redl539c5062010-08-18 23:57:32 +00004244 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00004245 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004246 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004247 return QualType();
4248 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004249 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00004250 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4251 (CallingConv)Record[4], Record[5]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004252 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004253 }
4254
Sebastian Redl539c5062010-08-18 23:57:32 +00004255 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004256 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00004257
4258 FunctionProtoType::ExtProtoInfo EPI;
4259 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00004260 /*hasregparm*/ Record[2],
4261 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00004262 static_cast<CallingConv>(Record[4]),
4263 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00004264
John McCall31168b02011-06-15 23:02:42 +00004265 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004266 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004267 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004268 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00004269 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00004270
4271 EPI.Variadic = Record[Idx++];
Richard Smith5e580292012-02-10 09:58:53 +00004272 EPI.HasTrailingReturn = Record[Idx++];
John McCalldb40c7f2010-12-14 08:05:40 +00004273 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00004274 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004275 ExceptionSpecificationType EST =
4276 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4277 EPI.ExceptionSpecType = EST;
Douglas Gregor6a377842012-04-04 00:34:49 +00004278 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004279 if (EST == EST_Dynamic) {
4280 EPI.NumExceptions = Record[Idx++];
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004281 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00004282 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004283 EPI.Exceptions = Exceptions.data();
4284 } else if (EST == EST_ComputedNoexcept) {
4285 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith8b987a92012-04-21 17:47:47 +00004286 } else if (EST == EST_Uninstantiated) {
4287 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4288 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithd3b5c9082012-07-27 04:22:15 +00004289 } else if (EST == EST_Unevaluated) {
4290 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004291 }
Douglas Gregor4163aca2011-09-09 21:34:22 +00004292 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00004293 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004294 }
4295
Douglas Gregor7fb09192011-07-21 22:35:25 +00004296 case TYPE_UNRESOLVED_USING: {
4297 unsigned Idx = 0;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004298 return Context.getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00004299 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4300 }
4301
Sebastian Redl539c5062010-08-18 23:57:32 +00004302 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004303 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004304 Error("incorrect encoding of typedef type");
4305 return QualType();
4306 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004307 unsigned Idx = 0;
4308 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004309 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00004310 if (!Canonical.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004311 Canonical = Context.getCanonicalType(Canonical);
4312 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004313 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004314
Sebastian Redl539c5062010-08-18 23:57:32 +00004315 case TYPE_TYPEOF_EXPR:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004316 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004317
Sebastian Redl539c5062010-08-18 23:57:32 +00004318 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004319 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004320 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004321 return QualType();
4322 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004323 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004324 return Context.getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004325 }
Mike Stump11289f42009-09-09 15:08:12 +00004326
Douglas Gregor81495f32012-02-12 18:42:33 +00004327 case TYPE_DECLTYPE: {
4328 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4329 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4330 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00004331
Alexis Hunte852b102011-05-24 22:41:36 +00004332 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004333 QualType BaseType = readType(*Loc.F, Record, Idx);
4334 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00004335 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004336 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Alexis Hunte852b102011-05-24 22:41:36 +00004337 }
4338
Richard Smith30482bc2011-02-20 03:19:35 +00004339 case TYPE_AUTO:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004340 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00004341
Sebastian Redl539c5062010-08-18 23:57:32 +00004342 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004343 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004344 Error("incorrect encoding of record type");
4345 return QualType();
4346 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004347 unsigned Idx = 0;
4348 bool IsDependent = Record[Idx++];
Douglas Gregorf3bccd72012-01-17 19:21:53 +00004349 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4350 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4351 QualType T = Context.getRecordType(RD);
John McCall424cec92011-01-19 06:33:43 +00004352 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004353 return T;
4354 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004355
Sebastian Redl539c5062010-08-18 23:57:32 +00004356 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004357 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004358 Error("incorrect encoding of enum type");
4359 return QualType();
4360 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004361 unsigned Idx = 0;
4362 bool IsDependent = Record[Idx++];
4363 QualType T
Douglas Gregor4163aca2011-09-09 21:34:22 +00004364 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00004365 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004366 return T;
4367 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00004368
John McCall81904512011-01-06 01:58:22 +00004369 case TYPE_ATTRIBUTED: {
4370 if (Record.size() != 3) {
4371 Error("incorrect encoding of attributed type");
4372 return QualType();
4373 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004374 QualType modifiedType = readType(*Loc.F, Record, Idx);
4375 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00004376 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004377 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall81904512011-01-06 01:58:22 +00004378 }
4379
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004380 case TYPE_PAREN: {
4381 if (Record.size() != 1) {
4382 Error("incorrect encoding of paren type");
4383 return QualType();
4384 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004385 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004386 return Context.getParenType(InnerType);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004387 }
4388
Douglas Gregord2fa7662010-12-20 02:24:11 +00004389 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00004390 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00004391 Error("incorrect encoding of pack expansion type");
4392 return QualType();
4393 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004394 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00004395 if (Pattern.isNull())
4396 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00004397 llvm::Optional<unsigned> NumExpansions;
4398 if (Record[1])
4399 NumExpansions = Record[1] - 1;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004400 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00004401 }
4402
Sebastian Redl539c5062010-08-18 23:57:32 +00004403 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004404 unsigned Idx = 0;
4405 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004406 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004407 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004408 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00004409 }
4410
Sebastian Redl539c5062010-08-18 23:57:32 +00004411 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00004412 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004413 ObjCInterfaceDecl *ItfD
4414 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregorf3bccd72012-01-17 19:21:53 +00004415 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCall8b07ec22010-05-15 11:32:37 +00004416 }
4417
Sebastian Redl539c5062010-08-18 23:57:32 +00004418 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00004419 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004420 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00004421 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004422 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00004423 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00004424 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00004425 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00004426 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004427
Sebastian Redl539c5062010-08-18 23:57:32 +00004428 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00004429 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004430 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004431 return Context.getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00004432 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00004433
Sebastian Redl539c5062010-08-18 23:57:32 +00004434 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00004435 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004436 QualType Parm = readType(*Loc.F, Record, Idx);
4437 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00004438 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00004439 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCallcebee162009-10-18 09:09:24 +00004440 Replacement);
4441 }
John McCalle78aac42010-03-10 03:28:59 +00004442
Douglas Gregorada4b792011-01-14 02:55:32 +00004443 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4444 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004445 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00004446 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004447 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorada4b792011-01-14 02:55:32 +00004448 cast<TemplateTypeParmType>(Parm),
4449 ArgPack);
4450 }
4451
Sebastian Redl539c5062010-08-18 23:57:32 +00004452 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00004453 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004454 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00004455 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004456 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00004457 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00004458 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00004459 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004460
Sebastian Redl539c5062010-08-18 23:57:32 +00004461 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004462 unsigned Idx = 0;
4463 unsigned Depth = Record[Idx++];
4464 unsigned Index = Record[Idx++];
4465 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004466 TemplateTypeParmDecl *D
4467 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004468 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004469 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004470
Sebastian Redl539c5062010-08-18 23:57:32 +00004471 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00004472 unsigned Idx = 0;
4473 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004474 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00004475 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004476 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00004477 if (!Canon.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004478 Canon = Context.getCanonicalType(Canon);
4479 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00004480 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004481
Sebastian Redl539c5062010-08-18 23:57:32 +00004482 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004483 unsigned Idx = 0;
4484 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004485 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00004486 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004487 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004488 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004489 Args.reserve(NumArgs);
4490 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004491 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00004492 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004493 Args.size(), Args.data());
4494 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004495
Sebastian Redl539c5062010-08-18 23:57:32 +00004496 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004497 unsigned Idx = 0;
4498
4499 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00004500 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004501 ArrayType::ArraySizeModifier ASM
4502 = (ArrayType::ArraySizeModifier)Record[Idx++];
4503 unsigned IndexTypeQuals = Record[Idx++];
4504
4505 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00004506 Expr *NumElts = ReadExpr(*Loc.F);
4507 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004508
Douglas Gregor4163aca2011-09-09 21:34:22 +00004509 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004510 IndexTypeQuals, Brackets);
4511 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004512
Sebastian Redl539c5062010-08-18 23:57:32 +00004513 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004514 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004515 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00004516 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004517 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00004518 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004519 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004520 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00004521 if (Underlying.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004522 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004523 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004524 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00004525 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00004526 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00004527 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004528 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004529 }
Eli Friedman0dfb8892011-10-06 23:00:33 +00004530
4531 case TYPE_ATOMIC: {
4532 if (Record.size() != 1) {
4533 Error("Incorrect encoding of atomic type");
4534 return QualType();
4535 }
4536 QualType ValueType = readType(*Loc.F, Record, Idx);
4537 return Context.getAtomicType(ValueType);
4538 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004539 }
David Blaikie8a40f702012-01-17 06:56:22 +00004540 llvm_unreachable("Invalid TypeCode!");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004541}
4542
Sebastian Redl2c373b92010-10-05 15:59:54 +00004543class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00004544 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00004545 ModuleFile &F;
Sebastian Redl2c499f62010-08-18 23:56:43 +00004546 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00004547 unsigned &Idx;
4548
Sebastian Redl2c373b92010-10-05 15:59:54 +00004549 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4550 unsigned &I) {
4551 return Reader.ReadSourceLocation(F, R, I);
4552 }
4553
Douglas Gregor7fb09192011-07-21 22:35:25 +00004554 template<typename T>
4555 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4556 return Reader.ReadDeclAs<T>(F, Record, Idx);
4557 }
4558
John McCall8f115c62009-10-16 21:56:05 +00004559public:
Douglas Gregorde3ef502011-11-30 23:21:26 +00004560 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00004561 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00004562 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004563 { }
John McCall8f115c62009-10-16 21:56:05 +00004564
John McCall17001972009-10-18 01:05:36 +00004565 // We want compile-time assurance that we've enumerated all of
4566 // these, so unfortunately we have to declare them first, then
4567 // define them out-of-line.
4568#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00004569#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00004570 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00004571#include "clang/AST/TypeLocNodes.def"
4572
John McCall17001972009-10-18 01:05:36 +00004573 void VisitFunctionTypeLoc(FunctionTypeLoc);
4574 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00004575};
4576
John McCall17001972009-10-18 01:05:36 +00004577void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00004578 // nothing to do
4579}
John McCall17001972009-10-18 01:05:36 +00004580void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004581 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00004582 if (TL.needsExtraLocalData()) {
4583 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4584 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4585 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4586 TL.setModeAttr(Record[Idx++]);
4587 }
John McCall8f115c62009-10-16 21:56:05 +00004588}
John McCall17001972009-10-18 01:05:36 +00004589void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004590 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004591}
John McCall17001972009-10-18 01:05:36 +00004592void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004593 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004594}
John McCall17001972009-10-18 01:05:36 +00004595void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004596 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004597}
John McCall17001972009-10-18 01:05:36 +00004598void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004599 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004600}
John McCall17001972009-10-18 01:05:36 +00004601void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004602 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004603}
John McCall17001972009-10-18 01:05:36 +00004604void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004605 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00004606 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004607}
John McCall17001972009-10-18 01:05:36 +00004608void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004609 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4610 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004611 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00004612 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004613 else
John McCall17001972009-10-18 01:05:36 +00004614 TL.setSizeExpr(0);
4615}
4616void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4617 VisitArrayTypeLoc(TL);
4618}
4619void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4620 VisitArrayTypeLoc(TL);
4621}
4622void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4623 VisitArrayTypeLoc(TL);
4624}
4625void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4626 DependentSizedArrayTypeLoc TL) {
4627 VisitArrayTypeLoc(TL);
4628}
4629void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4630 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004631 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004632}
4633void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004634 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004635}
4636void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004637 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004638}
4639void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004640 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004641 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4642 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004643 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004644 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00004645 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004646 }
4647}
4648void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4649 VisitFunctionTypeLoc(TL);
4650}
4651void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4652 VisitFunctionTypeLoc(TL);
4653}
John McCallb96ec562009-12-04 22:46:56 +00004654void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004655 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00004656}
John McCall17001972009-10-18 01:05:36 +00004657void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004658 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004659}
4660void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004661 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4662 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4663 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004664}
4665void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004666 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4667 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4668 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4669 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004670}
4671void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004672 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004673}
Alexis Hunte852b102011-05-24 22:41:36 +00004674void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4675 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4676 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4677 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4678 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4679}
Richard Smith30482bc2011-02-20 03:19:35 +00004680void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4681 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4682}
John McCall17001972009-10-18 01:05:36 +00004683void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004684 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004685}
4686void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004687 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004688}
John McCall81904512011-01-06 01:58:22 +00004689void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4690 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4691 if (TL.hasAttrOperand()) {
4692 SourceRange range;
4693 range.setBegin(ReadSourceLocation(Record, Idx));
4694 range.setEnd(ReadSourceLocation(Record, Idx));
4695 TL.setAttrOperandParensRange(range);
4696 }
4697 if (TL.hasAttrExprOperand()) {
4698 if (Record[Idx++])
4699 TL.setAttrExprOperand(Reader.ReadExpr(F));
4700 else
4701 TL.setAttrExprOperand(0);
4702 } else if (TL.hasAttrEnumOperand())
4703 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4704}
John McCall17001972009-10-18 01:05:36 +00004705void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004706 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004707}
John McCallcebee162009-10-18 09:09:24 +00004708void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4709 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004710 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00004711}
Douglas Gregorada4b792011-01-14 02:55:32 +00004712void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4713 SubstTemplateTypeParmPackTypeLoc TL) {
4714 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4715}
John McCall17001972009-10-18 01:05:36 +00004716void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4717 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004718 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004719 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4720 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4721 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00004722 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4723 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004724 Reader.GetTemplateArgumentLocInfo(F,
4725 TL.getTypePtr()->getArg(i).getKind(),
4726 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004727}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004728void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4729 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4730 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4731}
Abramo Bagnara6150c882010-05-11 21:36:43 +00004732void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +00004733 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00004734 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004735}
John McCalle78aac42010-03-10 03:28:59 +00004736void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004737 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00004738}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00004739void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +00004740 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00004741 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004742 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004743}
John McCallc392f372010-06-11 00:33:02 +00004744void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4745 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004746 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00004747 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +00004748 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004749 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004750 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4751 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00004752 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4753 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004754 Reader.GetTemplateArgumentLocInfo(F,
4755 TL.getTypePtr()->getArg(I).getKind(),
4756 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00004757}
Douglas Gregord2fa7662010-12-20 02:24:11 +00004758void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4759 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4760}
John McCall17001972009-10-18 01:05:36 +00004761void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004762 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00004763}
4764void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4765 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004766 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4767 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004768 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004769 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004770}
John McCallfc93cf92009-10-22 22:37:11 +00004771void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004772 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00004773}
Eli Friedman0dfb8892011-10-06 23:00:33 +00004774void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4775 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4776 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4777 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4778}
John McCall8f115c62009-10-16 21:56:05 +00004779
Douglas Gregorde3ef502011-11-30 23:21:26 +00004780TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004781 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00004782 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004783 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00004784 if (InfoTy.isNull())
4785 return 0;
4786
Douglas Gregor4163aca2011-09-09 21:34:22 +00004787 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004788 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00004789 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00004790 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00004791 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00004792}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004793
Sebastian Redl539c5062010-08-18 23:57:32 +00004794QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00004795 unsigned FastQuals = ID & Qualifiers::FastMask;
4796 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004797
Sebastian Redl539c5062010-08-18 23:57:32 +00004798 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004799 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00004800 switch ((PredefinedTypeIDs)Index) {
4801 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor4163aca2011-09-09 21:34:22 +00004802 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4803 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004804
Sebastian Redl539c5062010-08-18 23:57:32 +00004805 case PREDEF_TYPE_CHAR_U_ID:
4806 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004807 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor4163aca2011-09-09 21:34:22 +00004808 T = Context.CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004809 break;
4810
Douglas Gregor4163aca2011-09-09 21:34:22 +00004811 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4812 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4813 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4814 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4815 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4816 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4817 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4818 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4819 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4820 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4821 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4822 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4823 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004824 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004825 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4826 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4827 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4828 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4829 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall526ab472011-10-25 17:37:35 +00004830 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004831 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4832 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4833 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4834 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4835 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4836 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4837 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4838 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4839 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00004840
4841 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004842 T = Context.getAutoRRefDeductType();
Douglas Gregoreda8e122011-08-09 15:13:55 +00004843 break;
John McCall8a6b59a2011-10-17 18:09:15 +00004844
4845 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4846 T = Context.ARCUnbridgedCastTy;
4847 break;
4848
Meador Ingecfb60902012-07-01 15:57:25 +00004849 case PREDEF_TYPE_VA_LIST_TAG:
4850 T = Context.getVaListTagType();
4851 break;
Eli Friedman34866c72012-08-31 00:14:07 +00004852
4853 case PREDEF_TYPE_BUILTIN_FN:
4854 T = Context.BuiltinFnTy;
4855 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004856 }
4857
4858 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00004859 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004860 }
4861
Sebastian Redl539c5062010-08-18 23:57:32 +00004862 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004863 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00004864 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004865 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004866 if (TypesLoaded[Index].isNull())
4867 return QualType();
4868
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004869 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004870 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004871 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004872 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00004873 }
Mike Stump11289f42009-09-09 15:08:12 +00004874
John McCall8ccfcb52009-09-24 19:53:00 +00004875 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004876}
4877
Douglas Gregorde3ef502011-11-30 23:21:26 +00004878QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004879 return GetType(getGlobalTypeID(F, LocalID));
4880}
4881
4882serialization::TypeID
Douglas Gregorde3ef502011-11-30 23:21:26 +00004883ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004884 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4885 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4886
4887 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4888 return LocalID;
4889
4890 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4891 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4892 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4893
4894 unsigned GlobalIndex = LocalIndex + I->second;
4895 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4896}
4897
John McCall0ad16662009-10-29 08:12:44 +00004898TemplateArgumentLocInfo
Douglas Gregorde3ef502011-11-30 23:21:26 +00004899ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004900 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00004901 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004902 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00004903 switch (Kind) {
4904 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004905 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00004906 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004907 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004908 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004909 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4910 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004911 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004912 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004913 SourceLocation());
4914 }
4915 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004916 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4917 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004918 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004919 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004920 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00004921 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004922 }
John McCall0ad16662009-10-29 08:12:44 +00004923 case TemplateArgument::Null:
4924 case TemplateArgument::Integral:
4925 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004926 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004927 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004928 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004929 return TemplateArgumentLocInfo();
4930 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004931 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00004932}
4933
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004934TemplateArgumentLoc
Douglas Gregorde3ef502011-11-30 23:21:26 +00004935ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004936 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004937 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004938
4939 if (Arg.getKind() == TemplateArgument::Expression) {
4940 if (Record[Index++]) // bool InfoHasSameExpr.
4941 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4942 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004943 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004944 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004945}
4946
Sebastian Redl2c499f62010-08-18 23:56:43 +00004947Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004948 return GetDecl(ID);
4949}
4950
Douglas Gregorde3ef502011-11-30 23:21:26 +00004951uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregorc27b2872011-08-04 00:01:48 +00004952 unsigned &Idx){
4953 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004954 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004955
Douglas Gregorc27b2872011-08-04 00:01:48 +00004956 unsigned LocalID = Record[Idx++];
4957 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004958}
4959
4960CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00004961 RecordLocation Loc = getLocalBitOffset(Offset);
4962 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004963 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00004964 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004965 ReadingKindTracker ReadingKind(Read_Decl, *this);
4966 RecordData Record;
4967 unsigned Code = Cursor.ReadCode();
4968 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4969 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4970 Error("Malformed AST file: missing C++ base specifiers");
4971 return 0;
4972 }
4973
4974 unsigned Idx = 0;
4975 unsigned NumBases = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004976 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004977 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4978 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00004979 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004980 return Bases;
4981}
4982
Douglas Gregor7fb09192011-07-21 22:35:25 +00004983serialization::DeclID
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00004984ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004985 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00004986 return LocalID;
4987
4988 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00004989 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00004990 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4991
4992 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004993}
4994
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004995bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregorde3ef502011-11-30 23:21:26 +00004996 ModuleFile &M) const {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004997 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4998 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4999 return &M == I->second;
5000}
5001
Douglas Gregor404cdde2012-01-27 01:47:08 +00005002ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5003 if (!D->isFromASTFile())
5004 return 0;
5005 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5006 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5007 return I->second;
5008}
5009
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005010SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5011 if (ID < NUM_PREDEF_DECL_IDS)
5012 return SourceLocation();
5013
5014 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5015
5016 if (Index > DeclsLoaded.size()) {
5017 Error("declaration ID out-of-range for AST file");
5018 return SourceLocation();
5019 }
5020
5021 if (Decl *D = DeclsLoaded[Index])
5022 return D->getLocation();
5023
5024 unsigned RawLocation = 0;
5025 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5026 return ReadSourceLocation(*Rec.F, RawLocation);
5027}
5028
Sebastian Redl539c5062010-08-18 23:57:32 +00005029Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005030 if (ID < NUM_PREDEF_DECL_IDS) {
5031 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00005032 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005033 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00005034
5035 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005036 return Context.getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00005037
5038 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005039 return Context.getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00005040
Douglas Gregor52e02802011-08-12 06:17:30 +00005041 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005042 return Context.getObjCSelDecl();
Douglas Gregor52e02802011-08-12 06:17:30 +00005043
Douglas Gregor0a586182011-08-12 05:59:41 +00005044 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005045 return Context.getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00005046
Douglas Gregord53ae832012-01-17 18:09:05 +00005047 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5048 return Context.getObjCProtocolDecl();
5049
Douglas Gregor801c99d2011-08-12 06:49:56 +00005050 case PREDEF_DECL_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005051 return Context.getInt128Decl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00005052
5053 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005054 return Context.getUInt128Decl();
Douglas Gregorbab8a962011-09-08 01:46:34 +00005055
5056 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005057 return Context.getObjCInstanceTypeDecl();
Meador Inge5d3fb222012-06-16 03:34:49 +00005058
5059 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5060 return Context.getBuiltinVaListDecl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005061 }
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005062 }
5063
Douglas Gregordab42432011-08-12 00:15:20 +00005064 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5065
Richard Smithce3ad9a2011-12-20 04:39:57 +00005066 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005067 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005068 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005069 return 0;
Douglas Gregor745ed142009-04-25 18:35:21 +00005070 }
Douglas Gregordab42432011-08-12 00:15:20 +00005071
Douglas Gregor81252352011-12-16 22:37:11 +00005072 if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00005073 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005074 if (DeserializationListener)
5075 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5076 }
Douglas Gregor745ed142009-04-25 18:35:21 +00005077
5078 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005079}
5080
Douglas Gregor05f10352011-12-17 23:38:30 +00005081DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5082 DeclID GlobalID) {
5083 if (GlobalID < NUM_PREDEF_DECL_IDS)
5084 return GlobalID;
5085
5086 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5087 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5088 ModuleFile *Owner = I->second;
5089
5090 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5091 = M.GlobalToLocalDeclIDs.find(Owner);
5092 if (Pos == M.GlobalToLocalDeclIDs.end())
5093 return 0;
5094
5095 return GlobalID - Owner->BaseDeclID + Pos->second;
5096}
5097
Douglas Gregorde3ef502011-11-30 23:21:26 +00005098serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005099 const RecordData &Record,
5100 unsigned &Idx) {
5101 if (Idx >= Record.size()) {
5102 Error("Corrupted AST file");
5103 return 0;
5104 }
5105
5106 return getGlobalDeclID(F, Record[Idx++]);
5107}
5108
Chris Lattner9c28af02009-04-27 05:46:25 +00005109/// \brief Resolve the offset of a statement into a statement.
5110///
5111/// This operation will read a new statement from the external
5112/// source each time it is called, and is meant to be used via a
5113/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00005114Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005115 // Switch case IDs are per Decl.
5116 ClearSwitchCaseIDs();
5117
Sebastian Redl5c415f32010-07-22 17:01:13 +00005118 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00005119 RecordLocation Loc = getLocalBitOffset(Offset);
5120 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5121 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00005122}
5123
Douglas Gregor1257f972011-08-24 21:27:34 +00005124namespace {
5125 class FindExternalLexicalDeclsVisitor {
5126 ASTReader &Reader;
5127 const DeclContext *DC;
5128 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00005129
Douglas Gregor1257f972011-08-24 21:27:34 +00005130 SmallVectorImpl<Decl*> &Decls;
5131 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5132
5133 public:
5134 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5135 bool (*isKindWeWant)(Decl::Kind),
5136 SmallVectorImpl<Decl*> &Decls)
5137 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5138 {
5139 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5140 PredefsVisited[I] = false;
5141 }
5142
Douglas Gregorde3ef502011-11-30 23:21:26 +00005143 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor1257f972011-08-24 21:27:34 +00005144 if (Preorder)
5145 return false;
5146
5147 FindExternalLexicalDeclsVisitor *This
5148 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5149
Douglas Gregorde3ef502011-11-30 23:21:26 +00005150 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor1257f972011-08-24 21:27:34 +00005151 = M.DeclContextInfos.find(This->DC);
5152 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5153 return false;
5154
5155 // Load all of the declaration IDs
5156 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5157 *IDE = ID + Info->second.NumLexicalDecls;
5158 ID != IDE; ++ID) {
5159 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5160 continue;
5161
5162 // Don't add predefined declarations to the lexical context more
5163 // than once.
5164 if (ID->second < NUM_PREDEF_DECL_IDS) {
5165 if (This->PredefsVisited[ID->second])
5166 continue;
5167
5168 This->PredefsVisited[ID->second] = true;
5169 }
5170
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00005171 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5172 if (!This->DC->isDeclInLexicalTraversal(D))
5173 This->Decls.push_back(D);
5174 }
Douglas Gregor1257f972011-08-24 21:27:34 +00005175 }
5176
5177 return false;
5178 }
5179 };
5180}
5181
Douglas Gregor3d0adb32011-07-15 21:46:17 +00005182ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00005183 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005184 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00005185 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor1257f972011-08-24 21:27:34 +00005186 // least. Walk all of the modules in the order they were loaded.
5187 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5188 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00005189 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00005190 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005191}
5192
Douglas Gregor94619c82011-08-24 19:03:07 +00005193namespace {
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005194
5195class DeclIDComp {
5196 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00005197 ModuleFile &Mod;
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005198
5199public:
Douglas Gregorde3ef502011-11-30 23:21:26 +00005200 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005201
5202 bool operator()(LocalDeclID L, LocalDeclID R) const {
5203 SourceLocation LHS = getLocation(L);
5204 SourceLocation RHS = getLocation(R);
5205 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5206 }
5207
5208 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5209 SourceLocation RHS = getLocation(R);
5210 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5211 }
5212
5213 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5214 SourceLocation LHS = getLocation(L);
5215 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5216 }
5217
5218 SourceLocation getLocation(LocalDeclID ID) const {
5219 return Reader.getSourceManager().getFileLoc(
5220 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5221 }
5222};
5223
5224}
5225
5226void ASTReader::FindFileRegionDecls(FileID File,
5227 unsigned Offset, unsigned Length,
5228 SmallVectorImpl<Decl *> &Decls) {
5229 SourceManager &SM = getSourceManager();
5230
5231 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5232 if (I == FileDeclIDs.end())
5233 return;
5234
5235 FileDeclsInfo &DInfo = I->second;
5236 if (DInfo.Decls.empty())
5237 return;
5238
5239 SourceLocation
5240 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5241 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5242
5243 DeclIDComp DIDComp(*this, *DInfo.Mod);
5244 ArrayRef<serialization::LocalDeclID>::iterator
5245 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5246 BeginLoc, DIDComp);
5247 if (BeginIt != DInfo.Decls.begin())
5248 --BeginIt;
5249
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00005250 // If we are pointing at a top-level decl inside an objc container, we need
5251 // to backtrack until we find it otherwise we will fail to report that the
5252 // region overlaps with an objc container.
5253 while (BeginIt != DInfo.Decls.begin() &&
5254 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5255 ->isTopLevelDeclInObjCContainer())
5256 --BeginIt;
5257
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005258 ArrayRef<serialization::LocalDeclID>::iterator
5259 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5260 EndLoc, DIDComp);
5261 if (EndIt != DInfo.Decls.end())
5262 ++EndIt;
5263
5264 for (ArrayRef<serialization::LocalDeclID>::iterator
5265 DIt = BeginIt; DIt != EndIt; ++DIt)
5266 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5267}
5268
5269namespace {
Douglas Gregorde3ef502011-11-30 23:21:26 +00005270 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor94619c82011-08-24 19:03:07 +00005271 /// declaration context.
5272 class DeclContextNameLookupVisitor {
5273 ASTReader &Reader;
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005274 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor94619c82011-08-24 19:03:07 +00005275 DeclarationName Name;
5276 SmallVectorImpl<NamedDecl *> &Decls;
5277
5278 public:
5279 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005280 SmallVectorImpl<const DeclContext *> &Contexts,
5281 DeclarationName Name,
Douglas Gregor94619c82011-08-24 19:03:07 +00005282 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005283 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor94619c82011-08-24 19:03:07 +00005284
Douglas Gregorde3ef502011-11-30 23:21:26 +00005285 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor94619c82011-08-24 19:03:07 +00005286 DeclContextNameLookupVisitor *This
5287 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5288
5289 // Check whether we have any visible declaration information for
5290 // this context in this module.
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005291 ModuleFile::DeclContextInfosMap::iterator Info;
5292 bool FoundInfo = false;
5293 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5294 Info = M.DeclContextInfos.find(This->Contexts[I]);
5295 if (Info != M.DeclContextInfos.end() &&
5296 Info->second.NameLookupTableData) {
5297 FoundInfo = true;
5298 break;
5299 }
5300 }
Douglas Gregor94619c82011-08-24 19:03:07 +00005301
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005302 if (!FoundInfo)
5303 return false;
5304
Douglas Gregor94619c82011-08-24 19:03:07 +00005305 // Look for this name within this module.
5306 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00005307 Info->second.NameLookupTableData;
Douglas Gregor94619c82011-08-24 19:03:07 +00005308 ASTDeclContextNameLookupTable::iterator Pos
5309 = LookupTable->find(This->Name);
5310 if (Pos == LookupTable->end())
5311 return false;
5312
5313 bool FoundAnything = false;
5314 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5315 for (; Data.first != Data.second; ++Data.first) {
5316 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5317 if (!ND)
5318 continue;
5319
5320 if (ND->getDeclName() != This->Name) {
Axel Naumanna8243e92012-10-01 09:51:27 +00005321 // A name might be null because the decl's redeclarable part is
5322 // currently read before reading its name. The lookup is triggered by
5323 // building that decl (likely indirectly), and so it is later in the
5324 // sense of "already existing" and can be ignored here.
Douglas Gregor94619c82011-08-24 19:03:07 +00005325 continue;
5326 }
5327
5328 // Record this declaration.
5329 FoundAnything = true;
5330 This->Decls.push_back(ND);
5331 }
5332
5333 return FoundAnything;
5334 }
5335 };
5336}
5337
John McCall75b960e2010-06-01 09:23:16 +00005338DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00005339ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00005340 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00005341 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005342 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00005343 if (!Name)
5344 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5345 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00005346
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005347 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005348
5349 // Compute the declaration contexts we need to look into. Multiple such
5350 // declaration contexts occur when two declaration contexts from disjoint
5351 // modules get merged, e.g., when two namespaces with the same name are
5352 // independently defined in separate modules.
5353 SmallVector<const DeclContext *, 2> Contexts;
5354 Contexts.push_back(DC);
5355
5356 if (DC->isNamespace()) {
5357 MergedDeclsMap::iterator Merged
5358 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5359 if (Merged != MergedDecls.end()) {
5360 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5361 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5362 }
5363 }
5364
5365 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor94619c82011-08-24 19:03:07 +00005366 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00005367 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00005368 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00005369 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005370}
5371
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005372namespace {
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005373 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005374 /// declaration context.
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005375 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005376 ASTReader &Reader;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005377 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005378 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005379
5380 public:
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005381 DeclContextAllNamesVisitor(ASTReader &Reader,
5382 SmallVectorImpl<const DeclContext *> &Contexts,
5383 llvm::DenseMap<DeclarationName,
5384 SmallVector<NamedDecl *, 8> > &Decls)
5385 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005386
5387 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005388 DeclContextAllNamesVisitor *This
5389 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005390
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005391 // Check whether we have any visible declaration information for
5392 // this context in this module.
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005393 ModuleFile::DeclContextInfosMap::iterator Info;
5394 bool FoundInfo = false;
5395 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5396 Info = M.DeclContextInfos.find(This->Contexts[I]);
5397 if (Info != M.DeclContextInfos.end() &&
5398 Info->second.NameLookupTableData) {
5399 FoundInfo = true;
5400 break;
5401 }
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005402 }
5403
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005404 if (!FoundInfo)
5405 return false;
5406
5407 ASTDeclContextNameLookupTable *LookupTable =
5408 Info->second.NameLookupTableData;
5409 bool FoundAnything = false;
5410 for (ASTDeclContextNameLookupTable::data_iterator
5411 I = LookupTable->data_begin(), E = LookupTable->data_end();
5412 I != E; ++I) {
5413 ASTDeclContextNameLookupTrait::data_type Data = *I;
5414 for (; Data.first != Data.second; ++Data.first) {
5415 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5416 *Data.first);
5417 if (!ND)
5418 continue;
5419
5420 // Record this declaration.
5421 FoundAnything = true;
5422 This->Decls[ND->getDeclName()].push_back(ND);
5423 }
5424 }
5425
5426 return FoundAnything;
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005427 }
5428 };
5429}
5430
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005431void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005432 if (!DC->hasExternalVisibleStorage())
5433 return;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005434 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5435
5436 // Compute the declaration contexts we need to look into. Multiple such
5437 // declaration contexts occur when two declaration contexts from disjoint
5438 // modules get merged, e.g., when two namespaces with the same name are
5439 // independently defined in separate modules.
5440 SmallVector<const DeclContext *, 2> Contexts;
5441 Contexts.push_back(DC);
5442
5443 if (DC->isNamespace()) {
5444 MergedDeclsMap::iterator Merged
5445 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5446 if (Merged != MergedDecls.end()) {
5447 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5448 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5449 }
5450 }
5451
5452 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5453 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5454 ++NumVisibleDeclContextsRead;
5455
5456 for (llvm::DenseMap<DeclarationName,
5457 llvm::SmallVector<NamedDecl*, 8> >::iterator
5458 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5459 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5460 }
Argyrios Kyrtzidis0334d332012-04-26 18:34:14 +00005461 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005462}
5463
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005464/// \brief Under non-PCH compilation the consumer receives the objc methods
5465/// before receiving the implementation, and codegen depends on this.
5466/// We simulate this by deserializing and passing to consumer the methods of the
5467/// implementation before passing the deserialized implementation decl.
5468static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5469 ASTConsumer *Consumer) {
5470 assert(ImplD && Consumer);
5471
5472 for (ObjCImplDecl::method_iterator
5473 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie40ed2972012-06-06 20:45:41 +00005474 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005475
5476 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5477}
5478
Sebastian Redl2c499f62010-08-18 23:56:43 +00005479void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005480 assert(Consumer);
5481 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005482 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005483 InterestingDecls.pop_front();
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005484
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00005485 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005486 }
5487}
5488
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00005489void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5490 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5491 PassObjCImplDeclToConsumer(ImplD, Consumer);
5492 else
5493 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5494}
5495
Sebastian Redl2c499f62010-08-18 23:56:43 +00005496void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00005497 this->Consumer = Consumer;
5498
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005499 if (!Consumer)
5500 return;
5501
5502 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005503 // Force deserialization of this decl, which will cause it to be queued for
5504 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00005505 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005506 }
Douglas Gregor6137d322011-09-15 18:47:32 +00005507 ExternalDefinitions.clear();
Douglas Gregorf005eac2009-04-25 00:41:30 +00005508
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005509 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005510}
5511
Sebastian Redl2c499f62010-08-18 23:56:43 +00005512void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005513 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005514
Mike Stump11289f42009-09-09 15:08:12 +00005515 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00005516 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00005517 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00005518 unsigned NumDeclsLoaded
5519 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5520 (Decl *)0);
5521 unsigned NumIdentifiersLoaded
5522 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5523 IdentifiersLoaded.end(),
5524 (IdentifierInfo *)0);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005525 unsigned NumMacrosLoaded
5526 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5527 MacrosLoaded.end(),
5528 (MacroInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00005529 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00005530 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5531 SelectorsLoaded.end(),
5532 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00005533
Douglas Gregor49bf76b2011-07-21 18:46:38 +00005534 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00005535 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5536 NumSLocEntriesRead, TotalNumSLocEntries,
5537 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00005538 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005539 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00005540 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5541 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5542 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005543 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00005544 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5545 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00005546 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005547 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00005548 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5549 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005550 if (!MacrosLoaded.empty())
5551 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5552 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5553 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00005554 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005555 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00005556 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5557 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00005558 if (TotalNumStatements)
5559 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5560 NumStatementsRead, TotalNumStatements,
5561 ((float)NumStatementsRead/TotalNumStatements * 100));
5562 if (TotalNumMacros)
5563 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5564 NumMacrosRead, TotalNumMacros,
5565 ((float)NumMacrosRead/TotalNumMacros * 100));
5566 if (TotalLexicalDeclContexts)
5567 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5568 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5569 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5570 * 100));
5571 if (TotalVisibleDeclContexts)
5572 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5573 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5574 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5575 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005576 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00005577 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005578 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5579 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00005580 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005581 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00005582 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005583 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00005584 dump();
5585 std::fprintf(stderr, "\n");
5586}
5587
Douglas Gregorde3ef502011-11-30 23:21:26 +00005588template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00005589static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005590dumpModuleIDMap(StringRef Name,
Douglas Gregorde3ef502011-11-30 23:21:26 +00005591 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor204b8712011-07-21 19:50:14 +00005592 InitialCapacity> &Map) {
5593 if (Map.begin() == Map.end())
5594 return;
5595
Douglas Gregorde3ef502011-11-30 23:21:26 +00005596 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00005597 llvm::errs() << Name << ":\n";
5598 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5599 I != IEnd; ++I) {
5600 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5601 << "\n";
5602 }
5603}
5604
Douglas Gregor204b8712011-07-21 19:50:14 +00005605void ASTReader::dump() {
Douglas Gregorde3ef502011-11-30 23:21:26 +00005606 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00005607 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00005608 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00005609 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005610 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005611 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005612 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor253eefe2011-12-01 00:59:36 +00005613 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005614 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005615 dumpModuleIDMap("Global preprocessed entity map",
5616 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005617
5618 llvm::errs() << "\n*** PCH/Modules Loaded:";
5619 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5620 MEnd = ModuleMgr.end();
5621 M != MEnd; ++M)
5622 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005623}
5624
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005625/// Return the amount of memory used by memory buffers, breaking down
5626/// by heap-backed versus mmap'ed memory.
5627void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005628 for (ModuleConstIterator I = ModuleMgr.begin(),
5629 E = ModuleMgr.end(); I != E; ++I) {
5630 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005631 size_t bytes = buf->getBufferSize();
5632 switch (buf->getBufferKind()) {
5633 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5634 sizes.malloc_bytes += bytes;
5635 break;
5636 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5637 sizes.mmap_bytes += bytes;
5638 break;
5639 }
5640 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005641 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005642}
5643
Sebastian Redl2c499f62010-08-18 23:56:43 +00005644void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00005645 SemaObj = &S;
Axel Naumanndd433f02012-10-18 19:05:02 +00005646 S.addExternalSource(this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00005647
Douglas Gregor7cd60f72009-04-22 21:15:06 +00005648 // Makes sure any declarations that were deserialized "too early"
5649 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00005650 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005651 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5652 PreloadedDecls[I]->getDeclName());
Douglas Gregora868bbd2009-04-21 22:25:48 +00005653 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00005654 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00005655
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005656 // Load the offsets of the declarations that Sema references.
5657 // They will be lazily deserialized when needed.
5658 if (!SemaDeclRefs.empty()) {
5659 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00005660 if (!SemaObj->StdNamespace)
5661 SemaObj->StdNamespace = SemaDeclRefs[0];
5662 if (!SemaObj->StdBadAlloc)
5663 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005664 }
5665
Peter Collingbourne5df20e02011-02-15 19:46:30 +00005666 if (!FPPragmaOptions.empty()) {
5667 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5668 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5669 }
5670
5671 if (!OpenCLExtensions.empty()) {
5672 unsigned I = 0;
5673#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5674#include "clang/Basic/OpenCLExtensions.def"
5675
5676 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5677 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00005678}
5679
Douglas Gregorab443b92011-08-20 04:39:52 +00005680IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00005681 // Note that we are loading an identifier.
5682 Deserializing AnIdentifier(this);
5683
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00005684 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5685 /*PriorGeneration=*/0);
Douglas Gregorab443b92011-08-20 04:39:52 +00005686 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005687 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00005688 markIdentifierUpToDate(II);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005689 return II;
Douglas Gregora868bbd2009-04-21 22:25:48 +00005690}
5691
Douglas Gregor57756ea2010-10-14 22:11:03 +00005692namespace clang {
5693 /// \brief An identifier-lookup iterator that enumerates all of the
5694 /// identifiers stored within a set of AST files.
5695 class ASTIdentifierIterator : public IdentifierIterator {
5696 /// \brief The AST reader whose identifiers are being enumerated.
5697 const ASTReader &Reader;
5698
5699 /// \brief The current index into the chain of AST files stored in
5700 /// the AST reader.
5701 unsigned Index;
5702
5703 /// \brief The current position within the identifier lookup table
5704 /// of the current AST file.
5705 ASTIdentifierLookupTable::key_iterator Current;
5706
5707 /// \brief The end position within the identifier lookup table of
5708 /// the current AST file.
5709 ASTIdentifierLookupTable::key_iterator End;
5710
5711 public:
5712 explicit ASTIdentifierIterator(const ASTReader &Reader);
5713
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005714 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00005715 };
5716}
5717
5718ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005719 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00005720 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005721 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00005722 Current = IdTable->key_begin();
5723 End = IdTable->key_end();
5724}
5725
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005726StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00005727 while (Current == End) {
5728 // If we have exhausted all of our AST files, we're done.
5729 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005730 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00005731
5732 --Index;
5733 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005734 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5735 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00005736 Current = IdTable->key_begin();
5737 End = IdTable->key_end();
5738 }
5739
5740 // We have any identifiers remaining in the current AST file; return
5741 // the next one.
5742 std::pair<const char*, unsigned> Key = *Current;
5743 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005744 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00005745}
5746
5747IdentifierIterator *ASTReader::getIdentifiers() const {
5748 return new ASTIdentifierIterator(*this);
5749}
5750
Douglas Gregorc10edd62011-08-25 14:51:20 +00005751namespace clang { namespace serialization {
5752 class ReadMethodPoolVisitor {
5753 ASTReader &Reader;
Douglas Gregord1f01d72012-01-25 01:14:32 +00005754 Selector Sel;
5755 unsigned PriorGeneration;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005756 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5757 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorc78d3462009-04-24 21:10:55 +00005758
Douglas Gregorc10edd62011-08-25 14:51:20 +00005759 public:
Douglas Gregord1f01d72012-01-25 01:14:32 +00005760 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5761 unsigned PriorGeneration)
5762 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregorc10edd62011-08-25 14:51:20 +00005763
Douglas Gregorde3ef502011-11-30 23:21:26 +00005764 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregorc10edd62011-08-25 14:51:20 +00005765 ReadMethodPoolVisitor *This
5766 = static_cast<ReadMethodPoolVisitor *>(UserData);
5767
5768 if (!M.SelectorLookupTable)
5769 return false;
5770
Douglas Gregord1f01d72012-01-25 01:14:32 +00005771 // If we've already searched this module file, skip it now.
5772 if (M.Generation <= This->PriorGeneration)
5773 return true;
5774
Douglas Gregorc10edd62011-08-25 14:51:20 +00005775 ASTSelectorLookupTable *PoolTable
5776 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5777 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5778 if (Pos == PoolTable->end())
5779 return false;
5780
5781 ++This->Reader.NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005782 // FIXME: Not quite happy with the statistics here. We probably should
5783 // disable this tracking when called via LoadSelector.
5784 // Also, should entries without methods count as misses?
Douglas Gregorc10edd62011-08-25 14:51:20 +00005785 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005786 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005787 if (This->Reader.DeserializationListener)
5788 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5789 This->Sel);
5790
5791 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5792 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5793 return true;
Sebastian Redlada023c2010-08-04 20:40:17 +00005794 }
Douglas Gregorc10edd62011-08-25 14:51:20 +00005795
5796 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregore1716012012-01-25 00:49:42 +00005797 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5798 return InstanceMethods;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005799 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00005800
Douglas Gregorc10edd62011-08-25 14:51:20 +00005801 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregore1716012012-01-25 00:49:42 +00005802 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5803 return FactoryMethods;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005804 }
5805 };
5806} } // end namespace clang::serialization
5807
Douglas Gregore1716012012-01-25 00:49:42 +00005808/// \brief Add the given set of methods to the method list.
5809static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5810 ObjCMethodList &List) {
5811 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5812 S.addMethodToGlobalList(&List, Methods[I]);
5813 }
5814}
5815
5816void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregord1f01d72012-01-25 01:14:32 +00005817 // Get the selector generation and update it to the current generation.
5818 unsigned &Generation = SelectorGeneration[Sel];
5819 unsigned PriorGeneration = Generation;
5820 Generation = CurrentGeneration;
5821
5822 // Search for methods defined with this selector.
5823 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregorc10edd62011-08-25 14:51:20 +00005824 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregorc10edd62011-08-25 14:51:20 +00005825
Douglas Gregore1716012012-01-25 00:49:42 +00005826 if (Visitor.getInstanceMethods().empty() &&
5827 Visitor.getFactoryMethods().empty()) {
Douglas Gregorc10edd62011-08-25 14:51:20 +00005828 ++NumMethodPoolMisses;
Douglas Gregore1716012012-01-25 00:49:42 +00005829 return;
5830 }
5831
5832 if (!getSema())
5833 return;
5834
5835 Sema &S = *getSema();
5836 Sema::GlobalMethodPool::iterator Pos
5837 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5838
5839 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5840 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorc78d3462009-04-24 21:10:55 +00005841}
5842
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005843void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005844 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005845 Namespaces.clear();
5846
5847 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5848 if (NamespaceDecl *Namespace
5849 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5850 Namespaces.push_back(Namespace);
5851 }
5852}
5853
Douglas Gregoreb08bd42011-07-27 20:58:46 +00005854void ASTReader::ReadTentativeDefinitions(
5855 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5856 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5857 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5858 if (Var)
5859 TentativeDefs.push_back(Var);
5860 }
5861 TentativeDefinitions.clear();
5862}
5863
Douglas Gregora94a1542011-07-27 21:45:57 +00005864void ASTReader::ReadUnusedFileScopedDecls(
5865 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5866 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5867 DeclaratorDecl *D
5868 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5869 if (D)
5870 Decls.push_back(D);
5871 }
5872 UnusedFileScopedDecls.clear();
5873}
5874
Douglas Gregorbae31202011-07-27 21:57:17 +00005875void ASTReader::ReadDelegatingConstructors(
5876 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5877 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5878 CXXConstructorDecl *D
5879 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5880 if (D)
5881 Decls.push_back(D);
5882 }
5883 DelegatingCtorDecls.clear();
5884}
5885
Douglas Gregorb7098a32011-07-28 00:39:29 +00005886void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5887 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5888 TypedefNameDecl *D
5889 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5890 if (D)
5891 Decls.push_back(D);
5892 }
5893 ExtVectorDecls.clear();
5894}
5895
Douglas Gregor32002192011-07-28 00:53:40 +00005896void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5897 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5898 CXXRecordDecl *D
5899 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5900 if (D)
5901 Decls.push_back(D);
5902 }
5903 DynamicClasses.clear();
5904}
5905
Douglas Gregordc5c9582011-07-28 14:20:37 +00005906void
5907ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5908 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5909 NamedDecl *D
5910 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5911 if (D)
5912 Decls.push_back(D);
5913 }
5914 LocallyScopedExternalDecls.clear();
5915}
5916
Douglas Gregor72e357f2011-07-28 14:54:22 +00005917void ASTReader::ReadReferencedSelectors(
5918 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5919 if (ReferencedSelectorsData.empty())
5920 return;
5921
5922 // If there are @selector references added them to its pool. This is for
5923 // implementation of -Wselector.
5924 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5925 unsigned I = 0;
5926 while (I < DataSize) {
5927 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5928 SourceLocation SelLoc
5929 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5930 Sels.push_back(std::make_pair(Sel, SelLoc));
5931 }
5932 ReferencedSelectorsData.clear();
5933}
5934
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00005935void ASTReader::ReadWeakUndeclaredIdentifiers(
5936 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5937 if (WeakUndeclaredIdentifiers.empty())
5938 return;
5939
5940 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5941 IdentifierInfo *WeakId
5942 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5943 IdentifierInfo *AliasId
5944 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5945 SourceLocation Loc
5946 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5947 bool Used = WeakUndeclaredIdentifiers[I++];
5948 WeakInfo WI(AliasId, Loc);
5949 WI.setUsed(Used);
5950 WeakIDs.push_back(std::make_pair(WeakId, WI));
5951 }
5952 WeakUndeclaredIdentifiers.clear();
5953}
5954
Douglas Gregor4daf6a32011-07-28 19:11:31 +00005955void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5956 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5957 ExternalVTableUse VT;
5958 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5959 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5960 VT.DefinitionRequired = VTableUses[Idx++];
5961 VTables.push_back(VT);
5962 }
5963
5964 VTableUses.clear();
5965}
5966
Douglas Gregore39f97c2011-07-28 19:49:54 +00005967void ASTReader::ReadPendingInstantiations(
5968 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5969 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5970 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5971 SourceLocation Loc
5972 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann63469422c2012-10-02 09:09:43 +00005973
Douglas Gregor559458c2012-10-03 18:34:48 +00005974 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregore39f97c2011-07-28 19:49:54 +00005975 }
5976 PendingInstantiations.clear();
5977}
5978
Sebastian Redl2c499f62010-08-18 23:56:43 +00005979void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005980 // It would be complicated to avoid reading the methods anyway. So don't.
5981 ReadMethodPool(Sel);
5982}
5983
Douglas Gregora3e41532011-07-28 20:55:49 +00005984void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00005985 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00005986 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00005987 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005988 if (DeserializationListener)
5989 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00005990}
5991
Douglas Gregor1342e842009-07-06 18:54:52 +00005992/// \brief Set the globally-visible declarations associated with the given
5993/// identifier.
5994///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005995/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00005996/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00005997/// them.
5998///
5999/// \param II an IdentifierInfo that refers to one or more globally-visible
6000/// declarations.
6001///
6002/// \param DeclIDs the set of declaration IDs with the name @p II that are
6003/// visible at global scope.
6004///
6005/// \param Nonrecursive should be true to indicate that the caller knows that
6006/// this call is non-recursive, and therefore the globally-visible declarations
6007/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00006008void
Sebastian Redl2c499f62010-08-18 23:56:43 +00006009ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006010 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00006011 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00006012 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00006013 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6014 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6015 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00006016 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00006017 return;
6018 }
Mike Stump11289f42009-09-09 15:08:12 +00006019
Douglas Gregor1342e842009-07-06 18:54:52 +00006020 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6021 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6022 if (SemaObj) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00006023 // Introduce this declaration into the translation-unit scope
6024 // and add it to the declaration chain for this identifier, so
6025 // that (unqualified) name lookup will find it.
6026 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregor1342e842009-07-06 18:54:52 +00006027 } else {
6028 // Queue this declaration so that it will be added to the
6029 // translation unit scope and identifier's declaration chain
6030 // once a Sema object is known.
6031 PreloadedDecls.push_back(D);
6032 }
6033 }
6034}
6035
Douglas Gregora3e41532011-07-28 20:55:49 +00006036IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006037 if (ID == 0)
6038 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00006039
Sebastian Redlc713b962010-07-21 00:46:22 +00006040 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006041 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006042 return 0;
6043 }
Mike Stump11289f42009-09-09 15:08:12 +00006044
Sebastian Redlc713b962010-07-21 00:46:22 +00006045 ID -= 1;
6046 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00006047 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6048 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00006049 ModuleFile *M = I->second;
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006050 unsigned Index = ID - M->BaseIdentifierID;
6051 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00006052
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006053 // All of the strings in the AST file are preceded by a 16-bit length.
6054 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00006055 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6056 // unsigned integers. This is important to avoid integer overflow when
6057 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00006058 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00006059 unsigned StrLen = (((unsigned) StrLenPtr[0])
6060 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00006061 IdentifiersLoaded[ID]
Douglas Gregor51825b42011-09-09 22:02:16 +00006062 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00006063 if (DeserializationListener)
6064 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006065 }
Mike Stump11289f42009-09-09 15:08:12 +00006066
Sebastian Redlc713b962010-07-21 00:46:22 +00006067 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006068}
6069
Douglas Gregorde3ef502011-11-30 23:21:26 +00006070IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregora3e41532011-07-28 20:55:49 +00006071 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6072}
6073
Douglas Gregorde3ef502011-11-30 23:21:26 +00006074IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00006075 if (LocalID < NUM_PREDEF_IDENT_IDS)
6076 return LocalID;
6077
6078 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6079 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6080 assert(I != M.IdentifierRemap.end()
6081 && "Invalid index into identifier index remap");
6082
6083 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00006084}
6085
Douglas Gregore7400892012-10-11 17:41:54 +00006086MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006087 if (ID == 0)
6088 return 0;
6089
6090 if (MacrosLoaded.empty()) {
6091 Error("no macro table in AST file");
6092 return 0;
6093 }
6094
6095 ID -= NUM_PREDEF_MACRO_IDS;
6096 if (!MacrosLoaded[ID]) {
6097 GlobalMacroMapType::iterator I
6098 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6099 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6100 ModuleFile *M = I->second;
6101 unsigned Index = ID - M->BaseMacroID;
Douglas Gregore7400892012-10-11 17:41:54 +00006102 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006103 }
6104
6105 return MacrosLoaded[ID];
6106}
6107
6108MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6109 if (LocalID < NUM_PREDEF_MACRO_IDS)
6110 return LocalID;
6111
6112 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6113 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6114 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6115
6116 return LocalID + I->second;
6117}
6118
Douglas Gregor253eefe2011-12-01 00:59:36 +00006119serialization::SubmoduleID
6120ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6121 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6122 return LocalID;
6123
6124 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6125 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6126 assert(I != M.SubmoduleRemap.end()
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006127 && "Invalid index into submodule index remap");
Douglas Gregor253eefe2011-12-01 00:59:36 +00006128
6129 return LocalID + I->second;
6130}
6131
6132Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6133 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6134 assert(GlobalID == 0 && "Unhandled global submodule ID");
6135 return 0;
6136 }
6137
6138 if (GlobalID > SubmodulesLoaded.size()) {
6139 Error("submodule ID out of range in AST file");
6140 return 0;
6141 }
6142
6143 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6144}
6145
Douglas Gregorde3ef502011-11-30 23:21:26 +00006146Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor074fdc52011-07-28 21:16:51 +00006147 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6148}
6149
6150Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00006151 if (ID == 0)
6152 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00006153
Sebastian Redlada023c2010-08-04 20:40:17 +00006154 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006155 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00006156 return Selector();
6157 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00006158
Sebastian Redlada023c2010-08-04 20:40:17 +00006159 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00006160 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00006161 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6162 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00006163 ModuleFile &M = *I->second;
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006164 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006165 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00006166 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006167 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00006168 if (DeserializationListener)
6169 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00006170 }
6171
Sebastian Redlada023c2010-08-04 20:40:17 +00006172 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00006173}
6174
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00006175Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00006176 return DecodeSelector(ID);
6177}
6178
Sebastian Redl2c499f62010-08-18 23:56:43 +00006179uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00006180 // ID 0 (the null selector) is considered an external selector.
6181 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00006182}
6183
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006184serialization::SelectorID
Douglas Gregorde3ef502011-11-30 23:21:26 +00006185ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006186 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6187 return LocalID;
6188
6189 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6190 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6191 assert(I != M.SelectorRemap.end()
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006192 && "Invalid index into selector index remap");
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006193
6194 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00006195}
6196
Mike Stump11289f42009-09-09 15:08:12 +00006197DeclarationName
Douglas Gregorde3ef502011-11-30 23:21:26 +00006198ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00006199 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006200 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6201 switch (Kind) {
6202 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00006203 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006204
6205 case DeclarationName::ObjCZeroArgSelector:
6206 case DeclarationName::ObjCOneArgSelector:
6207 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00006208 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006209
6210 case DeclarationName::CXXConstructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006211 return Context.DeclarationNames.getCXXConstructorName(
6212 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006213
6214 case DeclarationName::CXXDestructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006215 return Context.DeclarationNames.getCXXDestructorName(
6216 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006217
6218 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006219 return Context.DeclarationNames.getCXXConversionFunctionName(
6220 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006221
6222 case DeclarationName::CXXOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006223 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006224 (OverloadedOperatorKind)Record[Idx++]);
6225
Alexis Hunt3d221f22009-11-29 07:34:05 +00006226 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006227 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00006228 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00006229
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006230 case DeclarationName::CXXUsingDirective:
6231 return DeclarationName::getUsingDirectiveName();
6232 }
6233
David Blaikie8a40f702012-01-17 06:56:22 +00006234 llvm_unreachable("Invalid NameKind!");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006235}
Douglas Gregor55abb232009-04-10 20:39:37 +00006236
Douglas Gregorde3ef502011-11-30 23:21:26 +00006237void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006238 DeclarationNameLoc &DNLoc,
6239 DeclarationName Name,
6240 const RecordData &Record, unsigned &Idx) {
6241 switch (Name.getNameKind()) {
6242 case DeclarationName::CXXConstructorName:
6243 case DeclarationName::CXXDestructorName:
6244 case DeclarationName::CXXConversionFunctionName:
6245 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6246 break;
6247
6248 case DeclarationName::CXXOperatorName:
6249 DNLoc.CXXOperatorName.BeginOpNameLoc
6250 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6251 DNLoc.CXXOperatorName.EndOpNameLoc
6252 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6253 break;
6254
6255 case DeclarationName::CXXLiteralOperatorName:
6256 DNLoc.CXXLiteralOperatorName.OpNameLoc
6257 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6258 break;
6259
6260 case DeclarationName::Identifier:
6261 case DeclarationName::ObjCZeroArgSelector:
6262 case DeclarationName::ObjCOneArgSelector:
6263 case DeclarationName::ObjCMultiArgSelector:
6264 case DeclarationName::CXXUsingDirective:
6265 break;
6266 }
6267}
6268
Douglas Gregorde3ef502011-11-30 23:21:26 +00006269void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006270 DeclarationNameInfo &NameInfo,
6271 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00006272 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006273 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6274 DeclarationNameLoc DNLoc;
6275 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6276 NameInfo.setInfo(DNLoc);
6277}
6278
Douglas Gregorde3ef502011-11-30 23:21:26 +00006279void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006280 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00006281 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006282 unsigned NumTPLists = Record[Idx++];
6283 Info.NumTemplParamLists = NumTPLists;
6284 if (NumTPLists) {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006285 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006286 for (unsigned i=0; i != NumTPLists; ++i)
6287 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6288 }
6289}
6290
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006291TemplateName
Douglas Gregorde3ef502011-11-30 23:21:26 +00006292ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00006293 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006294 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006295 switch (Kind) {
6296 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006297 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006298
6299 case TemplateName::OverloadedTemplate: {
6300 unsigned size = Record[Idx++];
6301 UnresolvedSet<8> Decls;
6302 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006303 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006304
Douglas Gregor4163aca2011-09-09 21:34:22 +00006305 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006306 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006307
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006308 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006309 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006310 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00006311 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006312 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006313 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006314
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006315 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006316 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006317 if (Record[Idx++]) // isIdentifier
Douglas Gregor4163aca2011-09-09 21:34:22 +00006318 return Context.getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00006319 GetIdentifierInfo(F, Record,
6320 Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00006321 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00006322 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006323 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006324
6325 case TemplateName::SubstTemplateTemplateParm: {
6326 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00006327 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00006328 if (!param) return TemplateName();
6329 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006330 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCalld9dfe3a2011-06-30 08:33:18 +00006331 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006332
6333 case TemplateName::SubstTemplateTemplateParmPack: {
6334 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00006335 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00006336 if (!Param)
6337 return TemplateName();
6338
6339 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6340 if (ArgPack.getKind() != TemplateArgument::Pack)
6341 return TemplateName();
6342
Douglas Gregor4163aca2011-09-09 21:34:22 +00006343 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00006344 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006345 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006346
David Blaikie83d382b2011-09-23 05:06:16 +00006347 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006348}
6349
6350TemplateArgument
Douglas Gregorde3ef502011-11-30 23:21:26 +00006351ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00006352 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006353 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6354 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006355 case TemplateArgument::Null:
6356 return TemplateArgument();
6357 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00006358 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmanb826a002012-09-26 02:36:12 +00006359 case TemplateArgument::Declaration: {
6360 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6361 bool ForReferenceParam = Record[Idx++];
6362 return TemplateArgument(D, ForReferenceParam);
6363 }
6364 case TemplateArgument::NullPtr:
6365 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00006366 case TemplateArgument::Integral: {
6367 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00006368 QualType T = readType(F, Record, Idx);
Benjamin Kramer6003ad52012-06-07 15:09:51 +00006369 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00006370 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006371 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00006372 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006373 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00006374 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00006375 llvm::Optional<unsigned> NumTemplateExpansions;
6376 if (unsigned NumExpansions = Record[Idx++])
6377 NumTemplateExpansions = NumExpansions - 1;
6378 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00006379 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006380 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00006381 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006382 case TemplateArgument::Pack: {
6383 unsigned NumArgs = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00006384 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00006385 for (unsigned I = 0; I != NumArgs; ++I)
6386 Args[I] = ReadTemplateArgument(F, Record, Idx);
6387 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006388 }
6389 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006390
David Blaikie83d382b2011-09-23 05:06:16 +00006391 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006392}
6393
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006394TemplateParameterList *
Douglas Gregorde3ef502011-11-30 23:21:26 +00006395ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006396 const RecordData &Record, unsigned &Idx) {
6397 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6398 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6399 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006400
6401 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006402 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006403 Params.reserve(NumParams);
6404 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006405 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006406
6407 TemplateParameterList* TemplateParams =
Douglas Gregor4163aca2011-09-09 21:34:22 +00006408 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006409 Params.data(), Params.size(), RAngleLoc);
6410 return TemplateParams;
6411}
6412
6413void
Sebastian Redl2c499f62010-08-18 23:56:43 +00006414ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006415ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregorde3ef502011-11-30 23:21:26 +00006416 ModuleFile &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006417 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006418 unsigned NumTemplateArgs = Record[Idx++];
6419 TemplArgs.reserve(NumTemplateArgs);
6420 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00006421 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006422}
6423
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006424/// \brief Read a UnresolvedSet structure.
Douglas Gregorde3ef502011-11-30 23:21:26 +00006425void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006426 const RecordData &Record, unsigned &Idx) {
6427 unsigned NumDecls = Record[Idx++];
6428 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006429 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006430 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6431 Set.addDecl(D, AS);
6432 }
6433}
6434
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006435CXXBaseSpecifier
Douglas Gregorde3ef502011-11-30 23:21:26 +00006436ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00006437 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006438 bool isVirtual = static_cast<bool>(Record[Idx++]);
6439 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6440 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00006441 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006442 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6443 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00006444 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00006445 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00006446 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00006447 Result.setInheritConstructors(inheritConstructors);
6448 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006449}
6450
Alexis Hunt1d792652011-01-08 20:30:50 +00006451std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregorde3ef502011-11-30 23:21:26 +00006452ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00006453 unsigned &Idx) {
6454 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006455 unsigned NumInitializers = Record[Idx++];
6456 if (NumInitializers) {
Alexis Hunt1d792652011-01-08 20:30:50 +00006457 CtorInitializers
Douglas Gregor4163aca2011-09-09 21:34:22 +00006458 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006459 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006460 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006461 bool IsBaseVirtual = false;
6462 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00006463 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006464
Alexis Hunt37a477f2011-05-04 01:19:08 +00006465 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6466 switch (Type) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006467 case CTOR_INITIALIZER_BASE:
6468 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006469 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00006470 break;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006471
6472 case CTOR_INITIALIZER_DELEGATING:
6473 TInfo = GetTypeSourceInfo(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006474 break;
6475
6476 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006477 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006478 break;
6479
6480 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006481 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006482 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006483 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00006484
Douglas Gregor44e7df62011-01-04 00:32:56 +00006485 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006486 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006487 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6488 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006489 bool IsWritten = Record[Idx++];
6490 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006491 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006492 if (IsWritten) {
6493 SourceOrderOrNumArrayIndices = Record[Idx++];
6494 } else {
6495 SourceOrderOrNumArrayIndices = Record[Idx++];
6496 Indices.reserve(SourceOrderOrNumArrayIndices);
6497 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006498 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006499 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006500
Alexis Hunt1d792652011-01-08 20:30:50 +00006501 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00006502 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006503 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Alexis Hunt1d792652011-01-08 20:30:50 +00006504 LParenLoc, Init, RParenLoc,
6505 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006506 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006507 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6508 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006509 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00006510 if (Member)
Douglas Gregor4163aca2011-09-09 21:34:22 +00006511 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00006512 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00006513 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00006514 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Alexis Hunt1d792652011-01-08 20:30:50 +00006515 MemberOrEllipsisLoc, LParenLoc,
6516 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006517 } else {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006518 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00006519 LParenLoc, Init, RParenLoc,
6520 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006521 }
6522
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00006523 if (IsWritten)
6524 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00006525 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006526 }
6527 }
6528
Alexis Hunt1d792652011-01-08 20:30:50 +00006529 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006530}
6531
Chris Lattnerca025db2010-05-07 21:43:38 +00006532NestedNameSpecifier *
Douglas Gregorde3ef502011-11-30 23:21:26 +00006533ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00006534 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00006535 unsigned N = Record[Idx++];
6536 NestedNameSpecifier *NNS = 0, *Prev = 0;
6537 for (unsigned I = 0; I != N; ++I) {
6538 NestedNameSpecifier::SpecifierKind Kind
6539 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6540 switch (Kind) {
6541 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00006542 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006543 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattnerca025db2010-05-07 21:43:38 +00006544 break;
6545 }
6546
6547 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006548 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006549 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattnerca025db2010-05-07 21:43:38 +00006550 break;
6551 }
6552
Douglas Gregor7b26ff92011-02-24 02:36:08 +00006553 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006554 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006555 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00006556 break;
6557 }
6558
Chris Lattnerca025db2010-05-07 21:43:38 +00006559 case NestedNameSpecifier::TypeSpec:
6560 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00006561 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00006562 if (!T)
6563 return 0;
6564
Chris Lattnerca025db2010-05-07 21:43:38 +00006565 bool Template = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00006566 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattnerca025db2010-05-07 21:43:38 +00006567 break;
6568 }
6569
6570 case NestedNameSpecifier::Global: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006571 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattnerca025db2010-05-07 21:43:38 +00006572 // No associated value, and there can't be a prefix.
6573 break;
6574 }
Chris Lattnerca025db2010-05-07 21:43:38 +00006575 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00006576 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00006577 }
6578 return NNS;
6579}
6580
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006581NestedNameSpecifierLoc
Douglas Gregorde3ef502011-11-30 23:21:26 +00006582ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006583 unsigned &Idx) {
6584 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00006585 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006586 for (unsigned I = 0; I != N; ++I) {
6587 NestedNameSpecifier::SpecifierKind Kind
6588 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6589 switch (Kind) {
6590 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00006591 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006592 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006593 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006594 break;
6595 }
6596
6597 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006598 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006599 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006600 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006601 break;
6602 }
6603
6604 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006605 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006606 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006607 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006608 break;
6609 }
6610
6611 case NestedNameSpecifier::TypeSpec:
6612 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006613 bool Template = Record[Idx++];
6614 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6615 if (!T)
6616 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006617 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00006618
6619 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor4163aca2011-09-09 21:34:22 +00006620 Builder.Extend(Context,
Douglas Gregor9b272512011-02-28 23:58:31 +00006621 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6622 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006623 break;
6624 }
6625
6626 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006627 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006628 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006629 break;
6630 }
6631 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006632 }
6633
Douglas Gregor4163aca2011-09-09 21:34:22 +00006634 return Builder.getWithLocInContext(Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006635}
6636
Chris Lattnerca025db2010-05-07 21:43:38 +00006637SourceRange
Douglas Gregorde3ef502011-11-30 23:21:26 +00006638ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006639 unsigned &Idx) {
6640 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6641 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00006642 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00006643}
6644
Douglas Gregor1daeb692009-04-13 18:14:40 +00006645/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006646llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00006647 unsigned BitWidth = Record[Idx++];
6648 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6649 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6650 Idx += NumWords;
6651 return Result;
6652}
6653
6654/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006655llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00006656 bool isUnsigned = Record[Idx++];
6657 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6658}
6659
Douglas Gregore0a3a512009-04-14 21:55:33 +00006660/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006661llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00006662 return llvm::APFloat(ReadAPInt(Record, Idx));
6663}
6664
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006665// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00006666std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006667 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00006668 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006669 Idx += Len;
6670 return Result;
6671}
6672
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006673VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6674 unsigned &Idx) {
6675 unsigned Major = Record[Idx++];
6676 unsigned Minor = Record[Idx++];
6677 unsigned Subminor = Record[Idx++];
6678 if (Minor == 0)
6679 return VersionTuple(Major);
6680 if (Subminor == 0)
6681 return VersionTuple(Major, Minor - 1);
6682 return VersionTuple(Major, Minor - 1, Subminor - 1);
6683}
6684
Douglas Gregorde3ef502011-11-30 23:21:26 +00006685CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00006686 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00006687 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006688 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006689 return CXXTemporary::Create(Context, Decl);
Chris Lattnercba86142010-05-10 00:25:06 +00006690}
6691
Sebastian Redl2c499f62010-08-18 23:56:43 +00006692DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00006693 return Diag(SourceLocation(), DiagID);
6694}
6695
Sebastian Redl2c499f62010-08-18 23:56:43 +00006696DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006697 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00006698}
Douglas Gregora9af1d12009-04-17 00:04:06 +00006699
Douglas Gregora868bbd2009-04-21 22:25:48 +00006700/// \brief Retrieve the identifier table associated with the
6701/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006702IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor51825b42011-09-09 22:02:16 +00006703 return PP.getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00006704}
6705
Douglas Gregora9af1d12009-04-17 00:04:06 +00006706/// \brief Record that the given ID maps to the given switch-case
6707/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006708void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006709 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6710 "Already have a SwitchCase with this ID");
6711 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregora9af1d12009-04-17 00:04:06 +00006712}
6713
6714/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006715SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006716 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6717 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregora9af1d12009-04-17 00:04:06 +00006718}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00006719
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00006720void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006721 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00006722}
6723
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006724void ASTReader::ReadComments() {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00006725 std::vector<RawComment *> Comments;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006726 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6727 serialization::ModuleFile *> >::iterator
6728 I = CommentsCursors.begin(),
6729 E = CommentsCursors.end();
6730 I != E; ++I) {
6731 llvm::BitstreamCursor &Cursor = I->first;
6732 serialization::ModuleFile &F = *I->second;
6733 SavedStreamPosition SavedPosition(Cursor);
6734
6735 RecordData Record;
6736 while (true) {
6737 unsigned Code = Cursor.ReadCode();
6738 if (Code == llvm::bitc::END_BLOCK)
6739 break;
6740
6741 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6742 // No known subblocks, always skip them.
6743 Cursor.ReadSubBlockID();
6744 if (Cursor.SkipBlock()) {
6745 Error("malformed block record in AST file");
6746 return;
6747 }
6748 continue;
6749 }
6750
6751 if (Code == llvm::bitc::DEFINE_ABBREV) {
6752 Cursor.ReadAbbrevRecord();
6753 continue;
6754 }
6755
6756 // Read a record.
6757 Record.clear();
6758 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth029ea4a2012-06-20 06:47:54 +00006759 case COMMENTS_RAW_COMMENT: {
6760 unsigned Idx = 0;
6761 SourceRange SR = ReadSourceRange(F, Record, Idx);
6762 RawComment::CommentKind Kind =
6763 (RawComment::CommentKind) Record[Idx++];
6764 bool IsTrailingComment = Record[Idx++];
6765 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00006766 Comments.push_back(new (Context) RawComment(SR, Kind,
6767 IsTrailingComment,
6768 IsAlmostTrailingComment));
Chandler Carruth029ea4a2012-06-20 06:47:54 +00006769 break;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006770 }
6771 }
6772 }
6773 }
6774 Context.Comments.addCommentsToFront(Comments);
6775}
6776
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006777void ASTReader::finishPendingActions() {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00006778 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6779 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006780 // If any identifiers with corresponding top-level declarations have
6781 // been loaded, load those declarations now.
6782 while (!PendingIdentifierInfos.empty()) {
6783 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6784 PendingIdentifierInfos.front().DeclIDs, true);
6785 PendingIdentifierInfos.pop_front();
6786 }
6787
Douglas Gregor05f10352011-12-17 23:38:30 +00006788 // Load pending declaration chains.
6789 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6790 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006791 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregor05f10352011-12-17 23:38:30 +00006792 }
6793 PendingDeclChains.clear();
Douglas Gregor5a4649b2012-10-11 00:46:49 +00006794
6795 // Load any pending macro definitions.
Douglas Gregord2acff92012-10-11 17:31:34 +00006796 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6797 // FIXME: std::move here
6798 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregore7400892012-10-11 17:41:54 +00006799 MacroInfo *Hint = 0;
Douglas Gregord2acff92012-10-11 17:31:34 +00006800 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6801 ++IDIdx) {
Douglas Gregore7400892012-10-11 17:41:54 +00006802 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregord2acff92012-10-11 17:31:34 +00006803 }
6804 }
6805 PendingMacroIDs.clear();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006806 }
Douglas Gregore80b31f2011-12-19 19:00:47 +00006807
Douglas Gregor68444de2012-01-14 15:13:49 +00006808 // If we deserialized any C++ or Objective-C class definitions, any
6809 // Objective-C protocol definitions, or any redeclarable templates, make sure
6810 // that all redeclarations point to the definitions. Note that this can only
6811 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregore80b31f2011-12-19 19:00:47 +00006812 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6813 DEnd = PendingDefinitions.end();
6814 D != DEnd; ++D) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006815 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6816 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6817 // Make sure that the TagType points at the definition.
6818 const_cast<TagType*>(TagT)->decl = TD;
6819 }
Douglas Gregore80b31f2011-12-19 19:00:47 +00006820
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006821 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6822 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6823 REnd = RD->redecls_end();
6824 R != REnd; ++R)
6825 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6826
6827 }
6828
Douglas Gregore80b31f2011-12-19 19:00:47 +00006829 continue;
6830 }
6831
Douglas Gregora715bff2012-01-01 19:51:50 +00006832 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006833 // Make sure that the ObjCInterfaceType points at the definition.
6834 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6835 ->Decl = ID;
6836
Douglas Gregora715bff2012-01-01 19:51:50 +00006837 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6838 REnd = ID->redecls_end();
6839 R != REnd; ++R)
6840 R->Data = ID->Data;
6841
6842 continue;
6843 }
6844
Douglas Gregor68444de2012-01-14 15:13:49 +00006845 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6846 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6847 REnd = PD->redecls_end();
6848 R != REnd; ++R)
6849 R->Data = PD->Data;
6850
6851 continue;
6852 }
6853
6854 RedeclarableTemplateDecl *RTD
6855 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6856 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6857 REnd = RTD->redecls_end();
Douglas Gregore80b31f2011-12-19 19:00:47 +00006858 R != REnd; ++R)
Douglas Gregora6017bb2012-10-09 17:21:28 +00006859 R->Common = RTD->Common;
Douglas Gregore80b31f2011-12-19 19:00:47 +00006860 }
6861 PendingDefinitions.clear();
Douglas Gregora6017bb2012-10-09 17:21:28 +00006862
6863 // Load the bodies of any functions or methods we've encountered. We do
6864 // this now (delayed) so that we can be sure that the declaration chains
6865 // have been fully wired up.
Douglas Gregor7c0990b2012-10-09 17:50:23 +00006866 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6867 PBEnd = PendingBodies.end();
Douglas Gregora6017bb2012-10-09 17:21:28 +00006868 PB != PBEnd; ++PB) {
6869 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6870 // FIXME: Check for =delete/=default?
6871 // FIXME: Complain about ODR violations here?
6872 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6873 FD->setLazyBody(PB->second);
6874 continue;
6875 }
6876
6877 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6878 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6879 MD->setLazyBody(PB->second);
6880 }
6881 PendingBodies.clear();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006882}
6883
Sebastian Redl2c499f62010-08-18 23:56:43 +00006884void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00006885 assert(NumCurrentElementsDeserializing &&
6886 "FinishedDeserializing not paired with StartedDeserializing");
6887 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006888 // We decrease NumCurrentElementsDeserializing only after pending actions
6889 // are finished, to avoid recursively re-calling finishPendingActions().
6890 finishPendingActions();
6891 }
6892 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis97ea7d62011-12-17 04:13:28 +00006893
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006894 if (NumCurrentElementsDeserializing == 0 &&
6895 Consumer && !PassingDeclsToConsumer) {
6896 // Guard variable to avoid recursively redoing the process of passing
6897 // decls to consumer.
6898 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6899 true);
Argyrios Kyrtzidis97ea7d62011-12-17 04:13:28 +00006900
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006901 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00006902 // We are not in recursive loading, so it's safe to pass the "interesting"
6903 // decls to the consumer.
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006904 Decl *D = InterestingDecls.front();
6905 InterestingDecls.pop_front();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006906 PassInterestingDeclToConsumer(D);
6907 }
Douglas Gregor1342e842009-07-06 18:54:52 +00006908 }
Douglas Gregor1342e842009-07-06 18:54:52 +00006909}
Douglas Gregorb473b072010-08-19 00:28:17 +00006910
Douglas Gregor8835e032011-09-02 00:26:20 +00006911ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00006912 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00006913 bool AllowASTWithCompilerErrors)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006914 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6915 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor51825b42011-09-09 22:02:16 +00006916 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +00006917 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00006918 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00006919 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006920 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
Douglas Gregor925296b2011-07-19 16:10:42 +00006921 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00006922 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6923 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6924 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6925 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00006926 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6927 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006928 PassingDeclsToConsumer(false),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00006929 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00006930{
Douglas Gregor925296b2011-07-19 16:10:42 +00006931 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006932}
6933
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006934ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006935 for (DeclContextVisibleUpdatesPending::iterator
6936 I = PendingVisibleUpdates.begin(),
6937 E = PendingVisibleUpdates.end();
6938 I != E; ++I) {
6939 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6940 F = I->second.end();
6941 J != F; ++J)
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00006942 delete J->first;
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006943 }
6944}