blob: c1428d5c68bfdaa057394d5b5b3b262f46ea1edc [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]);
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +0000967 if (IncludeLoc.isInvalid() && F->Kind == MK_Module) {
968 IncludeLoc = getImportLocation(F);
969 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000970 unsigned Code = SLocEntryCursor.ReadCode();
971 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000972 unsigned RecCode
Douglas Gregor258ae542009-04-27 06:38:32 +0000973 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000974
Sebastian Redl539c5062010-08-18 23:57:32 +0000975 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +0000976 Error("AST record has invalid code");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +0000977 return true;
Ted Kremenek1ff615c2010-03-18 00:56:54 +0000978 }
979
Douglas Gregor258ae542009-04-27 06:38:32 +0000980 llvm::MemoryBuffer *Buffer
Douglas Gregor9dc32122011-11-16 20:05:18 +0000981 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
982 Name);
Argyrios Kyrtzidis6566e232012-11-09 19:40:45 +0000983 SourceMgr.createFileIDForMemBuffer(Buffer, FileCharacter, ID,
984 BaseOffset + Offset, IncludeLoc);
Douglas Gregor258ae542009-04-27 06:38:32 +0000985 break;
986 }
987
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000988 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redl2c373b92010-10-05 15:59:54 +0000989 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruth115b0772011-07-26 03:03:05 +0000990 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redl2c373b92010-10-05 15:59:54 +0000991 ReadSourceLocation(*F, Record[2]),
992 ReadSourceLocation(*F, Record[3]),
Douglas Gregor258ae542009-04-27 06:38:32 +0000993 Record[4],
994 ID,
Douglas Gregor925296b2011-07-19 16:10:42 +0000995 BaseOffset + Record[0]);
Douglas Gregor258ae542009-04-27 06:38:32 +0000996 break;
Mike Stump11289f42009-09-09 15:08:12 +0000997 }
Douglas Gregor258ae542009-04-27 06:38:32 +0000998 }
999
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001000 return false;
Douglas Gregor258ae542009-04-27 06:38:32 +00001001}
1002
Douglas Gregor925296b2011-07-19 16:10:42 +00001003/// \brief Find the location where the module F is imported.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001004SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001005 if (F->ImportLoc.isValid())
1006 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001007
Douglas Gregor925296b2011-07-19 16:10:42 +00001008 // Otherwise we have a PCH. It's considered to be "imported" at the first
1009 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001010 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001011 // Main file is the importer. We assume that it is the first entry in the
1012 // entry table. We can't ask the manager, because at the time of PCH loading
1013 // the main file entry doesn't exist yet.
1014 // The very first entry is the invalid instantiation loc, which takes up
1015 // offsets 0 and 1.
1016 return SourceLocation::getFromRawEncoding(2U);
1017 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001018 //return F->Loaders[0]->FirstLoc;
1019 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001020}
1021
Chris Lattnere78a6be2009-04-27 01:05:14 +00001022/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1023/// specified cursor. Read the abbreviations that are at the top of the block
1024/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001025bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001026 unsigned BlockID) {
1027 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001028 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001029 return Failure;
1030 }
Mike Stump11289f42009-09-09 15:08:12 +00001031
Chris Lattnere78a6be2009-04-27 01:05:14 +00001032 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001033 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001034 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001035
Chris Lattnere78a6be2009-04-27 01:05:14 +00001036 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001037 if (Code != llvm::bitc::DEFINE_ABBREV) {
1038 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001039 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001040 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001041 Cursor.ReadAbbrevRecord();
1042 }
1043}
1044
Douglas Gregore7400892012-10-11 17:41:54 +00001045void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1046 MacroInfo *Hint) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001047 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001048
Douglas Gregorc3366a52009-04-21 23:56:24 +00001049 // Keep track of where we are in the stream, then jump back there
1050 // after reading this macro.
1051 SavedStreamPosition SavedPosition(Stream);
1052
1053 Stream.JumpToBit(Offset);
1054 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001055 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001056 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001057
Douglas Gregor5968b1b2012-10-11 21:07:39 +00001058 // RAII object to add the loaded macro information once we're done
1059 // adding tokens.
1060 struct AddLoadedMacroInfoRAII {
1061 Preprocessor &PP;
1062 MacroInfo *Hint;
1063 MacroInfo *MI;
1064 IdentifierInfo *II;
1065
1066 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1067 : PP(PP), Hint(Hint), MI(), II() { }
1068 ~AddLoadedMacroInfoRAII( ) {
1069 if (MI) {
1070 // Finally, install the macro.
1071 PP.addLoadedMacroInfo(II, MI, Hint);
1072 }
1073 }
1074 } AddLoadedMacroInfo(PP, Hint);
1075
Douglas Gregorc3366a52009-04-21 23:56:24 +00001076 while (true) {
1077 unsigned Code = Stream.ReadCode();
1078 switch (Code) {
1079 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001080 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001081
1082 case llvm::bitc::ENTER_SUBBLOCK:
1083 // No known subblocks, always skip them.
1084 Stream.ReadSubBlockID();
1085 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001086 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001087 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001088 }
1089 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001090
Douglas Gregorc3366a52009-04-21 23:56:24 +00001091 case llvm::bitc::DEFINE_ABBREV:
1092 Stream.ReadAbbrevRecord();
1093 continue;
1094 default: break;
1095 }
1096
1097 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001098 const char *BlobStart = 0;
1099 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001100 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001101 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001102 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001103 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001104 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001105 case PP_MACRO_OBJECT_LIKE:
1106 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001107 // If we already have a macro, that means that we've hit the end
1108 // of the definition of the macro we were looking for. We're
1109 // done.
1110 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001111 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001112
Douglas Gregora3e41532011-07-28 20:55:49 +00001113 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001114 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001115 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001116 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001117 }
Mike Stump11289f42009-09-09 15:08:12 +00001118
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001119 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1120
1121 // If this macro has already been loaded, don't do so again.
1122 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1123 return;
1124
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001125 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1126 unsigned NextIndex = 3;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001127 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor51825b42011-09-09 22:02:16 +00001128 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001129
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001130 // Record this macro.
1131 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1132
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001133 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1134 if (UndefLoc.isValid())
1135 MI->setUndefLoc(UndefLoc);
1136
1137 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001138 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001139
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001140 bool IsPublic = Record[NextIndex++];
Douglas Gregorebf00492011-10-17 15:32:29 +00001141 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001142
Sebastian Redl539c5062010-08-18 23:57:32 +00001143 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001144 // Decode function-like macro info.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001145 bool isC99VarArgs = Record[NextIndex++];
1146 bool isGNUVarArgs = Record[NextIndex++];
Eli Friedman14d3c792012-11-14 02:18:46 +00001147 bool hasCommaPasting = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001148 MacroArgs.clear();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001149 unsigned NumArgs = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001150 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001151 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001152
1153 // Install function-like macro info.
1154 MI->setIsFunctionLike();
1155 if (isC99VarArgs) MI->setIsC99Varargs();
1156 if (isGNUVarArgs) MI->setIsGNUVarargs();
Eli Friedman14d3c792012-11-14 02:18:46 +00001157 if (hasCommaPasting) MI->setHasCommaPasting();
Douglas Gregor038c3382009-05-22 22:45:36 +00001158 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor51825b42011-09-09 22:02:16 +00001159 PP.getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001160 }
1161
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001162 if (DeserializationListener)
1163 DeserializationListener->MacroRead(GlobalID, MI);
1164
1165 // If an update record marked this as undefined, do so now.
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001166 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001167 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1168 if (Update != MacroUpdates.end()) {
1169 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregorcfa46a82012-10-12 00:16:50 +00001170 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1171 bool Hidden = false;
1172 if (unsigned SubmoduleID = Update->second[I].first) {
1173 if (Module *Owner = getSubmodule(SubmoduleID)) {
1174 if (Owner->NameVisibility == Module::Hidden) {
1175 // Note that this #undef is hidden.
1176 Hidden = true;
1177
1178 // Record this hiding for later.
1179 HiddenNamesMap[Owner].push_back(
1180 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1181 }
1182 }
1183 }
1184
1185 if (!Hidden) {
1186 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1187 if (PPMutationListener *Listener = PP.getPPMutationListener())
1188 Listener->UndefinedMacro(MI);
1189 break;
1190 }
1191 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001192 }
1193 MacroUpdates.erase(Update);
1194 }
1195
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001196 // Determine whether this macro definition is visible.
1197 bool Hidden = !MI->isPublic();
1198 if (!Hidden && GlobalSubmoduleID) {
1199 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1200 if (Owner->NameVisibility == Module::Hidden) {
1201 // The owning module is not visible, and this macro definition
1202 // should not be, either.
1203 Hidden = true;
1204
1205 // Note that this macro definition was hidden because its owning
1206 // module is not yet visible.
1207 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1208 }
1209 }
1210 }
1211 MI->setHidden(Hidden);
1212
Douglas Gregor5968b1b2012-10-11 21:07:39 +00001213 // Make sure we install the macro once we're done.
1214 AddLoadedMacroInfo.MI = MI;
1215 AddLoadedMacroInfo.II = II;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001216
1217 // Remember that we saw this macro last so that we add the tokens that
1218 // form its body to it.
1219 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001220
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001221 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1222 Record[NextIndex]) {
1223 // We have a macro definition. Register the association
1224 PreprocessedEntityID
1225 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1226 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1227 PPRec.RegisterMacroDefinition(Macro,
1228 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregoraae92242010-03-19 21:51:54 +00001229 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001230
Douglas Gregorc3366a52009-04-21 23:56:24 +00001231 ++NumMacrosRead;
1232 break;
1233 }
Mike Stump11289f42009-09-09 15:08:12 +00001234
Sebastian Redl539c5062010-08-18 23:57:32 +00001235 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001236 // If we see a TOKEN before a PP_MACRO_*, then the file is
1237 // erroneous, just pretend we didn't see this.
1238 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregorc3366a52009-04-21 23:56:24 +00001240 Token Tok;
1241 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001242 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001243 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001244 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001245 Tok.setIdentifierInfo(II);
1246 Tok.setKind((tok::TokenKind)Record[3]);
1247 Tok.setFlag((Token::TokenFlags)Record[4]);
1248 Macro->AddTokenToBody(Tok);
1249 break;
1250 }
David Blaikie8a40f702012-01-17 06:56:22 +00001251 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001252 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001253}
1254
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001255PreprocessedEntityID
Douglas Gregorde3ef502011-11-30 23:21:26 +00001256ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidisd67164e2011-09-19 20:40:02 +00001257 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001258 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1259 assert(I != M.PreprocessedEntityRemap.end()
1260 && "Invalid index into preprocessed entity index remap");
1261
1262 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001263}
1264
Douglas Gregord44252e2011-08-25 20:47:51 +00001265unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1266 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregor09b69892011-02-10 17:09:37 +00001267}
Douglas Gregord44252e2011-08-25 20:47:51 +00001268
1269HeaderFileInfoTrait::internal_key_type
1270HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1271
1272bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1273 if (strcmp(a, b) == 0)
1274 return true;
1275
1276 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1277 return false;
Douglas Gregore32e0542011-12-09 16:22:07 +00001278
1279 // Determine whether the actual files are equivalent.
1280 bool Result = false;
1281 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregord44252e2011-08-25 20:47:51 +00001282 return false;
1283
Douglas Gregore32e0542011-12-09 16:22:07 +00001284 return Result;
Douglas Gregord44252e2011-08-25 20:47:51 +00001285}
1286
1287std::pair<unsigned, unsigned>
1288HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1289 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1290 unsigned DataLen = (unsigned) *d++;
1291 return std::make_pair(KeyLen + 1, DataLen);
1292}
1293
1294HeaderFileInfoTrait::data_type
1295HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1296 unsigned DataLen) {
1297 const unsigned char *End = d + DataLen;
1298 using namespace clang::io;
1299 HeaderFileInfo HFI;
1300 unsigned Flags = *d++;
1301 HFI.isImport = (Flags >> 5) & 0x01;
1302 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1303 HFI.DirInfo = (Flags >> 2) & 0x03;
1304 HFI.Resolved = (Flags >> 1) & 0x01;
1305 HFI.IndexHeaderMapHeader = Flags & 0x01;
1306 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor7d75bf62011-10-17 18:53:12 +00001307 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1308 ReadUnalignedLE32(d));
Douglas Gregord44252e2011-08-25 20:47:51 +00001309 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1310 // The framework offset is 1 greater than the actual offset,
1311 // since 0 is used as an indicator for "no framework name".
1312 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1313 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1314 }
1315
1316 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1317 (void)End;
1318
1319 // This HeaderFileInfo was externally loaded.
1320 HFI.External = true;
1321 return HFI;
1322}
Douglas Gregor09b69892011-02-10 17:09:37 +00001323
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001324void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1325 II->setHadMacroDefinition(true);
1326 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1327 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001328}
1329
Sebastian Redl2c499f62010-08-18 23:56:43 +00001330void ASTReader::ReadDefinedMacros() {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001331 // Note that we are loading defined macros.
1332 Deserializing Macros(this);
1333
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001334 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1335 E = ModuleMgr.rend(); I != E; ++I) {
1336 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001337
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001338 // If there was no preprocessor block, skip this file.
1339 if (!MacroCursor.getBitStreamReader())
1340 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001341
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001342 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001343 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001344
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001345 RecordData Record;
1346 while (true) {
1347 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001348 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001349 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001350
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001351 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1352 // No known subblocks, always skip them.
1353 Cursor.ReadSubBlockID();
1354 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001355 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001356 return;
1357 }
1358 continue;
1359 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001360
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001361 if (Code == llvm::bitc::DEFINE_ABBREV) {
1362 Cursor.ReadAbbrevRecord();
1363 continue;
1364 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001365
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001366 // Read a record.
1367 const char *BlobStart;
1368 unsigned BlobLen;
1369 Record.clear();
1370 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1371 default: // Default behavior: ignore.
1372 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001373
Sebastian Redl539c5062010-08-18 23:57:32 +00001374 case PP_MACRO_OBJECT_LIKE:
1375 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001376 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001377 break;
1378
Sebastian Redl539c5062010-08-18 23:57:32 +00001379 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001380 // Ignore tokens.
1381 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001382 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001383 }
1384 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001385}
1386
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001387namespace {
1388 /// \brief Visitor class used to look up identifirs in an AST file.
1389 class IdentifierLookupVisitor {
1390 StringRef Name;
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001391 unsigned PriorGeneration;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001392 IdentifierInfo *Found;
1393 public:
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001394 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1395 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001396
Douglas Gregorde3ef502011-11-30 23:21:26 +00001397 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001398 IdentifierLookupVisitor *This
1399 = static_cast<IdentifierLookupVisitor *>(UserData);
1400
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001401 // If we've already searched this module file, skip it now.
1402 if (M.Generation <= This->PriorGeneration)
1403 return true;
1404
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001405 ASTIdentifierLookupTable *IdTable
1406 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1407 if (!IdTable)
1408 return false;
1409
Douglas Gregor247afcc2012-01-24 15:24:38 +00001410 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1411 M, This->Found);
1412
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001413 std::pair<const char*, unsigned> Key(This->Name.begin(),
1414 This->Name.size());
Douglas Gregor247afcc2012-01-24 15:24:38 +00001415 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001416 if (Pos == IdTable->end())
1417 return false;
1418
1419 // Dereferencing the iterator has the effect of building the
1420 // IdentifierInfo node and populating it with the various
1421 // declarations it needs.
1422 This->Found = *Pos;
1423 return true;
1424 }
1425
1426 // \brief Retrieve the identifier info found within the module
1427 // files.
1428 IdentifierInfo *getIdentifierInfo() const { return Found; }
1429 };
1430}
1431
1432void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001433 // Note that we are loading an identifier.
1434 Deserializing AnIdentifier(this);
1435
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001436 unsigned PriorGeneration = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001437 if (getContext().getLangOpts().Modules)
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001438 PriorGeneration = IdentifierGeneration[&II];
1439
1440 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1441 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1442 markIdentifierUpToDate(&II);
1443}
1444
1445void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1446 if (!II)
1447 return;
1448
1449 II->setOutOfDate(false);
1450
1451 // Update the generation for this identifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001452 if (getContext().getLangOpts().Modules)
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001453 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001454}
1455
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001456llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor4b29c162012-10-22 23:51:00 +00001457ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001458 // If this ID is bogus, just return an empty input file.
1459 if (ID == 0 || ID > F.InputFilesLoaded.size())
1460 return InputFile();
1461
1462 // If we've already loaded this input file, return it.
1463 if (F.InputFilesLoaded[ID-1].getPointer())
1464 return F.InputFilesLoaded[ID-1];
1465
1466 // Go find this input file.
1467 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1468 SavedStreamPosition SavedPosition(Cursor);
1469 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1470
1471 unsigned Code = Cursor.ReadCode();
1472 RecordData Record;
1473 const char *BlobStart = 0;
1474 unsigned BlobLen = 0;
1475 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1476 &BlobStart, &BlobLen)) {
1477 case INPUT_FILE: {
1478 unsigned StoredID = Record[0];
1479 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi395a5742012-10-22 21:50:39 +00001480 (void)StoredID;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001481 off_t StoredSize = (off_t)Record[1];
1482 time_t StoredTime = (time_t)Record[2];
1483 bool Overridden = (bool)Record[3];
1484
1485 // Get the file entry for this input file.
1486 StringRef OrigFilename(BlobStart, BlobLen);
1487 std::string Filename = OrigFilename;
1488 MaybeAddSystemRootToFilename(F, Filename);
1489 const FileEntry *File
1490 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1491 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1492
1493 // If we didn't find the file, resolve it relative to the
1494 // original directory from which this AST file was created.
1495 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1496 F.OriginalDir != CurrentDir) {
1497 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1498 F.OriginalDir,
1499 CurrentDir);
1500 if (!Resolved.empty())
1501 File = FileMgr.getFile(Resolved);
1502 }
1503
1504 // For an overridden file, create a virtual file with the stored
1505 // size/timestamp.
1506 if (Overridden && File == 0) {
1507 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1508 }
1509
1510 if (File == 0) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001511 if (Complain) {
1512 std::string ErrorStr = "could not find file '";
1513 ErrorStr += Filename;
1514 ErrorStr += "' referenced by AST file";
1515 Error(ErrorStr.c_str());
1516 }
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001517 return InputFile();
1518 }
1519
1520 // Note that we've loaded this input file.
1521 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1522
1523 // Check if there was a request to override the contents of the file
1524 // that was part of the precompiled header. Overridding such a file
1525 // can lead to problems when lexing using the source locations from the
1526 // PCH.
1527 SourceManager &SM = getSourceManager();
1528 if (!Overridden && SM.isFileOverridden(File)) {
1529 Error(diag::err_fe_pch_file_overridden, Filename);
1530 // After emitting the diagnostic, recover by disabling the override so
1531 // that the original file will be used.
1532 SM.disableFileContentsOverride(File);
1533 // The FileEntry is a virtual file entry with the size of the contents
1534 // that would override the original contents. Set it to the original's
1535 // size/time.
1536 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1537 StoredSize, StoredTime);
1538 }
1539
1540 // For an overridden file, there is nothing to validate.
1541 if (Overridden)
1542 return InputFile(File, Overridden);
1543
1544 // The stat info from the FileEntry came from the cached stat
1545 // info of the PCH, so we cannot trust it.
1546 struct stat StatBuf;
1547 if (::stat(File->getName(), &StatBuf) != 0) {
1548 StatBuf.st_size = File->getSize();
1549 StatBuf.st_mtime = File->getModificationTime();
1550 }
1551
1552 if ((StoredSize != StatBuf.st_size
1553#if !defined(LLVM_ON_WIN32)
1554 // In our regression testing, the Windows file system seems to
1555 // have inconsistent modification times that sometimes
1556 // erroneously trigger this error-handling path.
1557 || StoredTime != StatBuf.st_mtime
1558#endif
1559 )) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001560 if (Complain)
1561 Error(diag::err_fe_pch_file_modified, Filename);
1562
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001563 return InputFile();
1564 }
1565
1566 return InputFile(File, Overridden);
1567 }
1568 }
1569
1570 return InputFile();
1571}
1572
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001573const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor451dffa2012-10-18 21:47:16 +00001574 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001575 std::string Filename = filenameStrRef;
Douglas Gregor451dffa2012-10-18 21:47:16 +00001576 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001577 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor451dffa2012-10-18 21:47:16 +00001578 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1579 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001580 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor451dffa2012-10-18 21:47:16 +00001581 M.OriginalDir,
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001582 CurrentDir);
1583 if (!resolved.empty())
1584 File = FileMgr.getFile(resolved);
1585 }
1586
1587 return File;
1588}
1589
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001590/// \brief If we are loading a relocatable PCH file, and the filename is
1591/// not an absolute path, add the system root to the beginning of the file
1592/// name.
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001593void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1594 std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001595 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00001596 if (!M.RelocatablePCH)
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001597 return;
Mike Stump11289f42009-09-09 15:08:12 +00001598
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001599 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001600 return;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001601
Douglas Gregorc567ba22011-07-22 16:35:34 +00001602 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001603 // If no system root was given, default to '/'
1604 Filename.insert(Filename.begin(), '/');
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001605 return;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001606 }
Mike Stump11289f42009-09-09 15:08:12 +00001607
Douglas Gregorc567ba22011-07-22 16:35:34 +00001608 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001609 if (isysroot[Length - 1] != '/')
1610 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001611
Douglas Gregorc567ba22011-07-22 16:35:34 +00001612 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001613}
1614
Douglas Gregor4b29c162012-10-22 23:51:00 +00001615ASTReader::ASTReadResult
1616ASTReader::ReadControlBlock(ModuleFile &F,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001617 llvm::SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor4b29c162012-10-22 23:51:00 +00001618 unsigned ClientLoadCapabilities) {
Douglas Gregor112b9072012-10-18 05:31:06 +00001619 llvm::BitstreamCursor &Stream = F.Stream;
1620
1621 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1622 Error("malformed block record in AST file");
1623 return Failure;
1624 }
1625
1626 // Read all of the records and blocks in the control block.
1627 RecordData Record;
1628 while (!Stream.AtEndOfStream()) {
1629 unsigned Code = Stream.ReadCode();
1630 if (Code == llvm::bitc::END_BLOCK) {
1631 if (Stream.ReadBlockEnd()) {
1632 Error("error at end of control block in AST file");
1633 return Failure;
1634 }
1635
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001636 // Validate all of the input files.
1637 if (!DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001638 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001639 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor4b29c162012-10-22 23:51:00 +00001640 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001641 return OutOfDate;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001642 }
1643
Douglas Gregor112b9072012-10-18 05:31:06 +00001644 return Success;
1645 }
1646
1647 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001648 switch (Stream.ReadSubBlockID()) {
1649 case INPUT_FILES_BLOCK_ID:
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001650 F.InputFilesCursor = Stream;
1651 if (Stream.SkipBlock() || // Skip with the main cursor
1652 // Read the abbreviations
1653 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1654 Error("malformed block record in AST file");
Douglas Gregor72be3902012-10-19 00:38:02 +00001655 return Failure;
Douglas Gregor72be3902012-10-19 00:38:02 +00001656 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001657 continue;
Douglas Gregor72be3902012-10-19 00:38:02 +00001658
1659 default:
1660 if (!Stream.SkipBlock())
1661 continue;
1662 break;
1663 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001664
1665 Error("malformed block record in AST file");
1666 return Failure;
1667 }
1668
1669 if (Code == llvm::bitc::DEFINE_ABBREV) {
1670 Stream.ReadAbbrevRecord();
1671 continue;
1672 }
1673
1674 // Read and process a record.
1675 Record.clear();
1676 const char *BlobStart = 0;
1677 unsigned BlobLen = 0;
1678 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1679 &BlobStart, &BlobLen)) {
1680 case METADATA: {
1681 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001682 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1683 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1684 : diag::warn_pch_version_too_new);
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001685 return VersionMismatch;
Douglas Gregor112b9072012-10-18 05:31:06 +00001686 }
1687
1688 bool hasErrors = Record[5];
1689 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1690 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001691 return HadErrors;
Douglas Gregor112b9072012-10-18 05:31:06 +00001692 }
1693
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00001694 F.RelocatablePCH = Record[4];
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001695
1696 const std::string &CurBranch = getClangFullRepositoryVersion();
1697 StringRef ASTBranch(BlobStart, BlobLen);
1698 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001699 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1700 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001701 return VersionMismatch;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001702 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001703 break;
1704 }
1705
1706 case IMPORTS: {
1707 // Load each of the imported PCH files.
1708 unsigned Idx = 0, N = Record.size();
1709 while (Idx < N) {
1710 // Read information about the AST file.
1711 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001712 // The import location will be the local one for now; we will adjust
1713 // all import locations of module imports after the global source
1714 // location info are setup.
1715 SourceLocation ImportLoc =
1716 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor112b9072012-10-18 05:31:06 +00001717 unsigned Length = Record[Idx++];
1718 SmallString<128> ImportedFile(Record.begin() + Idx,
1719 Record.begin() + Idx + Length);
1720 Idx += Length;
1721
1722 // Load the AST file.
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001723 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor4b29c162012-10-22 23:51:00 +00001724 ClientLoadCapabilities)) {
Douglas Gregor112b9072012-10-18 05:31:06 +00001725 case Failure: return Failure;
1726 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001727 case OutOfDate: return OutOfDate;
1728 case VersionMismatch: return VersionMismatch;
1729 case ConfigurationMismatch: return ConfigurationMismatch;
1730 case HadErrors: return HadErrors;
Douglas Gregor112b9072012-10-18 05:31:06 +00001731 case Success: break;
1732 }
1733 }
1734 break;
1735 }
1736
Douglas Gregor4b29c162012-10-22 23:51:00 +00001737 case LANGUAGE_OPTIONS: {
1738 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1739 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00001740 ParseLanguageOptions(Record, Complain, *Listener) &&
1741 !DisableValidation)
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001742 return ConfigurationMismatch;
Douglas Gregor112b9072012-10-18 05:31:06 +00001743 break;
Douglas Gregor4b29c162012-10-22 23:51:00 +00001744 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001745
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001746 case TARGET_OPTIONS: {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00001747 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1748 if (Listener && &F == *ModuleMgr.begin() &&
1749 ParseTargetOptions(Record, Complain, *Listener) &&
1750 !DisableValidation)
1751 return ConfigurationMismatch;
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001752 break;
1753 }
1754
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001755 case DIAGNOSTIC_OPTIONS: {
1756 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1757 if (Listener && &F == *ModuleMgr.begin() &&
1758 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1759 !DisableValidation)
1760 return ConfigurationMismatch;
1761 break;
1762 }
Douglas Gregorc6317db2012-10-24 15:49:58 +00001763
1764 case FILE_SYSTEM_OPTIONS: {
1765 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1766 if (Listener && &F == *ModuleMgr.begin() &&
1767 ParseFileSystemOptions(Record, Complain, *Listener) &&
1768 !DisableValidation)
1769 return ConfigurationMismatch;
1770 break;
1771 }
1772
Douglas Gregor2d302362012-10-24 16:50:34 +00001773 case HEADER_SEARCH_OPTIONS: {
1774 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1775 if (Listener && &F == *ModuleMgr.begin() &&
1776 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1777 !DisableValidation)
1778 return ConfigurationMismatch;
1779 break;
1780 }
1781
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001782 case PREPROCESSOR_OPTIONS: {
1783 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1784 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor55358ed2012-10-25 00:07:54 +00001785 ParsePreprocessorOptions(Record, Complain, *Listener,
1786 SuggestedPredefines) &&
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001787 !DisableValidation)
1788 return ConfigurationMismatch;
1789 break;
1790 }
1791
Douglas Gregorfad10d82012-10-18 18:36:53 +00001792 case ORIGINAL_FILE:
Douglas Gregor451dffa2012-10-18 21:47:16 +00001793 F.OriginalSourceFileID = FileID::get(Record[0]);
1794 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
1795 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1796 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor112b9072012-10-18 05:31:06 +00001797 break;
1798
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001799 case ORIGINAL_FILE_ID:
1800 F.OriginalSourceFileID = FileID::get(Record[0]);
1801 break;
1802
Douglas Gregor112b9072012-10-18 05:31:06 +00001803 case ORIGINAL_PCH_DIR:
Douglas Gregor451dffa2012-10-18 21:47:16 +00001804 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor112b9072012-10-18 05:31:06 +00001805 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00001806
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001807 case INPUT_FILE_OFFSETS:
1808 F.InputFileOffsets = (const uint32_t *)BlobStart;
1809 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor72be3902012-10-19 00:38:02 +00001810 break;
1811 }
Douglas Gregor72be3902012-10-19 00:38:02 +00001812 }
1813
1814 Error("premature end of bitstream in AST file");
1815 return Failure;
1816}
1817
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001818bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001819 llvm::BitstreamCursor &Stream = F.Stream;
1820
Sebastian Redl539c5062010-08-18 23:57:32 +00001821 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001822 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001823 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001824 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001825
Douglas Gregor112b9072012-10-18 05:31:06 +00001826 // Read all of the records and blocks for the AST file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001827 RecordData Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001828 while (!Stream.AtEndOfStream()) {
1829 unsigned Code = Stream.ReadCode();
1830 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001831 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001832 Error("error at end of module block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001833 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001834 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001835
Argyrios Kyrtzidis6fa16822012-09-21 01:30:00 +00001836 DeclContext *DC = Context.getTranslationUnitDecl();
1837 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1838 DC->setMustBuildLookupTable();
1839
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001840 return false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001841 }
1842
1843 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1844 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001845 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001846 // We lazily load the decls block, but we want to set up the
1847 // DeclsCursor cursor to point into it. Clone our current bitcode
1848 // cursor to it, enter the block and read the abbrevs in that block.
1849 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001850 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001851 if (Stream.SkipBlock() || // Skip with the main cursor.
1852 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001853 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001854 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001855 return true;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001856 }
1857 break;
Mike Stump11289f42009-09-09 15:08:12 +00001858
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001859 case DECL_UPDATES_BLOCK_ID:
1860 if (Stream.SkipBlock()) {
1861 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001862 return true;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001863 }
1864 break;
1865
Sebastian Redl539c5062010-08-18 23:57:32 +00001866 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001867 F.MacroCursor = Stream;
Douglas Gregor51825b42011-09-09 22:02:16 +00001868 if (!PP.getExternalSource())
1869 PP.setExternalSource(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001870
Douglas Gregor796d76a2010-10-20 22:00:55 +00001871 if (Stream.SkipBlock() ||
1872 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001873 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001874 return true;
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001875 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001876 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001877 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001878
Douglas Gregor92a96f52011-02-08 21:58:10 +00001879 case PREPROCESSOR_DETAIL_BLOCK_ID:
1880 F.PreprocessorDetailCursor = Stream;
1881 if (Stream.SkipBlock() ||
1882 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1883 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1884 Error("malformed preprocessor detail record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001885 return true;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001886 }
1887 F.PreprocessorDetailStartOffset
1888 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor51825b42011-09-09 22:02:16 +00001889
1890 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +00001891 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor51825b42011-09-09 22:02:16 +00001892 if (!PP.getPreprocessingRecord()->getExternalSource())
1893 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001894 break;
1895
Sebastian Redl539c5062010-08-18 23:57:32 +00001896 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001897 if (ReadSourceManagerBlock(F))
1898 return true;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001899 break;
Douglas Gregor69021972011-11-30 17:33:56 +00001900
1901 case SUBMODULE_BLOCK_ID:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001902 if (ReadSubmoduleBlock(F))
1903 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00001904 break;
1905
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001906 case COMMENTS_BLOCK_ID: {
1907 llvm::BitstreamCursor C = Stream;
1908 if (Stream.SkipBlock() ||
1909 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1910 Error("malformed comments block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001911 return true;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001912 }
1913 CommentsCursors.push_back(std::make_pair(C, &F));
1914 break;
1915 }
1916
Douglas Gregor69021972011-11-30 17:33:56 +00001917 default:
1918 if (!Stream.SkipBlock())
1919 break;
1920 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001921 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001922 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001923 continue;
1924 }
1925
1926 if (Code == llvm::bitc::DEFINE_ABBREV) {
1927 Stream.ReadAbbrevRecord();
1928 continue;
1929 }
1930
1931 // Read and process a record.
1932 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001933 const char *BlobStart = 0;
1934 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001935 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001936 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001937 default: // Default behavior: ignore.
1938 break;
1939
Douglas Gregor5204bde2011-08-02 16:26:37 +00001940 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001941 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001942 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001943 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001944 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001945 F.TypeOffsets = (const uint32_t *)BlobStart;
1946 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001947 unsigned LocalBaseTypeIndex = Record[1];
1948 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00001949
Douglas Gregor5204bde2011-08-02 16:26:37 +00001950 if (F.LocalNumTypes > 0) {
1951 // Introduce the global -> local mapping for types within this module.
1952 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1953
1954 // Introduce the local -> global mapping for types within this module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00001955 F.TypeRemap.insertOrReplace(
1956 std::make_pair(LocalBaseTypeIndex,
1957 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00001958
1959 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1960 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001961 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00001962 }
1963
Douglas Gregorf7180622011-08-03 15:48:04 +00001964 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001965 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001966 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001967 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001968 }
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00001969 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl9e687992010-07-19 22:06:55 +00001970 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00001971 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001972 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00001973
Douglas Gregorf7180622011-08-03 15:48:04 +00001974 if (F.LocalNumDecls > 0) {
1975 // Introduce the global -> local mapping for declarations within this
1976 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00001977 GlobalDeclMap.insert(
1978 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00001979
1980 // Introduce the local -> global mapping for declarations within this
1981 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00001982 F.DeclRemap.insertOrReplace(
1983 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregorf7180622011-08-03 15:48:04 +00001984
Douglas Gregor05f10352011-12-17 23:38:30 +00001985 // Introduce the global -> local mapping for declarations within this
1986 // module.
1987 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1988
Douglas Gregorf7180622011-08-03 15:48:04 +00001989 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1990 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001991 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00001992 }
1993
Sebastian Redl539c5062010-08-18 23:57:32 +00001994 case TU_UPDATE_LEXICAL: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00001995 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00001996 DeclContextInfo &Info = F.DeclContextInfos[TU];
1997 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1998 Info.NumLexicalDecls
1999 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor4163aca2011-09-09 21:34:22 +00002000 TU->setHasExternalLexicalStorage(true);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002001 break;
2002 }
2003
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002004 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00002005 unsigned Idx = 0;
2006 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00002007 ASTDeclContextNameLookupTable *Table =
2008 ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00002009 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002010 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00002011 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor4163aca2011-09-09 21:34:22 +00002012 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2013 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002014 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregordab42432011-08-12 00:15:20 +00002015 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002016 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00002017 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002018 break;
2019 }
2020
Sebastian Redl539c5062010-08-18 23:57:32 +00002021 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002022 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002023 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002024 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002025 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002026 (const unsigned char *)F.IdentifierTableData + Record[0],
2027 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002028 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor51825b42011-09-09 22:02:16 +00002029
2030 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002031 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002032 break;
2033
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002034 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002035 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002036 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002037 return true;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002038 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002039 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2040 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002041 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002042 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00002043
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002044 if (F.LocalNumIdentifiers > 0) {
2045 // Introduce the global -> local mapping for identifiers within this
2046 // module.
2047 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2048 &F));
2049
2050 // Introduce the local -> global mapping for identifiers within this
2051 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002052 F.IdentifierRemap.insertOrReplace(
2053 std::make_pair(LocalBaseIdentifierID,
2054 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002055
2056 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2057 + F.LocalNumIdentifiers);
2058 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002059 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002060 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002061
Sebastian Redl539c5062010-08-18 23:57:32 +00002062 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002063 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2064 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002065 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002066
Sebastian Redl539c5062010-08-18 23:57:32 +00002067 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002068 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2069 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002070 break;
2071
Sebastian Redl539c5062010-08-18 23:57:32 +00002072 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002073 TotalNumStatements += Record[0];
2074 TotalNumMacros += Record[1];
2075 TotalLexicalDeclContexts += Record[2];
2076 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002077 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002078
Sebastian Redl539c5062010-08-18 23:57:32 +00002079 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002080 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2081 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002082 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002083
Alexis Hunt27a761d2011-05-04 23:29:54 +00002084 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002085 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2086 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002087 break;
2088
Sebastian Redl539c5062010-08-18 23:57:32 +00002089 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002090 if (Record.size() % 4 != 0) {
2091 Error("invalid weak identifiers record");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002092 return true;
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002093 }
2094
2095 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2096 // files. This isn't the way to do it :)
2097 WeakUndeclaredIdentifiers.clear();
2098
2099 // Translate the weak, undeclared identifiers into global IDs.
2100 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2101 WeakUndeclaredIdentifiers.push_back(
2102 getGlobalIdentifierID(F, Record[I++]));
2103 WeakUndeclaredIdentifiers.push_back(
2104 getGlobalIdentifierID(F, Record[I++]));
2105 WeakUndeclaredIdentifiers.push_back(
2106 ReadSourceLocation(F, Record, I).getRawEncoding());
2107 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2108 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002109 break;
2110
Sebastian Redl539c5062010-08-18 23:57:32 +00002111 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002112 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2113 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002114 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002115
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002116 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002117 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002118 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002119 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002120 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002121
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002122 if (F.LocalNumSelectors > 0) {
2123 // Introduce the global -> local mapping for selectors within this
2124 // module.
2125 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2126
2127 // Introduce the local -> global mapping for selectors within this
2128 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002129 F.SelectorRemap.insertOrReplace(
2130 std::make_pair(LocalBaseSelectorID,
2131 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002132
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002133 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2134 }
2135 break;
2136 }
2137
Sebastian Redl539c5062010-08-18 23:57:32 +00002138 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002139 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002140 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002141 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002142 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002143 F.SelectorLookupTableData + Record[0],
2144 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002145 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002146 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002147 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002148
Sebastian Redl96371b42010-09-22 00:42:30 +00002149 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002150 if (!Record.empty()) {
2151 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2152 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2153 Record[Idx++]));
2154 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2155 getRawEncoding());
2156 }
2157 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002158 break;
2159
Sebastian Redl539c5062010-08-18 23:57:32 +00002160 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002161 if (!Record.empty() && Listener)
Argyrios Kyrtzidise445c722012-10-10 02:12:47 +00002162 Listener->ReadCounter(F, Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002163 break;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002164
2165 case FILE_SORTED_DECLS:
2166 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002167 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002168 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002169
Douglas Gregor925296b2011-07-19 16:10:42 +00002170 case SOURCE_LOCATION_OFFSETS: {
2171 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002172 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002173 unsigned SLocSpaceSize = Record[1];
Douglas Gregor925296b2011-07-19 16:10:42 +00002174 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002175 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2176 SLocSpaceSize);
Douglas Gregor925296b2011-07-19 16:10:42 +00002177 // Make our entry in the range map. BaseID is negative and growing, so
2178 // we invert it. Because we invert it, though, we need the other end of
2179 // the range.
2180 unsigned RangeStart =
2181 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2182 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2183 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2184
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002185 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2186 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2187 GlobalSLocOffsetMap.insert(
2188 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2189 - SLocSpaceSize,&F));
2190
Douglas Gregor925296b2011-07-19 16:10:42 +00002191 // Initialize the remapping table.
2192 // Invalid stays invalid.
2193 F.SLocRemap.insert(std::make_pair(0U, 0));
2194 // This module. Base was 2 when being compiled.
2195 F.SLocRemap.insert(std::make_pair(2U,
2196 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002197
2198 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002199 break;
2200 }
2201
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002202 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002203 // Additional remapping information.
2204 const unsigned char *Data = (const unsigned char*)BlobStart;
2205 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002206
2207 // Continuous range maps we may be updating in our module.
2208 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002209 ContinuousRangeMap<uint32_t, int, 2>::Builder
2210 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002211 ContinuousRangeMap<uint32_t, int, 2>::Builder
2212 MacroRemap(F.MacroRemap);
2213 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002214 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2215 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor253eefe2011-12-01 00:59:36 +00002216 SubmoduleRemap(F.SubmoduleRemap);
2217 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002218 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002219 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002220 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2221
Douglas Gregor925296b2011-07-19 16:10:42 +00002222 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002223 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002224 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002225 Data += Len;
Douglas Gregorde3ef502011-11-30 23:21:26 +00002226 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002227 if (!OM) {
2228 Error("SourceLocation remap refers to unknown module");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002229 return true;
Douglas Gregor925296b2011-07-19 16:10:42 +00002230 }
Douglas Gregor00659902011-08-02 10:56:51 +00002231
2232 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2233 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002234 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002235 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002236 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002237 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2238 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002239 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002240
2241 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2242 SLocRemap.insert(std::make_pair(SLocOffset,
2243 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002244 IdentifierRemap.insert(
2245 std::make_pair(IdentifierIDOffset,
2246 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002247 MacroRemap.insert(std::make_pair(MacroIDOffset,
2248 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002249 PreprocessedEntityRemap.insert(
2250 std::make_pair(PreprocessedEntityIDOffset,
2251 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor253eefe2011-12-01 00:59:36 +00002252 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2253 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002254 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2255 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002256 DeclRemap.insert(std::make_pair(DeclIDOffset,
2257 OM->BaseDeclID - DeclIDOffset));
2258
Douglas Gregor5204bde2011-08-02 16:26:37 +00002259 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002260 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor05f10352011-12-17 23:38:30 +00002261
2262 // Global -> local mappings.
2263 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregor925296b2011-07-19 16:10:42 +00002264 }
2265 break;
2266 }
2267
Douglas Gregor925296b2011-07-19 16:10:42 +00002268 case SOURCE_MANAGER_LINE_TABLE:
2269 if (ParseLineTable(F, Record))
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002270 return true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002271 break;
2272
Douglas Gregor925296b2011-07-19 16:10:42 +00002273 case SOURCE_LOCATION_PRELOADS: {
2274 // Need to transform from the local view (1-based IDs) to the global view,
2275 // which is based off F.SLocEntryBaseID.
Douglas Gregora918bab2011-08-25 21:09:44 +00002276 if (!F.PreloadSLocEntries.empty()) {
2277 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002278 return true;
Douglas Gregora918bab2011-08-25 21:09:44 +00002279 }
2280
2281 F.PreloadSLocEntries.swap(Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002282 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002283 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002284
Sebastian Redl539c5062010-08-18 23:57:32 +00002285 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002286 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2287 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002288 break;
2289
Sebastian Redl539c5062010-08-18 23:57:32 +00002290 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002291 if (Record.size() % 3 != 0) {
2292 Error("Invalid VTABLE_USES record");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002293 return true;
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002294 }
2295
Sebastian Redl08aca90252010-08-05 18:21:25 +00002296 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002297 // FIXME: Modules will have some trouble with this. This is clearly not
2298 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002299 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002300
2301 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2302 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2303 VTableUses.push_back(
2304 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2305 VTableUses.push_back(Record[Idx++]);
2306 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002307 break;
2308
Sebastian Redl539c5062010-08-18 23:57:32 +00002309 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002310 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2311 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002312 break;
2313
Sebastian Redl539c5062010-08-18 23:57:32 +00002314 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002315 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann63469422c2012-10-02 09:09:43 +00002316 Error("Invalid existing PendingInstantiations");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002317 return true;
Axel Naumann63469422c2012-10-02 09:09:43 +00002318 }
2319
2320 if (Record.size() % 2 != 0) {
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002321 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002322 return true;
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002323 }
Axel Naumann63469422c2012-10-02 09:09:43 +00002324
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002325 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2326 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2327 PendingInstantiations.push_back(
2328 ReadSourceLocation(F, Record, I).getRawEncoding());
2329 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002330 break;
2331
Sebastian Redl539c5062010-08-18 23:57:32 +00002332 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002333 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002334 // FIXME: Modules will have some trouble with this.
2335 SemaDeclRefs.clear();
2336 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2337 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002338 break;
2339
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002340 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002341 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2342 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2343 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002344
2345 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002346
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002347 unsigned StartingID;
Douglas Gregor51825b42011-09-09 22:02:16 +00002348 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +00002349 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor51825b42011-09-09 22:02:16 +00002350 if (!PP.getPreprocessingRecord()->getExternalSource())
2351 PP.getPreprocessingRecord()->SetExternalSource(*this);
2352 StartingID
2353 = PP.getPreprocessingRecord()
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002354 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002355 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002356
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002357 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002358 // Introduce the global -> local mapping for preprocessed entities in
2359 // this module.
2360 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2361
2362 // Introduce the local -> global mapping for preprocessed entities in
2363 // this module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002364 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002365 std::make_pair(LocalBasePreprocessedEntityID,
2366 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2367 }
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002368
Douglas Gregoraae92242010-03-19 21:51:54 +00002369 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002370 }
2371
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002372 case DECL_UPDATE_OFFSETS: {
2373 if (Record.size() % 2 != 0) {
2374 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002375 return true;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002376 }
2377 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002378 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2379 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002380 break;
2381 }
2382
Sebastian Redl539c5062010-08-18 23:57:32 +00002383 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002384 if (Record.size() % 3 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002385 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002386 return true;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002387 }
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002388 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregorf7180622011-08-03 15:48:04 +00002389 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002390 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002391 break;
2392 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002393
Douglas Gregor404cdde2012-01-27 01:47:08 +00002394 case OBJC_CATEGORIES_MAP: {
2395 if (F.LocalNumObjCCategoriesInMap != 0) {
2396 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002397 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002398 }
Douglas Gregor404cdde2012-01-27 01:47:08 +00002399
2400 F.LocalNumObjCCategoriesInMap = Record[0];
2401 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002402 break;
2403 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002404
Douglas Gregor404cdde2012-01-27 01:47:08 +00002405 case OBJC_CATEGORIES:
2406 F.ObjCCategories.swap(Record);
2407 break;
2408
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002409 case CXX_BASE_SPECIFIER_OFFSETS: {
2410 if (F.LocalNumCXXBaseSpecifiers != 0) {
2411 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002412 return true;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002413 }
2414
2415 F.LocalNumCXXBaseSpecifiers = Record[0];
2416 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002417 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002418 break;
2419 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002420
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002421 case DIAG_PRAGMA_MAPPINGS:
Douglas Gregor925296b2011-07-19 16:10:42 +00002422 if (F.PragmaDiagMappings.empty())
2423 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002424 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002425 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2426 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002427 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002428
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002429 case CUDA_SPECIAL_DECL_REFS:
2430 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002431 // FIXME: Modules will have trouble with this.
2432 CUDASpecialDeclRefs.clear();
2433 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2434 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002435 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002436
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002437 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002438 F.HeaderFileInfoTableData = BlobStart;
2439 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002440 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002441 if (Record[0]) {
2442 F.HeaderFileInfoTable
2443 = HeaderFileInfoLookupTable::Create(
2444 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002445 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002446 HeaderFileInfoTrait(*this, F,
Douglas Gregor51825b42011-09-09 22:02:16 +00002447 &PP.getHeaderSearchInfo(),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002448 BlobStart + Record[2]));
Douglas Gregor51825b42011-09-09 22:02:16 +00002449
2450 PP.getHeaderSearchInfo().SetExternalSource(this);
2451 if (!PP.getHeaderSearchInfo().getExternalLookup())
2452 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002453 }
2454 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002455 }
2456
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002457 case FP_PRAGMA_OPTIONS:
2458 // Later tables overwrite earlier ones.
2459 FPPragmaOptions.swap(Record);
2460 break;
2461
2462 case OPENCL_EXTENSIONS:
2463 // Later tables overwrite earlier ones.
2464 OpenCLExtensions.swap(Record);
2465 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002466
2467 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002468 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2469 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002470 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002471
2472 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002473 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2474 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002475 break;
Douglas Gregor0a839132011-12-03 00:59:55 +00002476
2477 case IMPORTED_MODULES: {
2478 if (F.Kind != MK_Module) {
2479 // If we aren't loading a module (which has its own exports), make
2480 // all of the imported modules visible.
2481 // FIXME: Deal with macros-only imports.
2482 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2483 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2484 ImportedModules.push_back(GlobalID);
2485 }
2486 }
2487 break;
Douglas Gregor05f10352011-12-17 23:38:30 +00002488 }
Douglas Gregor358cd442012-01-15 16:58:34 +00002489
Douglas Gregor05f10352011-12-17 23:38:30 +00002490 case LOCAL_REDECLARATIONS: {
Douglas Gregor358cd442012-01-15 16:58:34 +00002491 F.RedeclarationChains.swap(Record);
2492 break;
2493 }
2494
2495 case LOCAL_REDECLARATIONS_MAP: {
2496 if (F.LocalNumRedeclarationsInMap != 0) {
2497 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002498 return true;
Douglas Gregor05f10352011-12-17 23:38:30 +00002499 }
Douglas Gregor0a839132011-12-03 00:59:55 +00002500
Douglas Gregor358cd442012-01-15 16:58:34 +00002501 F.LocalNumRedeclarationsInMap = Record[0];
2502 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregor05f10352011-12-17 23:38:30 +00002503 break;
Douglas Gregor0a839132011-12-03 00:59:55 +00002504 }
Douglas Gregor464b0ca2011-12-22 21:40:42 +00002505
2506 case MERGED_DECLARATIONS: {
2507 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2508 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2509 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2510 for (unsigned N = Record[Idx++]; N > 0; --N)
2511 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2512 }
2513 break;
2514 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002515
2516 case MACRO_OFFSET: {
2517 if (F.LocalNumMacros != 0) {
2518 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002519 return true;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002520 }
2521 F.MacroOffsets = (const uint32_t *)BlobStart;
2522 F.LocalNumMacros = Record[0];
2523 unsigned LocalBaseMacroID = Record[1];
2524 F.BaseMacroID = getTotalNumMacros();
2525
2526 if (F.LocalNumMacros > 0) {
2527 // Introduce the global -> local mapping for macros within this module.
2528 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2529
2530 // Introduce the local -> global mapping for macros within this module.
2531 F.MacroRemap.insertOrReplace(
2532 std::make_pair(LocalBaseMacroID,
2533 F.BaseMacroID - LocalBaseMacroID));
2534
2535 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2536 }
2537 break;
2538 }
2539
2540 case MACRO_UPDATES: {
2541 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2542 MacroID ID = getGlobalMacroID(F, Record[I++]);
2543 if (I == N)
2544 break;
2545
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002546 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2547 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2548 MacroUpdate Update;
2549 Update.UndefLoc = UndefLoc;
2550 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002551 }
2552 break;
2553 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002554 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002555 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002556 Error("premature end of bitstream in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002557 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002558}
2559
Douglas Gregorcf68c582011-12-01 22:20:10 +00002560void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002561 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002562 switch (Names[I].getKind()) {
2563 case HiddenName::Declaration:
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002564 Names[I].getDecl()->Hidden = false;
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002565 break;
2566
2567 case HiddenName::MacroVisibility: {
2568 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2569 Macro.second->setHidden(!Macro.second->isPublic());
2570 if (Macro.second->isDefined()) {
2571 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2572 }
2573 break;
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002574 }
2575
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002576 case HiddenName::MacroUndef: {
2577 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2578 if (Macro.second->isDefined()) {
2579 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2580 if (PPMutationListener *Listener = PP.getPPMutationListener())
2581 Listener->UndefinedMacro(Macro.second);
2582 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2583 }
2584 break;
2585 }
Douglas Gregor0abc2622011-12-20 22:06:13 +00002586 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002587 }
Douglas Gregorcf68c582011-12-01 22:20:10 +00002588}
2589
Douglas Gregorff2be532011-12-01 17:11:21 +00002590void ASTReader::makeModuleVisible(Module *Mod,
2591 Module::NameVisibilityKind NameVisibility) {
2592 llvm::SmallPtrSet<Module *, 4> Visited;
2593 llvm::SmallVector<Module *, 4> Stack;
2594 Stack.push_back(Mod);
2595 while (!Stack.empty()) {
2596 Mod = Stack.back();
2597 Stack.pop_back();
2598
2599 if (NameVisibility <= Mod->NameVisibility) {
2600 // This module already has this level of visibility (or greater), so
2601 // there is nothing more to do.
2602 continue;
2603 }
2604
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002605 if (!Mod->isAvailable()) {
2606 // Modules that aren't available cannot be made visible.
2607 continue;
2608 }
2609
Douglas Gregorff2be532011-12-01 17:11:21 +00002610 // Update the module's name visibility.
2611 Mod->NameVisibility = NameVisibility;
2612
Douglas Gregorcf68c582011-12-01 22:20:10 +00002613 // If we've already deserialized any names from this module,
Douglas Gregorff2be532011-12-01 17:11:21 +00002614 // mark them as visible.
Douglas Gregorcf68c582011-12-01 22:20:10 +00002615 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2616 if (Hidden != HiddenNamesMap.end()) {
2617 makeNamesVisible(Hidden->second);
2618 HiddenNamesMap.erase(Hidden);
2619 }
Douglas Gregorff2be532011-12-01 17:11:21 +00002620
2621 // Push any non-explicit submodules onto the stack to be marked as
2622 // visible.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002623 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2624 SubEnd = Mod->submodule_end();
Douglas Gregorff2be532011-12-01 17:11:21 +00002625 Sub != SubEnd; ++Sub) {
Douglas Gregoreb90e832012-01-04 23:32:19 +00002626 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2627 Stack.push_back(*Sub);
Douglas Gregorff2be532011-12-01 17:11:21 +00002628 }
Douglas Gregor54139282011-12-02 19:11:09 +00002629
2630 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002631 bool AnyWildcard = false;
2632 bool UnrestrictedWildcard = false;
2633 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor54139282011-12-02 19:11:09 +00002634 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2635 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002636 if (!Mod->Exports[I].getInt()) {
2637 // Export a named module directly; no wildcards involved.
2638 if (Visited.insert(Exported))
Douglas Gregor54139282011-12-02 19:11:09 +00002639 Stack.push_back(Exported);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002640
2641 continue;
Douglas Gregor54139282011-12-02 19:11:09 +00002642 }
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002643
2644 // Wildcard export: export all of the imported modules that match
2645 // the given pattern.
2646 AnyWildcard = true;
2647 if (UnrestrictedWildcard)
2648 continue;
2649
2650 if (Module *Restriction = Mod->Exports[I].getPointer())
2651 WildcardRestrictions.push_back(Restriction);
2652 else {
2653 WildcardRestrictions.clear();
2654 UnrestrictedWildcard = true;
2655 }
2656 }
2657
2658 // If there were any wildcards, push any imported modules that were
2659 // re-exported by the wildcard restriction.
2660 if (!AnyWildcard)
2661 continue;
2662
2663 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2664 Module *Imported = Mod->Imports[I];
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00002665 if (!Visited.insert(Imported))
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002666 continue;
2667
2668 bool Acceptable = UnrestrictedWildcard;
2669 if (!Acceptable) {
2670 // Check whether this module meets one of the restrictions.
2671 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2672 Module *Restriction = WildcardRestrictions[R];
2673 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2674 Acceptable = true;
2675 break;
2676 }
2677 }
2678 }
2679
2680 if (!Acceptable)
2681 continue;
2682
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002683 Stack.push_back(Imported);
Douglas Gregor54139282011-12-02 19:11:09 +00002684 }
Douglas Gregorff2be532011-12-01 17:11:21 +00002685 }
2686}
2687
Sebastian Redl009e7f22010-10-05 16:15:19 +00002688ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002689 ModuleKind Type,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002690 SourceLocation ImportLoc,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002691 unsigned ClientLoadCapabilities) {
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002692 // Bump the generation number.
Douglas Gregor404cdde2012-01-27 01:47:08 +00002693 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor112b9072012-10-18 05:31:06 +00002694
Douglas Gregor188dbef2012-11-07 17:46:15 +00002695 unsigned NumModules = ModuleMgr.size();
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002696 llvm::SmallVector<ImportedModule, 4> Loaded;
2697 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Douglas Gregor188dbef2012-11-07 17:46:15 +00002698 /*ImportedBy=*/0, Loaded,
2699 ClientLoadCapabilities)) {
2700 case Failure:
2701 case OutOfDate:
2702 case VersionMismatch:
2703 case ConfigurationMismatch:
2704 case HadErrors:
2705 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end());
2706 return ReadResult;
2707
2708 case Success:
2709 break;
Sebastian Redl2abc0382010-07-16 20:41:52 +00002710 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002711
2712 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002713
Douglas Gregor112b9072012-10-18 05:31:06 +00002714 // Load the AST blocks of all of the modules that we loaded.
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002715 for (llvm::SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
Douglas Gregor112b9072012-10-18 05:31:06 +00002716 MEnd = Loaded.end();
2717 M != MEnd; ++M) {
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002718 ModuleFile &F = *M->Mod;
Douglas Gregor112b9072012-10-18 05:31:06 +00002719
2720 // Read the AST block.
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002721 if (ReadASTBlock(F))
2722 return Failure;
Douglas Gregor112b9072012-10-18 05:31:06 +00002723
2724 // Once read, set the ModuleFile bit base offset and update the size in
2725 // bits of all files we've seen.
2726 F.GlobalBitOffset = TotalModulesSizeInBits;
2727 TotalModulesSizeInBits += F.SizeInBits;
2728 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2729
Douglas Gregor112b9072012-10-18 05:31:06 +00002730 // Preload SLocEntries.
2731 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2732 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor4750b772012-10-22 22:53:10 +00002733 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor112b9072012-10-18 05:31:06 +00002734 // directly because the entry may have already been loaded in which case
Douglas Gregor4750b772012-10-22 22:53:10 +00002735 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor112b9072012-10-18 05:31:06 +00002736 // SourceManager.
2737 SourceMgr.getLoadedSLocEntryByID(Index);
2738 }
2739 }
2740
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002741 // Setup the import locations.
2742 for (llvm::SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2743 MEnd = Loaded.end();
2744 M != MEnd; ++M) {
2745 ModuleFile &F = *M->Mod;
2746 if (!M->ImportedBy)
2747 F.ImportLoc = M->ImportLoc;
2748 else
2749 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2750 M->ImportLoc.getRawEncoding());
2751 }
2752
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002753 // Mark all of the identifiers in the identifier table as being out of date,
2754 // so that various accessors know to check the loaded modules when the
2755 // identifier is used.
Douglas Gregor51825b42011-09-09 22:02:16 +00002756 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2757 IdEnd = PP.getIdentifierTable().end();
2758 Id != IdEnd; ++Id)
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002759 Id->second->setOutOfDate(true);
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002760
Douglas Gregor24bb9232011-12-02 18:58:38 +00002761 // Resolve any unresolved module exports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002762 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2763 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2764 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002765 Module *ResolvedMod = getSubmodule(GlobalID);
2766
2767 if (Unresolved.IsImport) {
2768 if (ResolvedMod)
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002769 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002770 continue;
Douglas Gregor24bb9232011-12-02 18:58:38 +00002771 }
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002772
2773 if (ResolvedMod || Unresolved.IsWildcard)
2774 Unresolved.Mod->Exports.push_back(
2775 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregor24bb9232011-12-02 18:58:38 +00002776 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002777 UnresolvedModuleImportExports.clear();
Douglas Gregor24bb9232011-12-02 18:58:38 +00002778
Douglas Gregor4163aca2011-09-09 21:34:22 +00002779 InitializeContext();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002780
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002781 if (DeserializationListener)
2782 DeserializationListener->ReaderInitialized(this);
2783
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002784 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2785 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2786 PrimaryModule.OriginalSourceFileID
2787 = FileID::get(PrimaryModule.SLocEntryBaseID
2788 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002789
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002790 // If this AST file is a precompiled preamble, then set the
2791 // preamble file ID of the source manager to the file source file
2792 // from which the preamble was built.
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002793 if (Type == MK_Preamble) {
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002794 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002795 } else if (Type == MK_MainFile) {
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002796 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002797 }
Douglas Gregor936a5b42010-11-30 05:23:00 +00002798 }
2799
Douglas Gregor404cdde2012-01-27 01:47:08 +00002800 // For any Objective-C class definitions we have already loaded, make sure
2801 // that we load any additional categories.
2802 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2803 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2804 ObjCClassesLoaded[I],
2805 PreviousGeneration);
2806 }
2807
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002808 return Success;
2809}
2810
Douglas Gregor4b29c162012-10-22 23:51:00 +00002811ASTReader::ASTReadResult
2812ASTReader::ReadASTCore(StringRef FileName,
2813 ModuleKind Type,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002814 SourceLocation ImportLoc,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002815 ModuleFile *ImportedBy,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002816 llvm::SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002817 unsigned ClientLoadCapabilities) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002818 ModuleFile *M;
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002819 bool NewModule;
2820 std::string ErrorStr;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +00002821 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportLoc,
2822 ImportedBy, CurrentGeneration,
2823 ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002824
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002825 if (!M) {
2826 // We couldn't load the module.
2827 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2828 + ErrorStr;
2829 Error(Msg);
2830 return Failure;
2831 }
2832
2833 if (!NewModule) {
2834 // We've already loaded this module.
2835 return Success;
2836 }
2837
2838 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2839 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002840 if (FileName != "-") {
2841 CurrentDir = llvm::sys::path::parent_path(FileName);
2842 if (CurrentDir.empty()) CurrentDir = ".";
2843 }
2844
Douglas Gregorde3ef502011-11-30 23:21:26 +00002845 ModuleFile &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002846 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002847 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002848 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002849
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002850 // Sniff for the signature.
2851 if (Stream.Read(8) != 'C' ||
2852 Stream.Read(8) != 'P' ||
2853 Stream.Read(8) != 'C' ||
2854 Stream.Read(8) != 'H') {
2855 Diag(diag::err_not_a_pch_file) << FileName;
2856 return Failure;
2857 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002858
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002859 while (!Stream.AtEndOfStream()) {
2860 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002861
Douglas Gregor92863e42009-04-10 23:10:45 +00002862 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002863 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002864 return Failure;
2865 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002866
2867 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002868
Douglas Gregor0aa21c92012-10-18 18:27:37 +00002869 // We only know the control subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002870 switch (BlockID) {
2871 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002872 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002873 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002874 return Failure;
2875 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002876 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00002877 case CONTROL_BLOCK_ID:
Douglas Gregor4b29c162012-10-22 23:51:00 +00002878 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002879 case Success:
2880 break;
2881
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002882 case Failure: return Failure;
2883 case OutOfDate: return OutOfDate;
2884 case VersionMismatch: return VersionMismatch;
2885 case ConfigurationMismatch: return ConfigurationMismatch;
2886 case HadErrors: return HadErrors;
Douglas Gregor55abb232009-04-10 20:39:37 +00002887 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002888 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00002889 case AST_BLOCK_ID:
2890 // Record that we've loaded this module.
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002891 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
Douglas Gregor112b9072012-10-18 05:31:06 +00002892 return Success;
2893
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002894 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002895 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002896 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002897 return Failure;
2898 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002899 break;
2900 }
Mike Stump11289f42009-09-09 15:08:12 +00002901 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002902
Sebastian Redl2abc0382010-07-16 20:41:52 +00002903 return Success;
2904}
2905
Douglas Gregor51825b42011-09-09 22:02:16 +00002906void ASTReader::InitializeContext() {
Douglas Gregordab42432011-08-12 00:15:20 +00002907 // If there's a listener, notify them that we "read" the translation unit.
2908 if (DeserializationListener)
Douglas Gregor4163aca2011-09-09 21:34:22 +00002909 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2910 Context.getTranslationUnitDecl());
Douglas Gregoraa433012010-10-01 01:18:02 +00002911
Douglas Gregordab42432011-08-12 00:15:20 +00002912 // Make sure we load the declaration update records for the translation unit,
2913 // if there are any.
Douglas Gregor4163aca2011-09-09 21:34:22 +00002914 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2915 Context.getTranslationUnitDecl());
Douglas Gregordab42432011-08-12 00:15:20 +00002916
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002917 // FIXME: Find a better way to deal with collisions between these
2918 // built-in types. Right now, we just ignore the problem.
2919
2920 // Load the special types.
Douglas Gregord53ae832012-01-17 18:09:05 +00002921 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002922 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2923 if (!Context.CFConstantStringTypeDecl)
2924 Context.setCFConstantStringType(GetType(String));
2925 }
2926
2927 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2928 QualType FileType = GetType(File);
2929 if (FileType.isNull()) {
2930 Error("FILE type is NULL");
2931 return;
2932 }
2933
2934 if (!Context.FILEDecl) {
2935 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2936 Context.setFILEDecl(Typedef->getDecl());
2937 else {
2938 const TagType *Tag = FileType->getAs<TagType>();
2939 if (!Tag) {
2940 Error("Invalid FILE type in AST file");
2941 return;
2942 }
2943 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002944 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002945 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002946 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002947
Douglas Gregor3c267f7a2011-11-11 19:13:12 +00002948 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002949 QualType Jmp_bufType = GetType(Jmp_buf);
2950 if (Jmp_bufType.isNull()) {
2951 Error("jmp_buf type is NULL");
2952 return;
2953 }
2954
2955 if (!Context.jmp_bufDecl) {
2956 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2957 Context.setjmp_bufDecl(Typedef->getDecl());
2958 else {
2959 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2960 if (!Tag) {
2961 Error("Invalid jmp_buf type in AST file");
2962 return;
2963 }
2964 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002965 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002966 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002967 }
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002968
Douglas Gregor3c267f7a2011-11-11 19:13:12 +00002969 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002970 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2971 if (Sigjmp_bufType.isNull()) {
2972 Error("sigjmp_buf type is NULL");
2973 return;
2974 }
2975
2976 if (!Context.sigjmp_bufDecl) {
2977 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2978 Context.setsigjmp_bufDecl(Typedef->getDecl());
2979 else {
2980 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2981 assert(Tag && "Invalid sigjmp_buf type in AST file");
2982 Context.setsigjmp_bufDecl(Tag->getDecl());
2983 }
2984 }
2985 }
2986
2987 if (unsigned ObjCIdRedef
2988 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2989 if (Context.ObjCIdRedefinitionType.isNull())
2990 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2991 }
2992
2993 if (unsigned ObjCClassRedef
2994 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2995 if (Context.ObjCClassRedefinitionType.isNull())
2996 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2997 }
2998
2999 if (unsigned ObjCSelRedef
3000 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3001 if (Context.ObjCSelRedefinitionType.isNull())
3002 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3003 }
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003004
3005 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3006 QualType Ucontext_tType = GetType(Ucontext_t);
3007 if (Ucontext_tType.isNull()) {
3008 Error("ucontext_t type is NULL");
3009 return;
3010 }
3011
3012 if (!Context.ucontext_tDecl) {
3013 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3014 Context.setucontext_tDecl(Typedef->getDecl());
3015 else {
3016 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3017 assert(Tag && "Invalid ucontext_t type in AST file");
3018 Context.setucontext_tDecl(Tag->getDecl());
3019 }
3020 }
3021 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003022 }
3023
Douglas Gregor4163aca2011-09-09 21:34:22 +00003024 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003025
3026 // If there were any CUDA special declarations, deserialize them.
3027 if (!CUDASpecialDeclRefs.empty()) {
3028 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor4163aca2011-09-09 21:34:22 +00003029 Context.setcudaConfigureCallDecl(
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003030 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3031 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003032
3033 // Re-export any modules that were imported by a non-module AST file.
3034 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3035 if (Module *Imported = getSubmodule(ImportedModules[I]))
3036 makeModuleVisible(Imported, Module::AllVisible);
3037 }
3038 ImportedModules.clear();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003039}
3040
Douglas Gregorcf68c582011-12-01 22:20:10 +00003041void ASTReader::finalizeForWriting() {
3042 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3043 HiddenEnd = HiddenNamesMap.end();
3044 Hidden != HiddenEnd; ++Hidden) {
3045 makeNamesVisible(Hidden->second);
3046 }
3047 HiddenNamesMap.clear();
3048}
3049
Douglas Gregor45fe0362009-05-12 01:31:05 +00003050/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003051/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00003052/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003053std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00003054 FileManager &FileMgr,
David Blaikie9c902b52011-09-25 23:23:43 +00003055 DiagnosticsEngine &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003056 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003057 std::string ErrStr;
Dylan Noblesmithe2778992012-02-05 02:12:40 +00003058 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00003059 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00003060 if (!Buffer) {
Kaelyn Uhrain272d7182012-06-20 00:36:03 +00003061 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003062 return std::string();
3063 }
3064
3065 // Initialize the stream
3066 llvm::BitstreamReader StreamFile;
3067 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00003068 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00003069 (const unsigned char *)Buffer->getBufferEnd());
3070 Stream.init(StreamFile);
3071
3072 // Sniff for the signature.
3073 if (Stream.Read(8) != 'C' ||
3074 Stream.Read(8) != 'P' ||
3075 Stream.Read(8) != 'C' ||
3076 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003077 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003078 return std::string();
3079 }
3080
3081 RecordData Record;
3082 while (!Stream.AtEndOfStream()) {
3083 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00003084
Douglas Gregor45fe0362009-05-12 01:31:05 +00003085 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3086 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00003087
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003088 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003089 switch (BlockID) {
Douglas Gregor112b9072012-10-18 05:31:06 +00003090 case CONTROL_BLOCK_ID:
3091 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003092 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003093 return std::string();
3094 }
3095 break;
Mike Stump11289f42009-09-09 15:08:12 +00003096
Douglas Gregor45fe0362009-05-12 01:31:05 +00003097 default:
3098 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003099 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003100 return std::string();
3101 }
3102 break;
3103 }
3104 continue;
3105 }
3106
3107 if (Code == llvm::bitc::END_BLOCK) {
3108 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003109 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003110 return std::string();
3111 }
3112 continue;
3113 }
3114
3115 if (Code == llvm::bitc::DEFINE_ABBREV) {
3116 Stream.ReadAbbrevRecord();
3117 continue;
3118 }
3119
3120 Record.clear();
3121 const char *BlobStart = 0;
3122 unsigned BlobLen = 0;
Douglas Gregorfad10d82012-10-18 18:36:53 +00003123 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregor45fe0362009-05-12 01:31:05 +00003124 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00003125 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00003126
3127 return std::string();
3128}
3129
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003130namespace {
3131 class SimplePCHValidator : public ASTReaderListener {
3132 const LangOptions &ExistingLangOpts;
3133 const TargetOptions &ExistingTargetOpts;
Douglas Gregorb6368752012-10-24 23:41:50 +00003134 const PreprocessorOptions &ExistingPPOpts;
Douglas Gregor55358ed2012-10-25 00:07:54 +00003135 FileManager &FileMgr;
3136
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003137 public:
3138 SimplePCHValidator(const LangOptions &ExistingLangOpts,
Douglas Gregorb6368752012-10-24 23:41:50 +00003139 const TargetOptions &ExistingTargetOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003140 const PreprocessorOptions &ExistingPPOpts,
3141 FileManager &FileMgr)
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003142 : ExistingLangOpts(ExistingLangOpts),
Douglas Gregorb6368752012-10-24 23:41:50 +00003143 ExistingTargetOpts(ExistingTargetOpts),
Douglas Gregor55358ed2012-10-25 00:07:54 +00003144 ExistingPPOpts(ExistingPPOpts),
3145 FileMgr(FileMgr)
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003146 {
3147 }
3148
3149 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3150 bool Complain) {
3151 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3152 }
3153 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3154 bool Complain) {
3155 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3156 }
Douglas Gregorb6368752012-10-24 23:41:50 +00003157 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003158 bool Complain,
3159 std::string &SuggestedPredefines) {
3160 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3161 SuggestedPredefines);
Douglas Gregorb6368752012-10-24 23:41:50 +00003162 }
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003163 };
3164}
3165
Douglas Gregor52901822012-11-06 23:40:54 +00003166bool ASTReader::readASTFileControlBlock(StringRef Filename,
3167 FileManager &FileMgr,
3168 ASTReaderListener &Listener) {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003169 // Open the AST file.
3170 std::string ErrStr;
3171 OwningPtr<llvm::MemoryBuffer> Buffer;
3172 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3173 if (!Buffer) {
Douglas Gregor52901822012-11-06 23:40:54 +00003174 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003175 }
3176
3177 // Initialize the stream
3178 llvm::BitstreamReader StreamFile;
3179 llvm::BitstreamCursor Stream;
3180 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3181 (const unsigned char *)Buffer->getBufferEnd());
3182 Stream.init(StreamFile);
3183
3184 // Sniff for the signature.
3185 if (Stream.Read(8) != 'C' ||
3186 Stream.Read(8) != 'P' ||
3187 Stream.Read(8) != 'C' ||
3188 Stream.Read(8) != 'H') {
Douglas Gregor52901822012-11-06 23:40:54 +00003189 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003190 }
3191
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003192 RecordData Record;
3193 bool InControlBlock = false;
3194 while (!Stream.AtEndOfStream()) {
3195 unsigned Code = Stream.ReadCode();
3196
3197 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3198 unsigned BlockID = Stream.ReadSubBlockID();
3199
3200 // We only know the control subblock ID.
3201 switch (BlockID) {
3202 case CONTROL_BLOCK_ID:
3203 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Douglas Gregor52901822012-11-06 23:40:54 +00003204 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003205 } else {
3206 InControlBlock = true;
3207 }
3208 break;
3209
3210 default:
3211 if (Stream.SkipBlock())
Douglas Gregor52901822012-11-06 23:40:54 +00003212 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003213 break;
3214 }
3215 continue;
3216 }
3217
3218 if (Code == llvm::bitc::END_BLOCK) {
3219 if (Stream.ReadBlockEnd()) {
Douglas Gregor52901822012-11-06 23:40:54 +00003220 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003221 }
Douglas Gregor52901822012-11-06 23:40:54 +00003222
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003223 InControlBlock = false;
3224 continue;
3225 }
3226
3227 if (Code == llvm::bitc::DEFINE_ABBREV) {
3228 Stream.ReadAbbrevRecord();
3229 continue;
3230 }
3231
3232 Record.clear();
3233 const char *BlobStart = 0;
3234 unsigned BlobLen = 0;
3235 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3236 if (InControlBlock) {
3237 switch ((ControlRecordTypes)RecCode) {
3238 case METADATA: {
3239 if (Record[0] != VERSION_MAJOR) {
Douglas Gregor52901822012-11-06 23:40:54 +00003240 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003241 }
3242
3243 const std::string &CurBranch = getClangFullRepositoryVersion();
3244 StringRef ASTBranch(BlobStart, BlobLen);
3245 if (StringRef(CurBranch) != ASTBranch)
Douglas Gregor52901822012-11-06 23:40:54 +00003246 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003247
3248 break;
3249 }
3250 case LANGUAGE_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003251 if (ParseLanguageOptions(Record, false, Listener))
3252 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003253 break;
3254
3255 case TARGET_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003256 if (ParseTargetOptions(Record, false, Listener))
3257 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003258 break;
3259
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003260 case DIAGNOSTIC_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003261 if (ParseDiagnosticOptions(Record, false, Listener))
3262 return true;
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003263 break;
3264
Douglas Gregor2d302362012-10-24 16:50:34 +00003265 case FILE_SYSTEM_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003266 if (ParseFileSystemOptions(Record, false, Listener))
3267 return true;
Douglas Gregor2d302362012-10-24 16:50:34 +00003268 break;
3269
3270 case HEADER_SEARCH_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003271 if (ParseHeaderSearchOptions(Record, false, Listener))
3272 return true;
Douglas Gregor2d302362012-10-24 16:50:34 +00003273 break;
3274
Douglas Gregor55358ed2012-10-25 00:07:54 +00003275 case PREPROCESSOR_OPTIONS: {
3276 std::string IgnoredSuggestedPredefines;
Douglas Gregor52901822012-11-06 23:40:54 +00003277 if (ParsePreprocessorOptions(Record, false, Listener,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003278 IgnoredSuggestedPredefines))
Douglas Gregor52901822012-11-06 23:40:54 +00003279 return true;
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003280 break;
Douglas Gregor55358ed2012-10-25 00:07:54 +00003281 }
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003282
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003283 default:
3284 // No other validation to perform.
3285 break;
3286 }
3287 }
3288 }
3289
Douglas Gregor52901822012-11-06 23:40:54 +00003290 return false;
3291}
3292
3293
3294bool ASTReader::isAcceptableASTFile(StringRef Filename,
3295 FileManager &FileMgr,
3296 const LangOptions &LangOpts,
3297 const TargetOptions &TargetOpts,
3298 const PreprocessorOptions &PPOpts) {
3299 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3300 return !readASTFileControlBlock(Filename, FileMgr, validator);
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003301}
3302
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003303bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor69021972011-11-30 17:33:56 +00003304 // Enter the submodule block.
3305 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3306 Error("malformed submodule block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003307 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003308 }
3309
3310 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor253eefe2011-12-01 00:59:36 +00003311 bool First = true;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003312 Module *CurrentModule = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00003313 RecordData Record;
3314 while (true) {
3315 unsigned Code = F.Stream.ReadCode();
3316 if (Code == llvm::bitc::END_BLOCK) {
3317 if (F.Stream.ReadBlockEnd()) {
3318 Error("error at end of submodule block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003319 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003320 }
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003321 return false;
Douglas Gregor69021972011-11-30 17:33:56 +00003322 }
3323
3324 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3325 // No known subblocks, always skip them.
3326 F.Stream.ReadSubBlockID();
3327 if (F.Stream.SkipBlock()) {
3328 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003329 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003330 }
3331 continue;
3332 }
3333
3334 if (Code == llvm::bitc::DEFINE_ABBREV) {
3335 F.Stream.ReadAbbrevRecord();
3336 continue;
3337 }
3338
3339 // Read a record.
3340 const char *BlobStart;
3341 unsigned BlobLen;
3342 Record.clear();
3343 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3344 default: // Default behavior: ignore.
3345 break;
3346
3347 case SUBMODULE_DEFINITION: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003348 if (First) {
3349 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003350 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003351 }
3352
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003353 if (Record.size() < 7) {
Douglas Gregor73441092011-12-05 22:27:44 +00003354 Error("malformed module definition");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003355 return true;
Douglas Gregor73441092011-12-05 22:27:44 +00003356 }
3357
Douglas Gregor69021972011-11-30 17:33:56 +00003358 StringRef Name(BlobStart, BlobLen);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003359 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3360 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3361 bool IsFramework = Record[2];
3362 bool IsExplicit = Record[3];
Douglas Gregora686e1b2012-01-27 19:52:33 +00003363 bool IsSystem = Record[4];
3364 bool InferSubmodules = Record[5];
3365 bool InferExplicitSubmodules = Record[6];
3366 bool InferExportWildcard = Record[7];
Douglas Gregor73441092011-12-05 22:27:44 +00003367
Douglas Gregorde3ef502011-11-30 23:21:26 +00003368 Module *ParentModule = 0;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003369 if (Parent)
3370 ParentModule = getSubmodule(Parent);
Douglas Gregor69021972011-11-30 17:33:56 +00003371
3372 // Retrieve this (sub)module from the module map, creating it if
3373 // necessary.
3374 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3375 IsFramework,
3376 IsExplicit).first;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003377 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3378 if (GlobalIndex >= SubmodulesLoaded.size() ||
3379 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003380 Error("too many submodules");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003381 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003382 }
Douglas Gregore37a85a2011-12-02 17:30:13 +00003383
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +00003384 CurrentModule->setASTFile(F.File);
Douglas Gregor98a52db2011-12-20 00:28:52 +00003385 CurrentModule->IsFromModuleFile = true;
Douglas Gregora686e1b2012-01-27 19:52:33 +00003386 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor73441092011-12-05 22:27:44 +00003387 CurrentModule->InferSubmodules = InferSubmodules;
3388 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3389 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregore37a85a2011-12-02 17:30:13 +00003390 if (DeserializationListener)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003391 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregore37a85a2011-12-02 17:30:13 +00003392
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003393 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor69021972011-11-30 17:33:56 +00003394 break;
3395 }
3396
Douglas Gregor524e33e2011-12-08 19:11:24 +00003397 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003398 if (First) {
3399 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003400 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003401 }
3402
Douglas Gregor69021972011-11-30 17:33:56 +00003403 if (!CurrentModule)
3404 break;
3405
3406 StringRef FileName(BlobStart, BlobLen);
3407 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor73141fa2011-12-08 17:39:04 +00003408 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003409 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor73141fa2011-12-08 17:39:04 +00003410 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor69021972011-11-30 17:33:56 +00003411 Error("mismatched umbrella headers in submodule");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003412 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003413 }
3414 }
3415 break;
3416 }
3417
3418 case SUBMODULE_HEADER: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003419 if (First) {
3420 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003421 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003422 }
3423
Douglas Gregor69021972011-11-30 17:33:56 +00003424 if (!CurrentModule)
3425 break;
3426
3427 // FIXME: Be more lazy about this!
3428 StringRef FileName(BlobStart, BlobLen);
3429 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3430 if (std::find(CurrentModule->Headers.begin(),
3431 CurrentModule->Headers.end(),
3432 File) == CurrentModule->Headers.end())
Douglas Gregor59527662012-10-15 06:28:11 +00003433 ModMap.addHeader(CurrentModule, File, false);
3434 }
3435 break;
3436 }
3437
3438 case SUBMODULE_EXCLUDED_HEADER: {
3439 if (First) {
3440 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003441 return true;
Douglas Gregor59527662012-10-15 06:28:11 +00003442 }
3443
3444 if (!CurrentModule)
3445 break;
3446
3447 // FIXME: Be more lazy about this!
3448 StringRef FileName(BlobStart, BlobLen);
3449 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3450 if (std::find(CurrentModule->Headers.begin(),
3451 CurrentModule->Headers.end(),
3452 File) == CurrentModule->Headers.end())
3453 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor69021972011-11-30 17:33:56 +00003454 }
3455 break;
3456 }
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00003457
3458 case SUBMODULE_TOPHEADER: {
3459 if (First) {
3460 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003461 return true;
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00003462 }
3463
3464 if (!CurrentModule)
3465 break;
3466
3467 // FIXME: Be more lazy about this!
3468 StringRef FileName(BlobStart, BlobLen);
3469 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3470 CurrentModule->TopHeaders.insert(File);
3471 break;
3472 }
3473
Douglas Gregor524e33e2011-12-08 19:11:24 +00003474 case SUBMODULE_UMBRELLA_DIR: {
3475 if (First) {
3476 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003477 return true;
Douglas Gregor524e33e2011-12-08 19:11:24 +00003478 }
3479
3480 if (!CurrentModule)
3481 break;
3482
3483 StringRef DirName(BlobStart, BlobLen);
3484 if (const DirectoryEntry *Umbrella
3485 = PP.getFileManager().getDirectory(DirName)) {
3486 if (!CurrentModule->getUmbrellaDir())
3487 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3488 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3489 Error("mismatched umbrella directories in submodule");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003490 return true;
Douglas Gregor524e33e2011-12-08 19:11:24 +00003491 }
3492 }
3493 break;
3494 }
3495
Douglas Gregor253eefe2011-12-01 00:59:36 +00003496 case SUBMODULE_METADATA: {
3497 if (!First) {
3498 Error("submodule metadata record not at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003499 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003500 }
3501 First = false;
3502
3503 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor253eefe2011-12-01 00:59:36 +00003504 F.LocalNumSubmodules = Record[0];
3505 unsigned LocalBaseSubmoduleID = Record[1];
3506 if (F.LocalNumSubmodules > 0) {
3507 // Introduce the global -> local mapping for submodules within this
3508 // module.
3509 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3510
3511 // Introduce the local -> global mapping for submodules within this
3512 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00003513 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor253eefe2011-12-01 00:59:36 +00003514 std::make_pair(LocalBaseSubmoduleID,
3515 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3516
3517 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3518 }
3519 break;
3520 }
Douglas Gregor24bb9232011-12-02 18:58:38 +00003521
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003522 case SUBMODULE_IMPORTS: {
3523 if (First) {
3524 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003525 return true;
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003526 }
3527
3528 if (!CurrentModule)
3529 break;
3530
3531 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3532 UnresolvedModuleImportExport Unresolved;
3533 Unresolved.File = &F;
3534 Unresolved.Mod = CurrentModule;
3535 Unresolved.ID = Record[Idx];
3536 Unresolved.IsImport = true;
3537 Unresolved.IsWildcard = false;
3538 UnresolvedModuleImportExports.push_back(Unresolved);
3539 }
3540 break;
3541 }
3542
Douglas Gregor24bb9232011-12-02 18:58:38 +00003543 case SUBMODULE_EXPORTS: {
3544 if (First) {
3545 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003546 return true;
Douglas Gregor24bb9232011-12-02 18:58:38 +00003547 }
3548
3549 if (!CurrentModule)
3550 break;
3551
3552 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003553 UnresolvedModuleImportExport Unresolved;
Douglas Gregor24bb9232011-12-02 18:58:38 +00003554 Unresolved.File = &F;
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003555 Unresolved.Mod = CurrentModule;
3556 Unresolved.ID = Record[Idx];
3557 Unresolved.IsImport = false;
3558 Unresolved.IsWildcard = Record[Idx + 1];
3559 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregor24bb9232011-12-02 18:58:38 +00003560 }
3561
3562 // Once we've loaded the set of exports, there's no reason to keep
3563 // the parsed, unresolved exports around.
3564 CurrentModule->UnresolvedExports.clear();
3565 break;
3566 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003567 case SUBMODULE_REQUIRES: {
3568 if (First) {
3569 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003570 return true;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003571 }
3572
3573 if (!CurrentModule)
3574 break;
3575
3576 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikiebbafb8a2012-03-11 07:00:24 +00003577 Context.getLangOpts(),
Douglas Gregor89929282012-01-30 06:01:29 +00003578 Context.getTargetInfo());
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003579 break;
3580 }
Douglas Gregor69021972011-11-30 17:33:56 +00003581 }
3582 }
Douglas Gregor69021972011-11-30 17:33:56 +00003583}
3584
Douglas Gregor55abb232009-04-10 20:39:37 +00003585/// \brief Parse the record that corresponds to a LangOptions data
3586/// structure.
3587///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003588/// This routine parses the language options from the AST file and then gives
3589/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00003590///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003591/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003592bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3593 bool Complain,
3594 ASTReaderListener &Listener) {
3595 LangOptions LangOpts;
3596 unsigned Idx = 0;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00003597#define LANGOPT(Name, Bits, Default, Description) \
3598 LangOpts.Name = Record[Idx++];
3599#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3600 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3601#include "clang/Basic/LangOptions.def"
John McCall5fb5df92012-06-20 06:18:46 +00003602
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003603 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3604 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3605 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3606
3607 unsigned Length = Record[Idx++];
3608 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3609 Record.begin() + Idx + Length);
3610 return Listener.ReadLanguageOptions(LangOpts, Complain);
3611}
3612
3613bool ASTReader::ParseTargetOptions(const RecordData &Record,
3614 bool Complain,
3615 ASTReaderListener &Listener) {
3616 unsigned Idx = 0;
3617 TargetOptions TargetOpts;
3618 TargetOpts.Triple = ReadString(Record, Idx);
3619 TargetOpts.CPU = ReadString(Record, Idx);
3620 TargetOpts.ABI = ReadString(Record, Idx);
3621 TargetOpts.CXXABI = ReadString(Record, Idx);
3622 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3623 for (unsigned N = Record[Idx++]; N; --N) {
3624 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3625 }
3626 for (unsigned N = Record[Idx++]; N; --N) {
3627 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor55abb232009-04-10 20:39:37 +00003628 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003629
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003630 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor55abb232009-04-10 20:39:37 +00003631}
3632
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003633bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3634 ASTReaderListener &Listener) {
3635 DiagnosticOptions DiagOpts;
3636 unsigned Idx = 0;
3637#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3638#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3639 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3640#include "clang/Basic/DiagnosticOptions.def"
3641
3642 for (unsigned N = Record[Idx++]; N; --N) {
3643 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3644 }
3645
3646 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3647}
3648
Douglas Gregorc6317db2012-10-24 15:49:58 +00003649bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3650 ASTReaderListener &Listener) {
3651 FileSystemOptions FSOpts;
3652 unsigned Idx = 0;
3653 FSOpts.WorkingDir = ReadString(Record, Idx);
3654 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3655}
3656
Douglas Gregor2d302362012-10-24 16:50:34 +00003657bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3658 bool Complain,
3659 ASTReaderListener &Listener) {
3660 HeaderSearchOptions HSOpts;
3661 unsigned Idx = 0;
3662 HSOpts.Sysroot = ReadString(Record, Idx);
3663
3664 // Include entries.
3665 for (unsigned N = Record[Idx++]; N; --N) {
3666 std::string Path = ReadString(Record, Idx);
3667 frontend::IncludeDirGroup Group
3668 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3669 bool IsUserSupplied = Record[Idx++];
3670 bool IsFramework = Record[Idx++];
3671 bool IgnoreSysRoot = Record[Idx++];
3672 bool IsInternal = Record[Idx++];
3673 bool ImplicitExternC = Record[Idx++];
3674 HSOpts.UserEntries.push_back(
3675 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3676 IgnoreSysRoot, IsInternal, ImplicitExternC));
3677 }
3678
3679 // System header prefixes.
3680 for (unsigned N = Record[Idx++]; N; --N) {
3681 std::string Prefix = ReadString(Record, Idx);
3682 bool IsSystemHeader = Record[Idx++];
3683 HSOpts.SystemHeaderPrefixes.push_back(
3684 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3685 }
3686
3687 HSOpts.ResourceDir = ReadString(Record, Idx);
3688 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3689 HSOpts.DisableModuleHash = Record[Idx++];
3690 HSOpts.UseBuiltinIncludes = Record[Idx++];
3691 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3692 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3693 HSOpts.UseLibcxx = Record[Idx++];
3694
3695 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3696}
3697
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003698bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3699 bool Complain,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003700 ASTReaderListener &Listener,
3701 std::string &SuggestedPredefines) {
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003702 PreprocessorOptions PPOpts;
3703 unsigned Idx = 0;
3704
3705 // Macro definitions/undefs
3706 for (unsigned N = Record[Idx++]; N; --N) {
3707 std::string Macro = ReadString(Record, Idx);
3708 bool IsUndef = Record[Idx++];
3709 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3710 }
3711
3712 // Includes
3713 for (unsigned N = Record[Idx++]; N; --N) {
3714 PPOpts.Includes.push_back(ReadString(Record, Idx));
3715 }
3716
3717 // Macro Includes
3718 for (unsigned N = Record[Idx++]; N; --N) {
3719 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3720 }
3721
Douglas Gregorb6368752012-10-24 23:41:50 +00003722 PPOpts.UsePredefines = Record[Idx++];
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003723 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3724 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3725 PPOpts.ObjCXXARCStandardLibrary =
3726 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
Douglas Gregor55358ed2012-10-25 00:07:54 +00003727 SuggestedPredefines.clear();
3728 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3729 SuggestedPredefines);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003730}
3731
Douglas Gregorde3ef502011-11-30 23:21:26 +00003732std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003733ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003734 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003735 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003736 assert(I != GlobalPreprocessedEntityMap.end() &&
3737 "Corrupted global preprocessed entity map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00003738 ModuleFile *M = I->second;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003739 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3740 return std::make_pair(M, LocalIndex);
3741}
3742
Argyrios Kyrtzidisd4fcf5802012-10-02 16:10:51 +00003743std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3744ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3745 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3746 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3747 Mod.NumPreprocessedEntities);
3748
3749 return std::make_pair(PreprocessingRecord::iterator(),
3750 PreprocessingRecord::iterator());
3751}
3752
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003753std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3754ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3755 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3756 ModuleDeclIterator(this, &Mod,
3757 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3758}
3759
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003760PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3761 PreprocessedEntityID PPID = Index+1;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003762 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3763 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003764 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003765 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor92a96f52011-02-08 21:58:10 +00003766
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003767 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003768 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003769
3770 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3771 switch (Code) {
3772 case llvm::bitc::END_BLOCK:
3773 return 0;
3774
3775 case llvm::bitc::ENTER_SUBBLOCK:
3776 Error("unexpected subblock record in preprocessor detail block");
3777 return 0;
3778
3779 case llvm::bitc::DEFINE_ABBREV:
3780 Error("unexpected abbrevation record in preprocessor detail block");
3781 return 0;
3782
3783 default:
3784 break;
3785 }
3786
3787 if (!PP.getPreprocessingRecord()) {
3788 Error("no preprocessing record");
3789 return 0;
3790 }
3791
3792 // Read the record.
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003793 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3794 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003795 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3796 const char *BlobStart = 0;
3797 unsigned BlobLen = 0;
3798 RecordData Record;
3799 PreprocessorDetailRecordTypes RecType =
3800 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3801 Code, Record, BlobStart, BlobLen);
3802 switch (RecType) {
3803 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003804 bool isBuiltin = Record[0];
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003805 IdentifierInfo *Name = 0;
3806 MacroDefinition *Def = 0;
3807 if (isBuiltin)
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003808 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003809 else {
3810 PreprocessedEntityID
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003811 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003812 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3813 }
3814
3815 MacroExpansion *ME;
3816 if (isBuiltin)
3817 ME = new (PPRec) MacroExpansion(Name, Range);
3818 else
3819 ME = new (PPRec) MacroExpansion(Def, Range);
3820
3821 return ME;
3822 }
3823
3824 case PPD_MACRO_DEFINITION: {
3825 // Decode the identifier info and then check again; if the macro is
3826 // still defined and associated with the identifier,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003827 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003828 MacroDefinition *MD
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003829 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003830
3831 if (DeserializationListener)
3832 DeserializationListener->MacroDefinitionRead(PPID, MD);
3833
3834 return MD;
3835 }
3836
3837 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003838 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00003839 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3840 const FileEntry *File = 0;
3841 if (!FullFileName.empty())
3842 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003843
3844 // FIXME: Stable encoding
3845 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003846 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003847 InclusionDirective *ID
3848 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003849 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00003850 Record[1], Record[3],
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003851 File,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003852 Range);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003853 return ID;
3854 }
3855 }
David Blaikie8a40f702012-01-17 06:56:22 +00003856
3857 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregoraae92242010-03-19 21:51:54 +00003858}
3859
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003860/// \brief \arg SLocMapI points at a chunk of a module that contains no
3861/// preprocessed entities or the entities it contains are not the ones we are
3862/// looking for. Find the next module that contains entities and return the ID
3863/// of the first entry.
3864PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3865 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3866 ++SLocMapI;
3867 for (GlobalSLocOffsetMapType::const_iterator
3868 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00003869 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003870 if (M.NumPreprocessedEntities)
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003871 return M.BasePreprocessedEntityID;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003872 }
3873
3874 return getTotalNumPreprocessedEntities();
3875}
3876
3877namespace {
3878
3879template <unsigned PPEntityOffset::*PPLoc>
3880struct PPEntityComp {
3881 const ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003882 ModuleFile &M;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003883
Douglas Gregorde3ef502011-11-30 23:21:26 +00003884 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003885
Benjamin Kramer5ce7f102011-09-21 06:42:26 +00003886 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3887 SourceLocation LHS = getLoc(L);
3888 SourceLocation RHS = getLoc(R);
3889 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3890 }
3891
3892 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003893 SourceLocation LHS = getLoc(L);
3894 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3895 }
3896
Benjamin Kramer5ce7f102011-09-21 06:42:26 +00003897 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003898 SourceLocation RHS = getLoc(R);
3899 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3900 }
3901
3902 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3903 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3904 }
3905};
3906
3907}
3908
3909/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3910PreprocessedEntityID
3911ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3912 if (SourceMgr.isLocalSourceLocation(BLoc))
3913 return getTotalNumPreprocessedEntities();
3914
3915 GlobalSLocOffsetMapType::const_iterator
3916 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3917 BLoc.getOffset());
3918 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3919 "Corrupted global sloc offset map");
3920
3921 if (SLocMapI->second->NumPreprocessedEntities == 0)
3922 return findNextPreprocessedEntity(SLocMapI);
3923
Douglas Gregorde3ef502011-11-30 23:21:26 +00003924 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003925 typedef const PPEntityOffset *pp_iterator;
3926 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3927 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +00003928
3929 size_t Count = M.NumPreprocessedEntities;
3930 size_t Half;
3931 pp_iterator First = pp_begin;
3932 pp_iterator PPI;
3933
3934 // Do a binary search manually instead of using std::lower_bound because
3935 // The end locations of entities may be unordered (when a macro expansion
3936 // is inside another macro argument), but for this case it is not important
3937 // whether we get the first macro expansion or its containing macro.
3938 while (Count > 0) {
3939 Half = Count/2;
3940 PPI = First;
3941 std::advance(PPI, Half);
3942 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3943 BLoc)){
3944 First = PPI;
3945 ++First;
3946 Count = Count - Half - 1;
3947 } else
3948 Count = Half;
3949 }
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003950
3951 if (PPI == pp_end)
3952 return findNextPreprocessedEntity(SLocMapI);
3953
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003954 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003955}
3956
3957/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3958PreprocessedEntityID
3959ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3960 if (SourceMgr.isLocalSourceLocation(ELoc))
3961 return getTotalNumPreprocessedEntities();
3962
3963 GlobalSLocOffsetMapType::const_iterator
3964 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3965 ELoc.getOffset());
3966 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3967 "Corrupted global sloc offset map");
3968
3969 if (SLocMapI->second->NumPreprocessedEntities == 0)
3970 return findNextPreprocessedEntity(SLocMapI);
3971
Douglas Gregorde3ef502011-11-30 23:21:26 +00003972 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003973 typedef const PPEntityOffset *pp_iterator;
3974 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3975 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3976 pp_iterator PPI =
3977 std::upper_bound(pp_begin, pp_end, ELoc,
3978 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3979
3980 if (PPI == pp_end)
3981 return findNextPreprocessedEntity(SLocMapI);
3982
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003983 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003984}
3985
3986/// \brief Returns a pair of [Begin, End) indices of preallocated
3987/// preprocessed entities that \arg Range encompasses.
3988std::pair<unsigned, unsigned>
3989 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3990 if (Range.isInvalid())
3991 return std::make_pair(0,0);
3992 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3993
3994 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3995 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3996 return std::make_pair(BeginID, EndID);
3997}
3998
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003999/// \brief Optionally returns true or false if the preallocated preprocessed
4000/// entity with index \arg Index came from file \arg FID.
4001llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4002 FileID FID) {
4003 if (FID.isInvalid())
4004 return false;
4005
Douglas Gregorde3ef502011-11-30 23:21:26 +00004006 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4007 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00004008 unsigned LocalIndex = PPInfo.second;
4009 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4010
4011 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4012 if (Loc.isInvalid())
4013 return false;
4014
4015 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4016 return true;
4017 else
4018 return false;
4019}
4020
Douglas Gregor69e94642011-08-25 18:14:34 +00004021namespace {
4022 /// \brief Visitor used to search for information about a header file.
4023 class HeaderFileInfoVisitor {
4024 ASTReader &Reader;
4025 const FileEntry *FE;
4026
4027 llvm::Optional<HeaderFileInfo> HFI;
4028
4029 public:
4030 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4031 : Reader(Reader), FE(FE) { }
4032
Douglas Gregorde3ef502011-11-30 23:21:26 +00004033 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor69e94642011-08-25 18:14:34 +00004034 HeaderFileInfoVisitor *This
4035 = static_cast<HeaderFileInfoVisitor *>(UserData);
4036
4037 HeaderFileInfoTrait Trait(This->Reader, M,
4038 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4039 M.HeaderFileFrameworkStrings,
4040 This->FE->getName());
4041
4042 HeaderFileInfoLookupTable *Table
4043 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4044 if (!Table)
4045 return false;
4046
4047 // Look in the on-disk hash table for an entry for this file name.
4048 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4049 &Trait);
4050 if (Pos == Table->end())
4051 return false;
4052
4053 This->HFI = *Pos;
4054 return true;
4055 }
4056
4057 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4058 };
4059}
4060
Douglas Gregor09b69892011-02-10 17:09:37 +00004061HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregor69e94642011-08-25 18:14:34 +00004062 HeaderFileInfoVisitor Visitor(*this, FE);
4063 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4064 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregor09b69892011-02-10 17:09:37 +00004065 if (Listener)
Douglas Gregor69e94642011-08-25 18:14:34 +00004066 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4067 return *HFI;
Douglas Gregor09b69892011-02-10 17:09:37 +00004068 }
4069
4070 return HeaderFileInfo();
4071}
4072
David Blaikie9c902b52011-09-25 23:23:43 +00004073void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004074 // FIXME: Make it work properly with modules.
4075 llvm::SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004076 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00004077 ModuleFile &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00004078 unsigned Idx = 0;
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004079 DiagStates.clear();
4080 assert(!Diag.DiagStates.empty());
4081 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
Douglas Gregor925296b2011-07-19 16:10:42 +00004082 while (Idx < F.PragmaDiagMappings.size()) {
4083 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004084 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4085 if (DiagStateID != 0) {
4086 Diag.DiagStatePoints.push_back(
4087 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4088 FullSourceLoc(Loc, SourceMgr)));
4089 continue;
4090 }
4091
4092 assert(DiagStateID == 0);
4093 // A new DiagState was created here.
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004094 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004095 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4096 DiagStates.push_back(NewState);
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004097 Diag.DiagStatePoints.push_back(
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004098 DiagnosticsEngine::DiagStatePoint(NewState,
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004099 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregor925296b2011-07-19 16:10:42 +00004100 while (1) {
4101 assert(Idx < F.PragmaDiagMappings.size() &&
4102 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4103 if (Idx >= F.PragmaDiagMappings.size()) {
4104 break; // Something is messed up but at least avoid infinite loop in
4105 // release build.
4106 }
4107 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4108 if (DiagID == (unsigned)-1) {
4109 break; // no more diag/map pairs for this location.
4110 }
4111 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004112 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4113 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregor925296b2011-07-19 16:10:42 +00004114 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00004115 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00004116 }
4117}
4118
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004119/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004120ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004121 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00004122 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00004123 ModuleFile *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00004124 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004125}
4126
4127/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004128///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004129/// The index is the type ID, shifted and minus the number of predefs. This
4130/// routine actually reads the record corresponding to the type at the given
4131/// location. It is a helper routine for GetType, which deals with reading type
4132/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00004133QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004134 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004135 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00004136
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004137 // Keep track of where we are in the stream, then jump back there
4138 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00004139 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004140
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004141 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004142
Douglas Gregor1342e842009-07-06 18:54:52 +00004143 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004144 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00004145
Douglas Gregor903b7e92011-07-22 00:38:23 +00004146 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00004147 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004148 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004149 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00004150 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4151 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004152 if (Record.size() != 2) {
4153 Error("Incorrect encoding of extended qualifier type");
4154 return QualType();
4155 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004156 QualType Base = readType(*Loc.F, Record, Idx);
4157 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004158 return Context.getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00004159 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004160
Sebastian Redl539c5062010-08-18 23:57:32 +00004161 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004162 if (Record.size() != 1) {
4163 Error("Incorrect encoding of complex type");
4164 return QualType();
4165 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004166 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004167 return Context.getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004168 }
4169
Sebastian Redl539c5062010-08-18 23:57:32 +00004170 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004171 if (Record.size() != 1) {
4172 Error("Incorrect encoding of pointer 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.getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004177 }
4178
Sebastian Redl539c5062010-08-18 23:57:32 +00004179 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004180 if (Record.size() != 1) {
4181 Error("Incorrect encoding of block pointer type");
4182 return QualType();
4183 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004184 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004185 return Context.getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004186 }
4187
Sebastian Redl539c5062010-08-18 23:57:32 +00004188 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00004189 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004190 Error("Incorrect encoding of lvalue reference type");
4191 return QualType();
4192 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004193 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004194 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004195 }
4196
Sebastian Redl539c5062010-08-18 23:57:32 +00004197 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004198 if (Record.size() != 1) {
4199 Error("Incorrect encoding of rvalue reference type");
4200 return QualType();
4201 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004202 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004203 return Context.getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004204 }
4205
Sebastian Redl539c5062010-08-18 23:57:32 +00004206 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00004207 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004208 Error("Incorrect encoding of member pointer type");
4209 return QualType();
4210 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004211 QualType PointeeType = readType(*Loc.F, Record, Idx);
4212 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004213 if (PointeeType.isNull() || ClassType.isNull())
4214 return QualType();
4215
Douglas Gregor4163aca2011-09-09 21:34:22 +00004216 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004217 }
4218
Sebastian Redl539c5062010-08-18 23:57:32 +00004219 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004220 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004221 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4222 unsigned IndexTypeQuals = Record[2];
4223 unsigned Idx = 3;
4224 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004225 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor04318252009-07-06 15:59:29 +00004226 ASM, IndexTypeQuals);
4227 }
4228
Sebastian Redl539c5062010-08-18 23:57:32 +00004229 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004230 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004231 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4232 unsigned IndexTypeQuals = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004233 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004234 }
4235
Sebastian Redl539c5062010-08-18 23:57:32 +00004236 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004237 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004238 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4239 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00004240 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4241 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004242 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00004243 ASM, IndexTypeQuals,
4244 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004245 }
4246
Sebastian Redl539c5062010-08-18 23:57:32 +00004247 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00004248 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004249 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004250 return QualType();
4251 }
4252
Douglas Gregor903b7e92011-07-22 00:38:23 +00004253 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004254 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00004255 unsigned VecKind = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004256 return Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00004257 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004258 }
4259
Sebastian Redl539c5062010-08-18 23:57:32 +00004260 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00004261 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004262 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004263 return QualType();
4264 }
4265
Douglas Gregor903b7e92011-07-22 00:38:23 +00004266 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004267 unsigned NumElements = Record[1];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004268 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004269 }
4270
Sebastian Redl539c5062010-08-18 23:57:32 +00004271 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00004272 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004273 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004274 return QualType();
4275 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004276 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00004277 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4278 (CallingConv)Record[4], Record[5]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004279 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004280 }
4281
Sebastian Redl539c5062010-08-18 23:57:32 +00004282 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004283 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00004284
4285 FunctionProtoType::ExtProtoInfo EPI;
4286 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00004287 /*hasregparm*/ Record[2],
4288 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00004289 static_cast<CallingConv>(Record[4]),
4290 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00004291
John McCall31168b02011-06-15 23:02:42 +00004292 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004293 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004294 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004295 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00004296 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00004297
4298 EPI.Variadic = Record[Idx++];
Richard Smith5e580292012-02-10 09:58:53 +00004299 EPI.HasTrailingReturn = Record[Idx++];
John McCalldb40c7f2010-12-14 08:05:40 +00004300 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00004301 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004302 ExceptionSpecificationType EST =
4303 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4304 EPI.ExceptionSpecType = EST;
Douglas Gregor6a377842012-04-04 00:34:49 +00004305 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004306 if (EST == EST_Dynamic) {
4307 EPI.NumExceptions = Record[Idx++];
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004308 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00004309 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004310 EPI.Exceptions = Exceptions.data();
4311 } else if (EST == EST_ComputedNoexcept) {
4312 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith8b987a92012-04-21 17:47:47 +00004313 } else if (EST == EST_Uninstantiated) {
4314 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4315 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithd3b5c9082012-07-27 04:22:15 +00004316 } else if (EST == EST_Unevaluated) {
4317 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004318 }
Douglas Gregor4163aca2011-09-09 21:34:22 +00004319 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00004320 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004321 }
4322
Douglas Gregor7fb09192011-07-21 22:35:25 +00004323 case TYPE_UNRESOLVED_USING: {
4324 unsigned Idx = 0;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004325 return Context.getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00004326 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4327 }
4328
Sebastian Redl539c5062010-08-18 23:57:32 +00004329 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004330 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004331 Error("incorrect encoding of typedef type");
4332 return QualType();
4333 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004334 unsigned Idx = 0;
4335 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004336 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00004337 if (!Canonical.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004338 Canonical = Context.getCanonicalType(Canonical);
4339 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004340 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004341
Sebastian Redl539c5062010-08-18 23:57:32 +00004342 case TYPE_TYPEOF_EXPR:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004343 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004344
Sebastian Redl539c5062010-08-18 23:57:32 +00004345 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004346 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004347 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004348 return QualType();
4349 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004350 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004351 return Context.getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004352 }
Mike Stump11289f42009-09-09 15:08:12 +00004353
Douglas Gregor81495f32012-02-12 18:42:33 +00004354 case TYPE_DECLTYPE: {
4355 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4356 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4357 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00004358
Alexis Hunte852b102011-05-24 22:41:36 +00004359 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004360 QualType BaseType = readType(*Loc.F, Record, Idx);
4361 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00004362 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004363 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Alexis Hunte852b102011-05-24 22:41:36 +00004364 }
4365
Richard Smith30482bc2011-02-20 03:19:35 +00004366 case TYPE_AUTO:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004367 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00004368
Sebastian Redl539c5062010-08-18 23:57:32 +00004369 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004370 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004371 Error("incorrect encoding of record type");
4372 return QualType();
4373 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004374 unsigned Idx = 0;
4375 bool IsDependent = Record[Idx++];
Douglas Gregorf3bccd72012-01-17 19:21:53 +00004376 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4377 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4378 QualType T = Context.getRecordType(RD);
John McCall424cec92011-01-19 06:33:43 +00004379 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004380 return T;
4381 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004382
Sebastian Redl539c5062010-08-18 23:57:32 +00004383 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004384 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004385 Error("incorrect encoding of enum type");
4386 return QualType();
4387 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004388 unsigned Idx = 0;
4389 bool IsDependent = Record[Idx++];
4390 QualType T
Douglas Gregor4163aca2011-09-09 21:34:22 +00004391 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00004392 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004393 return T;
4394 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00004395
John McCall81904512011-01-06 01:58:22 +00004396 case TYPE_ATTRIBUTED: {
4397 if (Record.size() != 3) {
4398 Error("incorrect encoding of attributed type");
4399 return QualType();
4400 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004401 QualType modifiedType = readType(*Loc.F, Record, Idx);
4402 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00004403 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004404 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall81904512011-01-06 01:58:22 +00004405 }
4406
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004407 case TYPE_PAREN: {
4408 if (Record.size() != 1) {
4409 Error("incorrect encoding of paren type");
4410 return QualType();
4411 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004412 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004413 return Context.getParenType(InnerType);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004414 }
4415
Douglas Gregord2fa7662010-12-20 02:24:11 +00004416 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00004417 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00004418 Error("incorrect encoding of pack expansion type");
4419 return QualType();
4420 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004421 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00004422 if (Pattern.isNull())
4423 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00004424 llvm::Optional<unsigned> NumExpansions;
4425 if (Record[1])
4426 NumExpansions = Record[1] - 1;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004427 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00004428 }
4429
Sebastian Redl539c5062010-08-18 23:57:32 +00004430 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004431 unsigned Idx = 0;
4432 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004433 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004434 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004435 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00004436 }
4437
Sebastian Redl539c5062010-08-18 23:57:32 +00004438 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00004439 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004440 ObjCInterfaceDecl *ItfD
4441 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregorf3bccd72012-01-17 19:21:53 +00004442 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCall8b07ec22010-05-15 11:32:37 +00004443 }
4444
Sebastian Redl539c5062010-08-18 23:57:32 +00004445 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00004446 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004447 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00004448 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004449 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00004450 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00004451 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00004452 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00004453 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004454
Sebastian Redl539c5062010-08-18 23:57:32 +00004455 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00004456 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004457 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004458 return Context.getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00004459 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00004460
Sebastian Redl539c5062010-08-18 23:57:32 +00004461 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00004462 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004463 QualType Parm = readType(*Loc.F, Record, Idx);
4464 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00004465 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00004466 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCallcebee162009-10-18 09:09:24 +00004467 Replacement);
4468 }
John McCalle78aac42010-03-10 03:28:59 +00004469
Douglas Gregorada4b792011-01-14 02:55:32 +00004470 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4471 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004472 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00004473 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004474 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorada4b792011-01-14 02:55:32 +00004475 cast<TemplateTypeParmType>(Parm),
4476 ArgPack);
4477 }
4478
Sebastian Redl539c5062010-08-18 23:57:32 +00004479 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00004480 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004481 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00004482 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004483 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00004484 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00004485 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00004486 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004487
Sebastian Redl539c5062010-08-18 23:57:32 +00004488 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004489 unsigned Idx = 0;
4490 unsigned Depth = Record[Idx++];
4491 unsigned Index = Record[Idx++];
4492 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004493 TemplateTypeParmDecl *D
4494 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004495 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004496 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004497
Sebastian Redl539c5062010-08-18 23:57:32 +00004498 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00004499 unsigned Idx = 0;
4500 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004501 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00004502 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004503 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00004504 if (!Canon.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004505 Canon = Context.getCanonicalType(Canon);
4506 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00004507 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004508
Sebastian Redl539c5062010-08-18 23:57:32 +00004509 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004510 unsigned Idx = 0;
4511 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004512 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00004513 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004514 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004515 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004516 Args.reserve(NumArgs);
4517 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004518 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00004519 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004520 Args.size(), Args.data());
4521 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004522
Sebastian Redl539c5062010-08-18 23:57:32 +00004523 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004524 unsigned Idx = 0;
4525
4526 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00004527 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004528 ArrayType::ArraySizeModifier ASM
4529 = (ArrayType::ArraySizeModifier)Record[Idx++];
4530 unsigned IndexTypeQuals = Record[Idx++];
4531
4532 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00004533 Expr *NumElts = ReadExpr(*Loc.F);
4534 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004535
Douglas Gregor4163aca2011-09-09 21:34:22 +00004536 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004537 IndexTypeQuals, Brackets);
4538 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004539
Sebastian Redl539c5062010-08-18 23:57:32 +00004540 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004541 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004542 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00004543 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004544 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00004545 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004546 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004547 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00004548 if (Underlying.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004549 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004550 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004551 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00004552 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00004553 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00004554 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004555 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004556 }
Eli Friedman0dfb8892011-10-06 23:00:33 +00004557
4558 case TYPE_ATOMIC: {
4559 if (Record.size() != 1) {
4560 Error("Incorrect encoding of atomic type");
4561 return QualType();
4562 }
4563 QualType ValueType = readType(*Loc.F, Record, Idx);
4564 return Context.getAtomicType(ValueType);
4565 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004566 }
David Blaikie8a40f702012-01-17 06:56:22 +00004567 llvm_unreachable("Invalid TypeCode!");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004568}
4569
Sebastian Redl2c373b92010-10-05 15:59:54 +00004570class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00004571 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00004572 ModuleFile &F;
Sebastian Redl2c499f62010-08-18 23:56:43 +00004573 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00004574 unsigned &Idx;
4575
Sebastian Redl2c373b92010-10-05 15:59:54 +00004576 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4577 unsigned &I) {
4578 return Reader.ReadSourceLocation(F, R, I);
4579 }
4580
Douglas Gregor7fb09192011-07-21 22:35:25 +00004581 template<typename T>
4582 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4583 return Reader.ReadDeclAs<T>(F, Record, Idx);
4584 }
4585
John McCall8f115c62009-10-16 21:56:05 +00004586public:
Douglas Gregorde3ef502011-11-30 23:21:26 +00004587 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00004588 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00004589 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004590 { }
John McCall8f115c62009-10-16 21:56:05 +00004591
John McCall17001972009-10-18 01:05:36 +00004592 // We want compile-time assurance that we've enumerated all of
4593 // these, so unfortunately we have to declare them first, then
4594 // define them out-of-line.
4595#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00004596#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00004597 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00004598#include "clang/AST/TypeLocNodes.def"
4599
John McCall17001972009-10-18 01:05:36 +00004600 void VisitFunctionTypeLoc(FunctionTypeLoc);
4601 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00004602};
4603
John McCall17001972009-10-18 01:05:36 +00004604void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00004605 // nothing to do
4606}
John McCall17001972009-10-18 01:05:36 +00004607void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004608 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00004609 if (TL.needsExtraLocalData()) {
4610 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4611 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4612 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4613 TL.setModeAttr(Record[Idx++]);
4614 }
John McCall8f115c62009-10-16 21:56:05 +00004615}
John McCall17001972009-10-18 01:05:36 +00004616void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004617 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004618}
John McCall17001972009-10-18 01:05:36 +00004619void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004620 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004621}
John McCall17001972009-10-18 01:05:36 +00004622void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004623 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004624}
John McCall17001972009-10-18 01:05:36 +00004625void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004626 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004627}
John McCall17001972009-10-18 01:05:36 +00004628void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004629 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004630}
John McCall17001972009-10-18 01:05:36 +00004631void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004632 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00004633 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004634}
John McCall17001972009-10-18 01:05:36 +00004635void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004636 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4637 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004638 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00004639 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004640 else
John McCall17001972009-10-18 01:05:36 +00004641 TL.setSizeExpr(0);
4642}
4643void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4644 VisitArrayTypeLoc(TL);
4645}
4646void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4647 VisitArrayTypeLoc(TL);
4648}
4649void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4650 VisitArrayTypeLoc(TL);
4651}
4652void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4653 DependentSizedArrayTypeLoc TL) {
4654 VisitArrayTypeLoc(TL);
4655}
4656void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4657 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004658 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004659}
4660void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004661 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004662}
4663void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004664 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004665}
4666void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004667 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004668 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4669 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004670 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004671 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00004672 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004673 }
4674}
4675void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4676 VisitFunctionTypeLoc(TL);
4677}
4678void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4679 VisitFunctionTypeLoc(TL);
4680}
John McCallb96ec562009-12-04 22:46:56 +00004681void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004682 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00004683}
John McCall17001972009-10-18 01:05:36 +00004684void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004685 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004686}
4687void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004688 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4689 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4690 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004691}
4692void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004693 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4694 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4695 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4696 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004697}
4698void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004699 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004700}
Alexis Hunte852b102011-05-24 22:41:36 +00004701void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4702 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4703 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4704 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4705 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4706}
Richard Smith30482bc2011-02-20 03:19:35 +00004707void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4708 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4709}
John McCall17001972009-10-18 01:05:36 +00004710void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004711 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004712}
4713void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004714 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004715}
John McCall81904512011-01-06 01:58:22 +00004716void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4717 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4718 if (TL.hasAttrOperand()) {
4719 SourceRange range;
4720 range.setBegin(ReadSourceLocation(Record, Idx));
4721 range.setEnd(ReadSourceLocation(Record, Idx));
4722 TL.setAttrOperandParensRange(range);
4723 }
4724 if (TL.hasAttrExprOperand()) {
4725 if (Record[Idx++])
4726 TL.setAttrExprOperand(Reader.ReadExpr(F));
4727 else
4728 TL.setAttrExprOperand(0);
4729 } else if (TL.hasAttrEnumOperand())
4730 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4731}
John McCall17001972009-10-18 01:05:36 +00004732void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004733 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004734}
John McCallcebee162009-10-18 09:09:24 +00004735void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4736 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004737 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00004738}
Douglas Gregorada4b792011-01-14 02:55:32 +00004739void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4740 SubstTemplateTypeParmPackTypeLoc TL) {
4741 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4742}
John McCall17001972009-10-18 01:05:36 +00004743void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4744 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004745 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004746 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4747 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4748 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00004749 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4750 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004751 Reader.GetTemplateArgumentLocInfo(F,
4752 TL.getTypePtr()->getArg(i).getKind(),
4753 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004754}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004755void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4756 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4757 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4758}
Abramo Bagnara6150c882010-05-11 21:36:43 +00004759void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +00004760 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00004761 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004762}
John McCalle78aac42010-03-10 03:28:59 +00004763void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004764 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00004765}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00004766void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +00004767 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00004768 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004769 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004770}
John McCallc392f372010-06-11 00:33:02 +00004771void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4772 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004773 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00004774 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +00004775 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004776 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004777 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4778 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00004779 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4780 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004781 Reader.GetTemplateArgumentLocInfo(F,
4782 TL.getTypePtr()->getArg(I).getKind(),
4783 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00004784}
Douglas Gregord2fa7662010-12-20 02:24:11 +00004785void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4786 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4787}
John McCall17001972009-10-18 01:05:36 +00004788void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004789 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00004790}
4791void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4792 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004793 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4794 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004795 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004796 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004797}
John McCallfc93cf92009-10-22 22:37:11 +00004798void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004799 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00004800}
Eli Friedman0dfb8892011-10-06 23:00:33 +00004801void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4802 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4803 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4804 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4805}
John McCall8f115c62009-10-16 21:56:05 +00004806
Douglas Gregorde3ef502011-11-30 23:21:26 +00004807TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004808 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00004809 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004810 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00004811 if (InfoTy.isNull())
4812 return 0;
4813
Douglas Gregor4163aca2011-09-09 21:34:22 +00004814 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004815 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00004816 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00004817 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00004818 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00004819}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004820
Sebastian Redl539c5062010-08-18 23:57:32 +00004821QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00004822 unsigned FastQuals = ID & Qualifiers::FastMask;
4823 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004824
Sebastian Redl539c5062010-08-18 23:57:32 +00004825 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004826 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00004827 switch ((PredefinedTypeIDs)Index) {
4828 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor4163aca2011-09-09 21:34:22 +00004829 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4830 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004831
Sebastian Redl539c5062010-08-18 23:57:32 +00004832 case PREDEF_TYPE_CHAR_U_ID:
4833 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004834 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor4163aca2011-09-09 21:34:22 +00004835 T = Context.CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004836 break;
4837
Douglas Gregor4163aca2011-09-09 21:34:22 +00004838 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4839 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4840 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4841 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4842 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4843 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4844 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4845 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4846 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4847 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4848 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4849 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4850 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004851 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004852 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4853 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4854 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4855 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4856 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall526ab472011-10-25 17:37:35 +00004857 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004858 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4859 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4860 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4861 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4862 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4863 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4864 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4865 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4866 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00004867
4868 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004869 T = Context.getAutoRRefDeductType();
Douglas Gregoreda8e122011-08-09 15:13:55 +00004870 break;
John McCall8a6b59a2011-10-17 18:09:15 +00004871
4872 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4873 T = Context.ARCUnbridgedCastTy;
4874 break;
4875
Meador Ingecfb60902012-07-01 15:57:25 +00004876 case PREDEF_TYPE_VA_LIST_TAG:
4877 T = Context.getVaListTagType();
4878 break;
Eli Friedman34866c72012-08-31 00:14:07 +00004879
4880 case PREDEF_TYPE_BUILTIN_FN:
4881 T = Context.BuiltinFnTy;
4882 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004883 }
4884
4885 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00004886 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004887 }
4888
Sebastian Redl539c5062010-08-18 23:57:32 +00004889 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004890 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00004891 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004892 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004893 if (TypesLoaded[Index].isNull())
4894 return QualType();
4895
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004896 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004897 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004898 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004899 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00004900 }
Mike Stump11289f42009-09-09 15:08:12 +00004901
John McCall8ccfcb52009-09-24 19:53:00 +00004902 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004903}
4904
Douglas Gregorde3ef502011-11-30 23:21:26 +00004905QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004906 return GetType(getGlobalTypeID(F, LocalID));
4907}
4908
4909serialization::TypeID
Douglas Gregorde3ef502011-11-30 23:21:26 +00004910ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004911 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4912 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4913
4914 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4915 return LocalID;
4916
4917 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4918 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4919 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4920
4921 unsigned GlobalIndex = LocalIndex + I->second;
4922 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4923}
4924
John McCall0ad16662009-10-29 08:12:44 +00004925TemplateArgumentLocInfo
Douglas Gregorde3ef502011-11-30 23:21:26 +00004926ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004927 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00004928 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004929 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00004930 switch (Kind) {
4931 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004932 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00004933 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004934 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004935 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004936 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4937 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004938 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004939 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004940 SourceLocation());
4941 }
4942 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004943 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4944 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004945 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004946 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004947 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00004948 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004949 }
John McCall0ad16662009-10-29 08:12:44 +00004950 case TemplateArgument::Null:
4951 case TemplateArgument::Integral:
4952 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004953 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004954 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004955 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004956 return TemplateArgumentLocInfo();
4957 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004958 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00004959}
4960
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004961TemplateArgumentLoc
Douglas Gregorde3ef502011-11-30 23:21:26 +00004962ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004963 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004964 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004965
4966 if (Arg.getKind() == TemplateArgument::Expression) {
4967 if (Record[Index++]) // bool InfoHasSameExpr.
4968 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4969 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004970 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004971 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004972}
4973
Sebastian Redl2c499f62010-08-18 23:56:43 +00004974Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004975 return GetDecl(ID);
4976}
4977
Douglas Gregorde3ef502011-11-30 23:21:26 +00004978uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregorc27b2872011-08-04 00:01:48 +00004979 unsigned &Idx){
4980 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004981 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004982
Douglas Gregorc27b2872011-08-04 00:01:48 +00004983 unsigned LocalID = Record[Idx++];
4984 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004985}
4986
4987CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00004988 RecordLocation Loc = getLocalBitOffset(Offset);
4989 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004990 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00004991 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004992 ReadingKindTracker ReadingKind(Read_Decl, *this);
4993 RecordData Record;
4994 unsigned Code = Cursor.ReadCode();
4995 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4996 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4997 Error("Malformed AST file: missing C++ base specifiers");
4998 return 0;
4999 }
5000
5001 unsigned Idx = 0;
5002 unsigned NumBases = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00005003 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005004 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5005 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00005006 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005007 return Bases;
5008}
5009
Douglas Gregor7fb09192011-07-21 22:35:25 +00005010serialization::DeclID
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00005011ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005012 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00005013 return LocalID;
5014
5015 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005016 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00005017 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5018
5019 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00005020}
5021
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005022bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregorde3ef502011-11-30 23:21:26 +00005023 ModuleFile &M) const {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005024 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5025 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5026 return &M == I->second;
5027}
5028
Douglas Gregor404cdde2012-01-27 01:47:08 +00005029ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5030 if (!D->isFromASTFile())
5031 return 0;
5032 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5033 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5034 return I->second;
5035}
5036
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005037SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5038 if (ID < NUM_PREDEF_DECL_IDS)
5039 return SourceLocation();
5040
5041 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5042
5043 if (Index > DeclsLoaded.size()) {
5044 Error("declaration ID out-of-range for AST file");
5045 return SourceLocation();
5046 }
5047
5048 if (Decl *D = DeclsLoaded[Index])
5049 return D->getLocation();
5050
5051 unsigned RawLocation = 0;
5052 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5053 return ReadSourceLocation(*Rec.F, RawLocation);
5054}
5055
Sebastian Redl539c5062010-08-18 23:57:32 +00005056Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005057 if (ID < NUM_PREDEF_DECL_IDS) {
5058 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00005059 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005060 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00005061
5062 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005063 return Context.getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00005064
5065 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005066 return Context.getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00005067
Douglas Gregor52e02802011-08-12 06:17:30 +00005068 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005069 return Context.getObjCSelDecl();
Douglas Gregor52e02802011-08-12 06:17:30 +00005070
Douglas Gregor0a586182011-08-12 05:59:41 +00005071 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005072 return Context.getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00005073
Douglas Gregord53ae832012-01-17 18:09:05 +00005074 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5075 return Context.getObjCProtocolDecl();
5076
Douglas Gregor801c99d2011-08-12 06:49:56 +00005077 case PREDEF_DECL_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005078 return Context.getInt128Decl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00005079
5080 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005081 return Context.getUInt128Decl();
Douglas Gregorbab8a962011-09-08 01:46:34 +00005082
5083 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005084 return Context.getObjCInstanceTypeDecl();
Meador Inge5d3fb222012-06-16 03:34:49 +00005085
5086 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5087 return Context.getBuiltinVaListDecl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005088 }
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005089 }
5090
Douglas Gregordab42432011-08-12 00:15:20 +00005091 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5092
Richard Smithce3ad9a2011-12-20 04:39:57 +00005093 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005094 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005095 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005096 return 0;
Douglas Gregor745ed142009-04-25 18:35:21 +00005097 }
Douglas Gregordab42432011-08-12 00:15:20 +00005098
Douglas Gregor81252352011-12-16 22:37:11 +00005099 if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00005100 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005101 if (DeserializationListener)
5102 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5103 }
Douglas Gregor745ed142009-04-25 18:35:21 +00005104
5105 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005106}
5107
Douglas Gregor05f10352011-12-17 23:38:30 +00005108DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5109 DeclID GlobalID) {
5110 if (GlobalID < NUM_PREDEF_DECL_IDS)
5111 return GlobalID;
5112
5113 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5114 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5115 ModuleFile *Owner = I->second;
5116
5117 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5118 = M.GlobalToLocalDeclIDs.find(Owner);
5119 if (Pos == M.GlobalToLocalDeclIDs.end())
5120 return 0;
5121
5122 return GlobalID - Owner->BaseDeclID + Pos->second;
5123}
5124
Douglas Gregorde3ef502011-11-30 23:21:26 +00005125serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005126 const RecordData &Record,
5127 unsigned &Idx) {
5128 if (Idx >= Record.size()) {
5129 Error("Corrupted AST file");
5130 return 0;
5131 }
5132
5133 return getGlobalDeclID(F, Record[Idx++]);
5134}
5135
Chris Lattner9c28af02009-04-27 05:46:25 +00005136/// \brief Resolve the offset of a statement into a statement.
5137///
5138/// This operation will read a new statement from the external
5139/// source each time it is called, and is meant to be used via a
5140/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00005141Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005142 // Switch case IDs are per Decl.
5143 ClearSwitchCaseIDs();
5144
Sebastian Redl5c415f32010-07-22 17:01:13 +00005145 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00005146 RecordLocation Loc = getLocalBitOffset(Offset);
5147 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5148 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00005149}
5150
Douglas Gregor1257f972011-08-24 21:27:34 +00005151namespace {
5152 class FindExternalLexicalDeclsVisitor {
5153 ASTReader &Reader;
5154 const DeclContext *DC;
5155 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00005156
Douglas Gregor1257f972011-08-24 21:27:34 +00005157 SmallVectorImpl<Decl*> &Decls;
5158 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5159
5160 public:
5161 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5162 bool (*isKindWeWant)(Decl::Kind),
5163 SmallVectorImpl<Decl*> &Decls)
5164 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5165 {
5166 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5167 PredefsVisited[I] = false;
5168 }
5169
Douglas Gregorde3ef502011-11-30 23:21:26 +00005170 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor1257f972011-08-24 21:27:34 +00005171 if (Preorder)
5172 return false;
5173
5174 FindExternalLexicalDeclsVisitor *This
5175 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5176
Douglas Gregorde3ef502011-11-30 23:21:26 +00005177 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor1257f972011-08-24 21:27:34 +00005178 = M.DeclContextInfos.find(This->DC);
5179 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5180 return false;
5181
5182 // Load all of the declaration IDs
5183 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5184 *IDE = ID + Info->second.NumLexicalDecls;
5185 ID != IDE; ++ID) {
5186 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5187 continue;
5188
5189 // Don't add predefined declarations to the lexical context more
5190 // than once.
5191 if (ID->second < NUM_PREDEF_DECL_IDS) {
5192 if (This->PredefsVisited[ID->second])
5193 continue;
5194
5195 This->PredefsVisited[ID->second] = true;
5196 }
5197
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00005198 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5199 if (!This->DC->isDeclInLexicalTraversal(D))
5200 This->Decls.push_back(D);
5201 }
Douglas Gregor1257f972011-08-24 21:27:34 +00005202 }
5203
5204 return false;
5205 }
5206 };
5207}
5208
Douglas Gregor3d0adb32011-07-15 21:46:17 +00005209ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00005210 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005211 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00005212 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor1257f972011-08-24 21:27:34 +00005213 // least. Walk all of the modules in the order they were loaded.
5214 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5215 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00005216 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00005217 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005218}
5219
Douglas Gregor94619c82011-08-24 19:03:07 +00005220namespace {
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005221
5222class DeclIDComp {
5223 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00005224 ModuleFile &Mod;
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005225
5226public:
Douglas Gregorde3ef502011-11-30 23:21:26 +00005227 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005228
5229 bool operator()(LocalDeclID L, LocalDeclID R) const {
5230 SourceLocation LHS = getLocation(L);
5231 SourceLocation RHS = getLocation(R);
5232 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5233 }
5234
5235 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5236 SourceLocation RHS = getLocation(R);
5237 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5238 }
5239
5240 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5241 SourceLocation LHS = getLocation(L);
5242 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5243 }
5244
5245 SourceLocation getLocation(LocalDeclID ID) const {
5246 return Reader.getSourceManager().getFileLoc(
5247 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5248 }
5249};
5250
5251}
5252
5253void ASTReader::FindFileRegionDecls(FileID File,
5254 unsigned Offset, unsigned Length,
5255 SmallVectorImpl<Decl *> &Decls) {
5256 SourceManager &SM = getSourceManager();
5257
5258 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5259 if (I == FileDeclIDs.end())
5260 return;
5261
5262 FileDeclsInfo &DInfo = I->second;
5263 if (DInfo.Decls.empty())
5264 return;
5265
5266 SourceLocation
5267 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5268 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5269
5270 DeclIDComp DIDComp(*this, *DInfo.Mod);
5271 ArrayRef<serialization::LocalDeclID>::iterator
5272 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5273 BeginLoc, DIDComp);
5274 if (BeginIt != DInfo.Decls.begin())
5275 --BeginIt;
5276
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00005277 // If we are pointing at a top-level decl inside an objc container, we need
5278 // to backtrack until we find it otherwise we will fail to report that the
5279 // region overlaps with an objc container.
5280 while (BeginIt != DInfo.Decls.begin() &&
5281 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5282 ->isTopLevelDeclInObjCContainer())
5283 --BeginIt;
5284
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005285 ArrayRef<serialization::LocalDeclID>::iterator
5286 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5287 EndLoc, DIDComp);
5288 if (EndIt != DInfo.Decls.end())
5289 ++EndIt;
5290
5291 for (ArrayRef<serialization::LocalDeclID>::iterator
5292 DIt = BeginIt; DIt != EndIt; ++DIt)
5293 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5294}
5295
5296namespace {
Douglas Gregorde3ef502011-11-30 23:21:26 +00005297 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor94619c82011-08-24 19:03:07 +00005298 /// declaration context.
5299 class DeclContextNameLookupVisitor {
5300 ASTReader &Reader;
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005301 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor94619c82011-08-24 19:03:07 +00005302 DeclarationName Name;
5303 SmallVectorImpl<NamedDecl *> &Decls;
5304
5305 public:
5306 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005307 SmallVectorImpl<const DeclContext *> &Contexts,
5308 DeclarationName Name,
Douglas Gregor94619c82011-08-24 19:03:07 +00005309 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005310 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor94619c82011-08-24 19:03:07 +00005311
Douglas Gregorde3ef502011-11-30 23:21:26 +00005312 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor94619c82011-08-24 19:03:07 +00005313 DeclContextNameLookupVisitor *This
5314 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5315
5316 // Check whether we have any visible declaration information for
5317 // this context in this module.
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005318 ModuleFile::DeclContextInfosMap::iterator Info;
5319 bool FoundInfo = false;
5320 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5321 Info = M.DeclContextInfos.find(This->Contexts[I]);
5322 if (Info != M.DeclContextInfos.end() &&
5323 Info->second.NameLookupTableData) {
5324 FoundInfo = true;
5325 break;
5326 }
5327 }
Douglas Gregor94619c82011-08-24 19:03:07 +00005328
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005329 if (!FoundInfo)
5330 return false;
5331
Douglas Gregor94619c82011-08-24 19:03:07 +00005332 // Look for this name within this module.
5333 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00005334 Info->second.NameLookupTableData;
Douglas Gregor94619c82011-08-24 19:03:07 +00005335 ASTDeclContextNameLookupTable::iterator Pos
5336 = LookupTable->find(This->Name);
5337 if (Pos == LookupTable->end())
5338 return false;
5339
5340 bool FoundAnything = false;
5341 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5342 for (; Data.first != Data.second; ++Data.first) {
5343 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5344 if (!ND)
5345 continue;
5346
5347 if (ND->getDeclName() != This->Name) {
Axel Naumanna8243e92012-10-01 09:51:27 +00005348 // A name might be null because the decl's redeclarable part is
5349 // currently read before reading its name. The lookup is triggered by
5350 // building that decl (likely indirectly), and so it is later in the
5351 // sense of "already existing" and can be ignored here.
Douglas Gregor94619c82011-08-24 19:03:07 +00005352 continue;
5353 }
5354
5355 // Record this declaration.
5356 FoundAnything = true;
5357 This->Decls.push_back(ND);
5358 }
5359
5360 return FoundAnything;
5361 }
5362 };
5363}
5364
John McCall75b960e2010-06-01 09:23:16 +00005365DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00005366ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00005367 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00005368 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005369 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00005370 if (!Name)
5371 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5372 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00005373
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005374 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005375
5376 // Compute the declaration contexts we need to look into. Multiple such
5377 // declaration contexts occur when two declaration contexts from disjoint
5378 // modules get merged, e.g., when two namespaces with the same name are
5379 // independently defined in separate modules.
5380 SmallVector<const DeclContext *, 2> Contexts;
5381 Contexts.push_back(DC);
5382
5383 if (DC->isNamespace()) {
5384 MergedDeclsMap::iterator Merged
5385 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5386 if (Merged != MergedDecls.end()) {
5387 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5388 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5389 }
5390 }
5391
5392 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor94619c82011-08-24 19:03:07 +00005393 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00005394 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00005395 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00005396 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005397}
5398
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005399namespace {
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005400 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005401 /// declaration context.
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005402 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005403 ASTReader &Reader;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005404 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005405 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005406
5407 public:
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005408 DeclContextAllNamesVisitor(ASTReader &Reader,
5409 SmallVectorImpl<const DeclContext *> &Contexts,
5410 llvm::DenseMap<DeclarationName,
5411 SmallVector<NamedDecl *, 8> > &Decls)
5412 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005413
5414 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005415 DeclContextAllNamesVisitor *This
5416 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005417
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005418 // Check whether we have any visible declaration information for
5419 // this context in this module.
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005420 ModuleFile::DeclContextInfosMap::iterator Info;
5421 bool FoundInfo = false;
5422 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5423 Info = M.DeclContextInfos.find(This->Contexts[I]);
5424 if (Info != M.DeclContextInfos.end() &&
5425 Info->second.NameLookupTableData) {
5426 FoundInfo = true;
5427 break;
5428 }
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005429 }
5430
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005431 if (!FoundInfo)
5432 return false;
5433
5434 ASTDeclContextNameLookupTable *LookupTable =
5435 Info->second.NameLookupTableData;
5436 bool FoundAnything = false;
5437 for (ASTDeclContextNameLookupTable::data_iterator
5438 I = LookupTable->data_begin(), E = LookupTable->data_end();
5439 I != E; ++I) {
5440 ASTDeclContextNameLookupTrait::data_type Data = *I;
5441 for (; Data.first != Data.second; ++Data.first) {
5442 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5443 *Data.first);
5444 if (!ND)
5445 continue;
5446
5447 // Record this declaration.
5448 FoundAnything = true;
5449 This->Decls[ND->getDeclName()].push_back(ND);
5450 }
5451 }
5452
5453 return FoundAnything;
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005454 }
5455 };
5456}
5457
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005458void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005459 if (!DC->hasExternalVisibleStorage())
5460 return;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005461 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5462
5463 // Compute the declaration contexts we need to look into. Multiple such
5464 // declaration contexts occur when two declaration contexts from disjoint
5465 // modules get merged, e.g., when two namespaces with the same name are
5466 // independently defined in separate modules.
5467 SmallVector<const DeclContext *, 2> Contexts;
5468 Contexts.push_back(DC);
5469
5470 if (DC->isNamespace()) {
5471 MergedDeclsMap::iterator Merged
5472 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5473 if (Merged != MergedDecls.end()) {
5474 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5475 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5476 }
5477 }
5478
5479 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5480 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5481 ++NumVisibleDeclContextsRead;
5482
5483 for (llvm::DenseMap<DeclarationName,
5484 llvm::SmallVector<NamedDecl*, 8> >::iterator
5485 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5486 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5487 }
Argyrios Kyrtzidis0334d332012-04-26 18:34:14 +00005488 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005489}
5490
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005491/// \brief Under non-PCH compilation the consumer receives the objc methods
5492/// before receiving the implementation, and codegen depends on this.
5493/// We simulate this by deserializing and passing to consumer the methods of the
5494/// implementation before passing the deserialized implementation decl.
5495static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5496 ASTConsumer *Consumer) {
5497 assert(ImplD && Consumer);
5498
5499 for (ObjCImplDecl::method_iterator
5500 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie40ed2972012-06-06 20:45:41 +00005501 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005502
5503 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5504}
5505
Sebastian Redl2c499f62010-08-18 23:56:43 +00005506void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005507 assert(Consumer);
5508 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005509 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005510 InterestingDecls.pop_front();
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005511
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00005512 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005513 }
5514}
5515
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00005516void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5517 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5518 PassObjCImplDeclToConsumer(ImplD, Consumer);
5519 else
5520 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5521}
5522
Sebastian Redl2c499f62010-08-18 23:56:43 +00005523void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00005524 this->Consumer = Consumer;
5525
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005526 if (!Consumer)
5527 return;
5528
5529 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005530 // Force deserialization of this decl, which will cause it to be queued for
5531 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00005532 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005533 }
Douglas Gregor6137d322011-09-15 18:47:32 +00005534 ExternalDefinitions.clear();
Douglas Gregorf005eac2009-04-25 00:41:30 +00005535
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005536 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005537}
5538
Sebastian Redl2c499f62010-08-18 23:56:43 +00005539void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005540 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005541
Mike Stump11289f42009-09-09 15:08:12 +00005542 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00005543 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00005544 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00005545 unsigned NumDeclsLoaded
5546 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5547 (Decl *)0);
5548 unsigned NumIdentifiersLoaded
5549 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5550 IdentifiersLoaded.end(),
5551 (IdentifierInfo *)0);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005552 unsigned NumMacrosLoaded
5553 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5554 MacrosLoaded.end(),
5555 (MacroInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00005556 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00005557 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5558 SelectorsLoaded.end(),
5559 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00005560
Douglas Gregor49bf76b2011-07-21 18:46:38 +00005561 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00005562 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5563 NumSLocEntriesRead, TotalNumSLocEntries,
5564 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00005565 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005566 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00005567 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5568 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5569 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005570 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00005571 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5572 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00005573 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005574 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00005575 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5576 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005577 if (!MacrosLoaded.empty())
5578 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5579 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5580 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00005581 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005582 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00005583 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5584 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00005585 if (TotalNumStatements)
5586 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5587 NumStatementsRead, TotalNumStatements,
5588 ((float)NumStatementsRead/TotalNumStatements * 100));
5589 if (TotalNumMacros)
5590 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5591 NumMacrosRead, TotalNumMacros,
5592 ((float)NumMacrosRead/TotalNumMacros * 100));
5593 if (TotalLexicalDeclContexts)
5594 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5595 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5596 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5597 * 100));
5598 if (TotalVisibleDeclContexts)
5599 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5600 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5601 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5602 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005603 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00005604 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005605 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5606 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00005607 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005608 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00005609 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005610 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00005611 dump();
5612 std::fprintf(stderr, "\n");
5613}
5614
Douglas Gregorde3ef502011-11-30 23:21:26 +00005615template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00005616static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005617dumpModuleIDMap(StringRef Name,
Douglas Gregorde3ef502011-11-30 23:21:26 +00005618 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor204b8712011-07-21 19:50:14 +00005619 InitialCapacity> &Map) {
5620 if (Map.begin() == Map.end())
5621 return;
5622
Douglas Gregorde3ef502011-11-30 23:21:26 +00005623 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00005624 llvm::errs() << Name << ":\n";
5625 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5626 I != IEnd; ++I) {
5627 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5628 << "\n";
5629 }
5630}
5631
Douglas Gregor204b8712011-07-21 19:50:14 +00005632void ASTReader::dump() {
Douglas Gregorde3ef502011-11-30 23:21:26 +00005633 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00005634 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00005635 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00005636 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005637 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005638 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005639 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor253eefe2011-12-01 00:59:36 +00005640 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005641 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005642 dumpModuleIDMap("Global preprocessed entity map",
5643 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005644
5645 llvm::errs() << "\n*** PCH/Modules Loaded:";
5646 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5647 MEnd = ModuleMgr.end();
5648 M != MEnd; ++M)
5649 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005650}
5651
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005652/// Return the amount of memory used by memory buffers, breaking down
5653/// by heap-backed versus mmap'ed memory.
5654void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005655 for (ModuleConstIterator I = ModuleMgr.begin(),
5656 E = ModuleMgr.end(); I != E; ++I) {
5657 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005658 size_t bytes = buf->getBufferSize();
5659 switch (buf->getBufferKind()) {
5660 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5661 sizes.malloc_bytes += bytes;
5662 break;
5663 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5664 sizes.mmap_bytes += bytes;
5665 break;
5666 }
5667 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005668 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005669}
5670
Sebastian Redl2c499f62010-08-18 23:56:43 +00005671void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00005672 SemaObj = &S;
Axel Naumanndd433f02012-10-18 19:05:02 +00005673 S.addExternalSource(this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00005674
Douglas Gregor7cd60f72009-04-22 21:15:06 +00005675 // Makes sure any declarations that were deserialized "too early"
5676 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00005677 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005678 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5679 PreloadedDecls[I]->getDeclName());
Douglas Gregora868bbd2009-04-21 22:25:48 +00005680 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00005681 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00005682
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005683 // Load the offsets of the declarations that Sema references.
5684 // They will be lazily deserialized when needed.
5685 if (!SemaDeclRefs.empty()) {
5686 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00005687 if (!SemaObj->StdNamespace)
5688 SemaObj->StdNamespace = SemaDeclRefs[0];
5689 if (!SemaObj->StdBadAlloc)
5690 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005691 }
5692
Peter Collingbourne5df20e02011-02-15 19:46:30 +00005693 if (!FPPragmaOptions.empty()) {
5694 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5695 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5696 }
5697
5698 if (!OpenCLExtensions.empty()) {
5699 unsigned I = 0;
5700#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5701#include "clang/Basic/OpenCLExtensions.def"
5702
5703 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5704 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00005705}
5706
Douglas Gregorab443b92011-08-20 04:39:52 +00005707IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00005708 // Note that we are loading an identifier.
5709 Deserializing AnIdentifier(this);
5710
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00005711 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5712 /*PriorGeneration=*/0);
Douglas Gregorab443b92011-08-20 04:39:52 +00005713 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005714 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00005715 markIdentifierUpToDate(II);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005716 return II;
Douglas Gregora868bbd2009-04-21 22:25:48 +00005717}
5718
Douglas Gregor57756ea2010-10-14 22:11:03 +00005719namespace clang {
5720 /// \brief An identifier-lookup iterator that enumerates all of the
5721 /// identifiers stored within a set of AST files.
5722 class ASTIdentifierIterator : public IdentifierIterator {
5723 /// \brief The AST reader whose identifiers are being enumerated.
5724 const ASTReader &Reader;
5725
5726 /// \brief The current index into the chain of AST files stored in
5727 /// the AST reader.
5728 unsigned Index;
5729
5730 /// \brief The current position within the identifier lookup table
5731 /// of the current AST file.
5732 ASTIdentifierLookupTable::key_iterator Current;
5733
5734 /// \brief The end position within the identifier lookup table of
5735 /// the current AST file.
5736 ASTIdentifierLookupTable::key_iterator End;
5737
5738 public:
5739 explicit ASTIdentifierIterator(const ASTReader &Reader);
5740
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005741 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00005742 };
5743}
5744
5745ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005746 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00005747 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005748 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00005749 Current = IdTable->key_begin();
5750 End = IdTable->key_end();
5751}
5752
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005753StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00005754 while (Current == End) {
5755 // If we have exhausted all of our AST files, we're done.
5756 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005757 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00005758
5759 --Index;
5760 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005761 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5762 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00005763 Current = IdTable->key_begin();
5764 End = IdTable->key_end();
5765 }
5766
5767 // We have any identifiers remaining in the current AST file; return
5768 // the next one.
5769 std::pair<const char*, unsigned> Key = *Current;
5770 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005771 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00005772}
5773
5774IdentifierIterator *ASTReader::getIdentifiers() const {
5775 return new ASTIdentifierIterator(*this);
5776}
5777
Douglas Gregorc10edd62011-08-25 14:51:20 +00005778namespace clang { namespace serialization {
5779 class ReadMethodPoolVisitor {
5780 ASTReader &Reader;
Douglas Gregord1f01d72012-01-25 01:14:32 +00005781 Selector Sel;
5782 unsigned PriorGeneration;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005783 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5784 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorc78d3462009-04-24 21:10:55 +00005785
Douglas Gregorc10edd62011-08-25 14:51:20 +00005786 public:
Douglas Gregord1f01d72012-01-25 01:14:32 +00005787 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5788 unsigned PriorGeneration)
5789 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregorc10edd62011-08-25 14:51:20 +00005790
Douglas Gregorde3ef502011-11-30 23:21:26 +00005791 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregorc10edd62011-08-25 14:51:20 +00005792 ReadMethodPoolVisitor *This
5793 = static_cast<ReadMethodPoolVisitor *>(UserData);
5794
5795 if (!M.SelectorLookupTable)
5796 return false;
5797
Douglas Gregord1f01d72012-01-25 01:14:32 +00005798 // If we've already searched this module file, skip it now.
5799 if (M.Generation <= This->PriorGeneration)
5800 return true;
5801
Douglas Gregorc10edd62011-08-25 14:51:20 +00005802 ASTSelectorLookupTable *PoolTable
5803 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5804 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5805 if (Pos == PoolTable->end())
5806 return false;
5807
5808 ++This->Reader.NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005809 // FIXME: Not quite happy with the statistics here. We probably should
5810 // disable this tracking when called via LoadSelector.
5811 // Also, should entries without methods count as misses?
Douglas Gregorc10edd62011-08-25 14:51:20 +00005812 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005813 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005814 if (This->Reader.DeserializationListener)
5815 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5816 This->Sel);
5817
5818 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5819 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5820 return true;
Sebastian Redlada023c2010-08-04 20:40:17 +00005821 }
Douglas Gregorc10edd62011-08-25 14:51:20 +00005822
5823 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregore1716012012-01-25 00:49:42 +00005824 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5825 return InstanceMethods;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005826 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00005827
Douglas Gregorc10edd62011-08-25 14:51:20 +00005828 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregore1716012012-01-25 00:49:42 +00005829 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5830 return FactoryMethods;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005831 }
5832 };
5833} } // end namespace clang::serialization
5834
Douglas Gregore1716012012-01-25 00:49:42 +00005835/// \brief Add the given set of methods to the method list.
5836static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5837 ObjCMethodList &List) {
5838 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5839 S.addMethodToGlobalList(&List, Methods[I]);
5840 }
5841}
5842
5843void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregord1f01d72012-01-25 01:14:32 +00005844 // Get the selector generation and update it to the current generation.
5845 unsigned &Generation = SelectorGeneration[Sel];
5846 unsigned PriorGeneration = Generation;
5847 Generation = CurrentGeneration;
5848
5849 // Search for methods defined with this selector.
5850 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregorc10edd62011-08-25 14:51:20 +00005851 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregorc10edd62011-08-25 14:51:20 +00005852
Douglas Gregore1716012012-01-25 00:49:42 +00005853 if (Visitor.getInstanceMethods().empty() &&
5854 Visitor.getFactoryMethods().empty()) {
Douglas Gregorc10edd62011-08-25 14:51:20 +00005855 ++NumMethodPoolMisses;
Douglas Gregore1716012012-01-25 00:49:42 +00005856 return;
5857 }
5858
5859 if (!getSema())
5860 return;
5861
5862 Sema &S = *getSema();
5863 Sema::GlobalMethodPool::iterator Pos
5864 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5865
5866 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5867 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorc78d3462009-04-24 21:10:55 +00005868}
5869
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005870void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005871 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005872 Namespaces.clear();
5873
5874 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5875 if (NamespaceDecl *Namespace
5876 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5877 Namespaces.push_back(Namespace);
5878 }
5879}
5880
Douglas Gregoreb08bd42011-07-27 20:58:46 +00005881void ASTReader::ReadTentativeDefinitions(
5882 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5883 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5884 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5885 if (Var)
5886 TentativeDefs.push_back(Var);
5887 }
5888 TentativeDefinitions.clear();
5889}
5890
Douglas Gregora94a1542011-07-27 21:45:57 +00005891void ASTReader::ReadUnusedFileScopedDecls(
5892 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5893 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5894 DeclaratorDecl *D
5895 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5896 if (D)
5897 Decls.push_back(D);
5898 }
5899 UnusedFileScopedDecls.clear();
5900}
5901
Douglas Gregorbae31202011-07-27 21:57:17 +00005902void ASTReader::ReadDelegatingConstructors(
5903 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5904 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5905 CXXConstructorDecl *D
5906 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5907 if (D)
5908 Decls.push_back(D);
5909 }
5910 DelegatingCtorDecls.clear();
5911}
5912
Douglas Gregorb7098a32011-07-28 00:39:29 +00005913void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5914 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5915 TypedefNameDecl *D
5916 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5917 if (D)
5918 Decls.push_back(D);
5919 }
5920 ExtVectorDecls.clear();
5921}
5922
Douglas Gregor32002192011-07-28 00:53:40 +00005923void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5924 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5925 CXXRecordDecl *D
5926 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5927 if (D)
5928 Decls.push_back(D);
5929 }
5930 DynamicClasses.clear();
5931}
5932
Douglas Gregordc5c9582011-07-28 14:20:37 +00005933void
5934ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5935 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5936 NamedDecl *D
5937 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5938 if (D)
5939 Decls.push_back(D);
5940 }
5941 LocallyScopedExternalDecls.clear();
5942}
5943
Douglas Gregor72e357f2011-07-28 14:54:22 +00005944void ASTReader::ReadReferencedSelectors(
5945 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5946 if (ReferencedSelectorsData.empty())
5947 return;
5948
5949 // If there are @selector references added them to its pool. This is for
5950 // implementation of -Wselector.
5951 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5952 unsigned I = 0;
5953 while (I < DataSize) {
5954 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5955 SourceLocation SelLoc
5956 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5957 Sels.push_back(std::make_pair(Sel, SelLoc));
5958 }
5959 ReferencedSelectorsData.clear();
5960}
5961
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00005962void ASTReader::ReadWeakUndeclaredIdentifiers(
5963 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5964 if (WeakUndeclaredIdentifiers.empty())
5965 return;
5966
5967 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5968 IdentifierInfo *WeakId
5969 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5970 IdentifierInfo *AliasId
5971 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5972 SourceLocation Loc
5973 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5974 bool Used = WeakUndeclaredIdentifiers[I++];
5975 WeakInfo WI(AliasId, Loc);
5976 WI.setUsed(Used);
5977 WeakIDs.push_back(std::make_pair(WeakId, WI));
5978 }
5979 WeakUndeclaredIdentifiers.clear();
5980}
5981
Douglas Gregor4daf6a32011-07-28 19:11:31 +00005982void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5983 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5984 ExternalVTableUse VT;
5985 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5986 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5987 VT.DefinitionRequired = VTableUses[Idx++];
5988 VTables.push_back(VT);
5989 }
5990
5991 VTableUses.clear();
5992}
5993
Douglas Gregore39f97c2011-07-28 19:49:54 +00005994void ASTReader::ReadPendingInstantiations(
5995 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5996 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5997 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5998 SourceLocation Loc
5999 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann63469422c2012-10-02 09:09:43 +00006000
Douglas Gregor559458c2012-10-03 18:34:48 +00006001 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregore39f97c2011-07-28 19:49:54 +00006002 }
6003 PendingInstantiations.clear();
6004}
6005
Sebastian Redl2c499f62010-08-18 23:56:43 +00006006void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00006007 // It would be complicated to avoid reading the methods anyway. So don't.
6008 ReadMethodPool(Sel);
6009}
6010
Douglas Gregora3e41532011-07-28 20:55:49 +00006011void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00006012 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00006013 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00006014 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00006015 if (DeserializationListener)
6016 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00006017}
6018
Douglas Gregor1342e842009-07-06 18:54:52 +00006019/// \brief Set the globally-visible declarations associated with the given
6020/// identifier.
6021///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006022/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00006023/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00006024/// them.
6025///
6026/// \param II an IdentifierInfo that refers to one or more globally-visible
6027/// declarations.
6028///
6029/// \param DeclIDs the set of declaration IDs with the name @p II that are
6030/// visible at global scope.
6031///
6032/// \param Nonrecursive should be true to indicate that the caller knows that
6033/// this call is non-recursive, and therefore the globally-visible declarations
6034/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00006035void
Sebastian Redl2c499f62010-08-18 23:56:43 +00006036ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006037 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00006038 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00006039 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00006040 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6041 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6042 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00006043 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00006044 return;
6045 }
Mike Stump11289f42009-09-09 15:08:12 +00006046
Douglas Gregor1342e842009-07-06 18:54:52 +00006047 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6048 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6049 if (SemaObj) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00006050 // Introduce this declaration into the translation-unit scope
6051 // and add it to the declaration chain for this identifier, so
6052 // that (unqualified) name lookup will find it.
6053 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregor1342e842009-07-06 18:54:52 +00006054 } else {
6055 // Queue this declaration so that it will be added to the
6056 // translation unit scope and identifier's declaration chain
6057 // once a Sema object is known.
6058 PreloadedDecls.push_back(D);
6059 }
6060 }
6061}
6062
Douglas Gregora3e41532011-07-28 20:55:49 +00006063IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006064 if (ID == 0)
6065 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00006066
Sebastian Redlc713b962010-07-21 00:46:22 +00006067 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006068 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006069 return 0;
6070 }
Mike Stump11289f42009-09-09 15:08:12 +00006071
Sebastian Redlc713b962010-07-21 00:46:22 +00006072 ID -= 1;
6073 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00006074 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6075 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00006076 ModuleFile *M = I->second;
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006077 unsigned Index = ID - M->BaseIdentifierID;
6078 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00006079
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006080 // All of the strings in the AST file are preceded by a 16-bit length.
6081 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00006082 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6083 // unsigned integers. This is important to avoid integer overflow when
6084 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00006085 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00006086 unsigned StrLen = (((unsigned) StrLenPtr[0])
6087 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00006088 IdentifiersLoaded[ID]
Douglas Gregor51825b42011-09-09 22:02:16 +00006089 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00006090 if (DeserializationListener)
6091 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006092 }
Mike Stump11289f42009-09-09 15:08:12 +00006093
Sebastian Redlc713b962010-07-21 00:46:22 +00006094 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006095}
6096
Douglas Gregorde3ef502011-11-30 23:21:26 +00006097IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregora3e41532011-07-28 20:55:49 +00006098 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6099}
6100
Douglas Gregorde3ef502011-11-30 23:21:26 +00006101IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00006102 if (LocalID < NUM_PREDEF_IDENT_IDS)
6103 return LocalID;
6104
6105 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6106 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6107 assert(I != M.IdentifierRemap.end()
6108 && "Invalid index into identifier index remap");
6109
6110 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00006111}
6112
Douglas Gregore7400892012-10-11 17:41:54 +00006113MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006114 if (ID == 0)
6115 return 0;
6116
6117 if (MacrosLoaded.empty()) {
6118 Error("no macro table in AST file");
6119 return 0;
6120 }
6121
6122 ID -= NUM_PREDEF_MACRO_IDS;
6123 if (!MacrosLoaded[ID]) {
6124 GlobalMacroMapType::iterator I
6125 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6126 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6127 ModuleFile *M = I->second;
6128 unsigned Index = ID - M->BaseMacroID;
Douglas Gregore7400892012-10-11 17:41:54 +00006129 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006130 }
6131
6132 return MacrosLoaded[ID];
6133}
6134
6135MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6136 if (LocalID < NUM_PREDEF_MACRO_IDS)
6137 return LocalID;
6138
6139 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6140 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6141 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6142
6143 return LocalID + I->second;
6144}
6145
Douglas Gregor253eefe2011-12-01 00:59:36 +00006146serialization::SubmoduleID
6147ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6148 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6149 return LocalID;
6150
6151 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6152 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6153 assert(I != M.SubmoduleRemap.end()
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006154 && "Invalid index into submodule index remap");
Douglas Gregor253eefe2011-12-01 00:59:36 +00006155
6156 return LocalID + I->second;
6157}
6158
6159Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6160 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6161 assert(GlobalID == 0 && "Unhandled global submodule ID");
6162 return 0;
6163 }
6164
6165 if (GlobalID > SubmodulesLoaded.size()) {
6166 Error("submodule ID out of range in AST file");
6167 return 0;
6168 }
6169
6170 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6171}
6172
Douglas Gregorde3ef502011-11-30 23:21:26 +00006173Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor074fdc52011-07-28 21:16:51 +00006174 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6175}
6176
6177Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00006178 if (ID == 0)
6179 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00006180
Sebastian Redlada023c2010-08-04 20:40:17 +00006181 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006182 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00006183 return Selector();
6184 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00006185
Sebastian Redlada023c2010-08-04 20:40:17 +00006186 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00006187 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00006188 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6189 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00006190 ModuleFile &M = *I->second;
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006191 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006192 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00006193 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006194 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00006195 if (DeserializationListener)
6196 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00006197 }
6198
Sebastian Redlada023c2010-08-04 20:40:17 +00006199 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00006200}
6201
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00006202Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00006203 return DecodeSelector(ID);
6204}
6205
Sebastian Redl2c499f62010-08-18 23:56:43 +00006206uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00006207 // ID 0 (the null selector) is considered an external selector.
6208 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00006209}
6210
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006211serialization::SelectorID
Douglas Gregorde3ef502011-11-30 23:21:26 +00006212ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006213 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6214 return LocalID;
6215
6216 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6217 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6218 assert(I != M.SelectorRemap.end()
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006219 && "Invalid index into selector index remap");
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006220
6221 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00006222}
6223
Mike Stump11289f42009-09-09 15:08:12 +00006224DeclarationName
Douglas Gregorde3ef502011-11-30 23:21:26 +00006225ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00006226 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006227 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6228 switch (Kind) {
6229 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00006230 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006231
6232 case DeclarationName::ObjCZeroArgSelector:
6233 case DeclarationName::ObjCOneArgSelector:
6234 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00006235 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006236
6237 case DeclarationName::CXXConstructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006238 return Context.DeclarationNames.getCXXConstructorName(
6239 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006240
6241 case DeclarationName::CXXDestructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006242 return Context.DeclarationNames.getCXXDestructorName(
6243 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006244
6245 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006246 return Context.DeclarationNames.getCXXConversionFunctionName(
6247 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006248
6249 case DeclarationName::CXXOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006250 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006251 (OverloadedOperatorKind)Record[Idx++]);
6252
Alexis Hunt3d221f22009-11-29 07:34:05 +00006253 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006254 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00006255 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00006256
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006257 case DeclarationName::CXXUsingDirective:
6258 return DeclarationName::getUsingDirectiveName();
6259 }
6260
David Blaikie8a40f702012-01-17 06:56:22 +00006261 llvm_unreachable("Invalid NameKind!");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006262}
Douglas Gregor55abb232009-04-10 20:39:37 +00006263
Douglas Gregorde3ef502011-11-30 23:21:26 +00006264void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006265 DeclarationNameLoc &DNLoc,
6266 DeclarationName Name,
6267 const RecordData &Record, unsigned &Idx) {
6268 switch (Name.getNameKind()) {
6269 case DeclarationName::CXXConstructorName:
6270 case DeclarationName::CXXDestructorName:
6271 case DeclarationName::CXXConversionFunctionName:
6272 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6273 break;
6274
6275 case DeclarationName::CXXOperatorName:
6276 DNLoc.CXXOperatorName.BeginOpNameLoc
6277 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6278 DNLoc.CXXOperatorName.EndOpNameLoc
6279 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6280 break;
6281
6282 case DeclarationName::CXXLiteralOperatorName:
6283 DNLoc.CXXLiteralOperatorName.OpNameLoc
6284 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6285 break;
6286
6287 case DeclarationName::Identifier:
6288 case DeclarationName::ObjCZeroArgSelector:
6289 case DeclarationName::ObjCOneArgSelector:
6290 case DeclarationName::ObjCMultiArgSelector:
6291 case DeclarationName::CXXUsingDirective:
6292 break;
6293 }
6294}
6295
Douglas Gregorde3ef502011-11-30 23:21:26 +00006296void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006297 DeclarationNameInfo &NameInfo,
6298 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00006299 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006300 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6301 DeclarationNameLoc DNLoc;
6302 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6303 NameInfo.setInfo(DNLoc);
6304}
6305
Douglas Gregorde3ef502011-11-30 23:21:26 +00006306void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006307 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00006308 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006309 unsigned NumTPLists = Record[Idx++];
6310 Info.NumTemplParamLists = NumTPLists;
6311 if (NumTPLists) {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006312 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006313 for (unsigned i=0; i != NumTPLists; ++i)
6314 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6315 }
6316}
6317
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006318TemplateName
Douglas Gregorde3ef502011-11-30 23:21:26 +00006319ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00006320 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006321 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006322 switch (Kind) {
6323 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006324 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006325
6326 case TemplateName::OverloadedTemplate: {
6327 unsigned size = Record[Idx++];
6328 UnresolvedSet<8> Decls;
6329 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006330 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006331
Douglas Gregor4163aca2011-09-09 21:34:22 +00006332 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006333 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006334
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006335 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006336 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006337 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00006338 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006339 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006340 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006341
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006342 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006343 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006344 if (Record[Idx++]) // isIdentifier
Douglas Gregor4163aca2011-09-09 21:34:22 +00006345 return Context.getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00006346 GetIdentifierInfo(F, Record,
6347 Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00006348 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00006349 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006350 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006351
6352 case TemplateName::SubstTemplateTemplateParm: {
6353 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00006354 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00006355 if (!param) return TemplateName();
6356 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006357 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCalld9dfe3a2011-06-30 08:33:18 +00006358 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006359
6360 case TemplateName::SubstTemplateTemplateParmPack: {
6361 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00006362 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00006363 if (!Param)
6364 return TemplateName();
6365
6366 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6367 if (ArgPack.getKind() != TemplateArgument::Pack)
6368 return TemplateName();
6369
Douglas Gregor4163aca2011-09-09 21:34:22 +00006370 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00006371 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006372 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006373
David Blaikie83d382b2011-09-23 05:06:16 +00006374 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006375}
6376
6377TemplateArgument
Douglas Gregorde3ef502011-11-30 23:21:26 +00006378ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00006379 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006380 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6381 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006382 case TemplateArgument::Null:
6383 return TemplateArgument();
6384 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00006385 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmanb826a002012-09-26 02:36:12 +00006386 case TemplateArgument::Declaration: {
6387 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6388 bool ForReferenceParam = Record[Idx++];
6389 return TemplateArgument(D, ForReferenceParam);
6390 }
6391 case TemplateArgument::NullPtr:
6392 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00006393 case TemplateArgument::Integral: {
6394 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00006395 QualType T = readType(F, Record, Idx);
Benjamin Kramer6003ad52012-06-07 15:09:51 +00006396 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00006397 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006398 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00006399 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006400 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00006401 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00006402 llvm::Optional<unsigned> NumTemplateExpansions;
6403 if (unsigned NumExpansions = Record[Idx++])
6404 NumTemplateExpansions = NumExpansions - 1;
6405 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00006406 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006407 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00006408 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006409 case TemplateArgument::Pack: {
6410 unsigned NumArgs = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00006411 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00006412 for (unsigned I = 0; I != NumArgs; ++I)
6413 Args[I] = ReadTemplateArgument(F, Record, Idx);
6414 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006415 }
6416 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006417
David Blaikie83d382b2011-09-23 05:06:16 +00006418 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006419}
6420
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006421TemplateParameterList *
Douglas Gregorde3ef502011-11-30 23:21:26 +00006422ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006423 const RecordData &Record, unsigned &Idx) {
6424 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6425 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6426 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006427
6428 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006429 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006430 Params.reserve(NumParams);
6431 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006432 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006433
6434 TemplateParameterList* TemplateParams =
Douglas Gregor4163aca2011-09-09 21:34:22 +00006435 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006436 Params.data(), Params.size(), RAngleLoc);
6437 return TemplateParams;
6438}
6439
6440void
Sebastian Redl2c499f62010-08-18 23:56:43 +00006441ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006442ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregorde3ef502011-11-30 23:21:26 +00006443 ModuleFile &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006444 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006445 unsigned NumTemplateArgs = Record[Idx++];
6446 TemplArgs.reserve(NumTemplateArgs);
6447 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00006448 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006449}
6450
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006451/// \brief Read a UnresolvedSet structure.
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006452void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006453 const RecordData &Record, unsigned &Idx) {
6454 unsigned NumDecls = Record[Idx++];
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006455 Set.reserve(Context, NumDecls);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006456 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006457 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006458 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006459 Set.addDecl(Context, D, AS);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006460 }
6461}
6462
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006463CXXBaseSpecifier
Douglas Gregorde3ef502011-11-30 23:21:26 +00006464ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00006465 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006466 bool isVirtual = static_cast<bool>(Record[Idx++]);
6467 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6468 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00006469 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006470 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6471 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00006472 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00006473 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00006474 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00006475 Result.setInheritConstructors(inheritConstructors);
6476 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006477}
6478
Alexis Hunt1d792652011-01-08 20:30:50 +00006479std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregorde3ef502011-11-30 23:21:26 +00006480ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00006481 unsigned &Idx) {
6482 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006483 unsigned NumInitializers = Record[Idx++];
6484 if (NumInitializers) {
Alexis Hunt1d792652011-01-08 20:30:50 +00006485 CtorInitializers
Douglas Gregor4163aca2011-09-09 21:34:22 +00006486 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006487 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006488 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006489 bool IsBaseVirtual = false;
6490 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00006491 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006492
Alexis Hunt37a477f2011-05-04 01:19:08 +00006493 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6494 switch (Type) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006495 case CTOR_INITIALIZER_BASE:
6496 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006497 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00006498 break;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006499
6500 case CTOR_INITIALIZER_DELEGATING:
6501 TInfo = GetTypeSourceInfo(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006502 break;
6503
6504 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006505 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006506 break;
6507
6508 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006509 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006510 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006511 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00006512
Douglas Gregor44e7df62011-01-04 00:32:56 +00006513 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006514 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006515 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6516 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006517 bool IsWritten = Record[Idx++];
6518 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006519 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006520 if (IsWritten) {
6521 SourceOrderOrNumArrayIndices = Record[Idx++];
6522 } else {
6523 SourceOrderOrNumArrayIndices = Record[Idx++];
6524 Indices.reserve(SourceOrderOrNumArrayIndices);
6525 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006526 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006527 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006528
Alexis Hunt1d792652011-01-08 20:30:50 +00006529 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00006530 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006531 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Alexis Hunt1d792652011-01-08 20:30:50 +00006532 LParenLoc, Init, RParenLoc,
6533 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006534 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006535 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6536 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006537 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00006538 if (Member)
Douglas Gregor4163aca2011-09-09 21:34:22 +00006539 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00006540 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00006541 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00006542 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Alexis Hunt1d792652011-01-08 20:30:50 +00006543 MemberOrEllipsisLoc, LParenLoc,
6544 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006545 } else {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006546 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00006547 LParenLoc, Init, RParenLoc,
6548 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006549 }
6550
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00006551 if (IsWritten)
6552 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00006553 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006554 }
6555 }
6556
Alexis Hunt1d792652011-01-08 20:30:50 +00006557 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006558}
6559
Chris Lattnerca025db2010-05-07 21:43:38 +00006560NestedNameSpecifier *
Douglas Gregorde3ef502011-11-30 23:21:26 +00006561ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00006562 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00006563 unsigned N = Record[Idx++];
6564 NestedNameSpecifier *NNS = 0, *Prev = 0;
6565 for (unsigned I = 0; I != N; ++I) {
6566 NestedNameSpecifier::SpecifierKind Kind
6567 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6568 switch (Kind) {
6569 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00006570 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006571 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattnerca025db2010-05-07 21:43:38 +00006572 break;
6573 }
6574
6575 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006576 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006577 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattnerca025db2010-05-07 21:43:38 +00006578 break;
6579 }
6580
Douglas Gregor7b26ff92011-02-24 02:36:08 +00006581 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006582 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006583 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00006584 break;
6585 }
6586
Chris Lattnerca025db2010-05-07 21:43:38 +00006587 case NestedNameSpecifier::TypeSpec:
6588 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00006589 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00006590 if (!T)
6591 return 0;
6592
Chris Lattnerca025db2010-05-07 21:43:38 +00006593 bool Template = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00006594 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattnerca025db2010-05-07 21:43:38 +00006595 break;
6596 }
6597
6598 case NestedNameSpecifier::Global: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006599 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattnerca025db2010-05-07 21:43:38 +00006600 // No associated value, and there can't be a prefix.
6601 break;
6602 }
Chris Lattnerca025db2010-05-07 21:43:38 +00006603 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00006604 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00006605 }
6606 return NNS;
6607}
6608
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006609NestedNameSpecifierLoc
Douglas Gregorde3ef502011-11-30 23:21:26 +00006610ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006611 unsigned &Idx) {
6612 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00006613 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006614 for (unsigned I = 0; I != N; ++I) {
6615 NestedNameSpecifier::SpecifierKind Kind
6616 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6617 switch (Kind) {
6618 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00006619 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006620 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006621 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006622 break;
6623 }
6624
6625 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006626 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006627 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006628 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006629 break;
6630 }
6631
6632 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006633 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006634 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006635 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006636 break;
6637 }
6638
6639 case NestedNameSpecifier::TypeSpec:
6640 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006641 bool Template = Record[Idx++];
6642 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6643 if (!T)
6644 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006645 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00006646
6647 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor4163aca2011-09-09 21:34:22 +00006648 Builder.Extend(Context,
Douglas Gregor9b272512011-02-28 23:58:31 +00006649 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6650 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006651 break;
6652 }
6653
6654 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006655 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006656 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006657 break;
6658 }
6659 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006660 }
6661
Douglas Gregor4163aca2011-09-09 21:34:22 +00006662 return Builder.getWithLocInContext(Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006663}
6664
Chris Lattnerca025db2010-05-07 21:43:38 +00006665SourceRange
Douglas Gregorde3ef502011-11-30 23:21:26 +00006666ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006667 unsigned &Idx) {
6668 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6669 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00006670 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00006671}
6672
Douglas Gregor1daeb692009-04-13 18:14:40 +00006673/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006674llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00006675 unsigned BitWidth = Record[Idx++];
6676 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6677 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6678 Idx += NumWords;
6679 return Result;
6680}
6681
6682/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006683llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00006684 bool isUnsigned = Record[Idx++];
6685 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6686}
6687
Douglas Gregore0a3a512009-04-14 21:55:33 +00006688/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006689llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00006690 return llvm::APFloat(ReadAPInt(Record, Idx));
6691}
6692
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006693// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00006694std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006695 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00006696 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006697 Idx += Len;
6698 return Result;
6699}
6700
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006701VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6702 unsigned &Idx) {
6703 unsigned Major = Record[Idx++];
6704 unsigned Minor = Record[Idx++];
6705 unsigned Subminor = Record[Idx++];
6706 if (Minor == 0)
6707 return VersionTuple(Major);
6708 if (Subminor == 0)
6709 return VersionTuple(Major, Minor - 1);
6710 return VersionTuple(Major, Minor - 1, Subminor - 1);
6711}
6712
Douglas Gregorde3ef502011-11-30 23:21:26 +00006713CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00006714 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00006715 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006716 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006717 return CXXTemporary::Create(Context, Decl);
Chris Lattnercba86142010-05-10 00:25:06 +00006718}
6719
Sebastian Redl2c499f62010-08-18 23:56:43 +00006720DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00006721 return Diag(SourceLocation(), DiagID);
6722}
6723
Sebastian Redl2c499f62010-08-18 23:56:43 +00006724DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006725 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00006726}
Douglas Gregora9af1d12009-04-17 00:04:06 +00006727
Douglas Gregora868bbd2009-04-21 22:25:48 +00006728/// \brief Retrieve the identifier table associated with the
6729/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006730IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor51825b42011-09-09 22:02:16 +00006731 return PP.getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00006732}
6733
Douglas Gregora9af1d12009-04-17 00:04:06 +00006734/// \brief Record that the given ID maps to the given switch-case
6735/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006736void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006737 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6738 "Already have a SwitchCase with this ID");
6739 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregora9af1d12009-04-17 00:04:06 +00006740}
6741
6742/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006743SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006744 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6745 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregora9af1d12009-04-17 00:04:06 +00006746}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00006747
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00006748void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006749 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00006750}
6751
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006752void ASTReader::ReadComments() {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00006753 std::vector<RawComment *> Comments;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006754 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6755 serialization::ModuleFile *> >::iterator
6756 I = CommentsCursors.begin(),
6757 E = CommentsCursors.end();
6758 I != E; ++I) {
6759 llvm::BitstreamCursor &Cursor = I->first;
6760 serialization::ModuleFile &F = *I->second;
6761 SavedStreamPosition SavedPosition(Cursor);
6762
6763 RecordData Record;
6764 while (true) {
6765 unsigned Code = Cursor.ReadCode();
6766 if (Code == llvm::bitc::END_BLOCK)
6767 break;
6768
6769 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6770 // No known subblocks, always skip them.
6771 Cursor.ReadSubBlockID();
6772 if (Cursor.SkipBlock()) {
6773 Error("malformed block record in AST file");
6774 return;
6775 }
6776 continue;
6777 }
6778
6779 if (Code == llvm::bitc::DEFINE_ABBREV) {
6780 Cursor.ReadAbbrevRecord();
6781 continue;
6782 }
6783
6784 // Read a record.
6785 Record.clear();
6786 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth029ea4a2012-06-20 06:47:54 +00006787 case COMMENTS_RAW_COMMENT: {
6788 unsigned Idx = 0;
6789 SourceRange SR = ReadSourceRange(F, Record, Idx);
6790 RawComment::CommentKind Kind =
6791 (RawComment::CommentKind) Record[Idx++];
6792 bool IsTrailingComment = Record[Idx++];
6793 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00006794 Comments.push_back(new (Context) RawComment(SR, Kind,
6795 IsTrailingComment,
6796 IsAlmostTrailingComment));
Chandler Carruth029ea4a2012-06-20 06:47:54 +00006797 break;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006798 }
6799 }
6800 }
6801 }
6802 Context.Comments.addCommentsToFront(Comments);
6803}
6804
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006805void ASTReader::finishPendingActions() {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00006806 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6807 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006808 // If any identifiers with corresponding top-level declarations have
6809 // been loaded, load those declarations now.
6810 while (!PendingIdentifierInfos.empty()) {
6811 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6812 PendingIdentifierInfos.front().DeclIDs, true);
6813 PendingIdentifierInfos.pop_front();
6814 }
6815
Douglas Gregor05f10352011-12-17 23:38:30 +00006816 // Load pending declaration chains.
6817 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6818 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006819 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregor05f10352011-12-17 23:38:30 +00006820 }
6821 PendingDeclChains.clear();
Douglas Gregor5a4649b2012-10-11 00:46:49 +00006822
6823 // Load any pending macro definitions.
Douglas Gregord2acff92012-10-11 17:31:34 +00006824 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6825 // FIXME: std::move here
6826 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregore7400892012-10-11 17:41:54 +00006827 MacroInfo *Hint = 0;
Douglas Gregord2acff92012-10-11 17:31:34 +00006828 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6829 ++IDIdx) {
Douglas Gregore7400892012-10-11 17:41:54 +00006830 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregord2acff92012-10-11 17:31:34 +00006831 }
6832 }
6833 PendingMacroIDs.clear();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006834 }
Douglas Gregore80b31f2011-12-19 19:00:47 +00006835
Douglas Gregor68444de2012-01-14 15:13:49 +00006836 // If we deserialized any C++ or Objective-C class definitions, any
6837 // Objective-C protocol definitions, or any redeclarable templates, make sure
6838 // that all redeclarations point to the definitions. Note that this can only
6839 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregore80b31f2011-12-19 19:00:47 +00006840 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6841 DEnd = PendingDefinitions.end();
6842 D != DEnd; ++D) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006843 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6844 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6845 // Make sure that the TagType points at the definition.
6846 const_cast<TagType*>(TagT)->decl = TD;
6847 }
Douglas Gregore80b31f2011-12-19 19:00:47 +00006848
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006849 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6850 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6851 REnd = RD->redecls_end();
6852 R != REnd; ++R)
6853 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6854
6855 }
6856
Douglas Gregore80b31f2011-12-19 19:00:47 +00006857 continue;
6858 }
6859
Douglas Gregora715bff2012-01-01 19:51:50 +00006860 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006861 // Make sure that the ObjCInterfaceType points at the definition.
6862 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6863 ->Decl = ID;
6864
Douglas Gregora715bff2012-01-01 19:51:50 +00006865 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6866 REnd = ID->redecls_end();
6867 R != REnd; ++R)
6868 R->Data = ID->Data;
6869
6870 continue;
6871 }
6872
Douglas Gregor68444de2012-01-14 15:13:49 +00006873 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6874 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6875 REnd = PD->redecls_end();
6876 R != REnd; ++R)
6877 R->Data = PD->Data;
6878
6879 continue;
6880 }
6881
6882 RedeclarableTemplateDecl *RTD
6883 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6884 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6885 REnd = RTD->redecls_end();
Douglas Gregore80b31f2011-12-19 19:00:47 +00006886 R != REnd; ++R)
Douglas Gregora6017bb2012-10-09 17:21:28 +00006887 R->Common = RTD->Common;
Douglas Gregore80b31f2011-12-19 19:00:47 +00006888 }
6889 PendingDefinitions.clear();
Douglas Gregora6017bb2012-10-09 17:21:28 +00006890
6891 // Load the bodies of any functions or methods we've encountered. We do
6892 // this now (delayed) so that we can be sure that the declaration chains
6893 // have been fully wired up.
Douglas Gregor7c0990b2012-10-09 17:50:23 +00006894 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6895 PBEnd = PendingBodies.end();
Douglas Gregora6017bb2012-10-09 17:21:28 +00006896 PB != PBEnd; ++PB) {
6897 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6898 // FIXME: Check for =delete/=default?
6899 // FIXME: Complain about ODR violations here?
6900 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6901 FD->setLazyBody(PB->second);
6902 continue;
6903 }
6904
6905 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6906 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6907 MD->setLazyBody(PB->second);
6908 }
6909 PendingBodies.clear();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006910}
6911
Sebastian Redl2c499f62010-08-18 23:56:43 +00006912void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00006913 assert(NumCurrentElementsDeserializing &&
6914 "FinishedDeserializing not paired with StartedDeserializing");
6915 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006916 // We decrease NumCurrentElementsDeserializing only after pending actions
6917 // are finished, to avoid recursively re-calling finishPendingActions().
6918 finishPendingActions();
6919 }
6920 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis97ea7d62011-12-17 04:13:28 +00006921
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006922 if (NumCurrentElementsDeserializing == 0 &&
6923 Consumer && !PassingDeclsToConsumer) {
6924 // Guard variable to avoid recursively redoing the process of passing
6925 // decls to consumer.
6926 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6927 true);
Argyrios Kyrtzidis97ea7d62011-12-17 04:13:28 +00006928
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006929 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00006930 // We are not in recursive loading, so it's safe to pass the "interesting"
6931 // decls to the consumer.
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006932 Decl *D = InterestingDecls.front();
6933 InterestingDecls.pop_front();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006934 PassInterestingDeclToConsumer(D);
6935 }
Douglas Gregor1342e842009-07-06 18:54:52 +00006936 }
Douglas Gregor1342e842009-07-06 18:54:52 +00006937}
Douglas Gregorb473b072010-08-19 00:28:17 +00006938
Douglas Gregor8835e032011-09-02 00:26:20 +00006939ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00006940 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00006941 bool AllowASTWithCompilerErrors)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006942 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6943 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor51825b42011-09-09 22:02:16 +00006944 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +00006945 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00006946 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00006947 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006948 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
Douglas Gregor925296b2011-07-19 16:10:42 +00006949 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00006950 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6951 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6952 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6953 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00006954 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6955 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006956 PassingDeclsToConsumer(false),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00006957 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00006958{
Douglas Gregor925296b2011-07-19 16:10:42 +00006959 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006960}
6961
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006962ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006963 for (DeclContextVisibleUpdatesPending::iterator
6964 I = PendingVisibleUpdates.begin(),
6965 E = PendingVisibleUpdates.end();
6966 I != E; ++I) {
6967 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6968 F = I->second.end();
6969 J != F; ++J)
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00006970 delete J->first;
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006971 }
6972}