blob: bc08f3f5f67a0462cc31d06a8e15521e2520339a [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 Gregor22103e32012-11-30 21:58:49 +00001003std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1004 if (ID == 0)
1005 return std::make_pair(SourceLocation(), "");
1006
1007 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1008 Error("source location entry ID out-of-range for AST file");
1009 return std::make_pair(SourceLocation(), "");
1010 }
1011
1012 // Find which module file this entry lands in.
1013 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1014 if (M->Kind != MK_Module)
1015 return std::make_pair(SourceLocation(), "");
1016
1017 // FIXME: Can we map this down to a particular submodule? That would be
1018 // ideal.
1019 return std::make_pair(M->ImportLoc, llvm::sys::path::stem(M->FileName));
1020}
1021
Douglas Gregor925296b2011-07-19 16:10:42 +00001022/// \brief Find the location where the module F is imported.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001023SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001024 if (F->ImportLoc.isValid())
1025 return F->ImportLoc;
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001026
Douglas Gregor925296b2011-07-19 16:10:42 +00001027 // Otherwise we have a PCH. It's considered to be "imported" at the first
1028 // location of its includer.
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001029 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregor925296b2011-07-19 16:10:42 +00001030 // Main file is the importer. We assume that it is the first entry in the
1031 // entry table. We can't ask the manager, because at the time of PCH loading
1032 // the main file entry doesn't exist yet.
1033 // The very first entry is the invalid instantiation loc, which takes up
1034 // offsets 0 and 1.
1035 return SourceLocation::getFromRawEncoding(2U);
1036 }
Jonathan D. Turner10d52012011-07-29 18:09:09 +00001037 //return F->Loaders[0]->FirstLoc;
1038 return F->ImportedBy[0]->FirstLoc;
Douglas Gregor925296b2011-07-19 16:10:42 +00001039}
1040
Chris Lattnere78a6be2009-04-27 01:05:14 +00001041/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1042/// specified cursor. Read the abbreviations that are at the top of the block
1043/// and then leave the cursor pointing into the block.
Sebastian Redl2c499f62010-08-18 23:56:43 +00001044bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattnere78a6be2009-04-27 01:05:14 +00001045 unsigned BlockID) {
1046 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001047 Error("malformed block record in AST file");
Chris Lattnere78a6be2009-04-27 01:05:14 +00001048 return Failure;
1049 }
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattnere78a6be2009-04-27 01:05:14 +00001051 while (true) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001052 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattnere78a6be2009-04-27 01:05:14 +00001053 unsigned Code = Cursor.ReadCode();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001054
Chris Lattnere78a6be2009-04-27 01:05:14 +00001055 // We expect all abbrevs to be at the start of the block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001056 if (Code != llvm::bitc::DEFINE_ABBREV) {
1057 Cursor.JumpToBit(Offset);
Chris Lattnere78a6be2009-04-27 01:05:14 +00001058 return false;
Douglas Gregor796d76a2010-10-20 22:00:55 +00001059 }
Chris Lattnere78a6be2009-04-27 01:05:14 +00001060 Cursor.ReadAbbrevRecord();
1061 }
1062}
1063
Douglas Gregore7400892012-10-11 17:41:54 +00001064void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1065 MacroInfo *Hint) {
Douglas Gregor796d76a2010-10-20 22:00:55 +00001066 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump11289f42009-09-09 15:08:12 +00001067
Douglas Gregorc3366a52009-04-21 23:56:24 +00001068 // Keep track of where we are in the stream, then jump back there
1069 // after reading this macro.
1070 SavedStreamPosition SavedPosition(Stream);
1071
1072 Stream.JumpToBit(Offset);
1073 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001074 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001075 MacroInfo *Macro = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001076
Douglas Gregor5968b1b2012-10-11 21:07:39 +00001077 // RAII object to add the loaded macro information once we're done
1078 // adding tokens.
1079 struct AddLoadedMacroInfoRAII {
1080 Preprocessor &PP;
1081 MacroInfo *Hint;
1082 MacroInfo *MI;
1083 IdentifierInfo *II;
1084
1085 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1086 : PP(PP), Hint(Hint), MI(), II() { }
1087 ~AddLoadedMacroInfoRAII( ) {
1088 if (MI) {
1089 // Finally, install the macro.
1090 PP.addLoadedMacroInfo(II, MI, Hint);
1091 }
1092 }
1093 } AddLoadedMacroInfo(PP, Hint);
1094
Douglas Gregorc3366a52009-04-21 23:56:24 +00001095 while (true) {
1096 unsigned Code = Stream.ReadCode();
1097 switch (Code) {
1098 case llvm::bitc::END_BLOCK:
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001099 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001100
1101 case llvm::bitc::ENTER_SUBBLOCK:
1102 // No known subblocks, always skip them.
1103 Stream.ReadSubBlockID();
1104 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001105 Error("malformed block record in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001106 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001107 }
1108 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001109
Douglas Gregorc3366a52009-04-21 23:56:24 +00001110 case llvm::bitc::DEFINE_ABBREV:
1111 Stream.ReadAbbrevRecord();
1112 continue;
1113 default: break;
1114 }
1115
1116 // Read a record.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001117 const char *BlobStart = 0;
1118 unsigned BlobLen = 0;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001119 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001120 PreprocessorRecordTypes RecType =
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001121 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregor796d76a2010-10-20 22:00:55 +00001122 BlobLen);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001123 switch (RecType) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001124 case PP_MACRO_OBJECT_LIKE:
1125 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001126 // If we already have a macro, that means that we've hit the end
1127 // of the definition of the macro we were looking for. We're
1128 // done.
1129 if (Macro)
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001130 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001131
Douglas Gregora3e41532011-07-28 20:55:49 +00001132 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001133 if (II == 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001134 Error("macro must have a name in AST file");
Douglas Gregor7cb0d012011-08-04 18:09:14 +00001135 return;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001138 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1139
1140 // If this macro has already been loaded, don't do so again.
1141 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1142 return;
1143
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001144 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1145 unsigned NextIndex = 3;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001146 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor51825b42011-09-09 22:02:16 +00001147 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001148
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001149 // Record this macro.
1150 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1151
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001152 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1153 if (UndefLoc.isValid())
1154 MI->setUndefLoc(UndefLoc);
1155
1156 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001157 MI->setIsFromAST();
Mike Stump11289f42009-09-09 15:08:12 +00001158
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001159 bool IsPublic = Record[NextIndex++];
Douglas Gregorebf00492011-10-17 15:32:29 +00001160 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001161
Sebastian Redl539c5062010-08-18 23:57:32 +00001162 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001163 // Decode function-like macro info.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001164 bool isC99VarArgs = Record[NextIndex++];
1165 bool isGNUVarArgs = Record[NextIndex++];
Eli Friedman14d3c792012-11-14 02:18:46 +00001166 bool hasCommaPasting = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001167 MacroArgs.clear();
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001168 unsigned NumArgs = Record[NextIndex++];
Douglas Gregorc3366a52009-04-21 23:56:24 +00001169 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001170 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001171
1172 // Install function-like macro info.
1173 MI->setIsFunctionLike();
1174 if (isC99VarArgs) MI->setIsC99Varargs();
1175 if (isGNUVarArgs) MI->setIsGNUVarargs();
Eli Friedman14d3c792012-11-14 02:18:46 +00001176 if (hasCommaPasting) MI->setHasCommaPasting();
Douglas Gregor038c3382009-05-22 22:45:36 +00001177 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor51825b42011-09-09 22:02:16 +00001178 PP.getPreprocessorAllocator());
Douglas Gregorc3366a52009-04-21 23:56:24 +00001179 }
1180
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001181 if (DeserializationListener)
1182 DeserializationListener->MacroRead(GlobalID, MI);
1183
1184 // If an update record marked this as undefined, do so now.
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001185 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001186 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1187 if (Update != MacroUpdates.end()) {
1188 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregorcfa46a82012-10-12 00:16:50 +00001189 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1190 bool Hidden = false;
1191 if (unsigned SubmoduleID = Update->second[I].first) {
1192 if (Module *Owner = getSubmodule(SubmoduleID)) {
1193 if (Owner->NameVisibility == Module::Hidden) {
1194 // Note that this #undef is hidden.
1195 Hidden = true;
1196
1197 // Record this hiding for later.
1198 HiddenNamesMap[Owner].push_back(
1199 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1200 }
1201 }
1202 }
1203
1204 if (!Hidden) {
1205 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1206 if (PPMutationListener *Listener = PP.getPPMutationListener())
1207 Listener->UndefinedMacro(MI);
1208 break;
1209 }
1210 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001211 }
1212 MacroUpdates.erase(Update);
1213 }
1214
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001215 // Determine whether this macro definition is visible.
1216 bool Hidden = !MI->isPublic();
1217 if (!Hidden && GlobalSubmoduleID) {
1218 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1219 if (Owner->NameVisibility == Module::Hidden) {
1220 // The owning module is not visible, and this macro definition
1221 // should not be, either.
1222 Hidden = true;
1223
1224 // Note that this macro definition was hidden because its owning
1225 // module is not yet visible.
1226 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1227 }
1228 }
1229 }
1230 MI->setHidden(Hidden);
1231
Douglas Gregor5968b1b2012-10-11 21:07:39 +00001232 // Make sure we install the macro once we're done.
1233 AddLoadedMacroInfo.MI = MI;
1234 AddLoadedMacroInfo.II = II;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001235
1236 // Remember that we saw this macro last so that we add the tokens that
1237 // form its body to it.
1238 Macro = MI;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001239
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001240 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1241 Record[NextIndex]) {
1242 // We have a macro definition. Register the association
1243 PreprocessedEntityID
1244 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1245 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1246 PPRec.RegisterMacroDefinition(Macro,
1247 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregoraae92242010-03-19 21:51:54 +00001248 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001249
Douglas Gregorc3366a52009-04-21 23:56:24 +00001250 ++NumMacrosRead;
1251 break;
1252 }
Mike Stump11289f42009-09-09 15:08:12 +00001253
Sebastian Redl539c5062010-08-18 23:57:32 +00001254 case PP_TOKEN: {
Douglas Gregorc3366a52009-04-21 23:56:24 +00001255 // If we see a TOKEN before a PP_MACRO_*, then the file is
1256 // erroneous, just pretend we didn't see this.
1257 if (Macro == 0) break;
Mike Stump11289f42009-09-09 15:08:12 +00001258
Douglas Gregorc3366a52009-04-21 23:56:24 +00001259 Token Tok;
1260 Tok.startToken();
Sebastian Redl2c373b92010-10-05 15:59:54 +00001261 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001262 Tok.setLength(Record[1]);
Douglas Gregora3e41532011-07-28 20:55:49 +00001263 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregorc3366a52009-04-21 23:56:24 +00001264 Tok.setIdentifierInfo(II);
1265 Tok.setKind((tok::TokenKind)Record[3]);
1266 Tok.setFlag((Token::TokenFlags)Record[4]);
1267 Macro->AddTokenToBody(Tok);
1268 break;
1269 }
David Blaikie8a40f702012-01-17 06:56:22 +00001270 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001271 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001272}
1273
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001274PreprocessedEntityID
Douglas Gregorde3ef502011-11-30 23:21:26 +00001275ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidisd67164e2011-09-19 20:40:02 +00001276 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001277 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1278 assert(I != M.PreprocessedEntityRemap.end()
1279 && "Invalid index into preprocessed entity index remap");
1280
1281 return LocalID + I->second;
Douglas Gregorcaed7c62011-07-28 22:39:26 +00001282}
1283
Douglas Gregord44252e2011-08-25 20:47:51 +00001284unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1285 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregor09b69892011-02-10 17:09:37 +00001286}
Douglas Gregord44252e2011-08-25 20:47:51 +00001287
1288HeaderFileInfoTrait::internal_key_type
1289HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1290
1291bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1292 if (strcmp(a, b) == 0)
1293 return true;
1294
1295 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1296 return false;
Douglas Gregore32e0542011-12-09 16:22:07 +00001297
1298 // Determine whether the actual files are equivalent.
1299 bool Result = false;
1300 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregord44252e2011-08-25 20:47:51 +00001301 return false;
1302
Douglas Gregore32e0542011-12-09 16:22:07 +00001303 return Result;
Douglas Gregord44252e2011-08-25 20:47:51 +00001304}
1305
1306std::pair<unsigned, unsigned>
1307HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1308 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1309 unsigned DataLen = (unsigned) *d++;
1310 return std::make_pair(KeyLen + 1, DataLen);
1311}
1312
1313HeaderFileInfoTrait::data_type
1314HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1315 unsigned DataLen) {
1316 const unsigned char *End = d + DataLen;
1317 using namespace clang::io;
1318 HeaderFileInfo HFI;
1319 unsigned Flags = *d++;
1320 HFI.isImport = (Flags >> 5) & 0x01;
1321 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1322 HFI.DirInfo = (Flags >> 2) & 0x03;
1323 HFI.Resolved = (Flags >> 1) & 0x01;
1324 HFI.IndexHeaderMapHeader = Flags & 0x01;
1325 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor7d75bf62011-10-17 18:53:12 +00001326 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1327 ReadUnalignedLE32(d));
Douglas Gregord44252e2011-08-25 20:47:51 +00001328 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1329 // The framework offset is 1 greater than the actual offset,
1330 // since 0 is used as an indicator for "no framework name".
1331 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1332 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1333 }
1334
1335 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1336 (void)End;
1337
1338 // This HeaderFileInfo was externally loaded.
1339 HFI.External = true;
1340 return HFI;
1341}
Douglas Gregor09b69892011-02-10 17:09:37 +00001342
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001343void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1344 II->setHadMacroDefinition(true);
1345 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1346 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001347}
1348
Sebastian Redl2c499f62010-08-18 23:56:43 +00001349void ASTReader::ReadDefinedMacros() {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001350 // Note that we are loading defined macros.
1351 Deserializing Macros(this);
1352
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001353 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1354 E = ModuleMgr.rend(); I != E; ++I) {
1355 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00001356
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001357 // If there was no preprocessor block, skip this file.
1358 if (!MacroCursor.getBitStreamReader())
1359 continue;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001360
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001361 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00001362 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001363
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001364 RecordData Record;
1365 while (true) {
1366 unsigned Code = Cursor.ReadCode();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001367 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001368 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001369
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001370 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1371 // No known subblocks, always skip them.
1372 Cursor.ReadSubBlockID();
1373 if (Cursor.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001374 Error("malformed block record in AST file");
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001375 return;
1376 }
1377 continue;
1378 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001379
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001380 if (Code == llvm::bitc::DEFINE_ABBREV) {
1381 Cursor.ReadAbbrevRecord();
1382 continue;
1383 }
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00001384
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001385 // Read a record.
1386 const char *BlobStart;
1387 unsigned BlobLen;
1388 Record.clear();
1389 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1390 default: // Default behavior: ignore.
1391 break;
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001392
Sebastian Redl539c5062010-08-18 23:57:32 +00001393 case PP_MACRO_OBJECT_LIKE:
1394 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregora3e41532011-07-28 20:55:49 +00001395 getLocalIdentifier(**I, Record[0]);
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001396 break;
1397
Sebastian Redl539c5062010-08-18 23:57:32 +00001398 case PP_TOKEN:
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001399 // Ignore tokens.
1400 break;
Sebastian Redl4e6c5672010-07-21 22:31:37 +00001401 }
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001402 }
1403 }
Douglas Gregor5ef9e332010-10-30 00:23:06 +00001404}
1405
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001406namespace {
1407 /// \brief Visitor class used to look up identifirs in an AST file.
1408 class IdentifierLookupVisitor {
1409 StringRef Name;
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001410 unsigned PriorGeneration;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001411 IdentifierInfo *Found;
1412 public:
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001413 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1414 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001415
Douglas Gregorde3ef502011-11-30 23:21:26 +00001416 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001417 IdentifierLookupVisitor *This
1418 = static_cast<IdentifierLookupVisitor *>(UserData);
1419
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001420 // If we've already searched this module file, skip it now.
1421 if (M.Generation <= This->PriorGeneration)
1422 return true;
1423
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001424 ASTIdentifierLookupTable *IdTable
1425 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1426 if (!IdTable)
1427 return false;
1428
Douglas Gregor247afcc2012-01-24 15:24:38 +00001429 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1430 M, This->Found);
1431
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001432 std::pair<const char*, unsigned> Key(This->Name.begin(),
1433 This->Name.size());
Douglas Gregor247afcc2012-01-24 15:24:38 +00001434 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001435 if (Pos == IdTable->end())
1436 return false;
1437
1438 // Dereferencing the iterator has the effect of building the
1439 // IdentifierInfo node and populating it with the various
1440 // declarations it needs.
1441 This->Found = *Pos;
1442 return true;
1443 }
1444
1445 // \brief Retrieve the identifier info found within the module
1446 // files.
1447 IdentifierInfo *getIdentifierInfo() const { return Found; }
1448 };
1449}
1450
1451void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001452 // Note that we are loading an identifier.
1453 Deserializing AnIdentifier(this);
1454
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001455 unsigned PriorGeneration = 0;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001456 if (getContext().getLangOpts().Modules)
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001457 PriorGeneration = IdentifierGeneration[&II];
1458
1459 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1460 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1461 markIdentifierUpToDate(&II);
1462}
1463
1464void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1465 if (!II)
1466 return;
1467
1468 II->setOutOfDate(false);
1469
1470 // Update the generation for this identifier.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001471 if (getContext().getLangOpts().Modules)
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00001472 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001473}
1474
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001475llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor4b29c162012-10-22 23:51:00 +00001476ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001477 // If this ID is bogus, just return an empty input file.
1478 if (ID == 0 || ID > F.InputFilesLoaded.size())
1479 return InputFile();
1480
1481 // If we've already loaded this input file, return it.
1482 if (F.InputFilesLoaded[ID-1].getPointer())
1483 return F.InputFilesLoaded[ID-1];
1484
1485 // Go find this input file.
1486 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1487 SavedStreamPosition SavedPosition(Cursor);
1488 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1489
1490 unsigned Code = Cursor.ReadCode();
1491 RecordData Record;
1492 const char *BlobStart = 0;
1493 unsigned BlobLen = 0;
1494 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1495 &BlobStart, &BlobLen)) {
1496 case INPUT_FILE: {
1497 unsigned StoredID = Record[0];
1498 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi395a5742012-10-22 21:50:39 +00001499 (void)StoredID;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001500 off_t StoredSize = (off_t)Record[1];
1501 time_t StoredTime = (time_t)Record[2];
1502 bool Overridden = (bool)Record[3];
1503
1504 // Get the file entry for this input file.
1505 StringRef OrigFilename(BlobStart, BlobLen);
1506 std::string Filename = OrigFilename;
1507 MaybeAddSystemRootToFilename(F, Filename);
1508 const FileEntry *File
1509 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1510 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1511
1512 // If we didn't find the file, resolve it relative to the
1513 // original directory from which this AST file was created.
1514 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1515 F.OriginalDir != CurrentDir) {
1516 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1517 F.OriginalDir,
1518 CurrentDir);
1519 if (!Resolved.empty())
1520 File = FileMgr.getFile(Resolved);
1521 }
1522
1523 // For an overridden file, create a virtual file with the stored
1524 // size/timestamp.
1525 if (Overridden && File == 0) {
1526 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1527 }
1528
1529 if (File == 0) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001530 if (Complain) {
1531 std::string ErrorStr = "could not find file '";
1532 ErrorStr += Filename;
1533 ErrorStr += "' referenced by AST file";
1534 Error(ErrorStr.c_str());
1535 }
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001536 return InputFile();
1537 }
1538
1539 // Note that we've loaded this input file.
1540 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1541
1542 // Check if there was a request to override the contents of the file
1543 // that was part of the precompiled header. Overridding such a file
1544 // can lead to problems when lexing using the source locations from the
1545 // PCH.
1546 SourceManager &SM = getSourceManager();
1547 if (!Overridden && SM.isFileOverridden(File)) {
1548 Error(diag::err_fe_pch_file_overridden, Filename);
1549 // After emitting the diagnostic, recover by disabling the override so
1550 // that the original file will be used.
1551 SM.disableFileContentsOverride(File);
1552 // The FileEntry is a virtual file entry with the size of the contents
1553 // that would override the original contents. Set it to the original's
1554 // size/time.
1555 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1556 StoredSize, StoredTime);
1557 }
1558
1559 // For an overridden file, there is nothing to validate.
1560 if (Overridden)
1561 return InputFile(File, Overridden);
1562
1563 // The stat info from the FileEntry came from the cached stat
1564 // info of the PCH, so we cannot trust it.
1565 struct stat StatBuf;
1566 if (::stat(File->getName(), &StatBuf) != 0) {
1567 StatBuf.st_size = File->getSize();
1568 StatBuf.st_mtime = File->getModificationTime();
1569 }
1570
1571 if ((StoredSize != StatBuf.st_size
1572#if !defined(LLVM_ON_WIN32)
1573 // In our regression testing, the Windows file system seems to
1574 // have inconsistent modification times that sometimes
1575 // erroneously trigger this error-handling path.
1576 || StoredTime != StatBuf.st_mtime
1577#endif
1578 )) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001579 if (Complain)
1580 Error(diag::err_fe_pch_file_modified, Filename);
1581
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001582 return InputFile();
1583 }
1584
1585 return InputFile(File, Overridden);
1586 }
1587 }
1588
1589 return InputFile();
1590}
1591
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001592const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor451dffa2012-10-18 21:47:16 +00001593 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001594 std::string Filename = filenameStrRef;
Douglas Gregor451dffa2012-10-18 21:47:16 +00001595 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001596 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor451dffa2012-10-18 21:47:16 +00001597 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1598 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001599 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor451dffa2012-10-18 21:47:16 +00001600 M.OriginalDir,
Argyrios Kyrtzidis460132d2011-06-01 05:43:53 +00001601 CurrentDir);
1602 if (!resolved.empty())
1603 File = FileMgr.getFile(resolved);
1604 }
1605
1606 return File;
1607}
1608
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001609/// \brief If we are loading a relocatable PCH file, and the filename is
1610/// not an absolute path, add the system root to the beginning of the file
1611/// name.
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001612void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1613 std::string &Filename) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001614 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00001615 if (!M.RelocatablePCH)
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001616 return;
Mike Stump11289f42009-09-09 15:08:12 +00001617
Michael J. Spencerf28df4c2010-12-17 21:22:22 +00001618 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001619 return;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001620
Douglas Gregorc567ba22011-07-22 16:35:34 +00001621 if (isysroot.empty()) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001622 // If no system root was given, default to '/'
1623 Filename.insert(Filename.begin(), '/');
Rafael Espindolafd5e7562012-10-30 00:38:13 +00001624 return;
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001625 }
Mike Stump11289f42009-09-09 15:08:12 +00001626
Douglas Gregorc567ba22011-07-22 16:35:34 +00001627 unsigned Length = isysroot.size();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001628 if (isysroot[Length - 1] != '/')
1629 Filename.insert(Filename.begin(), '/');
Mike Stump11289f42009-09-09 15:08:12 +00001630
Douglas Gregorc567ba22011-07-22 16:35:34 +00001631 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001632}
1633
Douglas Gregor4b29c162012-10-22 23:51:00 +00001634ASTReader::ASTReadResult
1635ASTReader::ReadControlBlock(ModuleFile &F,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001636 llvm::SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor4b29c162012-10-22 23:51:00 +00001637 unsigned ClientLoadCapabilities) {
Douglas Gregor112b9072012-10-18 05:31:06 +00001638 llvm::BitstreamCursor &Stream = F.Stream;
1639
1640 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1641 Error("malformed block record in AST file");
1642 return Failure;
1643 }
1644
1645 // Read all of the records and blocks in the control block.
1646 RecordData Record;
1647 while (!Stream.AtEndOfStream()) {
1648 unsigned Code = Stream.ReadCode();
1649 if (Code == llvm::bitc::END_BLOCK) {
1650 if (Stream.ReadBlockEnd()) {
1651 Error("error at end of control block in AST file");
1652 return Failure;
1653 }
1654
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001655 // Validate all of the input files.
1656 if (!DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001657 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001658 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor4b29c162012-10-22 23:51:00 +00001659 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001660 return OutOfDate;
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001661 }
1662
Douglas Gregor112b9072012-10-18 05:31:06 +00001663 return Success;
1664 }
1665
1666 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001667 switch (Stream.ReadSubBlockID()) {
1668 case INPUT_FILES_BLOCK_ID:
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001669 F.InputFilesCursor = Stream;
1670 if (Stream.SkipBlock() || // Skip with the main cursor
1671 // Read the abbreviations
1672 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1673 Error("malformed block record in AST file");
Douglas Gregor72be3902012-10-19 00:38:02 +00001674 return Failure;
Douglas Gregor72be3902012-10-19 00:38:02 +00001675 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001676 continue;
Douglas Gregor72be3902012-10-19 00:38:02 +00001677
1678 default:
1679 if (!Stream.SkipBlock())
1680 continue;
1681 break;
1682 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001683
1684 Error("malformed block record in AST file");
1685 return Failure;
1686 }
1687
1688 if (Code == llvm::bitc::DEFINE_ABBREV) {
1689 Stream.ReadAbbrevRecord();
1690 continue;
1691 }
1692
1693 // Read and process a record.
1694 Record.clear();
1695 const char *BlobStart = 0;
1696 unsigned BlobLen = 0;
1697 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1698 &BlobStart, &BlobLen)) {
1699 case METADATA: {
1700 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001701 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1702 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1703 : diag::warn_pch_version_too_new);
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001704 return VersionMismatch;
Douglas Gregor112b9072012-10-18 05:31:06 +00001705 }
1706
1707 bool hasErrors = Record[5];
1708 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1709 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001710 return HadErrors;
Douglas Gregor112b9072012-10-18 05:31:06 +00001711 }
1712
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00001713 F.RelocatablePCH = Record[4];
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001714
1715 const std::string &CurBranch = getClangFullRepositoryVersion();
1716 StringRef ASTBranch(BlobStart, BlobLen);
1717 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor4b29c162012-10-22 23:51:00 +00001718 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1719 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001720 return VersionMismatch;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001721 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001722 break;
1723 }
1724
1725 case IMPORTS: {
1726 // Load each of the imported PCH files.
1727 unsigned Idx = 0, N = Record.size();
1728 while (Idx < N) {
1729 // Read information about the AST file.
1730 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001731 // The import location will be the local one for now; we will adjust
1732 // all import locations of module imports after the global source
1733 // location info are setup.
1734 SourceLocation ImportLoc =
1735 SourceLocation::getFromRawEncoding(Record[Idx++]);
Douglas Gregor112b9072012-10-18 05:31:06 +00001736 unsigned Length = Record[Idx++];
1737 SmallString<128> ImportedFile(Record.begin() + Idx,
1738 Record.begin() + Idx + Length);
1739 Idx += Length;
1740
1741 // Load the AST file.
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001742 switch(ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, Loaded,
Douglas Gregor4b29c162012-10-22 23:51:00 +00001743 ClientLoadCapabilities)) {
Douglas Gregor112b9072012-10-18 05:31:06 +00001744 case Failure: return Failure;
1745 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001746 case OutOfDate: return OutOfDate;
1747 case VersionMismatch: return VersionMismatch;
1748 case ConfigurationMismatch: return ConfigurationMismatch;
1749 case HadErrors: return HadErrors;
Douglas Gregor112b9072012-10-18 05:31:06 +00001750 case Success: break;
1751 }
1752 }
1753 break;
1754 }
1755
Douglas Gregor4b29c162012-10-22 23:51:00 +00001756 case LANGUAGE_OPTIONS: {
1757 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1758 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00001759 ParseLanguageOptions(Record, Complain, *Listener) &&
1760 !DisableValidation)
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001761 return ConfigurationMismatch;
Douglas Gregor112b9072012-10-18 05:31:06 +00001762 break;
Douglas Gregor4b29c162012-10-22 23:51:00 +00001763 }
Douglas Gregor112b9072012-10-18 05:31:06 +00001764
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001765 case TARGET_OPTIONS: {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00001766 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1767 if (Listener && &F == *ModuleMgr.begin() &&
1768 ParseTargetOptions(Record, Complain, *Listener) &&
1769 !DisableValidation)
1770 return ConfigurationMismatch;
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001771 break;
1772 }
1773
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001774 case DIAGNOSTIC_OPTIONS: {
1775 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1776 if (Listener && &F == *ModuleMgr.begin() &&
1777 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1778 !DisableValidation)
1779 return ConfigurationMismatch;
1780 break;
1781 }
Douglas Gregorc6317db2012-10-24 15:49:58 +00001782
1783 case FILE_SYSTEM_OPTIONS: {
1784 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1785 if (Listener && &F == *ModuleMgr.begin() &&
1786 ParseFileSystemOptions(Record, Complain, *Listener) &&
1787 !DisableValidation)
1788 return ConfigurationMismatch;
1789 break;
1790 }
1791
Douglas Gregor2d302362012-10-24 16:50:34 +00001792 case HEADER_SEARCH_OPTIONS: {
1793 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1794 if (Listener && &F == *ModuleMgr.begin() &&
1795 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1796 !DisableValidation)
1797 return ConfigurationMismatch;
1798 break;
1799 }
1800
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001801 case PREPROCESSOR_OPTIONS: {
1802 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1803 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor55358ed2012-10-25 00:07:54 +00001804 ParsePreprocessorOptions(Record, Complain, *Listener,
1805 SuggestedPredefines) &&
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001806 !DisableValidation)
1807 return ConfigurationMismatch;
1808 break;
1809 }
1810
Douglas Gregorfad10d82012-10-18 18:36:53 +00001811 case ORIGINAL_FILE:
Douglas Gregor451dffa2012-10-18 21:47:16 +00001812 F.OriginalSourceFileID = FileID::get(Record[0]);
1813 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
1814 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1815 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor112b9072012-10-18 05:31:06 +00001816 break;
1817
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001818 case ORIGINAL_FILE_ID:
1819 F.OriginalSourceFileID = FileID::get(Record[0]);
1820 break;
1821
Douglas Gregor112b9072012-10-18 05:31:06 +00001822 case ORIGINAL_PCH_DIR:
Douglas Gregor451dffa2012-10-18 21:47:16 +00001823 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor112b9072012-10-18 05:31:06 +00001824 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00001825
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001826 case INPUT_FILE_OFFSETS:
1827 F.InputFileOffsets = (const uint32_t *)BlobStart;
1828 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor72be3902012-10-19 00:38:02 +00001829 break;
1830 }
Douglas Gregor72be3902012-10-19 00:38:02 +00001831 }
1832
1833 Error("premature end of bitstream in AST file");
1834 return Failure;
1835}
1836
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001837bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl34522812010-07-16 17:50:48 +00001838 llvm::BitstreamCursor &Stream = F.Stream;
1839
Sebastian Redl539c5062010-08-18 23:57:32 +00001840 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001841 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001842 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001843 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001844
Douglas Gregor112b9072012-10-18 05:31:06 +00001845 // Read all of the records and blocks for the AST file.
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001846 RecordData Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001847 while (!Stream.AtEndOfStream()) {
1848 unsigned Code = Stream.ReadCode();
1849 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001850 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001851 Error("error at end of module block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001852 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001853 }
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001854
Argyrios Kyrtzidis6fa16822012-09-21 01:30:00 +00001855 DeclContext *DC = Context.getTranslationUnitDecl();
1856 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1857 DC->setMustBuildLookupTable();
1858
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001859 return false;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001860 }
1861
1862 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1863 switch (Stream.ReadSubBlockID()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001864 case DECLTYPES_BLOCK_ID:
Chris Lattnere78a6be2009-04-27 01:05:14 +00001865 // We lazily load the decls block, but we want to set up the
1866 // DeclsCursor cursor to point into it. Clone our current bitcode
1867 // cursor to it, enter the block and read the abbrevs in that block.
1868 // With the main cursor, we just skip over it.
Sebastian Redl34522812010-07-16 17:50:48 +00001869 F.DeclsCursor = Stream;
Chris Lattnere78a6be2009-04-27 01:05:14 +00001870 if (Stream.SkipBlock() || // Skip with the main cursor.
1871 // Read the abbrevs.
Sebastian Redl539c5062010-08-18 23:57:32 +00001872 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_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 Lattnere78a6be2009-04-27 01:05:14 +00001875 }
1876 break;
Mike Stump11289f42009-09-09 15:08:12 +00001877
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001878 case DECL_UPDATES_BLOCK_ID:
1879 if (Stream.SkipBlock()) {
1880 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001881 return true;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00001882 }
1883 break;
1884
Sebastian Redl539c5062010-08-18 23:57:32 +00001885 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl34522812010-07-16 17:50:48 +00001886 F.MacroCursor = Stream;
Douglas Gregor51825b42011-09-09 22:02:16 +00001887 if (!PP.getExternalSource())
1888 PP.setExternalSource(this);
Douglas Gregor9882a5a2010-01-04 19:18:44 +00001889
Douglas Gregor796d76a2010-10-20 22:00:55 +00001890 if (Stream.SkipBlock() ||
1891 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001892 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001893 return true;
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001894 }
Douglas Gregor796d76a2010-10-20 22:00:55 +00001895 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001896 break;
Steve Naroff2ddea052009-04-23 10:39:46 +00001897
Douglas Gregor92a96f52011-02-08 21:58:10 +00001898 case PREPROCESSOR_DETAIL_BLOCK_ID:
1899 F.PreprocessorDetailCursor = Stream;
1900 if (Stream.SkipBlock() ||
1901 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1902 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1903 Error("malformed preprocessor detail record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001904 return true;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001905 }
1906 F.PreprocessorDetailStartOffset
1907 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor51825b42011-09-09 22:02:16 +00001908
1909 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +00001910 PP.createPreprocessingRecord();
Douglas Gregor51825b42011-09-09 22:02:16 +00001911 if (!PP.getPreprocessingRecord()->getExternalSource())
1912 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001913 break;
1914
Sebastian Redl539c5062010-08-18 23:57:32 +00001915 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001916 if (ReadSourceManagerBlock(F))
1917 return true;
Douglas Gregora7f71a92009-04-10 03:52:48 +00001918 break;
Douglas Gregor69021972011-11-30 17:33:56 +00001919
1920 case SUBMODULE_BLOCK_ID:
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001921 if (ReadSubmoduleBlock(F))
1922 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00001923 break;
1924
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001925 case COMMENTS_BLOCK_ID: {
1926 llvm::BitstreamCursor C = Stream;
1927 if (Stream.SkipBlock() ||
1928 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1929 Error("malformed comments block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001930 return true;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00001931 }
1932 CommentsCursors.push_back(std::make_pair(C, &F));
1933 break;
1934 }
1935
Douglas Gregor69021972011-11-30 17:33:56 +00001936 default:
1937 if (!Stream.SkipBlock())
1938 break;
1939 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001940 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001941 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001942 continue;
1943 }
1944
1945 if (Code == llvm::bitc::DEFINE_ABBREV) {
1946 Stream.ReadAbbrevRecord();
1947 continue;
1948 }
1949
1950 // Read and process a record.
1951 Record.clear();
Douglas Gregorbfbde532009-04-10 21:16:55 +00001952 const char *BlobStart = 0;
1953 unsigned BlobLen = 0;
Sebastian Redl539c5062010-08-18 23:57:32 +00001954 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00001955 &BlobStart, &BlobLen)) {
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001956 default: // Default behavior: ignore.
1957 break;
1958
Douglas Gregor5204bde2011-08-02 16:26:37 +00001959 case TYPE_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001960 if (F.LocalNumTypes != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001961 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001962 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001963 }
Sebastian Redl9e687992010-07-19 22:06:55 +00001964 F.TypeOffsets = (const uint32_t *)BlobStart;
1965 F.LocalNumTypes = Record[0];
Douglas Gregor3b65ed02011-08-02 18:32:54 +00001966 unsigned LocalBaseTypeIndex = Record[1];
1967 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00001968
Douglas Gregor5204bde2011-08-02 16:26:37 +00001969 if (F.LocalNumTypes > 0) {
1970 // Introduce the global -> local mapping for types within this module.
1971 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1972
1973 // Introduce the local -> global mapping for types within this module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00001974 F.TypeRemap.insertOrReplace(
1975 std::make_pair(LocalBaseTypeIndex,
1976 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregor5204bde2011-08-02 16:26:37 +00001977
1978 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1979 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00001980 break;
Douglas Gregor5204bde2011-08-02 16:26:37 +00001981 }
1982
Douglas Gregorf7180622011-08-03 15:48:04 +00001983 case DECL_OFFSET: {
Sebastian Redl9e687992010-07-19 22:06:55 +00001984 if (F.LocalNumDecls != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001985 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00001986 return true;
Douglas Gregor55abb232009-04-10 20:39:37 +00001987 }
Argyrios Kyrtzidis81ddd182011-10-27 18:47:35 +00001988 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl9e687992010-07-19 22:06:55 +00001989 F.LocalNumDecls = Record[0];
Douglas Gregorf7180622011-08-03 15:48:04 +00001990 unsigned LocalBaseDeclID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00001991 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor047d2ef2011-07-20 00:27:43 +00001992
Douglas Gregorf7180622011-08-03 15:48:04 +00001993 if (F.LocalNumDecls > 0) {
1994 // Introduce the global -> local mapping for declarations within this
1995 // module.
Douglas Gregordab42432011-08-12 00:15:20 +00001996 GlobalDeclMap.insert(
1997 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregorf7180622011-08-03 15:48:04 +00001998
1999 // Introduce the local -> global mapping for declarations within this
2000 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002001 F.DeclRemap.insertOrReplace(
2002 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregorf7180622011-08-03 15:48:04 +00002003
Douglas Gregor05f10352011-12-17 23:38:30 +00002004 // Introduce the global -> local mapping for declarations within this
2005 // module.
2006 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2007
Douglas Gregorf7180622011-08-03 15:48:04 +00002008 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2009 }
Douglas Gregor1e9bf3b2009-04-10 17:25:41 +00002010 break;
Douglas Gregorf7180622011-08-03 15:48:04 +00002011 }
2012
Sebastian Redl539c5062010-08-18 23:57:32 +00002013 case TU_UPDATE_LEXICAL: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00002014 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002015 DeclContextInfo &Info = F.DeclContextInfos[TU];
2016 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
2017 Info.NumLexicalDecls
2018 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor4163aca2011-09-09 21:34:22 +00002019 TU->setHasExternalLexicalStorage(true);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002020 break;
2021 }
2022
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002023 case UPDATE_VISIBLE: {
Douglas Gregorf7180622011-08-03 15:48:04 +00002024 unsigned Idx = 0;
2025 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00002026 ASTDeclContextNameLookupTable *Table =
2027 ASTDeclContextNameLookupTable::Create(
Douglas Gregorf7180622011-08-03 15:48:04 +00002028 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002029 (const unsigned char *)BlobStart,
Douglas Gregor903b7e92011-07-22 00:38:23 +00002030 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor4163aca2011-09-09 21:34:22 +00002031 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2032 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor94619c82011-08-24 19:03:07 +00002033 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregordab42432011-08-12 00:15:20 +00002034 TU->setHasExternalVisibleStorage(true);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002035 } else
Douglas Gregorf7180622011-08-03 15:48:04 +00002036 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redld7dce0a2010-08-24 00:50:04 +00002037 break;
2038 }
2039
Sebastian Redl539c5062010-08-18 23:57:32 +00002040 case IDENTIFIER_TABLE:
Sebastian Redl393f8b72010-07-19 20:52:06 +00002041 F.IdentifierTableData = BlobStart;
Douglas Gregor0e149972009-04-25 19:10:14 +00002042 if (Record[0]) {
Sebastian Redl393f8b72010-07-19 20:52:06 +00002043 F.IdentifierLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002044 = ASTIdentifierLookupTable::Create(
Sebastian Redl393f8b72010-07-19 20:52:06 +00002045 (const unsigned char *)F.IdentifierTableData + Record[0],
2046 (const unsigned char *)F.IdentifierTableData,
Sebastian Redl2c373b92010-10-05 15:59:54 +00002047 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor51825b42011-09-09 22:02:16 +00002048
2049 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor0e149972009-04-25 19:10:14 +00002050 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002051 break;
2052
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002053 case IDENTIFIER_OFFSET: {
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002054 if (F.LocalNumIdentifiers != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002055 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002056 return true;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002057 }
Sebastian Redlbd1b5be2010-07-19 22:28:42 +00002058 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2059 F.LocalNumIdentifiers = Record[0];
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002060 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002061 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor19d26352011-07-20 00:59:32 +00002062
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002063 if (F.LocalNumIdentifiers > 0) {
2064 // Introduce the global -> local mapping for identifiers within this
2065 // module.
2066 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2067 &F));
2068
2069 // Introduce the local -> global mapping for identifiers within this
2070 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002071 F.IdentifierRemap.insertOrReplace(
2072 std::make_pair(LocalBaseIdentifierID,
2073 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002074
2075 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2076 + F.LocalNumIdentifiers);
2077 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002078 break;
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002079 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002080
Sebastian Redl539c5062010-08-18 23:57:32 +00002081 case EXTERNAL_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002082 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2083 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002084 break;
Douglas Gregor08f01292009-04-17 22:13:46 +00002085
Sebastian Redl539c5062010-08-18 23:57:32 +00002086 case SPECIAL_TYPES:
Douglas Gregor903b7e92011-07-22 00:38:23 +00002087 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2088 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregor652d82a2009-04-18 05:55:16 +00002089 break;
2090
Sebastian Redl539c5062010-08-18 23:57:32 +00002091 case STATISTICS:
Sebastian Redlb293a452010-07-20 21:20:32 +00002092 TotalNumStatements += Record[0];
2093 TotalNumMacros += Record[1];
2094 TotalLexicalDeclContexts += Record[2];
2095 TotalVisibleDeclContexts += Record[3];
Douglas Gregor08f01292009-04-17 22:13:46 +00002096 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002097
Sebastian Redl539c5062010-08-18 23:57:32 +00002098 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002099 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2100 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattner90073802010-02-12 00:07:30 +00002101 break;
Kovarththanan Rajaratnam39f2fbd12010-03-07 19:10:13 +00002102
Alexis Hunt27a761d2011-05-04 23:29:54 +00002103 case DELEGATING_CTORS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002104 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2105 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002106 break;
2107
Sebastian Redl539c5062010-08-18 23:57:32 +00002108 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002109 if (Record.size() % 4 != 0) {
2110 Error("invalid weak identifiers record");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002111 return true;
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002112 }
2113
2114 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2115 // files. This isn't the way to do it :)
2116 WeakUndeclaredIdentifiers.clear();
2117
2118 // Translate the weak, undeclared identifiers into global IDs.
2119 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2120 WeakUndeclaredIdentifiers.push_back(
2121 getGlobalIdentifierID(F, Record[I++]));
2122 WeakUndeclaredIdentifiers.push_back(
2123 getGlobalIdentifierID(F, Record[I++]));
2124 WeakUndeclaredIdentifiers.push_back(
2125 ReadSourceLocation(F, Record, I).getRawEncoding());
2126 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2127 }
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002128 break;
2129
Sebastian Redl539c5062010-08-18 23:57:32 +00002130 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002131 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2132 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002133 break;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002134
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002135 case SELECTOR_OFFSETS: {
Sebastian Redla19a67f2010-08-03 21:58:15 +00002136 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redlada023c2010-08-04 20:40:17 +00002137 F.LocalNumSelectors = Record[0];
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002138 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002139 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor2262d282011-07-20 01:10:58 +00002140
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002141 if (F.LocalNumSelectors > 0) {
2142 // Introduce the global -> local mapping for selectors within this
2143 // module.
2144 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2145
2146 // Introduce the local -> global mapping for selectors within this
2147 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002148 F.SelectorRemap.insertOrReplace(
2149 std::make_pair(LocalBaseSelectorID,
2150 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002151
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002152 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2153 }
2154 break;
2155 }
2156
Sebastian Redl539c5062010-08-18 23:57:32 +00002157 case METHOD_POOL:
Sebastian Redlada023c2010-08-04 20:40:17 +00002158 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002159 if (Record[0])
Sebastian Redlada023c2010-08-04 20:40:17 +00002160 F.SelectorLookupTable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002161 = ASTSelectorLookupTable::Create(
Sebastian Redlada023c2010-08-04 20:40:17 +00002162 F.SelectorLookupTableData + Record[0],
2163 F.SelectorLookupTableData,
Douglas Gregor7fb09192011-07-21 22:35:25 +00002164 ASTSelectorLookupTrait(*this, F));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002165 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002166 break;
Douglas Gregoreda6a892009-04-26 00:07:37 +00002167
Sebastian Redl96371b42010-09-22 00:42:30 +00002168 case REFERENCED_SELECTOR_POOL:
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00002169 if (!Record.empty()) {
2170 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2171 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2172 Record[Idx++]));
2173 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2174 getRawEncoding());
2175 }
2176 }
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002177 break;
2178
Sebastian Redl539c5062010-08-18 23:57:32 +00002179 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00002180 if (!Record.empty() && Listener)
Argyrios Kyrtzidise445c722012-10-10 02:12:47 +00002181 Listener->ReadCounter(F, Record[0]);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002182 break;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002183
2184 case FILE_SORTED_DECLS:
2185 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002186 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002187 break;
Douglas Gregor258ae542009-04-27 06:38:32 +00002188
Douglas Gregor925296b2011-07-19 16:10:42 +00002189 case SOURCE_LOCATION_OFFSETS: {
2190 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redlb293a452010-07-20 21:20:32 +00002191 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002192 unsigned SLocSpaceSize = Record[1];
Douglas Gregor925296b2011-07-19 16:10:42 +00002193 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002194 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2195 SLocSpaceSize);
Douglas Gregor925296b2011-07-19 16:10:42 +00002196 // Make our entry in the range map. BaseID is negative and growing, so
2197 // we invert it. Because we invert it, though, we need the other end of
2198 // the range.
2199 unsigned RangeStart =
2200 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2201 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2202 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2203
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002204 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2205 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2206 GlobalSLocOffsetMap.insert(
2207 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2208 - SLocSpaceSize,&F));
2209
Douglas Gregor925296b2011-07-19 16:10:42 +00002210 // Initialize the remapping table.
2211 // Invalid stays invalid.
2212 F.SLocRemap.insert(std::make_pair(0U, 0));
2213 // This module. Base was 2 when being compiled.
2214 F.SLocRemap.insert(std::make_pair(2U,
2215 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor49bf76b2011-07-21 18:46:38 +00002216
2217 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregor925296b2011-07-19 16:10:42 +00002218 break;
2219 }
2220
Douglas Gregor5a1797c2011-08-01 16:01:55 +00002221 case MODULE_OFFSET_MAP: {
Douglas Gregor925296b2011-07-19 16:10:42 +00002222 // Additional remapping information.
2223 const unsigned char *Data = (const unsigned char*)BlobStart;
2224 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregor00659902011-08-02 10:56:51 +00002225
2226 // Continuous range maps we may be updating in our module.
2227 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002228 ContinuousRangeMap<uint32_t, int, 2>::Builder
2229 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002230 ContinuousRangeMap<uint32_t, int, 2>::Builder
2231 MacroRemap(F.MacroRemap);
2232 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002233 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2234 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor253eefe2011-12-01 00:59:36 +00002235 SubmoduleRemap(F.SubmoduleRemap);
2236 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002237 SelectorRemap(F.SelectorRemap);
Douglas Gregorf7180622011-08-03 15:48:04 +00002238 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002239 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2240
Douglas Gregor925296b2011-07-19 16:10:42 +00002241 while(Data < DataEnd) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002242 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002243 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregor00659902011-08-02 10:56:51 +00002244 Data += Len;
Douglas Gregorde3ef502011-11-30 23:21:26 +00002245 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregor925296b2011-07-19 16:10:42 +00002246 if (!OM) {
2247 Error("SourceLocation remap refers to unknown module");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002248 return true;
Douglas Gregor925296b2011-07-19 16:10:42 +00002249 }
Douglas Gregor00659902011-08-02 10:56:51 +00002250
2251 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2252 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002253 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002254 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002255 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002256 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2257 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor5204bde2011-08-02 16:26:37 +00002258 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor00659902011-08-02 10:56:51 +00002259
2260 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2261 SLocRemap.insert(std::make_pair(SLocOffset,
2262 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002263 IdentifierRemap.insert(
2264 std::make_pair(IdentifierIDOffset,
2265 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002266 MacroRemap.insert(std::make_pair(MacroIDOffset,
2267 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002268 PreprocessedEntityRemap.insert(
2269 std::make_pair(PreprocessedEntityIDOffset,
2270 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor253eefe2011-12-01 00:59:36 +00002271 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2272 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002273 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2274 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregorf7180622011-08-03 15:48:04 +00002275 DeclRemap.insert(std::make_pair(DeclIDOffset,
2276 OM->BaseDeclID - DeclIDOffset));
2277
Douglas Gregor5204bde2011-08-02 16:26:37 +00002278 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002279 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregor05f10352011-12-17 23:38:30 +00002280
2281 // Global -> local mappings.
2282 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregor925296b2011-07-19 16:10:42 +00002283 }
2284 break;
2285 }
2286
Douglas Gregor925296b2011-07-19 16:10:42 +00002287 case SOURCE_MANAGER_LINE_TABLE:
2288 if (ParseLineTable(F, Record))
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002289 return true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002290 break;
2291
Douglas Gregor925296b2011-07-19 16:10:42 +00002292 case SOURCE_LOCATION_PRELOADS: {
2293 // Need to transform from the local view (1-based IDs) to the global view,
2294 // which is based off F.SLocEntryBaseID.
Douglas Gregora918bab2011-08-25 21:09:44 +00002295 if (!F.PreloadSLocEntries.empty()) {
2296 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002297 return true;
Douglas Gregora918bab2011-08-25 21:09:44 +00002298 }
2299
2300 F.PreloadSLocEntries.swap(Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002301 break;
Douglas Gregor925296b2011-07-19 16:10:42 +00002302 }
Douglas Gregorc5046832009-04-27 18:38:38 +00002303
Sebastian Redl539c5062010-08-18 23:57:32 +00002304 case EXT_VECTOR_DECLS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002305 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2306 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002307 break;
2308
Sebastian Redl539c5062010-08-18 23:57:32 +00002309 case VTABLE_USES:
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002310 if (Record.size() % 3 != 0) {
2311 Error("Invalid VTABLE_USES record");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002312 return true;
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002313 }
2314
Sebastian Redl08aca90252010-08-05 18:21:25 +00002315 // Later tables overwrite earlier ones.
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002316 // FIXME: Modules will have some trouble with this. This is clearly not
2317 // the right way to do this.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002318 VTableUses.clear();
Douglas Gregor4daf6a32011-07-28 19:11:31 +00002319
2320 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2321 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2322 VTableUses.push_back(
2323 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2324 VTableUses.push_back(Record[Idx++]);
2325 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002326 break;
2327
Sebastian Redl539c5062010-08-18 23:57:32 +00002328 case DYNAMIC_CLASSES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002329 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2330 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002331 break;
2332
Sebastian Redl539c5062010-08-18 23:57:32 +00002333 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002334 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann63469422c2012-10-02 09:09:43 +00002335 Error("Invalid existing PendingInstantiations");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002336 return true;
Axel Naumann63469422c2012-10-02 09:09:43 +00002337 }
2338
2339 if (Record.size() % 2 != 0) {
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002340 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002341 return true;
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002342 }
Axel Naumann63469422c2012-10-02 09:09:43 +00002343
Douglas Gregorbbbc3672011-07-28 19:26:52 +00002344 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2345 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2346 PendingInstantiations.push_back(
2347 ReadSourceLocation(F, Record, I).getRawEncoding());
2348 }
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002349 break;
2350
Sebastian Redl539c5062010-08-18 23:57:32 +00002351 case SEMA_DECL_REFS:
Sebastian Redl08aca90252010-08-05 18:21:25 +00002352 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002353 // FIXME: Modules will have some trouble with this.
2354 SemaDeclRefs.clear();
2355 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2356 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002357 break;
2358
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002359 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002360 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2361 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2362 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002363
2364 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregora863b4b2011-08-04 16:36:56 +00002365
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002366 unsigned StartingID;
Douglas Gregor51825b42011-09-09 22:02:16 +00002367 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +00002368 PP.createPreprocessingRecord();
Douglas Gregor51825b42011-09-09 22:02:16 +00002369 if (!PP.getPreprocessingRecord()->getExternalSource())
2370 PP.getPreprocessingRecord()->SetExternalSource(*this);
2371 StartingID
2372 = PP.getPreprocessingRecord()
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002373 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00002374 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002375
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002376 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002377 // Introduce the global -> local mapping for preprocessed entities in
2378 // this module.
2379 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2380
2381 // Introduce the local -> global mapping for preprocessed entities in
2382 // this module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00002383 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002384 std::make_pair(LocalBasePreprocessedEntityID,
2385 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2386 }
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002387
Douglas Gregoraae92242010-03-19 21:51:54 +00002388 break;
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00002389 }
2390
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002391 case DECL_UPDATE_OFFSETS: {
2392 if (Record.size() % 2 != 0) {
2393 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002394 return true;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002395 }
2396 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregorf7180622011-08-03 15:48:04 +00002397 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2398 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002399 break;
2400 }
2401
Sebastian Redl539c5062010-08-18 23:57:32 +00002402 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002403 if (Record.size() % 3 != 0) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002404 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002405 return true;
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002406 }
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002407 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregorf7180622011-08-03 15:48:04 +00002408 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00002409 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002410 break;
2411 }
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002412
Douglas Gregor404cdde2012-01-27 01:47:08 +00002413 case OBJC_CATEGORIES_MAP: {
2414 if (F.LocalNumObjCCategoriesInMap != 0) {
2415 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002416 return true;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002417 }
Douglas Gregor404cdde2012-01-27 01:47:08 +00002418
2419 F.LocalNumObjCCategoriesInMap = Record[0];
2420 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00002421 break;
2422 }
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002423
Douglas Gregor404cdde2012-01-27 01:47:08 +00002424 case OBJC_CATEGORIES:
2425 F.ObjCCategories.swap(Record);
2426 break;
2427
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002428 case CXX_BASE_SPECIFIER_OFFSETS: {
2429 if (F.LocalNumCXXBaseSpecifiers != 0) {
2430 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002431 return true;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002432 }
2433
2434 F.LocalNumCXXBaseSpecifiers = Record[0];
2435 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00002436 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002437 break;
2438 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002439
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002440 case DIAG_PRAGMA_MAPPINGS:
Douglas Gregor925296b2011-07-19 16:10:42 +00002441 if (F.PragmaDiagMappings.empty())
2442 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002443 else
Douglas Gregor925296b2011-07-19 16:10:42 +00002444 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2445 Record.begin(), Record.end());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002446 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002447
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002448 case CUDA_SPECIAL_DECL_REFS:
2449 // Later tables overwrite earlier ones.
Douglas Gregor7fb09192011-07-21 22:35:25 +00002450 // FIXME: Modules will have trouble with this.
2451 CUDASpecialDeclRefs.clear();
2452 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2453 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002454 break;
Douglas Gregor09b69892011-02-10 17:09:37 +00002455
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002456 case HEADER_SEARCH_TABLE: {
Douglas Gregor09b69892011-02-10 17:09:37 +00002457 F.HeaderFileInfoTableData = BlobStart;
2458 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002459 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregor09b69892011-02-10 17:09:37 +00002460 if (Record[0]) {
2461 F.HeaderFileInfoTable
2462 = HeaderFileInfoLookupTable::Create(
2463 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002464 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregora3e41532011-07-28 20:55:49 +00002465 HeaderFileInfoTrait(*this, F,
Douglas Gregor51825b42011-09-09 22:02:16 +00002466 &PP.getHeaderSearchInfo(),
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002467 BlobStart + Record[2]));
Douglas Gregor51825b42011-09-09 22:02:16 +00002468
2469 PP.getHeaderSearchInfo().SetExternalSource(this);
2470 if (!PP.getHeaderSearchInfo().getExternalLookup())
2471 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregor09b69892011-02-10 17:09:37 +00002472 }
2473 break;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002474 }
2475
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002476 case FP_PRAGMA_OPTIONS:
2477 // Later tables overwrite earlier ones.
2478 FPPragmaOptions.swap(Record);
2479 break;
2480
2481 case OPENCL_EXTENSIONS:
2482 // Later tables overwrite earlier ones.
2483 OpenCLExtensions.swap(Record);
2484 break;
Alexis Hunt27a761d2011-05-04 23:29:54 +00002485
2486 case TENTATIVE_DEFINITIONS:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002487 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2488 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Alexis Hunt27a761d2011-05-04 23:29:54 +00002489 break;
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002490
2491 case KNOWN_NAMESPACES:
Douglas Gregor7fb09192011-07-21 22:35:25 +00002492 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2493 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002494 break;
Douglas Gregor0a839132011-12-03 00:59:55 +00002495
2496 case IMPORTED_MODULES: {
2497 if (F.Kind != MK_Module) {
2498 // If we aren't loading a module (which has its own exports), make
2499 // all of the imported modules visible.
2500 // FIXME: Deal with macros-only imports.
2501 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2502 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2503 ImportedModules.push_back(GlobalID);
2504 }
2505 }
2506 break;
Douglas Gregor05f10352011-12-17 23:38:30 +00002507 }
Douglas Gregor358cd442012-01-15 16:58:34 +00002508
Douglas Gregor05f10352011-12-17 23:38:30 +00002509 case LOCAL_REDECLARATIONS: {
Douglas Gregor358cd442012-01-15 16:58:34 +00002510 F.RedeclarationChains.swap(Record);
2511 break;
2512 }
2513
2514 case LOCAL_REDECLARATIONS_MAP: {
2515 if (F.LocalNumRedeclarationsInMap != 0) {
2516 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002517 return true;
Douglas Gregor05f10352011-12-17 23:38:30 +00002518 }
Douglas Gregor0a839132011-12-03 00:59:55 +00002519
Douglas Gregor358cd442012-01-15 16:58:34 +00002520 F.LocalNumRedeclarationsInMap = Record[0];
2521 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregor05f10352011-12-17 23:38:30 +00002522 break;
Douglas Gregor0a839132011-12-03 00:59:55 +00002523 }
Douglas Gregor464b0ca2011-12-22 21:40:42 +00002524
2525 case MERGED_DECLARATIONS: {
2526 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2527 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2528 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2529 for (unsigned N = Record[Idx++]; N > 0; --N)
2530 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2531 }
2532 break;
2533 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002534
2535 case MACRO_OFFSET: {
2536 if (F.LocalNumMacros != 0) {
2537 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002538 return true;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002539 }
2540 F.MacroOffsets = (const uint32_t *)BlobStart;
2541 F.LocalNumMacros = Record[0];
2542 unsigned LocalBaseMacroID = Record[1];
2543 F.BaseMacroID = getTotalNumMacros();
2544
2545 if (F.LocalNumMacros > 0) {
2546 // Introduce the global -> local mapping for macros within this module.
2547 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2548
2549 // Introduce the local -> global mapping for macros within this module.
2550 F.MacroRemap.insertOrReplace(
2551 std::make_pair(LocalBaseMacroID,
2552 F.BaseMacroID - LocalBaseMacroID));
2553
2554 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2555 }
2556 break;
2557 }
2558
2559 case MACRO_UPDATES: {
2560 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2561 MacroID ID = getGlobalMacroID(F, Record[I++]);
2562 if (I == N)
2563 break;
2564
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002565 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2566 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2567 MacroUpdate Update;
2568 Update.UndefLoc = UndefLoc;
2569 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002570 }
2571 break;
2572 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002573 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002574 }
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002575 Error("premature end of bitstream in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002576 return true;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002577}
2578
Douglas Gregorcf68c582011-12-01 22:20:10 +00002579void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002580 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002581 switch (Names[I].getKind()) {
2582 case HiddenName::Declaration:
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002583 Names[I].getDecl()->Hidden = false;
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002584 break;
2585
2586 case HiddenName::MacroVisibility: {
2587 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2588 Macro.second->setHidden(!Macro.second->isPublic());
2589 if (Macro.second->isDefined()) {
2590 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2591 }
2592 break;
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002593 }
2594
Douglas Gregorcfa46a82012-10-12 00:16:50 +00002595 case HiddenName::MacroUndef: {
2596 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2597 if (Macro.second->isDefined()) {
2598 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2599 if (PPMutationListener *Listener = PP.getPPMutationListener())
2600 Listener->UndefinedMacro(Macro.second);
2601 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2602 }
2603 break;
2604 }
Douglas Gregor0abc2622011-12-20 22:06:13 +00002605 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002606 }
Douglas Gregorcf68c582011-12-01 22:20:10 +00002607}
2608
Douglas Gregorff2be532011-12-01 17:11:21 +00002609void ASTReader::makeModuleVisible(Module *Mod,
2610 Module::NameVisibilityKind NameVisibility) {
2611 llvm::SmallPtrSet<Module *, 4> Visited;
2612 llvm::SmallVector<Module *, 4> Stack;
2613 Stack.push_back(Mod);
2614 while (!Stack.empty()) {
2615 Mod = Stack.back();
2616 Stack.pop_back();
2617
2618 if (NameVisibility <= Mod->NameVisibility) {
2619 // This module already has this level of visibility (or greater), so
2620 // there is nothing more to do.
2621 continue;
2622 }
2623
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002624 if (!Mod->isAvailable()) {
2625 // Modules that aren't available cannot be made visible.
2626 continue;
2627 }
2628
Douglas Gregorff2be532011-12-01 17:11:21 +00002629 // Update the module's name visibility.
2630 Mod->NameVisibility = NameVisibility;
2631
Douglas Gregorcf68c582011-12-01 22:20:10 +00002632 // If we've already deserialized any names from this module,
Douglas Gregorff2be532011-12-01 17:11:21 +00002633 // mark them as visible.
Douglas Gregorcf68c582011-12-01 22:20:10 +00002634 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2635 if (Hidden != HiddenNamesMap.end()) {
2636 makeNamesVisible(Hidden->second);
2637 HiddenNamesMap.erase(Hidden);
2638 }
Douglas Gregorff2be532011-12-01 17:11:21 +00002639
2640 // Push any non-explicit submodules onto the stack to be marked as
2641 // visible.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002642 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2643 SubEnd = Mod->submodule_end();
Douglas Gregorff2be532011-12-01 17:11:21 +00002644 Sub != SubEnd; ++Sub) {
Douglas Gregoreb90e832012-01-04 23:32:19 +00002645 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2646 Stack.push_back(*Sub);
Douglas Gregorff2be532011-12-01 17:11:21 +00002647 }
Douglas Gregor54139282011-12-02 19:11:09 +00002648
2649 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002650 bool AnyWildcard = false;
2651 bool UnrestrictedWildcard = false;
2652 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor54139282011-12-02 19:11:09 +00002653 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2654 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002655 if (!Mod->Exports[I].getInt()) {
2656 // Export a named module directly; no wildcards involved.
2657 if (Visited.insert(Exported))
Douglas Gregor54139282011-12-02 19:11:09 +00002658 Stack.push_back(Exported);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002659
2660 continue;
Douglas Gregor54139282011-12-02 19:11:09 +00002661 }
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002662
2663 // Wildcard export: export all of the imported modules that match
2664 // the given pattern.
2665 AnyWildcard = true;
2666 if (UnrestrictedWildcard)
2667 continue;
2668
2669 if (Module *Restriction = Mod->Exports[I].getPointer())
2670 WildcardRestrictions.push_back(Restriction);
2671 else {
2672 WildcardRestrictions.clear();
2673 UnrestrictedWildcard = true;
2674 }
2675 }
2676
2677 // If there were any wildcards, push any imported modules that were
2678 // re-exported by the wildcard restriction.
2679 if (!AnyWildcard)
2680 continue;
2681
2682 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2683 Module *Imported = Mod->Imports[I];
Benjamin Kramerfc6eb7d2012-08-22 15:37:55 +00002684 if (!Visited.insert(Imported))
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002685 continue;
2686
2687 bool Acceptable = UnrestrictedWildcard;
2688 if (!Acceptable) {
2689 // Check whether this module meets one of the restrictions.
2690 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2691 Module *Restriction = WildcardRestrictions[R];
2692 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2693 Acceptable = true;
2694 break;
2695 }
2696 }
2697 }
2698
2699 if (!Acceptable)
2700 continue;
2701
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002702 Stack.push_back(Imported);
Douglas Gregor54139282011-12-02 19:11:09 +00002703 }
Douglas Gregorff2be532011-12-01 17:11:21 +00002704 }
2705}
2706
Sebastian Redl009e7f22010-10-05 16:15:19 +00002707ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002708 ModuleKind Type,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002709 SourceLocation ImportLoc,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002710 unsigned ClientLoadCapabilities) {
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002711 // Bump the generation number.
Douglas Gregor404cdde2012-01-27 01:47:08 +00002712 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor112b9072012-10-18 05:31:06 +00002713
Douglas Gregor188dbef2012-11-07 17:46:15 +00002714 unsigned NumModules = ModuleMgr.size();
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002715 llvm::SmallVector<ImportedModule, 4> Loaded;
2716 switch(ASTReadResult ReadResult = ReadASTCore(FileName, Type, ImportLoc,
Douglas Gregor188dbef2012-11-07 17:46:15 +00002717 /*ImportedBy=*/0, Loaded,
2718 ClientLoadCapabilities)) {
2719 case Failure:
2720 case OutOfDate:
2721 case VersionMismatch:
2722 case ConfigurationMismatch:
2723 case HadErrors:
2724 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, ModuleMgr.end());
2725 return ReadResult;
2726
2727 case Success:
2728 break;
Sebastian Redl2abc0382010-07-16 20:41:52 +00002729 }
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002730
2731 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002732
Douglas Gregor112b9072012-10-18 05:31:06 +00002733 // Load the AST blocks of all of the modules that we loaded.
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002734 for (llvm::SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
Douglas Gregor112b9072012-10-18 05:31:06 +00002735 MEnd = Loaded.end();
2736 M != MEnd; ++M) {
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002737 ModuleFile &F = *M->Mod;
Douglas Gregor112b9072012-10-18 05:31:06 +00002738
2739 // Read the AST block.
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002740 if (ReadASTBlock(F))
2741 return Failure;
Douglas Gregor112b9072012-10-18 05:31:06 +00002742
2743 // Once read, set the ModuleFile bit base offset and update the size in
2744 // bits of all files we've seen.
2745 F.GlobalBitOffset = TotalModulesSizeInBits;
2746 TotalModulesSizeInBits += F.SizeInBits;
2747 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2748
Douglas Gregor112b9072012-10-18 05:31:06 +00002749 // Preload SLocEntries.
2750 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2751 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor4750b772012-10-22 22:53:10 +00002752 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor112b9072012-10-18 05:31:06 +00002753 // directly because the entry may have already been loaded in which case
Douglas Gregor4750b772012-10-22 22:53:10 +00002754 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor112b9072012-10-18 05:31:06 +00002755 // SourceManager.
2756 SourceMgr.getLoadedSLocEntryByID(Index);
2757 }
2758 }
2759
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002760 // Setup the import locations.
2761 for (llvm::SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(),
2762 MEnd = Loaded.end();
2763 M != MEnd; ++M) {
2764 ModuleFile &F = *M->Mod;
2765 if (!M->ImportedBy)
2766 F.ImportLoc = M->ImportLoc;
2767 else
2768 F.ImportLoc = ReadSourceLocation(*M->ImportedBy,
2769 M->ImportLoc.getRawEncoding());
2770 }
2771
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002772 // Mark all of the identifiers in the identifier table as being out of date,
2773 // so that various accessors know to check the loaded modules when the
2774 // identifier is used.
Douglas Gregor51825b42011-09-09 22:02:16 +00002775 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2776 IdEnd = PP.getIdentifierTable().end();
2777 Id != IdEnd; ++Id)
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002778 Id->second->setOutOfDate(true);
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00002779
Douglas Gregor24bb9232011-12-02 18:58:38 +00002780 // Resolve any unresolved module exports.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002781 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2782 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2783 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002784 Module *ResolvedMod = getSubmodule(GlobalID);
2785
2786 if (Unresolved.IsImport) {
2787 if (ResolvedMod)
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002788 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002789 continue;
Douglas Gregor24bb9232011-12-02 18:58:38 +00002790 }
Douglas Gregorf5eedd02011-12-05 17:28:06 +00002791
2792 if (ResolvedMod || Unresolved.IsWildcard)
2793 Unresolved.Mod->Exports.push_back(
2794 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregor24bb9232011-12-02 18:58:38 +00002795 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002796 UnresolvedModuleImportExports.clear();
Douglas Gregor24bb9232011-12-02 18:58:38 +00002797
Douglas Gregor4163aca2011-09-09 21:34:22 +00002798 InitializeContext();
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002799
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002800 if (DeserializationListener)
2801 DeserializationListener->ReaderInitialized(this);
2802
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002803 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2804 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2805 PrimaryModule.OriginalSourceFileID
2806 = FileID::get(PrimaryModule.SLocEntryBaseID
2807 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002808
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002809 // If this AST file is a precompiled preamble, then set the
2810 // preamble file ID of the source manager to the file source file
2811 // from which the preamble was built.
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002812 if (Type == MK_Preamble) {
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002813 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidis9afd4492012-01-05 21:36:25 +00002814 } else if (Type == MK_MainFile) {
Douglas Gregore68c2cb2012-10-18 21:18:25 +00002815 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002816 }
Douglas Gregor936a5b42010-11-30 05:23:00 +00002817 }
2818
Douglas Gregor404cdde2012-01-27 01:47:08 +00002819 // For any Objective-C class definitions we have already loaded, make sure
2820 // that we load any additional categories.
2821 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2822 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2823 ObjCClassesLoaded[I],
2824 PreviousGeneration);
2825 }
2826
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002827 return Success;
2828}
2829
Douglas Gregor4b29c162012-10-22 23:51:00 +00002830ASTReader::ASTReadResult
2831ASTReader::ReadASTCore(StringRef FileName,
2832 ModuleKind Type,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002833 SourceLocation ImportLoc,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002834 ModuleFile *ImportedBy,
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002835 llvm::SmallVectorImpl<ImportedModule> &Loaded,
Douglas Gregor4b29c162012-10-22 23:51:00 +00002836 unsigned ClientLoadCapabilities) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002837 ModuleFile *M;
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002838 bool NewModule;
2839 std::string ErrorStr;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +00002840 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportLoc,
2841 ImportedBy, CurrentGeneration,
2842 ErrorStr);
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002843
Douglas Gregor4dd3e942011-08-19 02:29:29 +00002844 if (!M) {
2845 // We couldn't load the module.
2846 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2847 + ErrorStr;
2848 Error(Msg);
2849 return Failure;
2850 }
2851
2852 if (!NewModule) {
2853 // We've already loaded this module.
2854 return Success;
2855 }
2856
2857 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2858 // module?
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002859 if (FileName != "-") {
2860 CurrentDir = llvm::sys::path::parent_path(FileName);
2861 if (CurrentDir.empty()) CurrentDir = ".";
2862 }
2863
Douglas Gregorde3ef502011-11-30 23:21:26 +00002864 ModuleFile &F = *M;
Sebastian Redl34522812010-07-16 17:50:48 +00002865 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002866 Stream.init(F.StreamFile);
Sebastian Redlfa061442010-07-21 20:07:32 +00002867 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregord32f0352011-07-22 06:10:01 +00002868
Sebastian Redlc2e6dbf2010-07-17 00:12:06 +00002869 // Sniff for the signature.
2870 if (Stream.Read(8) != 'C' ||
2871 Stream.Read(8) != 'P' ||
2872 Stream.Read(8) != 'C' ||
2873 Stream.Read(8) != 'H') {
2874 Diag(diag::err_not_a_pch_file) << FileName;
2875 return Failure;
2876 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002877
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002878 while (!Stream.AtEndOfStream()) {
2879 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00002880
Douglas Gregor92863e42009-04-10 23:10:45 +00002881 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002882 Error("invalid record at top-level of AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002883 return Failure;
2884 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002885
2886 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregora868bbd2009-04-21 22:25:48 +00002887
Douglas Gregor0aa21c92012-10-18 18:27:37 +00002888 // We only know the control subblock ID.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002889 switch (BlockID) {
2890 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregor92863e42009-04-10 23:10:45 +00002891 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002892 Error("malformed BlockInfoBlock in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002893 return Failure;
2894 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002895 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00002896 case CONTROL_BLOCK_ID:
Douglas Gregor4b29c162012-10-22 23:51:00 +00002897 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor55abb232009-04-10 20:39:37 +00002898 case Success:
2899 break;
2900
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00002901 case Failure: return Failure;
2902 case OutOfDate: return OutOfDate;
2903 case VersionMismatch: return VersionMismatch;
2904 case ConfigurationMismatch: return ConfigurationMismatch;
2905 case HadErrors: return HadErrors;
Douglas Gregor55abb232009-04-10 20:39:37 +00002906 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002907 break;
Douglas Gregor112b9072012-10-18 05:31:06 +00002908 case AST_BLOCK_ID:
2909 // Record that we've loaded this module.
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00002910 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
Douglas Gregor112b9072012-10-18 05:31:06 +00002911 return Success;
2912
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002913 default:
Douglas Gregor92863e42009-04-10 23:10:45 +00002914 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002915 Error("malformed block record in AST file");
Douglas Gregor92863e42009-04-10 23:10:45 +00002916 return Failure;
2917 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002918 break;
2919 }
Mike Stump11289f42009-09-09 15:08:12 +00002920 }
Douglas Gregord32f0352011-07-22 06:10:01 +00002921
Sebastian Redl2abc0382010-07-16 20:41:52 +00002922 return Success;
2923}
2924
Douglas Gregor51825b42011-09-09 22:02:16 +00002925void ASTReader::InitializeContext() {
Douglas Gregordab42432011-08-12 00:15:20 +00002926 // If there's a listener, notify them that we "read" the translation unit.
2927 if (DeserializationListener)
Douglas Gregor4163aca2011-09-09 21:34:22 +00002928 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2929 Context.getTranslationUnitDecl());
Douglas Gregoraa433012010-10-01 01:18:02 +00002930
Douglas Gregordab42432011-08-12 00:15:20 +00002931 // Make sure we load the declaration update records for the translation unit,
2932 // if there are any.
Douglas Gregor4163aca2011-09-09 21:34:22 +00002933 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2934 Context.getTranslationUnitDecl());
Douglas Gregordab42432011-08-12 00:15:20 +00002935
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002936 // FIXME: Find a better way to deal with collisions between these
2937 // built-in types. Right now, we just ignore the problem.
2938
2939 // Load the special types.
Douglas Gregord53ae832012-01-17 18:09:05 +00002940 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002941 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2942 if (!Context.CFConstantStringTypeDecl)
2943 Context.setCFConstantStringType(GetType(String));
2944 }
2945
2946 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2947 QualType FileType = GetType(File);
2948 if (FileType.isNull()) {
2949 Error("FILE type is NULL");
2950 return;
2951 }
2952
2953 if (!Context.FILEDecl) {
2954 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2955 Context.setFILEDecl(Typedef->getDecl());
2956 else {
2957 const TagType *Tag = FileType->getAs<TagType>();
2958 if (!Tag) {
2959 Error("Invalid FILE type in AST file");
2960 return;
2961 }
2962 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002963 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002964 }
Douglas Gregor27821ce2009-07-07 16:35:42 +00002965 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00002966
Douglas Gregor3c267f7a2011-11-11 19:13:12 +00002967 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002968 QualType Jmp_bufType = GetType(Jmp_buf);
2969 if (Jmp_bufType.isNull()) {
2970 Error("jmp_buf type is NULL");
2971 return;
2972 }
2973
2974 if (!Context.jmp_bufDecl) {
2975 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2976 Context.setjmp_bufDecl(Typedef->getDecl());
2977 else {
2978 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2979 if (!Tag) {
2980 Error("Invalid jmp_buf type in AST file");
2981 return;
2982 }
2983 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002984 }
Jonathan D. Turnerf07f1312011-08-05 23:07:10 +00002985 }
Mike Stumpa4de80b2009-07-28 02:25:19 +00002986 }
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002987
Douglas Gregor3c267f7a2011-11-11 19:13:12 +00002988 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregorabc5fbe2011-09-10 00:30:18 +00002989 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2990 if (Sigjmp_bufType.isNull()) {
2991 Error("sigjmp_buf type is NULL");
2992 return;
2993 }
2994
2995 if (!Context.sigjmp_bufDecl) {
2996 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2997 Context.setsigjmp_bufDecl(Typedef->getDecl());
2998 else {
2999 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3000 assert(Tag && "Invalid sigjmp_buf type in AST file");
3001 Context.setsigjmp_bufDecl(Tag->getDecl());
3002 }
3003 }
3004 }
3005
3006 if (unsigned ObjCIdRedef
3007 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3008 if (Context.ObjCIdRedefinitionType.isNull())
3009 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3010 }
3011
3012 if (unsigned ObjCClassRedef
3013 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3014 if (Context.ObjCClassRedefinitionType.isNull())
3015 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3016 }
3017
3018 if (unsigned ObjCSelRedef
3019 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3020 if (Context.ObjCSelRedefinitionType.isNull())
3021 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3022 }
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003023
3024 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3025 QualType Ucontext_tType = GetType(Ucontext_t);
3026 if (Ucontext_tType.isNull()) {
3027 Error("ucontext_t type is NULL");
3028 return;
3029 }
3030
3031 if (!Context.ucontext_tDecl) {
3032 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3033 Context.setucontext_tDecl(Typedef->getDecl());
3034 else {
3035 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3036 assert(Tag && "Invalid ucontext_t type in AST file");
3037 Context.setucontext_tDecl(Tag->getDecl());
3038 }
3039 }
3040 }
Douglas Gregoraa8a8272011-08-11 22:18:49 +00003041 }
3042
Douglas Gregor4163aca2011-09-09 21:34:22 +00003043 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003044
3045 // If there were any CUDA special declarations, deserialize them.
3046 if (!CUDASpecialDeclRefs.empty()) {
3047 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor4163aca2011-09-09 21:34:22 +00003048 Context.setcudaConfigureCallDecl(
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003049 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3050 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003051
3052 // Re-export any modules that were imported by a non-module AST file.
3053 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3054 if (Module *Imported = getSubmodule(ImportedModules[I]))
3055 makeModuleVisible(Imported, Module::AllVisible);
3056 }
3057 ImportedModules.clear();
Argyrios Kyrtzidis366985d2009-06-19 00:03:23 +00003058}
3059
Douglas Gregorcf68c582011-12-01 22:20:10 +00003060void ASTReader::finalizeForWriting() {
3061 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3062 HiddenEnd = HiddenNamesMap.end();
3063 Hidden != HiddenEnd; ++Hidden) {
3064 makeNamesVisible(Hidden->second);
3065 }
3066 HiddenNamesMap.clear();
3067}
3068
Douglas Gregor45fe0362009-05-12 01:31:05 +00003069/// \brief Retrieve the name of the original source file name
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003070/// directly from the AST file, without actually loading the AST
Douglas Gregor45fe0362009-05-12 01:31:05 +00003071/// file.
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003072std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +00003073 FileManager &FileMgr,
David Blaikie9c902b52011-09-25 23:23:43 +00003074 DiagnosticsEngine &Diags) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003075 // Open the AST file.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003076 std::string ErrStr;
Dylan Noblesmithe2778992012-02-05 02:12:40 +00003077 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner5159f612010-11-23 08:35:12 +00003078 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregor45fe0362009-05-12 01:31:05 +00003079 if (!Buffer) {
Kaelyn Uhrain272d7182012-06-20 00:36:03 +00003080 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003081 return std::string();
3082 }
3083
3084 // Initialize the stream
3085 llvm::BitstreamReader StreamFile;
3086 llvm::BitstreamCursor Stream;
Mike Stump11289f42009-09-09 15:08:12 +00003087 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregor45fe0362009-05-12 01:31:05 +00003088 (const unsigned char *)Buffer->getBufferEnd());
3089 Stream.init(StreamFile);
3090
3091 // Sniff for the signature.
3092 if (Stream.Read(8) != 'C' ||
3093 Stream.Read(8) != 'P' ||
3094 Stream.Read(8) != 'C' ||
3095 Stream.Read(8) != 'H') {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003096 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003097 return std::string();
3098 }
3099
3100 RecordData Record;
3101 while (!Stream.AtEndOfStream()) {
3102 unsigned Code = Stream.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00003103
Douglas Gregor45fe0362009-05-12 01:31:05 +00003104 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3105 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump11289f42009-09-09 15:08:12 +00003106
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003107 // We only know the AST subblock ID.
Douglas Gregor45fe0362009-05-12 01:31:05 +00003108 switch (BlockID) {
Douglas Gregor112b9072012-10-18 05:31:06 +00003109 case CONTROL_BLOCK_ID:
3110 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003111 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003112 return std::string();
3113 }
3114 break;
Mike Stump11289f42009-09-09 15:08:12 +00003115
Douglas Gregor45fe0362009-05-12 01:31:05 +00003116 default:
3117 if (Stream.SkipBlock()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003118 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003119 return std::string();
3120 }
3121 break;
3122 }
3123 continue;
3124 }
3125
3126 if (Code == llvm::bitc::END_BLOCK) {
3127 if (Stream.ReadBlockEnd()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003128 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregor45fe0362009-05-12 01:31:05 +00003129 return std::string();
3130 }
3131 continue;
3132 }
3133
3134 if (Code == llvm::bitc::DEFINE_ABBREV) {
3135 Stream.ReadAbbrevRecord();
3136 continue;
3137 }
3138
3139 Record.clear();
3140 const char *BlobStart = 0;
3141 unsigned BlobLen = 0;
Douglas Gregorfad10d82012-10-18 18:36:53 +00003142 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregor45fe0362009-05-12 01:31:05 +00003143 return std::string(BlobStart, BlobLen);
Mike Stump11289f42009-09-09 15:08:12 +00003144 }
Douglas Gregor45fe0362009-05-12 01:31:05 +00003145
3146 return std::string();
3147}
3148
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003149namespace {
3150 class SimplePCHValidator : public ASTReaderListener {
3151 const LangOptions &ExistingLangOpts;
3152 const TargetOptions &ExistingTargetOpts;
Douglas Gregorb6368752012-10-24 23:41:50 +00003153 const PreprocessorOptions &ExistingPPOpts;
Douglas Gregor55358ed2012-10-25 00:07:54 +00003154 FileManager &FileMgr;
3155
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003156 public:
3157 SimplePCHValidator(const LangOptions &ExistingLangOpts,
Douglas Gregorb6368752012-10-24 23:41:50 +00003158 const TargetOptions &ExistingTargetOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003159 const PreprocessorOptions &ExistingPPOpts,
3160 FileManager &FileMgr)
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003161 : ExistingLangOpts(ExistingLangOpts),
Douglas Gregorb6368752012-10-24 23:41:50 +00003162 ExistingTargetOpts(ExistingTargetOpts),
Douglas Gregor55358ed2012-10-25 00:07:54 +00003163 ExistingPPOpts(ExistingPPOpts),
3164 FileMgr(FileMgr)
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003165 {
3166 }
3167
3168 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3169 bool Complain) {
3170 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3171 }
3172 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3173 bool Complain) {
3174 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3175 }
Douglas Gregorb6368752012-10-24 23:41:50 +00003176 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003177 bool Complain,
3178 std::string &SuggestedPredefines) {
3179 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3180 SuggestedPredefines);
Douglas Gregorb6368752012-10-24 23:41:50 +00003181 }
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003182 };
3183}
3184
Douglas Gregor52901822012-11-06 23:40:54 +00003185bool ASTReader::readASTFileControlBlock(StringRef Filename,
3186 FileManager &FileMgr,
3187 ASTReaderListener &Listener) {
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003188 // Open the AST file.
3189 std::string ErrStr;
3190 OwningPtr<llvm::MemoryBuffer> Buffer;
3191 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3192 if (!Buffer) {
Douglas Gregor52901822012-11-06 23:40:54 +00003193 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003194 }
3195
3196 // Initialize the stream
3197 llvm::BitstreamReader StreamFile;
3198 llvm::BitstreamCursor Stream;
3199 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3200 (const unsigned char *)Buffer->getBufferEnd());
3201 Stream.init(StreamFile);
3202
3203 // Sniff for the signature.
3204 if (Stream.Read(8) != 'C' ||
3205 Stream.Read(8) != 'P' ||
3206 Stream.Read(8) != 'C' ||
3207 Stream.Read(8) != 'H') {
Douglas Gregor52901822012-11-06 23:40:54 +00003208 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003209 }
3210
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003211 RecordData Record;
3212 bool InControlBlock = false;
3213 while (!Stream.AtEndOfStream()) {
3214 unsigned Code = Stream.ReadCode();
3215
3216 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3217 unsigned BlockID = Stream.ReadSubBlockID();
3218
3219 // We only know the control subblock ID.
3220 switch (BlockID) {
3221 case CONTROL_BLOCK_ID:
3222 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Douglas Gregor52901822012-11-06 23:40:54 +00003223 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003224 } else {
3225 InControlBlock = true;
3226 }
3227 break;
3228
3229 default:
3230 if (Stream.SkipBlock())
Douglas Gregor52901822012-11-06 23:40:54 +00003231 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003232 break;
3233 }
3234 continue;
3235 }
3236
3237 if (Code == llvm::bitc::END_BLOCK) {
3238 if (Stream.ReadBlockEnd()) {
Douglas Gregor52901822012-11-06 23:40:54 +00003239 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003240 }
Douglas Gregor52901822012-11-06 23:40:54 +00003241
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003242 InControlBlock = false;
3243 continue;
3244 }
3245
3246 if (Code == llvm::bitc::DEFINE_ABBREV) {
3247 Stream.ReadAbbrevRecord();
3248 continue;
3249 }
3250
3251 Record.clear();
3252 const char *BlobStart = 0;
3253 unsigned BlobLen = 0;
3254 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3255 if (InControlBlock) {
3256 switch ((ControlRecordTypes)RecCode) {
3257 case METADATA: {
3258 if (Record[0] != VERSION_MAJOR) {
Douglas Gregor52901822012-11-06 23:40:54 +00003259 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003260 }
3261
3262 const std::string &CurBranch = getClangFullRepositoryVersion();
3263 StringRef ASTBranch(BlobStart, BlobLen);
3264 if (StringRef(CurBranch) != ASTBranch)
Douglas Gregor52901822012-11-06 23:40:54 +00003265 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003266
3267 break;
3268 }
3269 case LANGUAGE_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003270 if (ParseLanguageOptions(Record, false, Listener))
3271 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003272 break;
3273
3274 case TARGET_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003275 if (ParseTargetOptions(Record, false, Listener))
3276 return true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003277 break;
3278
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003279 case DIAGNOSTIC_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003280 if (ParseDiagnosticOptions(Record, false, Listener))
3281 return true;
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003282 break;
3283
Douglas Gregor2d302362012-10-24 16:50:34 +00003284 case FILE_SYSTEM_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003285 if (ParseFileSystemOptions(Record, false, Listener))
3286 return true;
Douglas Gregor2d302362012-10-24 16:50:34 +00003287 break;
3288
3289 case HEADER_SEARCH_OPTIONS:
Douglas Gregor52901822012-11-06 23:40:54 +00003290 if (ParseHeaderSearchOptions(Record, false, Listener))
3291 return true;
Douglas Gregor2d302362012-10-24 16:50:34 +00003292 break;
3293
Douglas Gregor55358ed2012-10-25 00:07:54 +00003294 case PREPROCESSOR_OPTIONS: {
3295 std::string IgnoredSuggestedPredefines;
Douglas Gregor52901822012-11-06 23:40:54 +00003296 if (ParsePreprocessorOptions(Record, false, Listener,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003297 IgnoredSuggestedPredefines))
Douglas Gregor52901822012-11-06 23:40:54 +00003298 return true;
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003299 break;
Douglas Gregor55358ed2012-10-25 00:07:54 +00003300 }
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003301
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003302 default:
3303 // No other validation to perform.
3304 break;
3305 }
3306 }
3307 }
3308
Douglas Gregor52901822012-11-06 23:40:54 +00003309 return false;
3310}
3311
3312
3313bool ASTReader::isAcceptableASTFile(StringRef Filename,
3314 FileManager &FileMgr,
3315 const LangOptions &LangOpts,
3316 const TargetOptions &TargetOpts,
3317 const PreprocessorOptions &PPOpts) {
3318 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, FileMgr);
3319 return !readASTFileControlBlock(Filename, FileMgr, validator);
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003320}
3321
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003322bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor69021972011-11-30 17:33:56 +00003323 // Enter the submodule block.
3324 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3325 Error("malformed submodule block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003326 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003327 }
3328
3329 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor253eefe2011-12-01 00:59:36 +00003330 bool First = true;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003331 Module *CurrentModule = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00003332 RecordData Record;
3333 while (true) {
3334 unsigned Code = F.Stream.ReadCode();
3335 if (Code == llvm::bitc::END_BLOCK) {
3336 if (F.Stream.ReadBlockEnd()) {
3337 Error("error at end of submodule block in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003338 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003339 }
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003340 return false;
Douglas Gregor69021972011-11-30 17:33:56 +00003341 }
3342
3343 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3344 // No known subblocks, always skip them.
3345 F.Stream.ReadSubBlockID();
3346 if (F.Stream.SkipBlock()) {
3347 Error("malformed block record in AST file");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003348 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003349 }
3350 continue;
3351 }
3352
3353 if (Code == llvm::bitc::DEFINE_ABBREV) {
3354 F.Stream.ReadAbbrevRecord();
3355 continue;
3356 }
3357
3358 // Read a record.
3359 const char *BlobStart;
3360 unsigned BlobLen;
3361 Record.clear();
3362 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3363 default: // Default behavior: ignore.
3364 break;
3365
3366 case SUBMODULE_DEFINITION: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003367 if (First) {
3368 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003369 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003370 }
3371
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003372 if (Record.size() < 7) {
Douglas Gregor73441092011-12-05 22:27:44 +00003373 Error("malformed module definition");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003374 return true;
Douglas Gregor73441092011-12-05 22:27:44 +00003375 }
3376
Douglas Gregor69021972011-11-30 17:33:56 +00003377 StringRef Name(BlobStart, BlobLen);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003378 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3379 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3380 bool IsFramework = Record[2];
3381 bool IsExplicit = Record[3];
Douglas Gregora686e1b2012-01-27 19:52:33 +00003382 bool IsSystem = Record[4];
3383 bool InferSubmodules = Record[5];
3384 bool InferExplicitSubmodules = Record[6];
3385 bool InferExportWildcard = Record[7];
Douglas Gregor73441092011-12-05 22:27:44 +00003386
Douglas Gregorde3ef502011-11-30 23:21:26 +00003387 Module *ParentModule = 0;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003388 if (Parent)
3389 ParentModule = getSubmodule(Parent);
Douglas Gregor69021972011-11-30 17:33:56 +00003390
3391 // Retrieve this (sub)module from the module map, creating it if
3392 // necessary.
3393 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3394 IsFramework,
3395 IsExplicit).first;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003396 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3397 if (GlobalIndex >= SubmodulesLoaded.size() ||
3398 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003399 Error("too many submodules");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003400 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003401 }
Douglas Gregore37a85a2011-12-02 17:30:13 +00003402
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +00003403 CurrentModule->setASTFile(F.File);
Douglas Gregor98a52db2011-12-20 00:28:52 +00003404 CurrentModule->IsFromModuleFile = true;
Douglas Gregora686e1b2012-01-27 19:52:33 +00003405 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor73441092011-12-05 22:27:44 +00003406 CurrentModule->InferSubmodules = InferSubmodules;
3407 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3408 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregore37a85a2011-12-02 17:30:13 +00003409 if (DeserializationListener)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003410 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregore37a85a2011-12-02 17:30:13 +00003411
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003412 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor69021972011-11-30 17:33:56 +00003413 break;
3414 }
3415
Douglas Gregor524e33e2011-12-08 19:11:24 +00003416 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003417 if (First) {
3418 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003419 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003420 }
3421
Douglas Gregor69021972011-11-30 17:33:56 +00003422 if (!CurrentModule)
3423 break;
3424
3425 StringRef FileName(BlobStart, BlobLen);
3426 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor73141fa2011-12-08 17:39:04 +00003427 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003428 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor73141fa2011-12-08 17:39:04 +00003429 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor69021972011-11-30 17:33:56 +00003430 Error("mismatched umbrella headers in submodule");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003431 return true;
Douglas Gregor69021972011-11-30 17:33:56 +00003432 }
3433 }
3434 break;
3435 }
3436
3437 case SUBMODULE_HEADER: {
Douglas Gregor253eefe2011-12-01 00:59:36 +00003438 if (First) {
3439 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003440 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003441 }
3442
Douglas Gregor69021972011-11-30 17:33:56 +00003443 if (!CurrentModule)
3444 break;
3445
3446 // FIXME: Be more lazy about this!
3447 StringRef FileName(BlobStart, BlobLen);
3448 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3449 if (std::find(CurrentModule->Headers.begin(),
3450 CurrentModule->Headers.end(),
3451 File) == CurrentModule->Headers.end())
Douglas Gregor59527662012-10-15 06:28:11 +00003452 ModMap.addHeader(CurrentModule, File, false);
3453 }
3454 break;
3455 }
3456
3457 case SUBMODULE_EXCLUDED_HEADER: {
3458 if (First) {
3459 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003460 return true;
Douglas Gregor59527662012-10-15 06:28:11 +00003461 }
3462
3463 if (!CurrentModule)
3464 break;
3465
3466 // FIXME: Be more lazy about this!
3467 StringRef FileName(BlobStart, BlobLen);
3468 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3469 if (std::find(CurrentModule->Headers.begin(),
3470 CurrentModule->Headers.end(),
3471 File) == CurrentModule->Headers.end())
3472 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor69021972011-11-30 17:33:56 +00003473 }
3474 break;
3475 }
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00003476
3477 case SUBMODULE_TOPHEADER: {
3478 if (First) {
3479 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003480 return true;
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00003481 }
3482
3483 if (!CurrentModule)
3484 break;
3485
3486 // FIXME: Be more lazy about this!
3487 StringRef FileName(BlobStart, BlobLen);
3488 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3489 CurrentModule->TopHeaders.insert(File);
3490 break;
3491 }
3492
Douglas Gregor524e33e2011-12-08 19:11:24 +00003493 case SUBMODULE_UMBRELLA_DIR: {
3494 if (First) {
3495 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003496 return true;
Douglas Gregor524e33e2011-12-08 19:11:24 +00003497 }
3498
3499 if (!CurrentModule)
3500 break;
3501
3502 StringRef DirName(BlobStart, BlobLen);
3503 if (const DirectoryEntry *Umbrella
3504 = PP.getFileManager().getDirectory(DirName)) {
3505 if (!CurrentModule->getUmbrellaDir())
3506 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3507 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3508 Error("mismatched umbrella directories in submodule");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003509 return true;
Douglas Gregor524e33e2011-12-08 19:11:24 +00003510 }
3511 }
3512 break;
3513 }
3514
Douglas Gregor253eefe2011-12-01 00:59:36 +00003515 case SUBMODULE_METADATA: {
3516 if (!First) {
3517 Error("submodule metadata record not at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003518 return true;
Douglas Gregor253eefe2011-12-01 00:59:36 +00003519 }
3520 First = false;
3521
3522 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor253eefe2011-12-01 00:59:36 +00003523 F.LocalNumSubmodules = Record[0];
3524 unsigned LocalBaseSubmoduleID = Record[1];
3525 if (F.LocalNumSubmodules > 0) {
3526 // Introduce the global -> local mapping for submodules within this
3527 // module.
3528 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3529
3530 // Introduce the local -> global mapping for submodules within this
3531 // module.
Douglas Gregor2682ba02011-12-19 16:14:14 +00003532 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor253eefe2011-12-01 00:59:36 +00003533 std::make_pair(LocalBaseSubmoduleID,
3534 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3535
3536 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3537 }
3538 break;
3539 }
Douglas Gregor24bb9232011-12-02 18:58:38 +00003540
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003541 case SUBMODULE_IMPORTS: {
3542 if (First) {
3543 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003544 return true;
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003545 }
3546
3547 if (!CurrentModule)
3548 break;
3549
3550 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3551 UnresolvedModuleImportExport Unresolved;
3552 Unresolved.File = &F;
3553 Unresolved.Mod = CurrentModule;
3554 Unresolved.ID = Record[Idx];
3555 Unresolved.IsImport = true;
3556 Unresolved.IsWildcard = false;
3557 UnresolvedModuleImportExports.push_back(Unresolved);
3558 }
3559 break;
3560 }
3561
Douglas Gregor24bb9232011-12-02 18:58:38 +00003562 case SUBMODULE_EXPORTS: {
3563 if (First) {
3564 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003565 return true;
Douglas Gregor24bb9232011-12-02 18:58:38 +00003566 }
3567
3568 if (!CurrentModule)
3569 break;
3570
3571 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003572 UnresolvedModuleImportExport Unresolved;
Douglas Gregor24bb9232011-12-02 18:58:38 +00003573 Unresolved.File = &F;
Douglas Gregor0093b3c2011-12-05 16:33:54 +00003574 Unresolved.Mod = CurrentModule;
3575 Unresolved.ID = Record[Idx];
3576 Unresolved.IsImport = false;
3577 Unresolved.IsWildcard = Record[Idx + 1];
3578 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregor24bb9232011-12-02 18:58:38 +00003579 }
3580
3581 // Once we've loaded the set of exports, there's no reason to keep
3582 // the parsed, unresolved exports around.
3583 CurrentModule->UnresolvedExports.clear();
3584 break;
3585 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003586 case SUBMODULE_REQUIRES: {
3587 if (First) {
3588 Error("missing submodule metadata record at beginning of block");
Douglas Gregorc9ad5fb2012-10-22 22:50:17 +00003589 return true;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003590 }
3591
3592 if (!CurrentModule)
3593 break;
3594
3595 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikiebbafb8a2012-03-11 07:00:24 +00003596 Context.getLangOpts(),
Douglas Gregor89929282012-01-30 06:01:29 +00003597 Context.getTargetInfo());
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00003598 break;
3599 }
Douglas Gregor69021972011-11-30 17:33:56 +00003600 }
3601 }
Douglas Gregor69021972011-11-30 17:33:56 +00003602}
3603
Douglas Gregor55abb232009-04-10 20:39:37 +00003604/// \brief Parse the record that corresponds to a LangOptions data
3605/// structure.
3606///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003607/// This routine parses the language options from the AST file and then gives
3608/// them to the AST listener if one is set.
Douglas Gregor55abb232009-04-10 20:39:37 +00003609///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003610/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003611bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3612 bool Complain,
3613 ASTReaderListener &Listener) {
3614 LangOptions LangOpts;
3615 unsigned Idx = 0;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00003616#define LANGOPT(Name, Bits, Default, Description) \
3617 LangOpts.Name = Record[Idx++];
3618#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3619 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3620#include "clang/Basic/LangOptions.def"
John McCall5fb5df92012-06-20 06:18:46 +00003621
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003622 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3623 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3624 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3625
3626 unsigned Length = Record[Idx++];
3627 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3628 Record.begin() + Idx + Length);
3629 return Listener.ReadLanguageOptions(LangOpts, Complain);
3630}
3631
3632bool ASTReader::ParseTargetOptions(const RecordData &Record,
3633 bool Complain,
3634 ASTReaderListener &Listener) {
3635 unsigned Idx = 0;
3636 TargetOptions TargetOpts;
3637 TargetOpts.Triple = ReadString(Record, Idx);
3638 TargetOpts.CPU = ReadString(Record, Idx);
3639 TargetOpts.ABI = ReadString(Record, Idx);
3640 TargetOpts.CXXABI = ReadString(Record, Idx);
3641 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3642 for (unsigned N = Record[Idx++]; N; --N) {
3643 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3644 }
3645 for (unsigned N = Record[Idx++]; N; --N) {
3646 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor55abb232009-04-10 20:39:37 +00003647 }
Douglas Gregor55abb232009-04-10 20:39:37 +00003648
Douglas Gregorfc9e7a22012-10-23 06:18:24 +00003649 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor55abb232009-04-10 20:39:37 +00003650}
3651
Douglas Gregor8263ffb2012-10-24 15:17:15 +00003652bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3653 ASTReaderListener &Listener) {
3654 DiagnosticOptions DiagOpts;
3655 unsigned Idx = 0;
3656#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3657#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3658 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3659#include "clang/Basic/DiagnosticOptions.def"
3660
3661 for (unsigned N = Record[Idx++]; N; --N) {
3662 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3663 }
3664
3665 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3666}
3667
Douglas Gregorc6317db2012-10-24 15:49:58 +00003668bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3669 ASTReaderListener &Listener) {
3670 FileSystemOptions FSOpts;
3671 unsigned Idx = 0;
3672 FSOpts.WorkingDir = ReadString(Record, Idx);
3673 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3674}
3675
Douglas Gregor2d302362012-10-24 16:50:34 +00003676bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3677 bool Complain,
3678 ASTReaderListener &Listener) {
3679 HeaderSearchOptions HSOpts;
3680 unsigned Idx = 0;
3681 HSOpts.Sysroot = ReadString(Record, Idx);
3682
3683 // Include entries.
3684 for (unsigned N = Record[Idx++]; N; --N) {
3685 std::string Path = ReadString(Record, Idx);
3686 frontend::IncludeDirGroup Group
3687 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3688 bool IsUserSupplied = Record[Idx++];
3689 bool IsFramework = Record[Idx++];
3690 bool IgnoreSysRoot = Record[Idx++];
3691 bool IsInternal = Record[Idx++];
3692 bool ImplicitExternC = Record[Idx++];
3693 HSOpts.UserEntries.push_back(
3694 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3695 IgnoreSysRoot, IsInternal, ImplicitExternC));
3696 }
3697
3698 // System header prefixes.
3699 for (unsigned N = Record[Idx++]; N; --N) {
3700 std::string Prefix = ReadString(Record, Idx);
3701 bool IsSystemHeader = Record[Idx++];
3702 HSOpts.SystemHeaderPrefixes.push_back(
3703 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3704 }
3705
3706 HSOpts.ResourceDir = ReadString(Record, Idx);
3707 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3708 HSOpts.DisableModuleHash = Record[Idx++];
3709 HSOpts.UseBuiltinIncludes = Record[Idx++];
3710 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3711 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3712 HSOpts.UseLibcxx = Record[Idx++];
3713
3714 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3715}
3716
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003717bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3718 bool Complain,
Douglas Gregor55358ed2012-10-25 00:07:54 +00003719 ASTReaderListener &Listener,
3720 std::string &SuggestedPredefines) {
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003721 PreprocessorOptions PPOpts;
3722 unsigned Idx = 0;
3723
3724 // Macro definitions/undefs
3725 for (unsigned N = Record[Idx++]; N; --N) {
3726 std::string Macro = ReadString(Record, Idx);
3727 bool IsUndef = Record[Idx++];
3728 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3729 }
3730
3731 // Includes
3732 for (unsigned N = Record[Idx++]; N; --N) {
3733 PPOpts.Includes.push_back(ReadString(Record, Idx));
3734 }
3735
3736 // Macro Includes
3737 for (unsigned N = Record[Idx++]; N; --N) {
3738 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3739 }
3740
Douglas Gregorb6368752012-10-24 23:41:50 +00003741 PPOpts.UsePredefines = Record[Idx++];
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003742 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3743 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3744 PPOpts.ObjCXXARCStandardLibrary =
3745 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
Douglas Gregor55358ed2012-10-25 00:07:54 +00003746 SuggestedPredefines.clear();
3747 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3748 SuggestedPredefines);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00003749}
3750
Douglas Gregorde3ef502011-11-30 23:21:26 +00003751std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003752ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003753 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003754 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003755 assert(I != GlobalPreprocessedEntityMap.end() &&
3756 "Corrupted global preprocessed entity map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00003757 ModuleFile *M = I->second;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003758 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3759 return std::make_pair(M, LocalIndex);
3760}
3761
Argyrios Kyrtzidisd4fcf5802012-10-02 16:10:51 +00003762std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3763ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3764 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3765 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3766 Mod.NumPreprocessedEntities);
3767
3768 return std::make_pair(PreprocessingRecord::iterator(),
3769 PreprocessingRecord::iterator());
3770}
3771
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003772std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3773ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3774 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3775 ModuleDeclIterator(this, &Mod,
3776 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3777}
3778
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003779PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3780 PreprocessedEntityID PPID = Index+1;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003781 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3782 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00003783 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003784 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor92a96f52011-02-08 21:58:10 +00003785
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00003786 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003787 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003788
3789 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3790 switch (Code) {
3791 case llvm::bitc::END_BLOCK:
3792 return 0;
3793
3794 case llvm::bitc::ENTER_SUBBLOCK:
3795 Error("unexpected subblock record in preprocessor detail block");
3796 return 0;
3797
3798 case llvm::bitc::DEFINE_ABBREV:
3799 Error("unexpected abbrevation record in preprocessor detail block");
3800 return 0;
3801
3802 default:
3803 break;
3804 }
3805
3806 if (!PP.getPreprocessingRecord()) {
3807 Error("no preprocessing record");
3808 return 0;
3809 }
3810
3811 // Read the record.
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003812 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3813 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003814 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3815 const char *BlobStart = 0;
3816 unsigned BlobLen = 0;
3817 RecordData Record;
3818 PreprocessorDetailRecordTypes RecType =
3819 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3820 Code, Record, BlobStart, BlobLen);
3821 switch (RecType) {
3822 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003823 bool isBuiltin = Record[0];
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003824 IdentifierInfo *Name = 0;
3825 MacroDefinition *Def = 0;
3826 if (isBuiltin)
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003827 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003828 else {
3829 PreprocessedEntityID
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003830 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003831 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3832 }
3833
3834 MacroExpansion *ME;
3835 if (isBuiltin)
3836 ME = new (PPRec) MacroExpansion(Name, Range);
3837 else
3838 ME = new (PPRec) MacroExpansion(Def, Range);
3839
3840 return ME;
3841 }
3842
3843 case PPD_MACRO_DEFINITION: {
3844 // Decode the identifier info and then check again; if the macro is
3845 // still defined and associated with the identifier,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003846 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003847 MacroDefinition *MD
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003848 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003849
3850 if (DeserializationListener)
3851 DeserializationListener->MacroDefinitionRead(PPID, MD);
3852
3853 return MD;
3854 }
3855
3856 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003857 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00003858 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3859 const FileEntry *File = 0;
3860 if (!FullFileName.empty())
3861 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003862
3863 // FIXME: Stable encoding
3864 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003865 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003866 InclusionDirective *ID
3867 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003868 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00003869 Record[1], Record[3],
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003870 File,
Argyrios Kyrtzidisb5735422011-09-20 23:27:41 +00003871 Range);
Argyrios Kyrtzidis86ec6002011-09-20 23:27:38 +00003872 return ID;
3873 }
3874 }
David Blaikie8a40f702012-01-17 06:56:22 +00003875
3876 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregoraae92242010-03-19 21:51:54 +00003877}
3878
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003879/// \brief \arg SLocMapI points at a chunk of a module that contains no
3880/// preprocessed entities or the entities it contains are not the ones we are
3881/// looking for. Find the next module that contains entities and return the ID
3882/// of the first entry.
3883PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3884 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3885 ++SLocMapI;
3886 for (GlobalSLocOffsetMapType::const_iterator
3887 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00003888 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003889 if (M.NumPreprocessedEntities)
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003890 return M.BasePreprocessedEntityID;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003891 }
3892
3893 return getTotalNumPreprocessedEntities();
3894}
3895
3896namespace {
3897
3898template <unsigned PPEntityOffset::*PPLoc>
3899struct PPEntityComp {
3900 const ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00003901 ModuleFile &M;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003902
Douglas Gregorde3ef502011-11-30 23:21:26 +00003903 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003904
Benjamin Kramer5ce7f102011-09-21 06:42:26 +00003905 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3906 SourceLocation LHS = getLoc(L);
3907 SourceLocation RHS = getLoc(R);
3908 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3909 }
3910
3911 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003912 SourceLocation LHS = getLoc(L);
3913 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3914 }
3915
Benjamin Kramer5ce7f102011-09-21 06:42:26 +00003916 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003917 SourceLocation RHS = getLoc(R);
3918 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3919 }
3920
3921 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3922 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3923 }
3924};
3925
3926}
3927
3928/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3929PreprocessedEntityID
3930ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3931 if (SourceMgr.isLocalSourceLocation(BLoc))
3932 return getTotalNumPreprocessedEntities();
3933
3934 GlobalSLocOffsetMapType::const_iterator
3935 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3936 BLoc.getOffset());
3937 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3938 "Corrupted global sloc offset map");
3939
3940 if (SLocMapI->second->NumPreprocessedEntities == 0)
3941 return findNextPreprocessedEntity(SLocMapI);
3942
Douglas Gregorde3ef502011-11-30 23:21:26 +00003943 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003944 typedef const PPEntityOffset *pp_iterator;
3945 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3946 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidise523e382011-09-22 21:17:02 +00003947
3948 size_t Count = M.NumPreprocessedEntities;
3949 size_t Half;
3950 pp_iterator First = pp_begin;
3951 pp_iterator PPI;
3952
3953 // Do a binary search manually instead of using std::lower_bound because
3954 // The end locations of entities may be unordered (when a macro expansion
3955 // is inside another macro argument), but for this case it is not important
3956 // whether we get the first macro expansion or its containing macro.
3957 while (Count > 0) {
3958 Half = Count/2;
3959 PPI = First;
3960 std::advance(PPI, Half);
3961 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3962 BLoc)){
3963 First = PPI;
3964 ++First;
3965 Count = Count - Half - 1;
3966 } else
3967 Count = Half;
3968 }
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003969
3970 if (PPI == pp_end)
3971 return findNextPreprocessedEntity(SLocMapI);
3972
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00003973 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003974}
3975
3976/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3977PreprocessedEntityID
3978ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3979 if (SourceMgr.isLocalSourceLocation(ELoc))
3980 return getTotalNumPreprocessedEntities();
3981
3982 GlobalSLocOffsetMapType::const_iterator
3983 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3984 ELoc.getOffset());
3985 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3986 "Corrupted global sloc offset map");
3987
3988 if (SLocMapI->second->NumPreprocessedEntities == 0)
3989 return findNextPreprocessedEntity(SLocMapI);
3990
Douglas Gregorde3ef502011-11-30 23:21:26 +00003991 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00003992 typedef const PPEntityOffset *pp_iterator;
3993 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3994 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3995 pp_iterator PPI =
3996 std::upper_bound(pp_begin, pp_end, ELoc,
3997 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3998
3999 if (PPI == pp_end)
4000 return findNextPreprocessedEntity(SLocMapI);
4001
Argyrios Kyrtzidis19c17062012-11-02 02:31:22 +00004002 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00004003}
4004
4005/// \brief Returns a pair of [Begin, End) indices of preallocated
4006/// preprocessed entities that \arg Range encompasses.
4007std::pair<unsigned, unsigned>
4008 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4009 if (Range.isInvalid())
4010 return std::make_pair(0,0);
4011 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4012
4013 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4014 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4015 return std::make_pair(BeginID, EndID);
4016}
4017
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00004018/// \brief Optionally returns true or false if the preallocated preprocessed
4019/// entity with index \arg Index came from file \arg FID.
4020llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4021 FileID FID) {
4022 if (FID.isInvalid())
4023 return false;
4024
Douglas Gregorde3ef502011-11-30 23:21:26 +00004025 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4026 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidis429ec022011-10-25 00:29:50 +00004027 unsigned LocalIndex = PPInfo.second;
4028 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4029
4030 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4031 if (Loc.isInvalid())
4032 return false;
4033
4034 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4035 return true;
4036 else
4037 return false;
4038}
4039
Douglas Gregor69e94642011-08-25 18:14:34 +00004040namespace {
4041 /// \brief Visitor used to search for information about a header file.
4042 class HeaderFileInfoVisitor {
4043 ASTReader &Reader;
4044 const FileEntry *FE;
4045
4046 llvm::Optional<HeaderFileInfo> HFI;
4047
4048 public:
4049 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4050 : Reader(Reader), FE(FE) { }
4051
Douglas Gregorde3ef502011-11-30 23:21:26 +00004052 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor69e94642011-08-25 18:14:34 +00004053 HeaderFileInfoVisitor *This
4054 = static_cast<HeaderFileInfoVisitor *>(UserData);
4055
4056 HeaderFileInfoTrait Trait(This->Reader, M,
4057 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4058 M.HeaderFileFrameworkStrings,
4059 This->FE->getName());
4060
4061 HeaderFileInfoLookupTable *Table
4062 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4063 if (!Table)
4064 return false;
4065
4066 // Look in the on-disk hash table for an entry for this file name.
4067 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4068 &Trait);
4069 if (Pos == Table->end())
4070 return false;
4071
4072 This->HFI = *Pos;
4073 return true;
4074 }
4075
4076 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4077 };
4078}
4079
Douglas Gregor09b69892011-02-10 17:09:37 +00004080HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregor69e94642011-08-25 18:14:34 +00004081 HeaderFileInfoVisitor Visitor(*this, FE);
4082 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4083 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregor09b69892011-02-10 17:09:37 +00004084 if (Listener)
Douglas Gregor69e94642011-08-25 18:14:34 +00004085 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4086 return *HFI;
Douglas Gregor09b69892011-02-10 17:09:37 +00004087 }
4088
4089 return HeaderFileInfo();
4090}
4091
David Blaikie9c902b52011-09-25 23:23:43 +00004092void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004093 // FIXME: Make it work properly with modules.
4094 llvm::SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00004095 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00004096 ModuleFile &F = *(*I);
Douglas Gregor925296b2011-07-19 16:10:42 +00004097 unsigned Idx = 0;
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004098 DiagStates.clear();
4099 assert(!Diag.DiagStates.empty());
4100 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
Douglas Gregor925296b2011-07-19 16:10:42 +00004101 while (Idx < F.PragmaDiagMappings.size()) {
4102 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004103 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4104 if (DiagStateID != 0) {
4105 Diag.DiagStatePoints.push_back(
4106 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4107 FullSourceLoc(Loc, SourceMgr)));
4108 continue;
4109 }
4110
4111 assert(DiagStateID == 0);
4112 // A new DiagState was created here.
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004113 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004114 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4115 DiagStates.push_back(NewState);
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004116 Diag.DiagStatePoints.push_back(
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00004117 DiagnosticsEngine::DiagStatePoint(NewState,
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004118 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregor925296b2011-07-19 16:10:42 +00004119 while (1) {
4120 assert(Idx < F.PragmaDiagMappings.size() &&
4121 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4122 if (Idx >= F.PragmaDiagMappings.size()) {
4123 break; // Something is messed up but at least avoid infinite loop in
4124 // release build.
4125 }
4126 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4127 if (DiagID == (unsigned)-1) {
4128 break; // no more diag/map pairs for this location.
4129 }
4130 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidisc137d0d2011-11-09 01:24:17 +00004131 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4132 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregor925296b2011-07-19 16:10:42 +00004133 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00004134 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00004135 }
4136}
4137
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004138/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redl2c499f62010-08-18 23:56:43 +00004139ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004140 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turner35005682011-07-20 21:31:32 +00004141 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00004142 ModuleFile *M = I->second;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00004143 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004144}
4145
4146/// \brief Read and return the type with the given index..
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004147///
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004148/// The index is the type ID, shifted and minus the number of predefs. This
4149/// routine actually reads the record corresponding to the type at the given
4150/// location. It is a helper routine for GetType, which deals with reading type
4151/// IDs.
Douglas Gregor903b7e92011-07-22 00:38:23 +00004152QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004153 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004154 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl34522812010-07-16 17:50:48 +00004155
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004156 // Keep track of where we are in the stream, then jump back there
4157 // after reading this type.
Douglas Gregor12bfa382009-10-17 00:13:19 +00004158 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004159
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004160 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redleaa4ade2010-08-11 18:52:41 +00004161
Douglas Gregor1342e842009-07-06 18:54:52 +00004162 // Note that we are loading a type record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00004163 Deserializing AType(this);
Mike Stump11289f42009-09-09 15:08:12 +00004164
Douglas Gregor903b7e92011-07-22 00:38:23 +00004165 unsigned Idx = 0;
Sebastian Redl2c373b92010-10-05 15:59:54 +00004166 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004167 RecordData Record;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004168 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl539c5062010-08-18 23:57:32 +00004169 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4170 case TYPE_EXT_QUAL: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004171 if (Record.size() != 2) {
4172 Error("Incorrect encoding of extended qualifier type");
4173 return QualType();
4174 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004175 QualType Base = readType(*Loc.F, Record, Idx);
4176 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004177 return Context.getQualifiedType(Base, Quals);
Douglas Gregor455b8f42009-04-15 22:00:08 +00004178 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004179
Sebastian Redl539c5062010-08-18 23:57:32 +00004180 case TYPE_COMPLEX: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004181 if (Record.size() != 1) {
4182 Error("Incorrect encoding of complex type");
4183 return QualType();
4184 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004185 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004186 return Context.getComplexType(ElemType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004187 }
4188
Sebastian Redl539c5062010-08-18 23:57:32 +00004189 case TYPE_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004190 if (Record.size() != 1) {
4191 Error("Incorrect encoding of pointer type");
4192 return QualType();
4193 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004194 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004195 return Context.getPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004196 }
4197
Sebastian Redl539c5062010-08-18 23:57:32 +00004198 case TYPE_BLOCK_POINTER: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004199 if (Record.size() != 1) {
4200 Error("Incorrect encoding of block pointer type");
4201 return QualType();
4202 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004203 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004204 return Context.getBlockPointerType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004205 }
4206
Sebastian Redl539c5062010-08-18 23:57:32 +00004207 case TYPE_LVALUE_REFERENCE: {
Richard Smith0f538462011-04-12 10:38:03 +00004208 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004209 Error("Incorrect encoding of lvalue reference type");
4210 return QualType();
4211 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004212 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004213 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004214 }
4215
Sebastian Redl539c5062010-08-18 23:57:32 +00004216 case TYPE_RVALUE_REFERENCE: {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004217 if (Record.size() != 1) {
4218 Error("Incorrect encoding of rvalue reference type");
4219 return QualType();
4220 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004221 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004222 return Context.getRValueReferenceType(PointeeType);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004223 }
4224
Sebastian Redl539c5062010-08-18 23:57:32 +00004225 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidisee776bc2010-07-02 11:55:15 +00004226 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004227 Error("Incorrect encoding of member pointer type");
4228 return QualType();
4229 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004230 QualType PointeeType = readType(*Loc.F, Record, Idx);
4231 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor0cdc8322010-12-10 17:03:06 +00004232 if (PointeeType.isNull() || ClassType.isNull())
4233 return QualType();
4234
Douglas Gregor4163aca2011-09-09 21:34:22 +00004235 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004236 }
4237
Sebastian Redl539c5062010-08-18 23:57:32 +00004238 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004239 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004240 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4241 unsigned IndexTypeQuals = Record[2];
4242 unsigned Idx = 3;
4243 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004244 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor04318252009-07-06 15:59:29 +00004245 ASM, IndexTypeQuals);
4246 }
4247
Sebastian Redl539c5062010-08-18 23:57:32 +00004248 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004249 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004250 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4251 unsigned IndexTypeQuals = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004252 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004253 }
4254
Sebastian Redl539c5062010-08-18 23:57:32 +00004255 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004256 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00004257 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4258 unsigned IndexTypeQuals = Record[2];
Sebastian Redl2c373b92010-10-05 15:59:54 +00004259 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4260 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004261 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor04318252009-07-06 15:59:29 +00004262 ASM, IndexTypeQuals,
4263 SourceRange(LBLoc, RBLoc));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004264 }
4265
Sebastian Redl539c5062010-08-18 23:57:32 +00004266 case TYPE_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00004267 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004268 Error("incorrect encoding of vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004269 return QualType();
4270 }
4271
Douglas Gregor903b7e92011-07-22 00:38:23 +00004272 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004273 unsigned NumElements = Record[1];
Bob Wilsonaeb56442010-11-10 21:56:12 +00004274 unsigned VecKind = Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004275 return Context.getVectorType(ElementType, NumElements,
Bob Wilsonaeb56442010-11-10 21:56:12 +00004276 (VectorType::VectorKind)VecKind);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004277 }
4278
Sebastian Redl539c5062010-08-18 23:57:32 +00004279 case TYPE_EXT_VECTOR: {
Chris Lattner37141f42010-06-23 06:00:24 +00004280 if (Record.size() != 3) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004281 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004282 return QualType();
4283 }
4284
Douglas Gregor903b7e92011-07-22 00:38:23 +00004285 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004286 unsigned NumElements = Record[1];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004287 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004288 }
4289
Sebastian Redl539c5062010-08-18 23:57:32 +00004290 case TYPE_FUNCTION_NO_PROTO: {
John McCall31168b02011-06-15 23:02:42 +00004291 if (Record.size() != 6) {
Douglas Gregor6f00bf82009-04-28 21:53:25 +00004292 Error("incorrect encoding of no-proto function type");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004293 return QualType();
4294 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004295 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCall31168b02011-06-15 23:02:42 +00004296 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4297 (CallingConv)Record[4], Record[5]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004298 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004299 }
4300
Sebastian Redl539c5062010-08-18 23:57:32 +00004301 case TYPE_FUNCTION_PROTO: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004302 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalldb40c7f2010-12-14 08:05:40 +00004303
4304 FunctionProtoType::ExtProtoInfo EPI;
4305 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmanc5b20b52011-04-09 08:18:08 +00004306 /*hasregparm*/ Record[2],
4307 /*regparm*/ Record[3],
John McCall31168b02011-06-15 23:02:42 +00004308 static_cast<CallingConv>(Record[4]),
4309 /*produces*/ Record[5]);
John McCalldb40c7f2010-12-14 08:05:40 +00004310
John McCall31168b02011-06-15 23:02:42 +00004311 unsigned Idx = 6;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004312 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004313 SmallVector<QualType, 16> ParamTypes;
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004314 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00004315 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalldb40c7f2010-12-14 08:05:40 +00004316
4317 EPI.Variadic = Record[Idx++];
Richard Smith5e580292012-02-10 09:58:53 +00004318 EPI.HasTrailingReturn = Record[Idx++];
John McCalldb40c7f2010-12-14 08:05:40 +00004319 EPI.TypeQuals = Record[Idx++];
Douglas Gregordb9d6642011-01-26 05:01:58 +00004320 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004321 ExceptionSpecificationType EST =
4322 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4323 EPI.ExceptionSpecType = EST;
Douglas Gregor6a377842012-04-04 00:34:49 +00004324 SmallVector<QualType, 2> Exceptions;
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004325 if (EST == EST_Dynamic) {
4326 EPI.NumExceptions = Record[Idx++];
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004327 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor903b7e92011-07-22 00:38:23 +00004328 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004329 EPI.Exceptions = Exceptions.data();
4330 } else if (EST == EST_ComputedNoexcept) {
4331 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith8b987a92012-04-21 17:47:47 +00004332 } else if (EST == EST_Uninstantiated) {
4333 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4334 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithd3b5c9082012-07-27 04:22:15 +00004335 } else if (EST == EST_Unevaluated) {
4336 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redlfa453cf2011-03-12 11:50:43 +00004337 }
Douglas Gregor4163aca2011-09-09 21:34:22 +00004338 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalldb40c7f2010-12-14 08:05:40 +00004339 EPI);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004340 }
4341
Douglas Gregor7fb09192011-07-21 22:35:25 +00004342 case TYPE_UNRESOLVED_USING: {
4343 unsigned Idx = 0;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004344 return Context.getTypeDeclType(
Douglas Gregor7fb09192011-07-21 22:35:25 +00004345 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4346 }
4347
Sebastian Redl539c5062010-08-18 23:57:32 +00004348 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004349 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004350 Error("incorrect encoding of typedef type");
4351 return QualType();
4352 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004353 unsigned Idx = 0;
4354 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004355 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00004356 if (!Canonical.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004357 Canonical = Context.getCanonicalType(Canonical);
4358 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004359 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004360
Sebastian Redl539c5062010-08-18 23:57:32 +00004361 case TYPE_TYPEOF_EXPR:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004362 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004363
Sebastian Redl539c5062010-08-18 23:57:32 +00004364 case TYPE_TYPEOF: {
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004365 if (Record.size() != 1) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004366 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004367 return QualType();
4368 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004369 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004370 return Context.getTypeOfType(UnderlyingType);
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004371 }
Mike Stump11289f42009-09-09 15:08:12 +00004372
Douglas Gregor81495f32012-02-12 18:42:33 +00004373 case TYPE_DECLTYPE: {
4374 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4375 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4376 }
Anders Carlsson81df7b82009-06-24 19:06:50 +00004377
Alexis Hunte852b102011-05-24 22:41:36 +00004378 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004379 QualType BaseType = readType(*Loc.F, Record, Idx);
4380 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Alexis Hunte852b102011-05-24 22:41:36 +00004381 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor4163aca2011-09-09 21:34:22 +00004382 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Alexis Hunte852b102011-05-24 22:41:36 +00004383 }
4384
Richard Smith30482bc2011-02-20 03:19:35 +00004385 case TYPE_AUTO:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004386 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith30482bc2011-02-20 03:19:35 +00004387
Sebastian Redl539c5062010-08-18 23:57:32 +00004388 case TYPE_RECORD: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004389 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004390 Error("incorrect encoding of record type");
4391 return QualType();
4392 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004393 unsigned Idx = 0;
4394 bool IsDependent = Record[Idx++];
Douglas Gregorf3bccd72012-01-17 19:21:53 +00004395 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4396 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4397 QualType T = Context.getRecordType(RD);
John McCall424cec92011-01-19 06:33:43 +00004398 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004399 return T;
4400 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004401
Sebastian Redl539c5062010-08-18 23:57:32 +00004402 case TYPE_ENUM: {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004403 if (Record.size() != 2) {
Ted Kremenek1ff615c2010-03-18 00:56:54 +00004404 Error("incorrect encoding of enum type");
4405 return QualType();
4406 }
Douglas Gregor7fb09192011-07-21 22:35:25 +00004407 unsigned Idx = 0;
4408 bool IsDependent = Record[Idx++];
4409 QualType T
Douglas Gregor4163aca2011-09-09 21:34:22 +00004410 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCall424cec92011-01-19 06:33:43 +00004411 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004412 return T;
4413 }
Douglas Gregor1daeb692009-04-13 18:14:40 +00004414
John McCall81904512011-01-06 01:58:22 +00004415 case TYPE_ATTRIBUTED: {
4416 if (Record.size() != 3) {
4417 Error("incorrect encoding of attributed type");
4418 return QualType();
4419 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004420 QualType modifiedType = readType(*Loc.F, Record, Idx);
4421 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall81904512011-01-06 01:58:22 +00004422 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004423 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall81904512011-01-06 01:58:22 +00004424 }
4425
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004426 case TYPE_PAREN: {
4427 if (Record.size() != 1) {
4428 Error("incorrect encoding of paren type");
4429 return QualType();
4430 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004431 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004432 return Context.getParenType(InnerType);
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004433 }
4434
Douglas Gregord2fa7662010-12-20 02:24:11 +00004435 case TYPE_PACK_EXPANSION: {
Douglas Gregor17328502011-02-01 15:24:58 +00004436 if (Record.size() != 2) {
Douglas Gregord2fa7662010-12-20 02:24:11 +00004437 Error("incorrect encoding of pack expansion type");
4438 return QualType();
4439 }
Douglas Gregor903b7e92011-07-22 00:38:23 +00004440 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregord2fa7662010-12-20 02:24:11 +00004441 if (Pattern.isNull())
4442 return QualType();
Douglas Gregor0dca5fd2011-01-14 17:04:44 +00004443 llvm::Optional<unsigned> NumExpansions;
4444 if (Record[1])
4445 NumExpansions = Record[1] - 1;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004446 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregord2fa7662010-12-20 02:24:11 +00004447 }
4448
Sebastian Redl539c5062010-08-18 23:57:32 +00004449 case TYPE_ELABORATED: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004450 unsigned Idx = 0;
4451 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004452 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004453 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004454 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCallfcc33b02009-09-05 00:15:47 +00004455 }
4456
Sebastian Redl539c5062010-08-18 23:57:32 +00004457 case TYPE_OBJC_INTERFACE: {
Chris Lattner587cbe12009-04-22 06:45:28 +00004458 unsigned Idx = 0;
Douglas Gregor7fb09192011-07-21 22:35:25 +00004459 ObjCInterfaceDecl *ItfD
4460 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregorf3bccd72012-01-17 19:21:53 +00004461 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCall8b07ec22010-05-15 11:32:37 +00004462 }
4463
Sebastian Redl539c5062010-08-18 23:57:32 +00004464 case TYPE_OBJC_OBJECT: {
John McCall8b07ec22010-05-15 11:32:37 +00004465 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004466 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattner587cbe12009-04-22 06:45:28 +00004467 unsigned NumProtos = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004468 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattner587cbe12009-04-22 06:45:28 +00004469 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor7fb09192011-07-21 22:35:25 +00004470 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00004471 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattner587cbe12009-04-22 06:45:28 +00004472 }
Douglas Gregor85c0fcd2009-04-13 20:46:52 +00004473
Sebastian Redl539c5062010-08-18 23:57:32 +00004474 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattner6e054af2009-04-22 06:40:03 +00004475 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004476 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004477 return Context.getObjCObjectPointerType(Pointee);
Chris Lattner6e054af2009-04-22 06:40:03 +00004478 }
Argyrios Kyrtzidisa7a36df2009-09-29 19:42:55 +00004479
Sebastian Redl539c5062010-08-18 23:57:32 +00004480 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCallcebee162009-10-18 09:09:24 +00004481 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004482 QualType Parm = readType(*Loc.F, Record, Idx);
4483 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCallcebee162009-10-18 09:09:24 +00004484 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00004485 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCallcebee162009-10-18 09:09:24 +00004486 Replacement);
4487 }
John McCalle78aac42010-03-10 03:28:59 +00004488
Douglas Gregorada4b792011-01-14 02:55:32 +00004489 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4490 unsigned Idx = 0;
Douglas Gregor903b7e92011-07-22 00:38:23 +00004491 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorada4b792011-01-14 02:55:32 +00004492 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004493 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorada4b792011-01-14 02:55:32 +00004494 cast<TemplateTypeParmType>(Parm),
4495 ArgPack);
4496 }
4497
Sebastian Redl539c5062010-08-18 23:57:32 +00004498 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00004499 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004500 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00004501 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004502 // for AST reading, too much interdependencies.
Argyrios Kyrtzidisdab33c52010-07-02 11:55:20 +00004503 return
Douglas Gregor4163aca2011-09-09 21:34:22 +00004504 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCalle78aac42010-03-10 03:28:59 +00004505 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004506
Sebastian Redl539c5062010-08-18 23:57:32 +00004507 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004508 unsigned Idx = 0;
4509 unsigned Depth = Record[Idx++];
4510 unsigned Index = Record[Idx++];
4511 bool Pack = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004512 TemplateTypeParmDecl *D
4513 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00004514 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004515 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004516
Sebastian Redl539c5062010-08-18 23:57:32 +00004517 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00004518 unsigned Idx = 0;
4519 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004520 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00004521 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004522 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregorf86c9392010-10-26 00:51:02 +00004523 if (!Canon.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004524 Canon = Context.getCanonicalType(Canon);
4525 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00004526 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004527
Sebastian Redl539c5062010-08-18 23:57:32 +00004528 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004529 unsigned Idx = 0;
4530 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00004531 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregora3e41532011-07-28 20:55:49 +00004532 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004533 unsigned NumArgs = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004534 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004535 Args.reserve(NumArgs);
4536 while (NumArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004537 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00004538 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +00004539 Args.size(), Args.data());
4540 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004541
Sebastian Redl539c5062010-08-18 23:57:32 +00004542 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004543 unsigned Idx = 0;
4544
4545 // ArrayType
Douglas Gregor903b7e92011-07-22 00:38:23 +00004546 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004547 ArrayType::ArraySizeModifier ASM
4548 = (ArrayType::ArraySizeModifier)Record[Idx++];
4549 unsigned IndexTypeQuals = Record[Idx++];
4550
4551 // DependentSizedArrayType
Sebastian Redl2c373b92010-10-05 15:59:54 +00004552 Expr *NumElts = ReadExpr(*Loc.F);
4553 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004554
Douglas Gregor4163aca2011-09-09 21:34:22 +00004555 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +00004556 IndexTypeQuals, Brackets);
4557 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004558
Sebastian Redl539c5062010-08-18 23:57:32 +00004559 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004560 unsigned Idx = 0;
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004561 bool IsDependent = Record[Idx++];
Douglas Gregor5590be02011-01-15 06:45:20 +00004562 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004563 SmallVector<TemplateArgument, 8> Args;
Sebastian Redl2c373b92010-10-05 15:59:54 +00004564 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00004565 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004566 QualType T;
Richard Smith3f1b5d02011-05-05 21:57:07 +00004567 if (Underlying.isNull())
Douglas Gregor4163aca2011-09-09 21:34:22 +00004568 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004569 Args.size());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +00004570 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00004571 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3f1b5d02011-05-05 21:57:07 +00004572 Args.size(), Underlying);
John McCall424cec92011-01-19 06:33:43 +00004573 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +00004574 return T;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00004575 }
Eli Friedman0dfb8892011-10-06 23:00:33 +00004576
4577 case TYPE_ATOMIC: {
4578 if (Record.size() != 1) {
4579 Error("Incorrect encoding of atomic type");
4580 return QualType();
4581 }
4582 QualType ValueType = readType(*Loc.F, Record, Idx);
4583 return Context.getAtomicType(ValueType);
4584 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004585 }
David Blaikie8a40f702012-01-17 06:56:22 +00004586 llvm_unreachable("Invalid TypeCode!");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004587}
4588
Sebastian Redl2c373b92010-10-05 15:59:54 +00004589class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redl2c499f62010-08-18 23:56:43 +00004590 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00004591 ModuleFile &F;
Sebastian Redl2c499f62010-08-18 23:56:43 +00004592 const ASTReader::RecordData &Record;
John McCall8f115c62009-10-16 21:56:05 +00004593 unsigned &Idx;
4594
Sebastian Redl2c373b92010-10-05 15:59:54 +00004595 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4596 unsigned &I) {
4597 return Reader.ReadSourceLocation(F, R, I);
4598 }
4599
Douglas Gregor7fb09192011-07-21 22:35:25 +00004600 template<typename T>
4601 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4602 return Reader.ReadDeclAs<T>(F, Record, Idx);
4603 }
4604
John McCall8f115c62009-10-16 21:56:05 +00004605public:
Douglas Gregorde3ef502011-11-30 23:21:26 +00004606 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redl2c499f62010-08-18 23:56:43 +00004607 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00004608 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004609 { }
John McCall8f115c62009-10-16 21:56:05 +00004610
John McCall17001972009-10-18 01:05:36 +00004611 // We want compile-time assurance that we've enumerated all of
4612 // these, so unfortunately we have to declare them first, then
4613 // define them out-of-line.
4614#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +00004615#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +00004616 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +00004617#include "clang/AST/TypeLocNodes.def"
4618
John McCall17001972009-10-18 01:05:36 +00004619 void VisitFunctionTypeLoc(FunctionTypeLoc);
4620 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCall8f115c62009-10-16 21:56:05 +00004621};
4622
John McCall17001972009-10-18 01:05:36 +00004623void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCall8f115c62009-10-16 21:56:05 +00004624 // nothing to do
4625}
John McCall17001972009-10-18 01:05:36 +00004626void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004627 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorc9b7a592010-01-18 18:04:31 +00004628 if (TL.needsExtraLocalData()) {
4629 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4630 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4631 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4632 TL.setModeAttr(Record[Idx++]);
4633 }
John McCall8f115c62009-10-16 21:56:05 +00004634}
John McCall17001972009-10-18 01:05:36 +00004635void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004636 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004637}
John McCall17001972009-10-18 01:05:36 +00004638void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004639 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004640}
John McCall17001972009-10-18 01:05:36 +00004641void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004642 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004643}
John McCall17001972009-10-18 01:05:36 +00004644void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004645 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004646}
John McCall17001972009-10-18 01:05:36 +00004647void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004648 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004649}
John McCall17001972009-10-18 01:05:36 +00004650void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004651 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara509357842011-03-05 14:42:21 +00004652 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004653}
John McCall17001972009-10-18 01:05:36 +00004654void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004655 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4656 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004657 if (Record[Idx++])
Sebastian Redl2c373b92010-10-05 15:59:54 +00004658 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004659 else
John McCall17001972009-10-18 01:05:36 +00004660 TL.setSizeExpr(0);
4661}
4662void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4663 VisitArrayTypeLoc(TL);
4664}
4665void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4666 VisitArrayTypeLoc(TL);
4667}
4668void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4669 VisitArrayTypeLoc(TL);
4670}
4671void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4672 DependentSizedArrayTypeLoc TL) {
4673 VisitArrayTypeLoc(TL);
4674}
4675void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4676 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004677 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004678}
4679void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004680 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004681}
4682void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004683 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004684}
4685void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004686 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004687 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4688 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004689 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004690 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00004691 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004692 }
4693}
4694void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4695 VisitFunctionTypeLoc(TL);
4696}
4697void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4698 VisitFunctionTypeLoc(TL);
4699}
John McCallb96ec562009-12-04 22:46:56 +00004700void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004701 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallb96ec562009-12-04 22:46:56 +00004702}
John McCall17001972009-10-18 01:05:36 +00004703void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004704 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004705}
4706void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004707 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4708 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4709 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004710}
4711void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004712 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4713 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4714 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4715 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004716}
4717void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004718 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004719}
Alexis Hunte852b102011-05-24 22:41:36 +00004720void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4721 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4722 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4723 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4724 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4725}
Richard Smith30482bc2011-02-20 03:19:35 +00004726void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4727 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4728}
John McCall17001972009-10-18 01:05:36 +00004729void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004730 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004731}
4732void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004733 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004734}
John McCall81904512011-01-06 01:58:22 +00004735void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4736 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4737 if (TL.hasAttrOperand()) {
4738 SourceRange range;
4739 range.setBegin(ReadSourceLocation(Record, Idx));
4740 range.setEnd(ReadSourceLocation(Record, Idx));
4741 TL.setAttrOperandParensRange(range);
4742 }
4743 if (TL.hasAttrExprOperand()) {
4744 if (Record[Idx++])
4745 TL.setAttrExprOperand(Reader.ReadExpr(F));
4746 else
4747 TL.setAttrExprOperand(0);
4748 } else if (TL.hasAttrEnumOperand())
4749 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4750}
John McCall17001972009-10-18 01:05:36 +00004751void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004752 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004753}
John McCallcebee162009-10-18 09:09:24 +00004754void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4755 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004756 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallcebee162009-10-18 09:09:24 +00004757}
Douglas Gregorada4b792011-01-14 02:55:32 +00004758void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4759 SubstTemplateTypeParmPackTypeLoc TL) {
4760 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4761}
John McCall17001972009-10-18 01:05:36 +00004762void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4763 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004764 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004765 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4766 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4767 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall0ad16662009-10-29 08:12:44 +00004768 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4769 TL.setArgLocInfo(i,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004770 Reader.GetTemplateArgumentLocInfo(F,
4771 TL.getTypePtr()->getArg(i).getKind(),
4772 Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004773}
Abramo Bagnara924a8f32010-12-10 16:29:40 +00004774void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4775 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4776 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4777}
Abramo Bagnara6150c882010-05-11 21:36:43 +00004778void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +00004779 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor844cb502011-03-01 18:12:44 +00004780 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004781}
John McCalle78aac42010-03-10 03:28:59 +00004782void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004783 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalle78aac42010-03-10 03:28:59 +00004784}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00004785void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +00004786 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor3d0da5f2011-03-01 01:34:45 +00004787 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004788 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004789}
John McCallc392f372010-06-11 00:33:02 +00004790void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4791 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004792 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregora7a795b2011-03-01 20:11:18 +00004793 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnarae0a70b22012-02-06 22:45:07 +00004794 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara48c05be2012-02-06 14:41:24 +00004795 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redl2c373b92010-10-05 15:59:54 +00004796 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4797 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00004798 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4799 TL.setArgLocInfo(I,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004800 Reader.GetTemplateArgumentLocInfo(F,
4801 TL.getTypePtr()->getArg(I).getKind(),
4802 Record, Idx));
John McCallc392f372010-06-11 00:33:02 +00004803}
Douglas Gregord2fa7662010-12-20 02:24:11 +00004804void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4805 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4806}
John McCall17001972009-10-18 01:05:36 +00004807void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004808 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall8b07ec22010-05-15 11:32:37 +00004809}
4810void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4811 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004812 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4813 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall17001972009-10-18 01:05:36 +00004814 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redl2c373b92010-10-05 15:59:54 +00004815 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCall8f115c62009-10-16 21:56:05 +00004816}
John McCallfc93cf92009-10-22 22:37:11 +00004817void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004818 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCallfc93cf92009-10-22 22:37:11 +00004819}
Eli Friedman0dfb8892011-10-06 23:00:33 +00004820void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4821 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4822 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4823 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4824}
John McCall8f115c62009-10-16 21:56:05 +00004825
Douglas Gregorde3ef502011-11-30 23:21:26 +00004826TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004827 const RecordData &Record,
John McCall8f115c62009-10-16 21:56:05 +00004828 unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004829 QualType InfoTy = readType(F, Record, Idx);
John McCall8f115c62009-10-16 21:56:05 +00004830 if (InfoTy.isNull())
4831 return 0;
4832
Douglas Gregor4163aca2011-09-09 21:34:22 +00004833 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004834 TypeLocReader TLR(*this, F, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +00004835 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCall8f115c62009-10-16 21:56:05 +00004836 TLR.Visit(TL);
John McCallbcd03502009-12-07 02:54:59 +00004837 return TInfo;
John McCall8f115c62009-10-16 21:56:05 +00004838}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004839
Sebastian Redl539c5062010-08-18 23:57:32 +00004840QualType ASTReader::GetType(TypeID ID) {
John McCall8ccfcb52009-09-24 19:53:00 +00004841 unsigned FastQuals = ID & Qualifiers::FastMask;
4842 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004843
Sebastian Redl539c5062010-08-18 23:57:32 +00004844 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004845 QualType T;
Sebastian Redl539c5062010-08-18 23:57:32 +00004846 switch ((PredefinedTypeIDs)Index) {
4847 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor4163aca2011-09-09 21:34:22 +00004848 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4849 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004850
Sebastian Redl539c5062010-08-18 23:57:32 +00004851 case PREDEF_TYPE_CHAR_U_ID:
4852 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004853 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor4163aca2011-09-09 21:34:22 +00004854 T = Context.CharTy;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004855 break;
4856
Douglas Gregor4163aca2011-09-09 21:34:22 +00004857 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4858 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4859 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4860 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4861 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4862 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4863 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4864 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4865 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4866 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4867 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4868 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4869 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004870 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004871 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4872 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4873 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4874 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4875 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall526ab472011-10-25 17:37:35 +00004876 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor4163aca2011-09-09 21:34:22 +00004877 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4878 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4879 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4880 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4881 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4882 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4883 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4884 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4885 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregoreda8e122011-08-09 15:13:55 +00004886
4887 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor4163aca2011-09-09 21:34:22 +00004888 T = Context.getAutoRRefDeductType();
Douglas Gregoreda8e122011-08-09 15:13:55 +00004889 break;
John McCall8a6b59a2011-10-17 18:09:15 +00004890
4891 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4892 T = Context.ARCUnbridgedCastTy;
4893 break;
4894
Meador Ingecfb60902012-07-01 15:57:25 +00004895 case PREDEF_TYPE_VA_LIST_TAG:
4896 T = Context.getVaListTagType();
4897 break;
Eli Friedman34866c72012-08-31 00:14:07 +00004898
4899 case PREDEF_TYPE_BUILTIN_FN:
4900 T = Context.BuiltinFnTy;
4901 break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004902 }
4903
4904 assert(!T.isNull() && "Unknown predefined type");
John McCall8ccfcb52009-09-24 19:53:00 +00004905 return T.withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004906 }
4907
Sebastian Redl539c5062010-08-18 23:57:32 +00004908 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redl837a6cb2010-07-20 22:37:49 +00004909 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl409183f2010-07-14 20:26:45 +00004910 if (TypesLoaded[Index].isNull()) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004911 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004912 if (TypesLoaded[Index].isNull())
4913 return QualType();
4914
Sebastian Redld44cd6a2010-08-18 23:57:06 +00004915 TypesLoaded[Index]->setFromAST();
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004916 if (DeserializationListener)
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004917 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004918 TypesLoaded[Index]);
Sebastian Redl409183f2010-07-14 20:26:45 +00004919 }
Mike Stump11289f42009-09-09 15:08:12 +00004920
John McCall8ccfcb52009-09-24 19:53:00 +00004921 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004922}
4923
Douglas Gregorde3ef502011-11-30 23:21:26 +00004924QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00004925 return GetType(getGlobalTypeID(F, LocalID));
4926}
4927
4928serialization::TypeID
Douglas Gregorde3ef502011-11-30 23:21:26 +00004929ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregor5204bde2011-08-02 16:26:37 +00004930 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4931 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4932
4933 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4934 return LocalID;
4935
4936 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4937 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4938 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4939
4940 unsigned GlobalIndex = LocalIndex + I->second;
4941 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4942}
4943
John McCall0ad16662009-10-29 08:12:44 +00004944TemplateArgumentLocInfo
Douglas Gregorde3ef502011-11-30 23:21:26 +00004945ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00004946 TemplateArgument::ArgKind Kind,
John McCall0ad16662009-10-29 08:12:44 +00004947 const RecordData &Record,
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004948 unsigned &Index) {
John McCall0ad16662009-10-29 08:12:44 +00004949 switch (Kind) {
4950 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004951 return ReadExpr(F);
John McCall0ad16662009-10-29 08:12:44 +00004952 case TemplateArgument::Type:
Sebastian Redl2c373b92010-10-05 15:59:54 +00004953 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004954 case TemplateArgument::Template: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004955 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4956 Index);
Sebastian Redl2c373b92010-10-05 15:59:54 +00004957 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004958 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004959 SourceLocation());
4960 }
4961 case TemplateArgument::TemplateExpansion: {
Douglas Gregor9d802122011-03-02 17:09:35 +00004962 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4963 Index);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004964 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004965 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregor9d802122011-03-02 17:09:35 +00004966 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregoreb29d182011-01-05 17:40:24 +00004967 EllipsisLoc);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004968 }
John McCall0ad16662009-10-29 08:12:44 +00004969 case TemplateArgument::Null:
4970 case TemplateArgument::Integral:
4971 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004972 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004973 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004974 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004975 return TemplateArgumentLocInfo();
4976 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00004977 llvm_unreachable("unexpected template argument loc");
John McCall0ad16662009-10-29 08:12:44 +00004978}
4979
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004980TemplateArgumentLoc
Douglas Gregorde3ef502011-11-30 23:21:26 +00004981ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00004982 const RecordData &Record, unsigned &Index) {
Sebastian Redl2c373b92010-10-05 15:59:54 +00004983 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004984
4985 if (Arg.getKind() == TemplateArgument::Expression) {
4986 if (Record[Index++]) // bool InfoHasSameExpr.
4987 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4988 }
Sebastian Redl2c373b92010-10-05 15:59:54 +00004989 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00004990 Record, Index));
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004991}
4992
Sebastian Redl2c499f62010-08-18 23:56:43 +00004993Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall75b960e2010-06-01 09:23:16 +00004994 return GetDecl(ID);
4995}
4996
Douglas Gregorde3ef502011-11-30 23:21:26 +00004997uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregorc27b2872011-08-04 00:01:48 +00004998 unsigned &Idx){
4999 if (Idx >= Record.size())
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005000 return 0;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005001
Douglas Gregorc27b2872011-08-04 00:01:48 +00005002 unsigned LocalID = Record[Idx++];
5003 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005004}
5005
5006CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregord32f0352011-07-22 06:10:01 +00005007 RecordLocation Loc = getLocalBitOffset(Offset);
5008 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005009 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregord32f0352011-07-22 06:10:01 +00005010 Cursor.JumpToBit(Loc.Offset);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005011 ReadingKindTracker ReadingKind(Read_Decl, *this);
5012 RecordData Record;
5013 unsigned Code = Cursor.ReadCode();
5014 unsigned RecCode = Cursor.ReadRecord(Code, Record);
5015 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5016 Error("Malformed AST file: missing C++ base specifiers");
5017 return 0;
5018 }
5019
5020 unsigned Idx = 0;
5021 unsigned NumBases = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00005022 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005023 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5024 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregord32f0352011-07-22 06:10:01 +00005025 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005026 return Bases;
5027}
5028
Douglas Gregor7fb09192011-07-21 22:35:25 +00005029serialization::DeclID
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00005030ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005031 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregorf7180622011-08-03 15:48:04 +00005032 return LocalID;
5033
5034 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005035 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregorf7180622011-08-03 15:48:04 +00005036 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5037
5038 return LocalID + I->second;
Douglas Gregor7fb09192011-07-21 22:35:25 +00005039}
5040
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005041bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregorde3ef502011-11-30 23:21:26 +00005042 ModuleFile &M) const {
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005043 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5044 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5045 return &M == I->second;
5046}
5047
Douglas Gregor404cdde2012-01-27 01:47:08 +00005048ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5049 if (!D->isFromASTFile())
5050 return 0;
5051 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5052 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5053 return I->second;
5054}
5055
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005056SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5057 if (ID < NUM_PREDEF_DECL_IDS)
5058 return SourceLocation();
5059
5060 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5061
5062 if (Index > DeclsLoaded.size()) {
5063 Error("declaration ID out-of-range for AST file");
5064 return SourceLocation();
5065 }
5066
5067 if (Decl *D = DeclsLoaded[Index])
5068 return D->getLocation();
5069
5070 unsigned RawLocation = 0;
5071 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5072 return ReadSourceLocation(*Rec.F, RawLocation);
5073}
5074
Sebastian Redl539c5062010-08-18 23:57:32 +00005075Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005076 if (ID < NUM_PREDEF_DECL_IDS) {
5077 switch ((PredefinedDeclIDs)ID) {
Douglas Gregordab42432011-08-12 00:15:20 +00005078 case PREDEF_DECL_NULL_ID:
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005079 return 0;
Douglas Gregordab42432011-08-12 00:15:20 +00005080
5081 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005082 return Context.getTranslationUnitDecl();
Douglas Gregor3ea72692011-08-12 05:46:01 +00005083
5084 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005085 return Context.getObjCIdDecl();
Douglas Gregor0a586182011-08-12 05:59:41 +00005086
Douglas Gregor52e02802011-08-12 06:17:30 +00005087 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005088 return Context.getObjCSelDecl();
Douglas Gregor52e02802011-08-12 06:17:30 +00005089
Douglas Gregor0a586182011-08-12 05:59:41 +00005090 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005091 return Context.getObjCClassDecl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00005092
Douglas Gregord53ae832012-01-17 18:09:05 +00005093 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5094 return Context.getObjCProtocolDecl();
5095
Douglas Gregor801c99d2011-08-12 06:49:56 +00005096 case PREDEF_DECL_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005097 return Context.getInt128Decl();
Douglas Gregor801c99d2011-08-12 06:49:56 +00005098
5099 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005100 return Context.getUInt128Decl();
Douglas Gregorbab8a962011-09-08 01:46:34 +00005101
5102 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor4163aca2011-09-09 21:34:22 +00005103 return Context.getObjCInstanceTypeDecl();
Meador Inge5d3fb222012-06-16 03:34:49 +00005104
5105 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5106 return Context.getBuiltinVaListDecl();
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005107 }
Douglas Gregor6f8912e2011-08-03 16:05:40 +00005108 }
5109
Douglas Gregordab42432011-08-12 00:15:20 +00005110 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5111
Richard Smithce3ad9a2011-12-20 04:39:57 +00005112 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005113 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005114 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005115 return 0;
Douglas Gregor745ed142009-04-25 18:35:21 +00005116 }
Douglas Gregordab42432011-08-12 00:15:20 +00005117
Douglas Gregor81252352011-12-16 22:37:11 +00005118 if (!DeclsLoaded[Index]) {
Douglas Gregorf7180622011-08-03 15:48:04 +00005119 ReadDeclRecord(ID);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005120 if (DeserializationListener)
5121 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5122 }
Douglas Gregor745ed142009-04-25 18:35:21 +00005123
5124 return DeclsLoaded[Index];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005125}
5126
Douglas Gregor05f10352011-12-17 23:38:30 +00005127DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5128 DeclID GlobalID) {
5129 if (GlobalID < NUM_PREDEF_DECL_IDS)
5130 return GlobalID;
5131
5132 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5133 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5134 ModuleFile *Owner = I->second;
5135
5136 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5137 = M.GlobalToLocalDeclIDs.find(Owner);
5138 if (Pos == M.GlobalToLocalDeclIDs.end())
5139 return 0;
5140
5141 return GlobalID - Owner->BaseDeclID + Pos->second;
5142}
5143
Douglas Gregorde3ef502011-11-30 23:21:26 +00005144serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00005145 const RecordData &Record,
5146 unsigned &Idx) {
5147 if (Idx >= Record.size()) {
5148 Error("Corrupted AST file");
5149 return 0;
5150 }
5151
5152 return getGlobalDeclID(F, Record[Idx++]);
5153}
5154
Chris Lattner9c28af02009-04-27 05:46:25 +00005155/// \brief Resolve the offset of a statement into a statement.
5156///
5157/// This operation will read a new statement from the external
5158/// source each time it is called, and is meant to be used via a
5159/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redl2c499f62010-08-18 23:56:43 +00005160Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00005161 // Switch case IDs are per Decl.
5162 ClearSwitchCaseIDs();
5163
Sebastian Redl5c415f32010-07-22 17:01:13 +00005164 // Offset here is a global offset across the entire chain.
Douglas Gregord32f0352011-07-22 06:10:01 +00005165 RecordLocation Loc = getLocalBitOffset(Offset);
5166 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5167 return ReadStmtFromStream(*Loc.F);
Douglas Gregor3c3aa612009-04-18 00:07:54 +00005168}
5169
Douglas Gregor1257f972011-08-24 21:27:34 +00005170namespace {
5171 class FindExternalLexicalDeclsVisitor {
5172 ASTReader &Reader;
5173 const DeclContext *DC;
5174 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00005175
Douglas Gregor1257f972011-08-24 21:27:34 +00005176 SmallVectorImpl<Decl*> &Decls;
5177 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5178
5179 public:
5180 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5181 bool (*isKindWeWant)(Decl::Kind),
5182 SmallVectorImpl<Decl*> &Decls)
5183 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5184 {
5185 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5186 PredefsVisited[I] = false;
5187 }
5188
Douglas Gregorde3ef502011-11-30 23:21:26 +00005189 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor1257f972011-08-24 21:27:34 +00005190 if (Preorder)
5191 return false;
5192
5193 FindExternalLexicalDeclsVisitor *This
5194 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5195
Douglas Gregorde3ef502011-11-30 23:21:26 +00005196 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor1257f972011-08-24 21:27:34 +00005197 = M.DeclContextInfos.find(This->DC);
5198 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5199 return false;
5200
5201 // Load all of the declaration IDs
5202 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5203 *IDE = ID + Info->second.NumLexicalDecls;
5204 ID != IDE; ++ID) {
5205 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5206 continue;
5207
5208 // Don't add predefined declarations to the lexical context more
5209 // than once.
5210 if (ID->second < NUM_PREDEF_DECL_IDS) {
5211 if (This->PredefsVisited[ID->second])
5212 continue;
5213
5214 This->PredefsVisited[ID->second] = true;
5215 }
5216
Douglas Gregor4e4c83e2011-08-26 22:04:51 +00005217 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5218 if (!This->DC->isDeclInLexicalTraversal(D))
5219 This->Decls.push_back(D);
5220 }
Douglas Gregor1257f972011-08-24 21:27:34 +00005221 }
5222
5223 return false;
5224 }
5225 };
5226}
5227
Douglas Gregor3d0adb32011-07-15 21:46:17 +00005228ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00005229 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005230 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor94619c82011-08-24 19:03:07 +00005231 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor1257f972011-08-24 21:27:34 +00005232 // least. Walk all of the modules in the order they were loaded.
5233 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5234 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00005235 ++NumLexicalDeclContextsRead;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00005236 return ELR_Success;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005237}
5238
Douglas Gregor94619c82011-08-24 19:03:07 +00005239namespace {
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005240
5241class DeclIDComp {
5242 ASTReader &Reader;
Douglas Gregorde3ef502011-11-30 23:21:26 +00005243 ModuleFile &Mod;
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005244
5245public:
Douglas Gregorde3ef502011-11-30 23:21:26 +00005246 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005247
5248 bool operator()(LocalDeclID L, LocalDeclID R) const {
5249 SourceLocation LHS = getLocation(L);
5250 SourceLocation RHS = getLocation(R);
5251 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5252 }
5253
5254 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5255 SourceLocation RHS = getLocation(R);
5256 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5257 }
5258
5259 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5260 SourceLocation LHS = getLocation(L);
5261 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5262 }
5263
5264 SourceLocation getLocation(LocalDeclID ID) const {
5265 return Reader.getSourceManager().getFileLoc(
5266 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5267 }
5268};
5269
5270}
5271
5272void ASTReader::FindFileRegionDecls(FileID File,
5273 unsigned Offset, unsigned Length,
5274 SmallVectorImpl<Decl *> &Decls) {
5275 SourceManager &SM = getSourceManager();
5276
5277 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5278 if (I == FileDeclIDs.end())
5279 return;
5280
5281 FileDeclsInfo &DInfo = I->second;
5282 if (DInfo.Decls.empty())
5283 return;
5284
5285 SourceLocation
5286 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5287 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5288
5289 DeclIDComp DIDComp(*this, *DInfo.Mod);
5290 ArrayRef<serialization::LocalDeclID>::iterator
5291 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5292 BeginLoc, DIDComp);
5293 if (BeginIt != DInfo.Decls.begin())
5294 --BeginIt;
5295
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00005296 // If we are pointing at a top-level decl inside an objc container, we need
5297 // to backtrack until we find it otherwise we will fail to report that the
5298 // region overlaps with an objc container.
5299 while (BeginIt != DInfo.Decls.begin() &&
5300 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5301 ->isTopLevelDeclInObjCContainer())
5302 --BeginIt;
5303
Argyrios Kyrtzidise9681522011-11-03 02:20:32 +00005304 ArrayRef<serialization::LocalDeclID>::iterator
5305 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5306 EndLoc, DIDComp);
5307 if (EndIt != DInfo.Decls.end())
5308 ++EndIt;
5309
5310 for (ArrayRef<serialization::LocalDeclID>::iterator
5311 DIt = BeginIt; DIt != EndIt; ++DIt)
5312 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5313}
5314
5315namespace {
Douglas Gregorde3ef502011-11-30 23:21:26 +00005316 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor94619c82011-08-24 19:03:07 +00005317 /// declaration context.
5318 class DeclContextNameLookupVisitor {
5319 ASTReader &Reader;
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005320 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor94619c82011-08-24 19:03:07 +00005321 DeclarationName Name;
5322 SmallVectorImpl<NamedDecl *> &Decls;
5323
5324 public:
5325 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005326 SmallVectorImpl<const DeclContext *> &Contexts,
5327 DeclarationName Name,
Douglas Gregor94619c82011-08-24 19:03:07 +00005328 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005329 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor94619c82011-08-24 19:03:07 +00005330
Douglas Gregorde3ef502011-11-30 23:21:26 +00005331 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor94619c82011-08-24 19:03:07 +00005332 DeclContextNameLookupVisitor *This
5333 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5334
5335 // Check whether we have any visible declaration information for
5336 // this context in this module.
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005337 ModuleFile::DeclContextInfosMap::iterator Info;
5338 bool FoundInfo = false;
5339 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5340 Info = M.DeclContextInfos.find(This->Contexts[I]);
5341 if (Info != M.DeclContextInfos.end() &&
5342 Info->second.NameLookupTableData) {
5343 FoundInfo = true;
5344 break;
5345 }
5346 }
Douglas Gregor94619c82011-08-24 19:03:07 +00005347
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005348 if (!FoundInfo)
5349 return false;
5350
Douglas Gregor94619c82011-08-24 19:03:07 +00005351 // Look for this name within this module.
5352 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00005353 Info->second.NameLookupTableData;
Douglas Gregor94619c82011-08-24 19:03:07 +00005354 ASTDeclContextNameLookupTable::iterator Pos
5355 = LookupTable->find(This->Name);
5356 if (Pos == LookupTable->end())
5357 return false;
5358
5359 bool FoundAnything = false;
5360 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5361 for (; Data.first != Data.second; ++Data.first) {
5362 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5363 if (!ND)
5364 continue;
5365
5366 if (ND->getDeclName() != This->Name) {
Axel Naumanna8243e92012-10-01 09:51:27 +00005367 // A name might be null because the decl's redeclarable part is
5368 // currently read before reading its name. The lookup is triggered by
5369 // building that decl (likely indirectly), and so it is later in the
5370 // sense of "already existing" and can be ignored here.
Douglas Gregor94619c82011-08-24 19:03:07 +00005371 continue;
5372 }
5373
5374 // Record this declaration.
5375 FoundAnything = true;
5376 This->Decls.push_back(ND);
5377 }
5378
5379 return FoundAnything;
5380 }
5381 };
5382}
5383
John McCall75b960e2010-06-01 09:23:16 +00005384DeclContext::lookup_result
Sebastian Redl2c499f62010-08-18 23:56:43 +00005385ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall75b960e2010-06-01 09:23:16 +00005386 DeclarationName Name) {
Mike Stump11289f42009-09-09 15:08:12 +00005387 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005388 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00005389 if (!Name)
5390 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5391 DeclContext::lookup_iterator(0));
Ted Kremenek1ff615c2010-03-18 00:56:54 +00005392
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005393 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorcfe7dc62012-01-09 17:30:44 +00005394
5395 // Compute the declaration contexts we need to look into. Multiple such
5396 // declaration contexts occur when two declaration contexts from disjoint
5397 // modules get merged, e.g., when two namespaces with the same name are
5398 // independently defined in separate modules.
5399 SmallVector<const DeclContext *, 2> Contexts;
5400 Contexts.push_back(DC);
5401
5402 if (DC->isNamespace()) {
5403 MergedDeclsMap::iterator Merged
5404 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5405 if (Merged != MergedDecls.end()) {
5406 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5407 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5408 }
5409 }
5410
5411 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor94619c82011-08-24 19:03:07 +00005412 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00005413 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00005414 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall75b960e2010-06-01 09:23:16 +00005415 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005416}
5417
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005418namespace {
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005419 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005420 /// declaration context.
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005421 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005422 ASTReader &Reader;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005423 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005424 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005425
5426 public:
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005427 DeclContextAllNamesVisitor(ASTReader &Reader,
5428 SmallVectorImpl<const DeclContext *> &Contexts,
5429 llvm::DenseMap<DeclarationName,
5430 SmallVector<NamedDecl *, 8> > &Decls)
5431 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005432
5433 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005434 DeclContextAllNamesVisitor *This
5435 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005436
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005437 // Check whether we have any visible declaration information for
5438 // this context in this module.
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005439 ModuleFile::DeclContextInfosMap::iterator Info;
5440 bool FoundInfo = false;
5441 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5442 Info = M.DeclContextInfos.find(This->Contexts[I]);
5443 if (Info != M.DeclContextInfos.end() &&
5444 Info->second.NameLookupTableData) {
5445 FoundInfo = true;
5446 break;
5447 }
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005448 }
5449
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005450 if (!FoundInfo)
5451 return false;
5452
5453 ASTDeclContextNameLookupTable *LookupTable =
5454 Info->second.NameLookupTableData;
5455 bool FoundAnything = false;
5456 for (ASTDeclContextNameLookupTable::data_iterator
5457 I = LookupTable->data_begin(), E = LookupTable->data_end();
5458 I != E; ++I) {
5459 ASTDeclContextNameLookupTrait::data_type Data = *I;
5460 for (; Data.first != Data.second; ++Data.first) {
5461 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5462 *Data.first);
5463 if (!ND)
5464 continue;
5465
5466 // Record this declaration.
5467 FoundAnything = true;
5468 This->Decls[ND->getDeclName()].push_back(ND);
5469 }
5470 }
5471
5472 return FoundAnything;
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005473 }
5474 };
5475}
5476
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005477void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005478 if (!DC->hasExternalVisibleStorage())
5479 return;
Nick Lewycky2bd0ab22012-04-16 02:51:46 +00005480 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5481
5482 // Compute the declaration contexts we need to look into. Multiple such
5483 // declaration contexts occur when two declaration contexts from disjoint
5484 // modules get merged, e.g., when two namespaces with the same name are
5485 // independently defined in separate modules.
5486 SmallVector<const DeclContext *, 2> Contexts;
5487 Contexts.push_back(DC);
5488
5489 if (DC->isNamespace()) {
5490 MergedDeclsMap::iterator Merged
5491 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5492 if (Merged != MergedDecls.end()) {
5493 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5494 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5495 }
5496 }
5497
5498 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5499 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5500 ++NumVisibleDeclContextsRead;
5501
5502 for (llvm::DenseMap<DeclarationName,
5503 llvm::SmallVector<NamedDecl*, 8> >::iterator
5504 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5505 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5506 }
Argyrios Kyrtzidis0334d332012-04-26 18:34:14 +00005507 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidisbf6c3392012-03-22 16:08:04 +00005508}
5509
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005510/// \brief Under non-PCH compilation the consumer receives the objc methods
5511/// before receiving the implementation, and codegen depends on this.
5512/// We simulate this by deserializing and passing to consumer the methods of the
5513/// implementation before passing the deserialized implementation decl.
5514static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5515 ASTConsumer *Consumer) {
5516 assert(ImplD && Consumer);
5517
5518 for (ObjCImplDecl::method_iterator
5519 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie40ed2972012-06-06 20:45:41 +00005520 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005521
5522 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5523}
5524
Sebastian Redl2c499f62010-08-18 23:56:43 +00005525void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005526 assert(Consumer);
5527 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005528 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005529 InterestingDecls.pop_front();
Argyrios Kyrtzidisa98e8612011-09-13 21:35:00 +00005530
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00005531 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005532 }
5533}
5534
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00005535void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5536 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5537 PassObjCImplDeclToConsumer(ImplD, Consumer);
5538 else
5539 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5540}
5541
Sebastian Redl2c499f62010-08-18 23:56:43 +00005542void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregorb985eeb2009-04-22 19:09:20 +00005543 this->Consumer = Consumer;
5544
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005545 if (!Consumer)
5546 return;
5547
5548 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005549 // Force deserialization of this decl, which will cause it to be queued for
5550 // passing to the consumer.
Daniel Dunbar865c2a72009-09-17 03:06:44 +00005551 GetDecl(ExternalDefinitions[I]);
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005552 }
Douglas Gregor6137d322011-09-15 18:47:32 +00005553 ExternalDefinitions.clear();
Douglas Gregorf005eac2009-04-25 00:41:30 +00005554
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00005555 PassInterestingDeclsToConsumer();
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00005556}
5557
Sebastian Redl2c499f62010-08-18 23:56:43 +00005558void ASTReader::PrintStats() {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005559 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005560
Mike Stump11289f42009-09-09 15:08:12 +00005561 unsigned NumTypesLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00005562 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall8ccfcb52009-09-24 19:53:00 +00005563 QualType());
Douglas Gregor0e149972009-04-25 19:10:14 +00005564 unsigned NumDeclsLoaded
5565 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5566 (Decl *)0);
5567 unsigned NumIdentifiersLoaded
5568 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5569 IdentifiersLoaded.end(),
5570 (IdentifierInfo *)0);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005571 unsigned NumMacrosLoaded
5572 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5573 MacrosLoaded.end(),
5574 (MacroInfo *)0);
Mike Stump11289f42009-09-09 15:08:12 +00005575 unsigned NumSelectorsLoaded
Douglas Gregor0e149972009-04-25 19:10:14 +00005576 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5577 SelectorsLoaded.end(),
5578 Selector());
Douglas Gregorc3b1dd12009-04-13 20:50:16 +00005579
Douglas Gregor49bf76b2011-07-21 18:46:38 +00005580 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor258ae542009-04-27 06:38:32 +00005581 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5582 NumSLocEntriesRead, TotalNumSLocEntries,
5583 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor745ed142009-04-25 18:35:21 +00005584 if (!TypesLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005585 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00005586 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5587 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5588 if (!DeclsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005589 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor745ed142009-04-25 18:35:21 +00005590 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5591 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor0e149972009-04-25 19:10:14 +00005592 if (!IdentifiersLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005593 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor0e149972009-04-25 19:10:14 +00005594 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5595 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005596 if (!MacrosLoaded.empty())
5597 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5598 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5599 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redlada023c2010-08-04 20:40:17 +00005600 if (!SelectorsLoaded.empty())
Douglas Gregor95c13f52009-04-25 17:48:32 +00005601 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redlada023c2010-08-04 20:40:17 +00005602 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5603 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor95c13f52009-04-25 17:48:32 +00005604 if (TotalNumStatements)
5605 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5606 NumStatementsRead, TotalNumStatements,
5607 ((float)NumStatementsRead/TotalNumStatements * 100));
5608 if (TotalNumMacros)
5609 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5610 NumMacrosRead, TotalNumMacros,
5611 ((float)NumMacrosRead/TotalNumMacros * 100));
5612 if (TotalLexicalDeclContexts)
5613 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5614 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5615 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5616 * 100));
5617 if (TotalVisibleDeclContexts)
5618 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5619 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5620 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5621 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005622 if (TotalNumMethodPoolEntries) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00005623 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005624 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5625 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor95c13f52009-04-25 17:48:32 +00005626 * 100));
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005627 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor95c13f52009-04-25 17:48:32 +00005628 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005629 std::fprintf(stderr, "\n");
Douglas Gregor204b8712011-07-21 19:50:14 +00005630 dump();
5631 std::fprintf(stderr, "\n");
5632}
5633
Douglas Gregorde3ef502011-11-30 23:21:26 +00005634template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor204b8712011-07-21 19:50:14 +00005635static void
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005636dumpModuleIDMap(StringRef Name,
Douglas Gregorde3ef502011-11-30 23:21:26 +00005637 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor204b8712011-07-21 19:50:14 +00005638 InitialCapacity> &Map) {
5639 if (Map.begin() == Map.end())
5640 return;
5641
Douglas Gregorde3ef502011-11-30 23:21:26 +00005642 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor204b8712011-07-21 19:50:14 +00005643 llvm::errs() << Name << ":\n";
5644 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5645 I != IEnd; ++I) {
5646 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5647 << "\n";
5648 }
5649}
5650
Douglas Gregor204b8712011-07-21 19:50:14 +00005651void ASTReader::dump() {
Douglas Gregorde3ef502011-11-30 23:21:26 +00005652 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregord32f0352011-07-22 06:10:01 +00005653 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor204b8712011-07-21 19:50:14 +00005654 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor8ab4ea82011-07-29 00:21:44 +00005655 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005656 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005657 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005658 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor253eefe2011-12-01 00:59:36 +00005659 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005660 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00005661 dumpModuleIDMap("Global preprocessed entity map",
5662 GlobalPreprocessedEntityMap);
Douglas Gregor1cc9c062011-08-02 11:12:41 +00005663
5664 llvm::errs() << "\n*** PCH/Modules Loaded:";
5665 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5666 MEnd = ModuleMgr.end();
5667 M != MEnd; ++M)
5668 (*M)->dump();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005669}
5670
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005671/// Return the amount of memory used by memory buffers, breaking down
5672/// by heap-backed versus mmap'ed memory.
5673void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005674 for (ModuleConstIterator I = ModuleMgr.begin(),
5675 E = ModuleMgr.end(); I != E; ++I) {
5676 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005677 size_t bytes = buf->getBufferSize();
5678 switch (buf->getBufferKind()) {
5679 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5680 sizes.malloc_bytes += bytes;
5681 break;
5682 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5683 sizes.mmap_bytes += bytes;
5684 break;
5685 }
5686 }
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005687 }
Ted Kremenek5e1ed7b2011-04-28 23:46:20 +00005688}
5689
Sebastian Redl2c499f62010-08-18 23:56:43 +00005690void ASTReader::InitializeSema(Sema &S) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00005691 SemaObj = &S;
Axel Naumanndd433f02012-10-18 19:05:02 +00005692 S.addExternalSource(this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00005693
Douglas Gregor7cd60f72009-04-22 21:15:06 +00005694 // Makes sure any declarations that were deserialized "too early"
5695 // still get added to the identifier's declaration chains.
Douglas Gregor2fb99df2010-09-24 23:29:12 +00005696 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005697 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5698 PreloadedDecls[I]->getDeclName());
Douglas Gregora868bbd2009-04-21 22:25:48 +00005699 }
Douglas Gregor7cd60f72009-04-22 21:15:06 +00005700 PreloadedDecls.clear();
Douglas Gregord4df8652009-04-22 22:02:47 +00005701
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005702 // Load the offsets of the declarations that Sema references.
5703 // They will be lazily deserialized when needed.
5704 if (!SemaDeclRefs.empty()) {
5705 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregorb0f3ae62011-07-28 00:57:24 +00005706 if (!SemaObj->StdNamespace)
5707 SemaObj->StdNamespace = SemaDeclRefs[0];
5708 if (!SemaObj->StdBadAlloc)
5709 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00005710 }
5711
Peter Collingbourne5df20e02011-02-15 19:46:30 +00005712 if (!FPPragmaOptions.empty()) {
5713 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5714 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5715 }
5716
5717 if (!OpenCLExtensions.empty()) {
5718 unsigned I = 0;
5719#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5720#include "clang/Basic/OpenCLExtensions.def"
5721
5722 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5723 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00005724}
5725
Douglas Gregorab443b92011-08-20 04:39:52 +00005726IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00005727 // Note that we are loading an identifier.
5728 Deserializing AnIdentifier(this);
5729
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00005730 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5731 /*PriorGeneration=*/0);
Douglas Gregorab443b92011-08-20 04:39:52 +00005732 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005733 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +00005734 markIdentifierUpToDate(II);
Douglas Gregor935bc7a22011-10-27 09:33:13 +00005735 return II;
Douglas Gregora868bbd2009-04-21 22:25:48 +00005736}
5737
Douglas Gregor57756ea2010-10-14 22:11:03 +00005738namespace clang {
5739 /// \brief An identifier-lookup iterator that enumerates all of the
5740 /// identifiers stored within a set of AST files.
5741 class ASTIdentifierIterator : public IdentifierIterator {
5742 /// \brief The AST reader whose identifiers are being enumerated.
5743 const ASTReader &Reader;
5744
5745 /// \brief The current index into the chain of AST files stored in
5746 /// the AST reader.
5747 unsigned Index;
5748
5749 /// \brief The current position within the identifier lookup table
5750 /// of the current AST file.
5751 ASTIdentifierLookupTable::key_iterator Current;
5752
5753 /// \brief The end position within the identifier lookup table of
5754 /// the current AST file.
5755 ASTIdentifierLookupTable::key_iterator End;
5756
5757 public:
5758 explicit ASTIdentifierIterator(const ASTReader &Reader);
5759
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005760 virtual StringRef Next();
Douglas Gregor57756ea2010-10-14 22:11:03 +00005761 };
5762}
5763
5764ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005765 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor57756ea2010-10-14 22:11:03 +00005766 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005767 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00005768 Current = IdTable->key_begin();
5769 End = IdTable->key_end();
5770}
5771
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005772StringRef ASTIdentifierIterator::Next() {
Douglas Gregor57756ea2010-10-14 22:11:03 +00005773 while (Current == End) {
5774 // If we have exhausted all of our AST files, we're done.
5775 if (Index == 0)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005776 return StringRef();
Douglas Gregor57756ea2010-10-14 22:11:03 +00005777
5778 --Index;
5779 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner16f57d32011-07-25 20:32:21 +00005780 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5781 IdentifierLookupTable;
Douglas Gregor57756ea2010-10-14 22:11:03 +00005782 Current = IdTable->key_begin();
5783 End = IdTable->key_end();
5784 }
5785
5786 // We have any identifiers remaining in the current AST file; return
5787 // the next one.
5788 std::pair<const char*, unsigned> Key = *Current;
5789 ++Current;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005790 return StringRef(Key.first, Key.second);
Douglas Gregor57756ea2010-10-14 22:11:03 +00005791}
5792
5793IdentifierIterator *ASTReader::getIdentifiers() const {
5794 return new ASTIdentifierIterator(*this);
5795}
5796
Douglas Gregorc10edd62011-08-25 14:51:20 +00005797namespace clang { namespace serialization {
5798 class ReadMethodPoolVisitor {
5799 ASTReader &Reader;
Douglas Gregord1f01d72012-01-25 01:14:32 +00005800 Selector Sel;
5801 unsigned PriorGeneration;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005802 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5803 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorc78d3462009-04-24 21:10:55 +00005804
Douglas Gregorc10edd62011-08-25 14:51:20 +00005805 public:
Douglas Gregord1f01d72012-01-25 01:14:32 +00005806 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5807 unsigned PriorGeneration)
5808 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregorc10edd62011-08-25 14:51:20 +00005809
Douglas Gregorde3ef502011-11-30 23:21:26 +00005810 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregorc10edd62011-08-25 14:51:20 +00005811 ReadMethodPoolVisitor *This
5812 = static_cast<ReadMethodPoolVisitor *>(UserData);
5813
5814 if (!M.SelectorLookupTable)
5815 return false;
5816
Douglas Gregord1f01d72012-01-25 01:14:32 +00005817 // If we've already searched this module file, skip it now.
5818 if (M.Generation <= This->PriorGeneration)
5819 return true;
5820
Douglas Gregorc10edd62011-08-25 14:51:20 +00005821 ASTSelectorLookupTable *PoolTable
5822 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5823 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5824 if (Pos == PoolTable->end())
5825 return false;
5826
5827 ++This->Reader.NumSelectorsRead;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00005828 // FIXME: Not quite happy with the statistics here. We probably should
5829 // disable this tracking when called via LoadSelector.
5830 // Also, should entries without methods count as misses?
Douglas Gregorc10edd62011-08-25 14:51:20 +00005831 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redld44cd6a2010-08-18 23:57:06 +00005832 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005833 if (This->Reader.DeserializationListener)
5834 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5835 This->Sel);
5836
5837 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5838 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5839 return true;
Sebastian Redlada023c2010-08-04 20:40:17 +00005840 }
Douglas Gregorc10edd62011-08-25 14:51:20 +00005841
5842 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregore1716012012-01-25 00:49:42 +00005843 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5844 return InstanceMethods;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005845 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00005846
Douglas Gregorc10edd62011-08-25 14:51:20 +00005847 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregore1716012012-01-25 00:49:42 +00005848 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5849 return FactoryMethods;
Douglas Gregorc10edd62011-08-25 14:51:20 +00005850 }
5851 };
5852} } // end namespace clang::serialization
5853
Douglas Gregore1716012012-01-25 00:49:42 +00005854/// \brief Add the given set of methods to the method list.
5855static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5856 ObjCMethodList &List) {
5857 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5858 S.addMethodToGlobalList(&List, Methods[I]);
5859 }
5860}
5861
5862void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregord1f01d72012-01-25 01:14:32 +00005863 // Get the selector generation and update it to the current generation.
5864 unsigned &Generation = SelectorGeneration[Sel];
5865 unsigned PriorGeneration = Generation;
5866 Generation = CurrentGeneration;
5867
5868 // Search for methods defined with this selector.
5869 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregorc10edd62011-08-25 14:51:20 +00005870 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregorc10edd62011-08-25 14:51:20 +00005871
Douglas Gregore1716012012-01-25 00:49:42 +00005872 if (Visitor.getInstanceMethods().empty() &&
5873 Visitor.getFactoryMethods().empty()) {
Douglas Gregorc10edd62011-08-25 14:51:20 +00005874 ++NumMethodPoolMisses;
Douglas Gregore1716012012-01-25 00:49:42 +00005875 return;
5876 }
5877
5878 if (!getSema())
5879 return;
5880
5881 Sema &S = *getSema();
5882 Sema::GlobalMethodPool::iterator Pos
5883 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5884
5885 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5886 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorc78d3462009-04-24 21:10:55 +00005887}
5888
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005889void ASTReader::ReadKnownNamespaces(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005890 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +00005891 Namespaces.clear();
5892
5893 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5894 if (NamespaceDecl *Namespace
5895 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5896 Namespaces.push_back(Namespace);
5897 }
5898}
5899
Douglas Gregoreb08bd42011-07-27 20:58:46 +00005900void ASTReader::ReadTentativeDefinitions(
5901 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5902 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5903 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5904 if (Var)
5905 TentativeDefs.push_back(Var);
5906 }
5907 TentativeDefinitions.clear();
5908}
5909
Douglas Gregora94a1542011-07-27 21:45:57 +00005910void ASTReader::ReadUnusedFileScopedDecls(
5911 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5912 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5913 DeclaratorDecl *D
5914 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5915 if (D)
5916 Decls.push_back(D);
5917 }
5918 UnusedFileScopedDecls.clear();
5919}
5920
Douglas Gregorbae31202011-07-27 21:57:17 +00005921void ASTReader::ReadDelegatingConstructors(
5922 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5923 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5924 CXXConstructorDecl *D
5925 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5926 if (D)
5927 Decls.push_back(D);
5928 }
5929 DelegatingCtorDecls.clear();
5930}
5931
Douglas Gregorb7098a32011-07-28 00:39:29 +00005932void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5933 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5934 TypedefNameDecl *D
5935 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5936 if (D)
5937 Decls.push_back(D);
5938 }
5939 ExtVectorDecls.clear();
5940}
5941
Douglas Gregor32002192011-07-28 00:53:40 +00005942void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5943 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5944 CXXRecordDecl *D
5945 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5946 if (D)
5947 Decls.push_back(D);
5948 }
5949 DynamicClasses.clear();
5950}
5951
Douglas Gregordc5c9582011-07-28 14:20:37 +00005952void
5953ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5954 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5955 NamedDecl *D
5956 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5957 if (D)
5958 Decls.push_back(D);
5959 }
5960 LocallyScopedExternalDecls.clear();
5961}
5962
Douglas Gregor72e357f2011-07-28 14:54:22 +00005963void ASTReader::ReadReferencedSelectors(
5964 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5965 if (ReferencedSelectorsData.empty())
5966 return;
5967
5968 // If there are @selector references added them to its pool. This is for
5969 // implementation of -Wselector.
5970 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5971 unsigned I = 0;
5972 while (I < DataSize) {
5973 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5974 SourceLocation SelLoc
5975 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5976 Sels.push_back(std::make_pair(Sel, SelLoc));
5977 }
5978 ReferencedSelectorsData.clear();
5979}
5980
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00005981void ASTReader::ReadWeakUndeclaredIdentifiers(
5982 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5983 if (WeakUndeclaredIdentifiers.empty())
5984 return;
5985
5986 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5987 IdentifierInfo *WeakId
5988 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5989 IdentifierInfo *AliasId
5990 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5991 SourceLocation Loc
5992 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5993 bool Used = WeakUndeclaredIdentifiers[I++];
5994 WeakInfo WI(AliasId, Loc);
5995 WI.setUsed(Used);
5996 WeakIDs.push_back(std::make_pair(WeakId, WI));
5997 }
5998 WeakUndeclaredIdentifiers.clear();
5999}
6000
Douglas Gregor4daf6a32011-07-28 19:11:31 +00006001void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6002 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6003 ExternalVTableUse VT;
6004 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6005 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6006 VT.DefinitionRequired = VTableUses[Idx++];
6007 VTables.push_back(VT);
6008 }
6009
6010 VTableUses.clear();
6011}
6012
Douglas Gregore39f97c2011-07-28 19:49:54 +00006013void ASTReader::ReadPendingInstantiations(
6014 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6015 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6016 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6017 SourceLocation Loc
6018 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann63469422c2012-10-02 09:09:43 +00006019
Douglas Gregor559458c2012-10-03 18:34:48 +00006020 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregore39f97c2011-07-28 19:49:54 +00006021 }
6022 PendingInstantiations.clear();
6023}
6024
Sebastian Redl2c499f62010-08-18 23:56:43 +00006025void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00006026 // It would be complicated to avoid reading the methods anyway. So don't.
6027 ReadMethodPool(Sel);
6028}
6029
Douglas Gregora3e41532011-07-28 20:55:49 +00006030void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregora868bbd2009-04-21 22:25:48 +00006031 assert(ID && "Non-zero identifier ID required");
Douglas Gregor6f00bf82009-04-28 21:53:25 +00006032 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor0e149972009-04-25 19:10:14 +00006033 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlff4a2952010-07-23 23:49:55 +00006034 if (DeserializationListener)
6035 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregora868bbd2009-04-21 22:25:48 +00006036}
6037
Douglas Gregor1342e842009-07-06 18:54:52 +00006038/// \brief Set the globally-visible declarations associated with the given
6039/// identifier.
6040///
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006041/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump11289f42009-09-09 15:08:12 +00006042/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregor1342e842009-07-06 18:54:52 +00006043/// them.
6044///
6045/// \param II an IdentifierInfo that refers to one or more globally-visible
6046/// declarations.
6047///
6048/// \param DeclIDs the set of declaration IDs with the name @p II that are
6049/// visible at global scope.
6050///
6051/// \param Nonrecursive should be true to indicate that the caller knows that
6052/// this call is non-recursive, and therefore the globally-visible declarations
6053/// will not be placed onto the pending queue.
Mike Stump11289f42009-09-09 15:08:12 +00006054void
Sebastian Redl2c499f62010-08-18 23:56:43 +00006055ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006056 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregor1342e842009-07-06 18:54:52 +00006057 bool Nonrecursive) {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00006058 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregor1342e842009-07-06 18:54:52 +00006059 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6060 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6061 PII.II = II;
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00006062 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregor1342e842009-07-06 18:54:52 +00006063 return;
6064 }
Mike Stump11289f42009-09-09 15:08:12 +00006065
Douglas Gregor1342e842009-07-06 18:54:52 +00006066 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6067 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6068 if (SemaObj) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00006069 // Introduce this declaration into the translation-unit scope
6070 // and add it to the declaration chain for this identifier, so
6071 // that (unqualified) name lookup will find it.
6072 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregor1342e842009-07-06 18:54:52 +00006073 } else {
6074 // Queue this declaration so that it will be added to the
6075 // translation unit scope and identifier's declaration chain
6076 // once a Sema object is known.
6077 PreloadedDecls.push_back(D);
6078 }
6079 }
6080}
6081
Douglas Gregora3e41532011-07-28 20:55:49 +00006082IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006083 if (ID == 0)
6084 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00006085
Sebastian Redlc713b962010-07-21 00:46:22 +00006086 if (IdentifiersLoaded.empty()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006087 Error("no identifier table in AST file");
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006088 return 0;
6089 }
Mike Stump11289f42009-09-09 15:08:12 +00006090
Sebastian Redlc713b962010-07-21 00:46:22 +00006091 ID -= 1;
6092 if (!IdentifiersLoaded[ID]) {
Douglas Gregor19d26352011-07-20 00:59:32 +00006093 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6094 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00006095 ModuleFile *M = I->second;
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006096 unsigned Index = ID - M->BaseIdentifierID;
6097 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregor5287b4e2009-04-25 21:04:17 +00006098
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006099 // All of the strings in the AST file are preceded by a 16-bit length.
6100 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenekca42a512009-10-23 04:45:31 +00006101 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6102 // unsigned integers. This is important to avoid integer overflow when
6103 // we cast them to 'unsigned'.
Ted Kremenek49c52322009-10-23 03:57:22 +00006104 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregorab4df582009-04-28 20:01:51 +00006105 unsigned StrLen = (((unsigned) StrLenPtr[0])
6106 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redlc713b962010-07-21 00:46:22 +00006107 IdentifiersLoaded[ID]
Douglas Gregor51825b42011-09-09 22:02:16 +00006108 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlff4a2952010-07-23 23:49:55 +00006109 if (DeserializationListener)
6110 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00006111 }
Mike Stump11289f42009-09-09 15:08:12 +00006112
Sebastian Redlc713b962010-07-21 00:46:22 +00006113 return IdentifiersLoaded[ID];
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006114}
6115
Douglas Gregorde3ef502011-11-30 23:21:26 +00006116IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregora3e41532011-07-28 20:55:49 +00006117 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6118}
6119
Douglas Gregorde3ef502011-11-30 23:21:26 +00006120IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor1ab036c2011-08-03 21:49:18 +00006121 if (LocalID < NUM_PREDEF_IDENT_IDS)
6122 return LocalID;
6123
6124 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6125 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6126 assert(I != M.IdentifierRemap.end()
6127 && "Invalid index into identifier index remap");
6128
6129 return LocalID + I->second;
Douglas Gregora3e41532011-07-28 20:55:49 +00006130}
6131
Douglas Gregore7400892012-10-11 17:41:54 +00006132MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006133 if (ID == 0)
6134 return 0;
6135
6136 if (MacrosLoaded.empty()) {
6137 Error("no macro table in AST file");
6138 return 0;
6139 }
6140
6141 ID -= NUM_PREDEF_MACRO_IDS;
6142 if (!MacrosLoaded[ID]) {
6143 GlobalMacroMapType::iterator I
6144 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6145 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6146 ModuleFile *M = I->second;
6147 unsigned Index = ID - M->BaseMacroID;
Douglas Gregore7400892012-10-11 17:41:54 +00006148 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006149 }
6150
6151 return MacrosLoaded[ID];
6152}
6153
6154MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6155 if (LocalID < NUM_PREDEF_MACRO_IDS)
6156 return LocalID;
6157
6158 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6159 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6160 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6161
6162 return LocalID + I->second;
6163}
6164
Douglas Gregor253eefe2011-12-01 00:59:36 +00006165serialization::SubmoduleID
6166ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6167 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6168 return LocalID;
6169
6170 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6171 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6172 assert(I != M.SubmoduleRemap.end()
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006173 && "Invalid index into submodule index remap");
Douglas Gregor253eefe2011-12-01 00:59:36 +00006174
6175 return LocalID + I->second;
6176}
6177
6178Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6179 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6180 assert(GlobalID == 0 && "Unhandled global submodule ID");
6181 return 0;
6182 }
6183
6184 if (GlobalID > SubmodulesLoaded.size()) {
6185 Error("submodule ID out of range in AST file");
6186 return 0;
6187 }
6188
6189 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6190}
6191
Douglas Gregorde3ef502011-11-30 23:21:26 +00006192Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor074fdc52011-07-28 21:16:51 +00006193 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6194}
6195
6196Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff2ddea052009-04-23 10:39:46 +00006197 if (ID == 0)
6198 return Selector();
Mike Stump11289f42009-09-09 15:08:12 +00006199
Sebastian Redlada023c2010-08-04 20:40:17 +00006200 if (ID > SelectorsLoaded.size()) {
Sebastian Redld44cd6a2010-08-18 23:57:06 +00006201 Error("selector ID out of range in AST file");
Steve Naroff2ddea052009-04-23 10:39:46 +00006202 return Selector();
6203 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00006204
Sebastian Redlada023c2010-08-04 20:40:17 +00006205 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00006206 // Load this selector from the selector table.
Douglas Gregor2262d282011-07-20 01:10:58 +00006207 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6208 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregorde3ef502011-11-30 23:21:26 +00006209 ModuleFile &M = *I->second;
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006210 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006211 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor2262d282011-07-20 01:10:58 +00006212 SelectorsLoaded[ID - 1] =
Douglas Gregorbab6d2c2011-07-29 00:56:45 +00006213 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor2262d282011-07-20 01:10:58 +00006214 if (DeserializationListener)
6215 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor95c13f52009-04-25 17:48:32 +00006216 }
6217
Sebastian Redlada023c2010-08-04 20:40:17 +00006218 return SelectorsLoaded[ID - 1];
Steve Naroff2ddea052009-04-23 10:39:46 +00006219}
6220
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00006221Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregord720daf2010-04-06 17:30:22 +00006222 return DecodeSelector(ID);
6223}
6224
Sebastian Redl2c499f62010-08-18 23:56:43 +00006225uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redlada023c2010-08-04 20:40:17 +00006226 // ID 0 (the null selector) is considered an external selector.
6227 return getTotalNumSelectors() + 1;
Douglas Gregord720daf2010-04-06 17:30:22 +00006228}
6229
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006230serialization::SelectorID
Douglas Gregorde3ef502011-11-30 23:21:26 +00006231ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006232 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6233 return LocalID;
6234
6235 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6236 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6237 assert(I != M.SelectorRemap.end()
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006238 && "Invalid index into selector index remap");
Douglas Gregor8f364fb2011-08-03 23:28:44 +00006239
6240 return LocalID + I->second;
Douglas Gregor3f8f04f2011-07-28 14:41:43 +00006241}
6242
Mike Stump11289f42009-09-09 15:08:12 +00006243DeclarationName
Douglas Gregorde3ef502011-11-30 23:21:26 +00006244ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor903b7e92011-07-22 00:38:23 +00006245 const RecordData &Record, unsigned &Idx) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006246 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6247 switch (Kind) {
6248 case DeclarationName::Identifier:
Douglas Gregora3e41532011-07-28 20:55:49 +00006249 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006250
6251 case DeclarationName::ObjCZeroArgSelector:
6252 case DeclarationName::ObjCOneArgSelector:
6253 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor074fdc52011-07-28 21:16:51 +00006254 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006255
6256 case DeclarationName::CXXConstructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006257 return Context.DeclarationNames.getCXXConstructorName(
6258 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006259
6260 case DeclarationName::CXXDestructorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006261 return Context.DeclarationNames.getCXXDestructorName(
6262 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006263
6264 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006265 return Context.DeclarationNames.getCXXConversionFunctionName(
6266 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006267
6268 case DeclarationName::CXXOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006269 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006270 (OverloadedOperatorKind)Record[Idx++]);
6271
Alexis Hunt3d221f22009-11-29 07:34:05 +00006272 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor4163aca2011-09-09 21:34:22 +00006273 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregora3e41532011-07-28 20:55:49 +00006274 GetIdentifierInfo(F, Record, Idx));
Alexis Hunt3d221f22009-11-29 07:34:05 +00006275
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006276 case DeclarationName::CXXUsingDirective:
6277 return DeclarationName::getUsingDirectiveName();
6278 }
6279
David Blaikie8a40f702012-01-17 06:56:22 +00006280 llvm_unreachable("Invalid NameKind!");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00006281}
Douglas Gregor55abb232009-04-10 20:39:37 +00006282
Douglas Gregorde3ef502011-11-30 23:21:26 +00006283void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006284 DeclarationNameLoc &DNLoc,
6285 DeclarationName Name,
6286 const RecordData &Record, unsigned &Idx) {
6287 switch (Name.getNameKind()) {
6288 case DeclarationName::CXXConstructorName:
6289 case DeclarationName::CXXDestructorName:
6290 case DeclarationName::CXXConversionFunctionName:
6291 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6292 break;
6293
6294 case DeclarationName::CXXOperatorName:
6295 DNLoc.CXXOperatorName.BeginOpNameLoc
6296 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6297 DNLoc.CXXOperatorName.EndOpNameLoc
6298 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6299 break;
6300
6301 case DeclarationName::CXXLiteralOperatorName:
6302 DNLoc.CXXLiteralOperatorName.OpNameLoc
6303 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6304 break;
6305
6306 case DeclarationName::Identifier:
6307 case DeclarationName::ObjCZeroArgSelector:
6308 case DeclarationName::ObjCOneArgSelector:
6309 case DeclarationName::ObjCMultiArgSelector:
6310 case DeclarationName::CXXUsingDirective:
6311 break;
6312 }
6313}
6314
Douglas Gregorde3ef502011-11-30 23:21:26 +00006315void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006316 DeclarationNameInfo &NameInfo,
6317 const RecordData &Record, unsigned &Idx) {
Douglas Gregor903b7e92011-07-22 00:38:23 +00006318 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006319 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6320 DeclarationNameLoc DNLoc;
6321 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6322 NameInfo.setInfo(DNLoc);
6323}
6324
Douglas Gregorde3ef502011-11-30 23:21:26 +00006325void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006326 const RecordData &Record, unsigned &Idx) {
Douglas Gregor14454802011-02-25 02:25:35 +00006327 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006328 unsigned NumTPLists = Record[Idx++];
6329 Info.NumTemplParamLists = NumTPLists;
6330 if (NumTPLists) {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006331 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00006332 for (unsigned i=0; i != NumTPLists; ++i)
6333 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6334 }
6335}
6336
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006337TemplateName
Douglas Gregorde3ef502011-11-30 23:21:26 +00006338ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor5590be02011-01-15 06:45:20 +00006339 unsigned &Idx) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006340 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006341 switch (Kind) {
6342 case TemplateName::Template:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006343 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006344
6345 case TemplateName::OverloadedTemplate: {
6346 unsigned size = Record[Idx++];
6347 UnresolvedSet<8> Decls;
6348 while (size--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006349 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006350
Douglas Gregor4163aca2011-09-09 21:34:22 +00006351 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006352 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006353
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006354 case TemplateName::QualifiedTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006355 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006356 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor7fb09192011-07-21 22:35:25 +00006357 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006358 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006359 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006360
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006361 case TemplateName::DependentTemplate: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006362 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006363 if (Record[Idx++]) // isIdentifier
Douglas Gregor4163aca2011-09-09 21:34:22 +00006364 return Context.getDependentTemplateName(NNS,
Douglas Gregora3e41532011-07-28 20:55:49 +00006365 GetIdentifierInfo(F, Record,
6366 Idx));
Douglas Gregor4163aca2011-09-09 21:34:22 +00006367 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00006368 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006369 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006370
6371 case TemplateName::SubstTemplateTemplateParm: {
6372 TemplateTemplateParmDecl *param
Douglas Gregor7fb09192011-07-21 22:35:25 +00006373 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCalld9dfe3a2011-06-30 08:33:18 +00006374 if (!param) return TemplateName();
6375 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006376 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCalld9dfe3a2011-06-30 08:33:18 +00006377 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006378
6379 case TemplateName::SubstTemplateTemplateParmPack: {
6380 TemplateTemplateParmDecl *Param
Douglas Gregor7fb09192011-07-21 22:35:25 +00006381 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor5590be02011-01-15 06:45:20 +00006382 if (!Param)
6383 return TemplateName();
6384
6385 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6386 if (ArgPack.getKind() != TemplateArgument::Pack)
6387 return TemplateName();
6388
Douglas Gregor4163aca2011-09-09 21:34:22 +00006389 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor5590be02011-01-15 06:45:20 +00006390 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006391 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006392
David Blaikie83d382b2011-09-23 05:06:16 +00006393 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006394}
6395
6396TemplateArgument
Douglas Gregorde3ef502011-11-30 23:21:26 +00006397ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redlc67764e2010-07-22 22:43:28 +00006398 const RecordData &Record, unsigned &Idx) {
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006399 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6400 switch (Kind) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006401 case TemplateArgument::Null:
6402 return TemplateArgument();
6403 case TemplateArgument::Type:
Douglas Gregor903b7e92011-07-22 00:38:23 +00006404 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmanb826a002012-09-26 02:36:12 +00006405 case TemplateArgument::Declaration: {
6406 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6407 bool ForReferenceParam = Record[Idx++];
6408 return TemplateArgument(D, ForReferenceParam);
6409 }
6410 case TemplateArgument::NullPtr:
6411 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00006412 case TemplateArgument::Integral: {
6413 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor903b7e92011-07-22 00:38:23 +00006414 QualType T = readType(F, Record, Idx);
Benjamin Kramer6003ad52012-06-07 15:09:51 +00006415 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +00006416 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006417 case TemplateArgument::Template:
Douglas Gregor5590be02011-01-15 06:45:20 +00006418 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregore4ff4b52011-01-05 18:58:31 +00006419 case TemplateArgument::TemplateExpansion: {
Douglas Gregor5590be02011-01-15 06:45:20 +00006420 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregore1d60df2011-01-14 23:41:42 +00006421 llvm::Optional<unsigned> NumTemplateExpansions;
6422 if (unsigned NumExpansions = Record[Idx++])
6423 NumTemplateExpansions = NumExpansions - 1;
6424 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregoreb29d182011-01-05 17:40:24 +00006425 }
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006426 case TemplateArgument::Expression:
Sebastian Redl2c373b92010-10-05 15:59:54 +00006427 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006428 case TemplateArgument::Pack: {
6429 unsigned NumArgs = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00006430 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor1ccc8412010-11-07 23:05:16 +00006431 for (unsigned I = 0; I != NumArgs; ++I)
6432 Args[I] = ReadTemplateArgument(F, Record, Idx);
6433 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006434 }
6435 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006436
David Blaikie83d382b2011-09-23 05:06:16 +00006437 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00006438}
6439
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006440TemplateParameterList *
Douglas Gregorde3ef502011-11-30 23:21:26 +00006441ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006442 const RecordData &Record, unsigned &Idx) {
6443 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6444 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6445 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006446
6447 unsigned NumParams = Record[Idx++];
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006448 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006449 Params.reserve(NumParams);
6450 while (NumParams--)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006451 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006452
6453 TemplateParameterList* TemplateParams =
Douglas Gregor4163aca2011-09-09 21:34:22 +00006454 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006455 Params.data(), Params.size(), RAngleLoc);
6456 return TemplateParams;
6457}
6458
6459void
Sebastian Redl2c499f62010-08-18 23:56:43 +00006460ASTReader::
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006461ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregorde3ef502011-11-30 23:21:26 +00006462 ModuleFile &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006463 unsigned &Idx) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006464 unsigned NumTemplateArgs = Record[Idx++];
6465 TemplArgs.reserve(NumTemplateArgs);
6466 while (NumTemplateArgs--)
Sebastian Redl2c373b92010-10-05 15:59:54 +00006467 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00006468}
6469
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006470/// \brief Read a UnresolvedSet structure.
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006471void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006472 const RecordData &Record, unsigned &Idx) {
6473 unsigned NumDecls = Record[Idx++];
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006474 Set.reserve(Context, NumDecls);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006475 while (NumDecls--) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006476 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006477 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00006478 Set.addDecl(Context, D, AS);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00006479 }
6480}
6481
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006482CXXBaseSpecifier
Douglas Gregorde3ef502011-11-30 23:21:26 +00006483ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky19b9f952010-07-26 16:56:01 +00006484 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006485 bool isVirtual = static_cast<bool>(Record[Idx++]);
6486 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6487 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redl08905022011-02-05 19:23:19 +00006488 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006489 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6490 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor752a5952011-01-03 22:36:02 +00006491 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl08905022011-02-05 19:23:19 +00006492 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregor752a5952011-01-03 22:36:02 +00006493 EllipsisLoc);
Sebastian Redl08905022011-02-05 19:23:19 +00006494 Result.setInheritConstructors(inheritConstructors);
6495 return Result;
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00006496}
6497
Alexis Hunt1d792652011-01-08 20:30:50 +00006498std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregorde3ef502011-11-30 23:21:26 +00006499ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Alexis Hunt1d792652011-01-08 20:30:50 +00006500 unsigned &Idx) {
6501 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006502 unsigned NumInitializers = Record[Idx++];
6503 if (NumInitializers) {
Alexis Hunt1d792652011-01-08 20:30:50 +00006504 CtorInitializers
Douglas Gregor4163aca2011-09-09 21:34:22 +00006505 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006506 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006507 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006508 bool IsBaseVirtual = false;
6509 FieldDecl *Member = 0;
Francois Pichetd583da02010-12-04 09:14:42 +00006510 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006511
Alexis Hunt37a477f2011-05-04 01:19:08 +00006512 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6513 switch (Type) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006514 case CTOR_INITIALIZER_BASE:
6515 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006516 IsBaseVirtual = Record[Idx++];
Alexis Hunt37a477f2011-05-04 01:19:08 +00006517 break;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006518
6519 case CTOR_INITIALIZER_DELEGATING:
6520 TInfo = GetTypeSourceInfo(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006521 break;
6522
6523 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006524 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006525 break;
6526
6527 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor7fb09192011-07-21 22:35:25 +00006528 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006529 break;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006530 }
Alexis Hunt37a477f2011-05-04 01:19:08 +00006531
Douglas Gregor44e7df62011-01-04 00:32:56 +00006532 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006533 Expr *Init = ReadExpr(F);
Sebastian Redl2c373b92010-10-05 15:59:54 +00006534 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6535 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006536 bool IsWritten = Record[Idx++];
6537 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006538 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006539 if (IsWritten) {
6540 SourceOrderOrNumArrayIndices = Record[Idx++];
6541 } else {
6542 SourceOrderOrNumArrayIndices = Record[Idx++];
6543 Indices.reserve(SourceOrderOrNumArrayIndices);
6544 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor7fb09192011-07-21 22:35:25 +00006545 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006546 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006547
Alexis Hunt1d792652011-01-08 20:30:50 +00006548 CXXCtorInitializer *BOMInit;
Alexis Hunt37a477f2011-05-04 01:19:08 +00006549 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006550 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Alexis Hunt1d792652011-01-08 20:30:50 +00006551 LParenLoc, Init, RParenLoc,
6552 MemberOrEllipsisLoc);
Alexis Hunt37a477f2011-05-04 01:19:08 +00006553 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00006554 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6555 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006556 } else if (IsWritten) {
Francois Pichetd583da02010-12-04 09:14:42 +00006557 if (Member)
Douglas Gregor4163aca2011-09-09 21:34:22 +00006558 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00006559 LParenLoc, Init, RParenLoc);
Francois Pichetd583da02010-12-04 09:14:42 +00006560 else
Douglas Gregor4163aca2011-09-09 21:34:22 +00006561 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Alexis Hunt1d792652011-01-08 20:30:50 +00006562 MemberOrEllipsisLoc, LParenLoc,
6563 Init, RParenLoc);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006564 } else {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006565 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Alexis Hunt1d792652011-01-08 20:30:50 +00006566 LParenLoc, Init, RParenLoc,
6567 Indices.data(), Indices.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006568 }
6569
Argyrios Kyrtzidisd05f3e32010-09-06 19:04:27 +00006570 if (IsWritten)
6571 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Alexis Hunt1d792652011-01-08 20:30:50 +00006572 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006573 }
6574 }
6575
Alexis Hunt1d792652011-01-08 20:30:50 +00006576 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00006577}
6578
Chris Lattnerca025db2010-05-07 21:43:38 +00006579NestedNameSpecifier *
Douglas Gregorde3ef502011-11-30 23:21:26 +00006580ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00006581 const RecordData &Record, unsigned &Idx) {
Chris Lattnerca025db2010-05-07 21:43:38 +00006582 unsigned N = Record[Idx++];
6583 NestedNameSpecifier *NNS = 0, *Prev = 0;
6584 for (unsigned I = 0; I != N; ++I) {
6585 NestedNameSpecifier::SpecifierKind Kind
6586 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6587 switch (Kind) {
6588 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00006589 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006590 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattnerca025db2010-05-07 21:43:38 +00006591 break;
6592 }
6593
6594 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006595 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006596 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattnerca025db2010-05-07 21:43:38 +00006597 break;
6598 }
6599
Douglas Gregor7b26ff92011-02-24 02:36:08 +00006600 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006601 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006602 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor7b26ff92011-02-24 02:36:08 +00006603 break;
6604 }
6605
Chris Lattnerca025db2010-05-07 21:43:38 +00006606 case NestedNameSpecifier::TypeSpec:
6607 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor903b7e92011-07-22 00:38:23 +00006608 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor0cdc8322010-12-10 17:03:06 +00006609 if (!T)
6610 return 0;
6611
Chris Lattnerca025db2010-05-07 21:43:38 +00006612 bool Template = Record[Idx++];
Douglas Gregor4163aca2011-09-09 21:34:22 +00006613 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattnerca025db2010-05-07 21:43:38 +00006614 break;
6615 }
6616
6617 case NestedNameSpecifier::Global: {
Douglas Gregor4163aca2011-09-09 21:34:22 +00006618 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattnerca025db2010-05-07 21:43:38 +00006619 // No associated value, and there can't be a prefix.
6620 break;
6621 }
Chris Lattnerca025db2010-05-07 21:43:38 +00006622 }
Argyrios Kyrtzidisad65c692010-07-07 15:46:30 +00006623 Prev = NNS;
Chris Lattnerca025db2010-05-07 21:43:38 +00006624 }
6625 return NNS;
6626}
6627
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006628NestedNameSpecifierLoc
Douglas Gregorde3ef502011-11-30 23:21:26 +00006629ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006630 unsigned &Idx) {
6631 unsigned N = Record[Idx++];
Douglas Gregor9b272512011-02-28 23:58:31 +00006632 NestedNameSpecifierLocBuilder Builder;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006633 for (unsigned I = 0; I != N; ++I) {
6634 NestedNameSpecifier::SpecifierKind Kind
6635 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6636 switch (Kind) {
6637 case NestedNameSpecifier::Identifier: {
Douglas Gregora3e41532011-07-28 20:55:49 +00006638 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006639 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006640 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006641 break;
6642 }
6643
6644 case NestedNameSpecifier::Namespace: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006645 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006646 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006647 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006648 break;
6649 }
6650
6651 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006652 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006653 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006654 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006655 break;
6656 }
6657
6658 case NestedNameSpecifier::TypeSpec:
6659 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006660 bool Template = Record[Idx++];
6661 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6662 if (!T)
6663 return NestedNameSpecifierLoc();
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006664 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor9b272512011-02-28 23:58:31 +00006665
6666 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor4163aca2011-09-09 21:34:22 +00006667 Builder.Extend(Context,
Douglas Gregor9b272512011-02-28 23:58:31 +00006668 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6669 T->getTypeLoc(), ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006670 break;
6671 }
6672
6673 case NestedNameSpecifier::Global: {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006674 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006675 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006676 break;
6677 }
6678 }
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006679 }
6680
Douglas Gregor4163aca2011-09-09 21:34:22 +00006681 return Builder.getWithLocInContext(Context);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00006682}
6683
Chris Lattnerca025db2010-05-07 21:43:38 +00006684SourceRange
Douglas Gregorde3ef502011-11-30 23:21:26 +00006685ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redl2c373b92010-10-05 15:59:54 +00006686 unsigned &Idx) {
6687 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6688 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar6d3bc082010-06-02 15:47:10 +00006689 return SourceRange(beg, end);
Chris Lattnerca025db2010-05-07 21:43:38 +00006690}
6691
Douglas Gregor1daeb692009-04-13 18:14:40 +00006692/// \brief Read an integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006693llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00006694 unsigned BitWidth = Record[Idx++];
6695 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6696 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6697 Idx += NumWords;
6698 return Result;
6699}
6700
6701/// \brief Read a signed integral value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006702llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00006703 bool isUnsigned = Record[Idx++];
6704 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6705}
6706
Douglas Gregore0a3a512009-04-14 21:55:33 +00006707/// \brief Read a floating-point value
Sebastian Redl2c499f62010-08-18 23:56:43 +00006708llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00006709 return llvm::APFloat(ReadAPInt(Record, Idx));
6710}
6711
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006712// \brief Read a string
Sebastian Redl2c499f62010-08-18 23:56:43 +00006713std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006714 unsigned Len = Record[Idx++];
Jay Foad7d0479f2009-05-21 09:52:38 +00006715 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00006716 Idx += Len;
6717 return Result;
6718}
6719
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006720VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6721 unsigned &Idx) {
6722 unsigned Major = Record[Idx++];
6723 unsigned Minor = Record[Idx++];
6724 unsigned Subminor = Record[Idx++];
6725 if (Minor == 0)
6726 return VersionTuple(Major);
6727 if (Subminor == 0)
6728 return VersionTuple(Major, Minor - 1);
6729 return VersionTuple(Major, Minor - 1, Subminor - 1);
6730}
6731
Douglas Gregorde3ef502011-11-30 23:21:26 +00006732CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor7fb09192011-07-21 22:35:25 +00006733 const RecordData &Record,
Chris Lattnercba86142010-05-10 00:25:06 +00006734 unsigned &Idx) {
Douglas Gregor7fb09192011-07-21 22:35:25 +00006735 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor4163aca2011-09-09 21:34:22 +00006736 return CXXTemporary::Create(Context, Decl);
Chris Lattnercba86142010-05-10 00:25:06 +00006737}
6738
Sebastian Redl2c499f62010-08-18 23:56:43 +00006739DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregor92863e42009-04-10 23:10:45 +00006740 return Diag(SourceLocation(), DiagID);
6741}
6742
Sebastian Redl2c499f62010-08-18 23:56:43 +00006743DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006744 return Diags.Report(Loc, DiagID);
Douglas Gregor55abb232009-04-10 20:39:37 +00006745}
Douglas Gregora9af1d12009-04-17 00:04:06 +00006746
Douglas Gregora868bbd2009-04-21 22:25:48 +00006747/// \brief Retrieve the identifier table associated with the
6748/// preprocessor.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006749IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor51825b42011-09-09 22:02:16 +00006750 return PP.getIdentifierTable();
Douglas Gregora868bbd2009-04-21 22:25:48 +00006751}
6752
Douglas Gregora9af1d12009-04-17 00:04:06 +00006753/// \brief Record that the given ID maps to the given switch-case
6754/// statement.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006755void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006756 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6757 "Already have a SwitchCase with this ID");
6758 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregora9af1d12009-04-17 00:04:06 +00006759}
6760
6761/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redl2c499f62010-08-18 23:56:43 +00006762SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006763 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6764 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregora9af1d12009-04-17 00:04:06 +00006765}
Douglas Gregor6cc68a42009-04-17 18:18:49 +00006766
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00006767void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006768 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidisd9f526f2010-10-28 09:29:32 +00006769}
6770
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006771void ASTReader::ReadComments() {
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00006772 std::vector<RawComment *> Comments;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006773 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6774 serialization::ModuleFile *> >::iterator
6775 I = CommentsCursors.begin(),
6776 E = CommentsCursors.end();
6777 I != E; ++I) {
6778 llvm::BitstreamCursor &Cursor = I->first;
6779 serialization::ModuleFile &F = *I->second;
6780 SavedStreamPosition SavedPosition(Cursor);
6781
6782 RecordData Record;
6783 while (true) {
6784 unsigned Code = Cursor.ReadCode();
6785 if (Code == llvm::bitc::END_BLOCK)
6786 break;
6787
6788 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6789 // No known subblocks, always skip them.
6790 Cursor.ReadSubBlockID();
6791 if (Cursor.SkipBlock()) {
6792 Error("malformed block record in AST file");
6793 return;
6794 }
6795 continue;
6796 }
6797
6798 if (Code == llvm::bitc::DEFINE_ABBREV) {
6799 Cursor.ReadAbbrevRecord();
6800 continue;
6801 }
6802
6803 // Read a record.
6804 Record.clear();
6805 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth029ea4a2012-06-20 06:47:54 +00006806 case COMMENTS_RAW_COMMENT: {
6807 unsigned Idx = 0;
6808 SourceRange SR = ReadSourceRange(F, Record, Idx);
6809 RawComment::CommentKind Kind =
6810 (RawComment::CommentKind) Record[Idx++];
6811 bool IsTrailingComment = Record[Idx++];
6812 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00006813 Comments.push_back(new (Context) RawComment(SR, Kind,
6814 IsTrailingComment,
6815 IsAlmostTrailingComment));
Chandler Carruth029ea4a2012-06-20 06:47:54 +00006816 break;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00006817 }
6818 }
6819 }
6820 }
6821 Context.Comments.addCommentsToFront(Comments);
6822}
6823
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006824void ASTReader::finishPendingActions() {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00006825 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6826 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006827 // If any identifiers with corresponding top-level declarations have
6828 // been loaded, load those declarations now.
6829 while (!PendingIdentifierInfos.empty()) {
6830 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6831 PendingIdentifierInfos.front().DeclIDs, true);
6832 PendingIdentifierInfos.pop_front();
6833 }
6834
Douglas Gregor05f10352011-12-17 23:38:30 +00006835 // Load pending declaration chains.
6836 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6837 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006838 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregor05f10352011-12-17 23:38:30 +00006839 }
6840 PendingDeclChains.clear();
Douglas Gregor5a4649b2012-10-11 00:46:49 +00006841
6842 // Load any pending macro definitions.
Douglas Gregord2acff92012-10-11 17:31:34 +00006843 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6844 // FIXME: std::move here
6845 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregore7400892012-10-11 17:41:54 +00006846 MacroInfo *Hint = 0;
Douglas Gregord2acff92012-10-11 17:31:34 +00006847 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6848 ++IDIdx) {
Douglas Gregore7400892012-10-11 17:41:54 +00006849 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregord2acff92012-10-11 17:31:34 +00006850 }
6851 }
6852 PendingMacroIDs.clear();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006853 }
Douglas Gregore80b31f2011-12-19 19:00:47 +00006854
Douglas Gregor68444de2012-01-14 15:13:49 +00006855 // If we deserialized any C++ or Objective-C class definitions, any
6856 // Objective-C protocol definitions, or any redeclarable templates, make sure
6857 // that all redeclarations point to the definitions. Note that this can only
6858 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregore80b31f2011-12-19 19:00:47 +00006859 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6860 DEnd = PendingDefinitions.end();
6861 D != DEnd; ++D) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006862 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6863 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6864 // Make sure that the TagType points at the definition.
6865 const_cast<TagType*>(TagT)->decl = TD;
6866 }
Douglas Gregore80b31f2011-12-19 19:00:47 +00006867
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006868 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6869 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6870 REnd = RD->redecls_end();
6871 R != REnd; ++R)
6872 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6873
6874 }
6875
Douglas Gregore80b31f2011-12-19 19:00:47 +00006876 continue;
6877 }
6878
Douglas Gregora715bff2012-01-01 19:51:50 +00006879 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +00006880 // Make sure that the ObjCInterfaceType points at the definition.
6881 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6882 ->Decl = ID;
6883
Douglas Gregora715bff2012-01-01 19:51:50 +00006884 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6885 REnd = ID->redecls_end();
6886 R != REnd; ++R)
6887 R->Data = ID->Data;
6888
6889 continue;
6890 }
6891
Douglas Gregor68444de2012-01-14 15:13:49 +00006892 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6893 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6894 REnd = PD->redecls_end();
6895 R != REnd; ++R)
6896 R->Data = PD->Data;
6897
6898 continue;
6899 }
6900
6901 RedeclarableTemplateDecl *RTD
6902 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6903 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6904 REnd = RTD->redecls_end();
Douglas Gregore80b31f2011-12-19 19:00:47 +00006905 R != REnd; ++R)
Douglas Gregora6017bb2012-10-09 17:21:28 +00006906 R->Common = RTD->Common;
Douglas Gregore80b31f2011-12-19 19:00:47 +00006907 }
6908 PendingDefinitions.clear();
Douglas Gregora6017bb2012-10-09 17:21:28 +00006909
6910 // Load the bodies of any functions or methods we've encountered. We do
6911 // this now (delayed) so that we can be sure that the declaration chains
6912 // have been fully wired up.
Douglas Gregor7c0990b2012-10-09 17:50:23 +00006913 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6914 PBEnd = PendingBodies.end();
Douglas Gregora6017bb2012-10-09 17:21:28 +00006915 PB != PBEnd; ++PB) {
6916 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6917 // FIXME: Check for =delete/=default?
6918 // FIXME: Complain about ODR violations here?
6919 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6920 FD->setLazyBody(PB->second);
6921 continue;
6922 }
6923
6924 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6925 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6926 MD->setLazyBody(PB->second);
6927 }
6928 PendingBodies.clear();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006929}
6930
Sebastian Redl2c499f62010-08-18 23:56:43 +00006931void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00006932 assert(NumCurrentElementsDeserializing &&
6933 "FinishedDeserializing not paired with StartedDeserializing");
6934 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006935 // We decrease NumCurrentElementsDeserializing only after pending actions
6936 // are finished, to avoid recursively re-calling finishPendingActions().
6937 finishPendingActions();
6938 }
6939 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis97ea7d62011-12-17 04:13:28 +00006940
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006941 if (NumCurrentElementsDeserializing == 0 &&
6942 Consumer && !PassingDeclsToConsumer) {
6943 // Guard variable to avoid recursively redoing the process of passing
6944 // decls to consumer.
6945 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6946 true);
Argyrios Kyrtzidis97ea7d62011-12-17 04:13:28 +00006947
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006948 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidisb9e53ed2011-11-30 23:18:26 +00006949 // We are not in recursive loading, so it's safe to pass the "interesting"
6950 // decls to the consumer.
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006951 Decl *D = InterestingDecls.front();
6952 InterestingDecls.pop_front();
Argyrios Kyrtzidisda32f5c2011-12-17 08:11:25 +00006953 PassInterestingDeclToConsumer(D);
6954 }
Douglas Gregor1342e842009-07-06 18:54:52 +00006955 }
Douglas Gregor1342e842009-07-06 18:54:52 +00006956}
Douglas Gregorb473b072010-08-19 00:28:17 +00006957
Douglas Gregor8835e032011-09-02 00:26:20 +00006958ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregorc567ba22011-07-22 16:35:34 +00006959 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00006960 bool AllowASTWithCompilerErrors)
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006961 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6962 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor51825b42011-09-09 22:02:16 +00006963 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +00006964 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregor6bdae4b2012-10-18 21:31:35 +00006965 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00006966 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidis0f7d7ab2012-05-04 01:49:36 +00006967 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
Douglas Gregor925296b2011-07-19 16:10:42 +00006968 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor606c4ac2011-02-05 19:42:43 +00006969 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6970 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6971 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6972 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00006973 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6974 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis5605de72012-02-09 07:31:52 +00006975 PassingDeclsToConsumer(false),
Jonathan D. Turner3766fdb2011-07-21 21:15:19 +00006976 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor606c4ac2011-02-05 19:42:43 +00006977{
Douglas Gregor925296b2011-07-19 16:10:42 +00006978 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006979}
6980
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006981ASTReader::~ASTReader() {
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006982 for (DeclContextVisibleUpdatesPending::iterator
6983 I = PendingVisibleUpdates.begin(),
6984 E = PendingVisibleUpdates.end();
6985 I != E; ++I) {
6986 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6987 F = I->second.end();
6988 J != F; ++J)
Benjamin Kramer89f0b2d2012-04-15 12:36:49 +00006989 delete J->first;
Sebastian Redld7dce0a2010-08-24 00:50:04 +00006990 }
6991}