blob: 874d2c4b1b91e6205bab7da67bca48e74247e350 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-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 Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Chandler Carrutha2398d72011-12-09 00:02:23 +000017#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000018#include "ASTCommon.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000019#include "ASTReaderInternals.h"
Douglas Gregore737f502010-08-12 20:07:10 +000020#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000021#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000022#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000027#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000028#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000029#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000030#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000031#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Lex/Preprocessor.h"
Douglas Gregora71a7d82012-10-24 20:05:57 +000033#include "clang/Lex/PreprocessorOptions.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000034#include "clang/Lex/HeaderSearch.h"
Douglas Gregorbbf38312012-10-24 16:50:34 +000035#include "clang/Lex/HeaderSearchOptions.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000036#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000038#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000039#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000040#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000041#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000042#include "clang/Basic/TargetOptions.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000043#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000044#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000045#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000048#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000051#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000052#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000053#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000054#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000056#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000057
Douglas Gregor2cf26342009-04-09 22:27:44 +000058using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000059using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000060using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000061
62//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000063// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000064//===----------------------------------------------------------------------===//
65
Sebastian Redl571db7f2010-08-18 23:56:56 +000066ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000067
Douglas Gregor27ffa6c2012-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 Kyrtzidis11e51102009-06-19 00:03:23 +000083 }
84
Douglas Gregor27ffa6c2012-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 Gregor38295be2012-10-22 23:51:00 +000091 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000092
Douglas Gregor27ffa6c2012-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 Gregor7d5e81b2011-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 McCall260611a2012-06-20 06:18:46 +0000104
Douglas Gregor27ffa6c2012-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 McCall260611a2012-06-20 06:18:46 +0000109 return true;
110 }
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000111
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000112 return false;
113}
114
Douglas Gregor27ffa6c2012-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 Gregor38295be2012-10-22 23:51:00 +0000124#define CHECK_TARGET_OPT(Field, Name) \
125 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000126 if (Diags) \
127 Diags->Report(diag::err_pch_targetopt_mismatch) \
Douglas Gregor38295be2012-10-22 23:51:00 +0000128 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
129 return true; \
Douglas Gregor57016dd2012-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 Gregor27ffa6c2012-10-23 06:18:24 +0000141 ExistingTargetOpts.FeaturesAsWritten.begin(),
142 ExistingTargetOpts.FeaturesAsWritten.end());
Douglas Gregor57016dd2012-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 Gregor27ffa6c2012-10-23 06:18:24 +0000158 if (Diags)
159 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000160 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000161 return true;
162 }
163
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000164 if (Diags)
165 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000166 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000167 return true;
168 }
169
170 if (ExistingIdx < ExistingN) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000171 if (Diags)
172 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000173 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000174 return true;
175 }
176
177 if (ReadIdx < ReadN) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000178 if (Diags)
179 Diags->Report(diag::err_pch_targetopt_feature_mismatch)
Douglas Gregor38295be2012-10-22 23:51:00 +0000180 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000181 return true;
182 }
183
184 return false;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000185}
186
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000187bool
188PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
189 bool Complain) {
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000190 const LangOptions &ExistingLangOpts = PP.getLangOpts();
191 return checkLanguageOptions(LangOpts, ExistingLangOpts,
Douglas Gregor27ffa6c2012-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 Kramer54353f42010-11-25 18:29:30 +0000202namespace {
Douglas Gregor4c0c7e82012-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 Gregor87699242012-10-25 00:07:54 +0000250 DiagnosticsEngine *Diags,
251 FileManager &FileMgr,
252 std::string &SuggestedPredefines) {
Douglas Gregor4c0c7e82012-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 Gregor87699242012-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 Gregora9b8da42012-10-25 00:25:27 +0000280 SuggestedPredefines += ' ';
Douglas Gregor87699242012-10-25 00:07:54 +0000281 SuggestedPredefines += Existing.first.str();
282 SuggestedPredefines += '\n';
283 }
Douglas Gregor4c0c7e82012-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 Gregor87699242012-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 Gregor4c0c7e82012-10-24 23:41:50 +0000347 return false;
348}
349
350bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +0000351 bool Complain,
352 std::string &SuggestedPredefines) {
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000353 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
354
355 return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +0000356 Complain? &Reader.Diags : 0,
357 PP.getFileManager(),
358 SuggestedPredefines);
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000359}
360
Douglas Gregor12fab312010-03-16 16:35:32 +0000361void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
362 unsigned ID) {
363 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
364 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000365}
366
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000367void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 PP.setCounterValue(Value);
369}
370
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000372// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000373//===----------------------------------------------------------------------===//
374
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000375void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000376ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000377 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000378}
379
Chris Lattner4c6f9522009-04-27 05:14:47 +0000380
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000381
Douglas Gregor98339b92011-08-25 20:47:51 +0000382unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
383 return serialization::ComputeHash(Sel);
384}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000385
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregor98339b92011-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 Gregor35942772011-09-09 21:34:22 +0000398 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-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 Gregor98339b92011-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 Gregorf0aaf7a2009-04-24 21:10:55 +0000431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregor98339b92011-08-25 20:47:51 +0000433 // Load factory methods
Douglas Gregor98339b92011-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 Gregorf0aaf7a2009-04-24 21:10:55 +0000438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Douglas Gregor98339b92011-08-25 20:47:51 +0000440 return Result;
441}
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregor98339b92011-08-25 20:47:51 +0000443unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
444 return llvm::HashString(StringRef(a.first, a.second));
445}
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Douglas Gregor98339b92011-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 Gregorf0aaf7a2009-04-24 21:10:55 +0000454
Douglas Gregor98339b92011-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 Gregorf0aaf7a2009-04-24 21:10:55 +0000460
Douglas Gregor98339b92011-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 Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor98339b92011-08-25 20:47:51 +0000468 // Wipe out the "is interesting" bit.
469 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000470
Douglas Gregor98339b92011-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 Gregor668c1a42009-04-21 22:25:48 +0000475 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000476 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000477 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000478 KnownII = II;
479 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000480 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000481 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000482 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000483 return II;
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000486 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-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 Kornienko4d7e0ce2012-09-25 17:18:14 +0000496 bool hadMacroDefinition = Bits & 0x01;
497 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000498
Douglas Gregor98339b92011-08-25 20:47:51 +0000499 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000500 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000501
Douglas Gregor98339b92011-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 Gregor5d5051f2012-01-24 15:24:38 +0000505 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000506 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000507 KnownII = II;
508 }
Douglas Gregor057df202012-01-18 20:56:22 +0000509 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000510 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000511
Douglas Gregor98339b92011-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 Kyrtzidis5d267682010-08-20 16:04:27 +0000525
Douglas Gregor98339b92011-08-25 20:47:51 +0000526 // If this identifier is a macro, deserialize the macro
527 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000528 if (hadMacroDefinition) {
Douglas Gregor6c6c54a2012-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 Gregor13292642011-12-02 15:45:10 +0000533 }
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000534 DataLen -= 4;
535 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000536 }
537
Douglas Gregoreee242f2011-10-27 09:33:13 +0000538 Reader.SetIdentifierInfo(ID, II);
539
Douglas Gregor98339b92011-08-25 20:47:51 +0000540 // Read all of the declarations visible at global scope with this
541 // name.
Douglas Gregor98339b92011-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 Kyrtzidis5d267682010-08-20 16:04:27 +0000547 }
548
Douglas Gregor98339b92011-08-25 20:47:51 +0000549 return II;
550}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000551
Douglas Gregor98339b92011-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 Kyrtzidis5d267682010-08-20 16:04:27 +0000575 }
576
Douglas Gregor98339b92011-08-25 20:47:51 +0000577 return ID.ComputeHash();
578}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000579
Douglas Gregor98339b92011-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 Kyrtzidisa60786b2010-08-20 23:35:55 +0000606 }
607
Douglas Gregor98339b92011-08-25 20:47:51 +0000608 return Key;
609}
610
Douglas Gregor98339b92011-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. Spencer20249a12010-10-21 03:16:25 +0000618
Douglas Gregor98339b92011-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 Kyrtzidis5d267682010-08-20 16:04:27 +0000648 }
649
Douglas Gregor98339b92011-08-25 20:47:51 +0000650 return Key;
651}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000652
Douglas Gregor98339b92011-08-25 20:47:51 +0000653ASTDeclContextNameLookupTrait::data_type
654ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
655 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000656 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000657 using namespace clang::io;
658 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000659 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000660 return std::make_pair(Start, Start + NumDecls);
661}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000662
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000663bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000664 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-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 Kyrtzidiseb5e9982010-10-14 20:14:34 +0000682 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
683 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-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 Gregor0d95f772011-08-24 19:03:07 +0000703 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000704 }
705
706 return false;
707}
708
Chris Lattner5f9e2722011-07-23 10:55:15 +0000709void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000710 Error(diag::err_fe_pch_malformed, Msg);
711}
712
713void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000714 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000715 if (Diags.isDiagnosticInFlight())
716 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
717 else
718 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000719}
720
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000721//===----------------------------------------------------------------------===//
722// Source Manager Deserialization
723//===----------------------------------------------------------------------===//
724
Douglas Gregorbd945002009-04-13 16:31:14 +0000725/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000726/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000727bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000728 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000729 unsigned Idx = 0;
730 LineTableInfo &LineTable = SourceMgr.getLineTable();
731
732 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000733 std::map<int, int> FileIDs;
734 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-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 Gregorcaed0602012-10-18 21:31:35 +0000739 MaybeAddSystemRootToFilename(F, Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000740 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000741 }
742
743 // Parse the line entries
744 std::vector<LineEntry> Entries;
745 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000746 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-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 Gregorbd945002009-04-13 16:31:14 +0000750
751 // Extract the line entries
752 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000753 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-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 Kyrtzidisf52a5d22010-07-02 11:55:05 +0000759 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000760 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-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 Gregor47d9de62012-06-08 16:40:28 +0000766 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000767 }
768
769 return false;
770}
771
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000772namespace {
773
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000774class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000775public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000776 const ino_t ino;
777 const dev_t dev;
778 const mode_t mode;
779 const time_t mtime;
780 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000782 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000783 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000784};
785
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000786class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000787 public:
788 typedef const char *external_key_type;
789 typedef const char *internal_key_type;
790
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000791 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000792
793 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000794 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000795 }
796
797 static internal_key_type GetInternalKey(const char *path) { return path; }
798
799 static bool EqualKey(internal_key_type a, internal_key_type b) {
800 return strcmp(a, b) == 0;
801 }
802
803 static std::pair<unsigned, unsigned>
804 ReadKeyDataLength(const unsigned char*& d) {
805 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
806 unsigned DataLen = (unsigned) *d++;
807 return std::make_pair(KeyLen + 1, DataLen);
808 }
809
810 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
811 return (const char *)d;
812 }
813
814 static data_type ReadData(const internal_key_type, const unsigned char *d,
815 unsigned /*DataLen*/) {
816 using namespace clang::io;
817
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000818 ino_t ino = (ino_t) ReadUnalignedLE32(d);
819 dev_t dev = (dev_t) ReadUnalignedLE32(d);
820 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000821 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000822 off_t size = (off_t) ReadUnalignedLE64(d);
823 return data_type(ino, dev, mode, mtime, size);
824 }
825};
826
827/// \brief stat() cache for precompiled headers.
828///
829/// This cache is very similar to the stat cache used by pretokenized
830/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000831class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000832 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000833 CacheTy *Cache;
834
835 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000836public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000837 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
838 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000839 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
840 Cache = CacheTy::Create(Buckets, Base);
841 }
842
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000843 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Chris Lattner898a0612010-11-23 21:17:56 +0000845 LookupResult getStat(const char *Path, struct stat &StatBuf,
846 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000847 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000848 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000849
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000850 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000851 if (I == Cache->end()) {
852 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000853 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000857 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattner10e286a2010-11-23 19:19:34 +0000859 StatBuf.st_ino = Data.ino;
860 StatBuf.st_dev = Data.dev;
861 StatBuf.st_mtime = Data.mtime;
862 StatBuf.st_mode = Data.mode;
863 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000864 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000865 }
866};
867} // end anonymous namespace
868
869
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000870/// \brief Read a source manager block
Douglas Gregor4825fd72012-10-22 22:50:17 +0000871bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000872 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000873
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000874 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000875
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000876 // Set the source-location entry cursor to the current position in
877 // the stream. This cursor will be used to read the contents of the
878 // source manager block initially, and then lazily read
879 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000880 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000881
882 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000883 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000884 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000885 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000886 }
887
888 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000889 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000890 Error("malformed source manager block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000891 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000892 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000893
Douglas Gregor14f79002009-04-10 03:52:48 +0000894 RecordData Record;
895 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000896 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000897 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000898 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000899 Error("error at end of Source Manager block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000900 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000901 }
Douglas Gregor4825fd72012-10-22 22:50:17 +0000902 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +0000903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregor14f79002009-04-10 03:52:48 +0000905 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
906 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000907 SLocEntryCursor.ReadSubBlockID();
908 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000909 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000910 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000911 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000912 continue;
913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregor14f79002009-04-10 03:52:48 +0000915 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000916 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000917 continue;
918 }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregor14f79002009-04-10 03:52:48 +0000920 // Read a record.
921 const char *BlobStart;
922 unsigned BlobLen;
923 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000924 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000925 default: // Default behavior: ignore.
926 break;
927
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000928 case SM_SLOC_FILE_ENTRY:
929 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000930 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000931 // Once we hit one of the source location entries, we're done.
Douglas Gregor4825fd72012-10-22 22:50:17 +0000932 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +0000933 }
934 }
935}
936
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000937/// \brief If a header file is not found at the path that we expect it to be
938/// and the PCH file was moved from its original location, try to resolve the
939/// file by assuming that header+PCH were moved together and the header is in
940/// the same place relative to the PCH.
941static std::string
942resolveFileRelativeToOriginalDir(const std::string &Filename,
943 const std::string &OriginalDir,
944 const std::string &CurrDir) {
945 assert(OriginalDir != CurrDir &&
946 "No point trying to resolve the file if the PCH dir didn't change");
947 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000948 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000949 fs::make_absolute(filePath);
950 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000951 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000952
953 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
954 fileDirE = path::end(path::parent_path(filePath));
955 path::const_iterator origDirI = path::begin(OriginalDir),
956 origDirE = path::end(OriginalDir);
957 // Skip the common path components from filePath and OriginalDir.
958 while (fileDirI != fileDirE && origDirI != origDirE &&
959 *fileDirI == *origDirI) {
960 ++fileDirI;
961 ++origDirI;
962 }
963 for (; origDirI != origDirE; ++origDirI)
964 path::append(currPCHPath, "..");
965 path::append(currPCHPath, fileDirI, fileDirE);
966 path::append(currPCHPath, path::filename(Filename));
967 return currPCHPath.str();
968}
969
Douglas Gregor8b53d142012-10-22 22:53:10 +0000970bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000971 if (ID == 0)
Douglas Gregor4825fd72012-10-22 22:50:17 +0000972 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000973
Douglas Gregor0cdd7982011-07-21 18:46:38 +0000974 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000975 Error("source location entry ID out-of-range for AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000976 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000977 }
978
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000979 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000980 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +0000981 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000982 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +0000983
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000984 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000985 unsigned Code = SLocEntryCursor.ReadCode();
986 if (Code == llvm::bitc::END_BLOCK ||
987 Code == llvm::bitc::ENTER_SUBBLOCK ||
988 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000989 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000990 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000991 }
992
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000993 RecordData Record;
994 const char *BlobStart;
995 unsigned BlobLen;
996 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
997 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000998 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000999 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001000
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001001 case SM_SLOC_FILE_ENTRY: {
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001002 // We will detect whether a file changed and return 'Failure' for it, but
1003 // we will also try to fail gracefully by setting up the SLocEntry.
Douglas Gregora930dc92012-10-22 18:42:04 +00001004 unsigned InputID = Record[4];
1005 InputFile IF = getInputFile(*F, InputID);
1006 const FileEntry *File = IF.getPointer();
1007 bool OverriddenBuffer = IF.getInt();
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001008
Douglas Gregora930dc92012-10-22 18:42:04 +00001009 if (!IF.getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001010 return true;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001011
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001012 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001013 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001014 // This is the module's main file.
1015 IncludeLoc = getImportLocation(F);
1016 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001017 SrcMgr::CharacteristicKind
1018 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1019 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001020 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001021 SrcMgr::FileInfo &FileInfo =
1022 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora930dc92012-10-22 18:42:04 +00001023 FileInfo.NumCreatedFIDs = Record[5];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001024 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001025 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001026
Douglas Gregora930dc92012-10-22 18:42:04 +00001027 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1028 unsigned NumFileDecls = Record[7];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001029 if (NumFileDecls) {
1030 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001031 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1032 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001033 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001034
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001035 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001036 = SourceMgr.getOrCreateContentCache(File,
1037 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001038 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1039 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001040 unsigned Code = SLocEntryCursor.ReadCode();
1041 Record.clear();
1042 unsigned RecCode
1043 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1044
1045 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1046 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001047 return true;
Douglas Gregora081da52011-11-16 20:05:18 +00001048 }
1049
1050 llvm::MemoryBuffer *Buffer
1051 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Douglas Gregora930dc92012-10-22 18:42:04 +00001052 File->getName());
Douglas Gregora081da52011-11-16 20:05:18 +00001053 SourceMgr.overrideFileContents(File, Buffer);
1054 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001055
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001056 break;
1057 }
1058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001059 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 const char *Name = BlobStart;
1061 unsigned Offset = Record[0];
1062 unsigned Code = SLocEntryCursor.ReadCode();
1063 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001064 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001065 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001066
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001067 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001068 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001069 return true;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001070 }
1071
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001072 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001073 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1074 Name);
Douglas Gregor0ca6e272012-10-25 00:30:23 +00001075 SourceMgr.createFileIDForMemBuffer(Buffer, ID, BaseOffset + Offset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001076 break;
1077 }
1078
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001079 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001080 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001081 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001082 ReadSourceLocation(*F, Record[2]),
1083 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001084 Record[4],
1085 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001086 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001087 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001088 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001089 }
1090
Douglas Gregor4825fd72012-10-22 22:50:17 +00001091 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001092}
1093
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001094/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001095SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001096 if (F->ImportLoc.isValid())
1097 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001098
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001099 // Otherwise we have a PCH. It's considered to be "imported" at the first
1100 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001101 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001102 // Main file is the importer. We assume that it is the first entry in the
1103 // entry table. We can't ask the manager, because at the time of PCH loading
1104 // the main file entry doesn't exist yet.
1105 // The very first entry is the invalid instantiation loc, which takes up
1106 // offsets 0 and 1.
1107 return SourceLocation::getFromRawEncoding(2U);
1108 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001109 //return F->Loaders[0]->FirstLoc;
1110 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001111}
1112
Chris Lattner6367f6d2009-04-27 01:05:14 +00001113/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1114/// specified cursor. Read the abbreviations that are at the top of the block
1115/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001116bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001117 unsigned BlockID) {
1118 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001119 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001120 return Failure;
1121 }
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattner6367f6d2009-04-27 01:05:14 +00001123 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001124 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001125 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001126
Chris Lattner6367f6d2009-04-27 01:05:14 +00001127 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001128 if (Code != llvm::bitc::DEFINE_ABBREV) {
1129 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001130 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001131 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001132 Cursor.ReadAbbrevRecord();
1133 }
1134}
1135
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00001136void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1137 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001138 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregor37e26842009-04-21 23:56:24 +00001140 // Keep track of where we are in the stream, then jump back there
1141 // after reading this macro.
1142 SavedStreamPosition SavedPosition(Stream);
1143
1144 Stream.JumpToBit(Offset);
1145 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001146 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001147 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregore8219a62012-10-11 21:07:39 +00001149 // RAII object to add the loaded macro information once we're done
1150 // adding tokens.
1151 struct AddLoadedMacroInfoRAII {
1152 Preprocessor &PP;
1153 MacroInfo *Hint;
1154 MacroInfo *MI;
1155 IdentifierInfo *II;
1156
1157 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1158 : PP(PP), Hint(Hint), MI(), II() { }
1159 ~AddLoadedMacroInfoRAII( ) {
1160 if (MI) {
1161 // Finally, install the macro.
1162 PP.addLoadedMacroInfo(II, MI, Hint);
1163 }
1164 }
1165 } AddLoadedMacroInfo(PP, Hint);
1166
Douglas Gregor37e26842009-04-21 23:56:24 +00001167 while (true) {
1168 unsigned Code = Stream.ReadCode();
1169 switch (Code) {
1170 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001171 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001172
1173 case llvm::bitc::ENTER_SUBBLOCK:
1174 // No known subblocks, always skip them.
1175 Stream.ReadSubBlockID();
1176 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001177 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001178 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001179 }
1180 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Douglas Gregor37e26842009-04-21 23:56:24 +00001182 case llvm::bitc::DEFINE_ABBREV:
1183 Stream.ReadAbbrevRecord();
1184 continue;
1185 default: break;
1186 }
1187
1188 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001189 const char *BlobStart = 0;
1190 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001191 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001192 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001193 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001194 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001195 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001196 case PP_MACRO_OBJECT_LIKE:
1197 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001198 // If we already have a macro, that means that we've hit the end
1199 // of the definition of the macro we were looking for. We're
1200 // done.
1201 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001202 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001203
Douglas Gregor95eab172011-07-28 20:55:49 +00001204 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001205 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001206 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001207 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001208 }
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Douglas Gregora8235d62012-10-09 23:05:51 +00001210 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1211
1212 // If this macro has already been loaded, don't do so again.
1213 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1214 return;
1215
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001216 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1217 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001218 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001219 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001220
Douglas Gregora8235d62012-10-09 23:05:51 +00001221 // Record this macro.
1222 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1223
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001224 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1225 if (UndefLoc.isValid())
1226 MI->setUndefLoc(UndefLoc);
1227
1228 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001229 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001231 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001232 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001233
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001234 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001235 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001236 bool isC99VarArgs = Record[NextIndex++];
1237 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001238 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001239 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001240 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001241 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001242
1243 // Install function-like macro info.
1244 MI->setIsFunctionLike();
1245 if (isC99VarArgs) MI->setIsC99Varargs();
1246 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001247 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001248 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001249 }
1250
Douglas Gregora8235d62012-10-09 23:05:51 +00001251 if (DeserializationListener)
1252 DeserializationListener->MacroRead(GlobalID, MI);
1253
1254 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001255 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001256 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1257 if (Update != MacroUpdates.end()) {
1258 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001259 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1260 bool Hidden = false;
1261 if (unsigned SubmoduleID = Update->second[I].first) {
1262 if (Module *Owner = getSubmodule(SubmoduleID)) {
1263 if (Owner->NameVisibility == Module::Hidden) {
1264 // Note that this #undef is hidden.
1265 Hidden = true;
1266
1267 // Record this hiding for later.
1268 HiddenNamesMap[Owner].push_back(
1269 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1270 }
1271 }
1272 }
1273
1274 if (!Hidden) {
1275 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1276 if (PPMutationListener *Listener = PP.getPPMutationListener())
1277 Listener->UndefinedMacro(MI);
1278 break;
1279 }
1280 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001281 }
1282 MacroUpdates.erase(Update);
1283 }
1284
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001285 // Determine whether this macro definition is visible.
1286 bool Hidden = !MI->isPublic();
1287 if (!Hidden && GlobalSubmoduleID) {
1288 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1289 if (Owner->NameVisibility == Module::Hidden) {
1290 // The owning module is not visible, and this macro definition
1291 // should not be, either.
1292 Hidden = true;
1293
1294 // Note that this macro definition was hidden because its owning
1295 // module is not yet visible.
1296 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1297 }
1298 }
1299 }
1300 MI->setHidden(Hidden);
1301
Douglas Gregore8219a62012-10-11 21:07:39 +00001302 // Make sure we install the macro once we're done.
1303 AddLoadedMacroInfo.MI = MI;
1304 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001305
1306 // Remember that we saw this macro last so that we add the tokens that
1307 // form its body to it.
1308 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001309
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001310 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1311 Record[NextIndex]) {
1312 // We have a macro definition. Register the association
1313 PreprocessedEntityID
1314 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1315 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1316 PPRec.RegisterMacroDefinition(Macro,
1317 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001318 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001319
Douglas Gregor37e26842009-04-21 23:56:24 +00001320 ++NumMacrosRead;
1321 break;
1322 }
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001324 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001325 // If we see a TOKEN before a PP_MACRO_*, then the file is
1326 // erroneous, just pretend we didn't see this.
1327 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 Token Tok;
1330 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001331 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001332 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001333 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001334 Tok.setIdentifierInfo(II);
1335 Tok.setKind((tok::TokenKind)Record[3]);
1336 Tok.setFlag((Token::TokenFlags)Record[4]);
1337 Macro->AddTokenToBody(Tok);
1338 break;
1339 }
David Blaikie7530c032012-01-17 06:56:22 +00001340 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001341 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001342}
1343
Douglas Gregor86c67d82011-07-28 22:39:26 +00001344PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001345ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001346 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001347 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1348 assert(I != M.PreprocessedEntityRemap.end()
1349 && "Invalid index into preprocessed entity index remap");
1350
1351 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001352}
1353
Douglas Gregor98339b92011-08-25 20:47:51 +00001354unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1355 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001356}
Douglas Gregor98339b92011-08-25 20:47:51 +00001357
1358HeaderFileInfoTrait::internal_key_type
1359HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1360
1361bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1362 if (strcmp(a, b) == 0)
1363 return true;
1364
1365 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1366 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001367
1368 // Determine whether the actual files are equivalent.
1369 bool Result = false;
1370 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001371 return false;
1372
Douglas Gregor99a922b2011-12-09 16:22:07 +00001373 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001374}
1375
1376std::pair<unsigned, unsigned>
1377HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1378 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1379 unsigned DataLen = (unsigned) *d++;
1380 return std::make_pair(KeyLen + 1, DataLen);
1381}
1382
1383HeaderFileInfoTrait::data_type
1384HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1385 unsigned DataLen) {
1386 const unsigned char *End = d + DataLen;
1387 using namespace clang::io;
1388 HeaderFileInfo HFI;
1389 unsigned Flags = *d++;
1390 HFI.isImport = (Flags >> 5) & 0x01;
1391 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1392 HFI.DirInfo = (Flags >> 2) & 0x03;
1393 HFI.Resolved = (Flags >> 1) & 0x01;
1394 HFI.IndexHeaderMapHeader = Flags & 0x01;
1395 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001396 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1397 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001398 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1399 // The framework offset is 1 greater than the actual offset,
1400 // since 0 is used as an indicator for "no framework name".
1401 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1402 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1403 }
1404
1405 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1406 (void)End;
1407
1408 // This HeaderFileInfo was externally loaded.
1409 HFI.External = true;
1410 return HFI;
1411}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001412
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001413void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1414 II->setHadMacroDefinition(true);
1415 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1416 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001417}
1418
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001419void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001420 // Note that we are loading defined macros.
1421 Deserializing Macros(this);
1422
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001423 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1424 E = ModuleMgr.rend(); I != E; ++I) {
1425 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001426
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001427 // If there was no preprocessor block, skip this file.
1428 if (!MacroCursor.getBitStreamReader())
1429 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001430
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001431 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001432 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001433
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001434 RecordData Record;
1435 while (true) {
1436 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001437 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001438 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001439
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001440 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1441 // No known subblocks, always skip them.
1442 Cursor.ReadSubBlockID();
1443 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001444 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001445 return;
1446 }
1447 continue;
1448 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001449
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001450 if (Code == llvm::bitc::DEFINE_ABBREV) {
1451 Cursor.ReadAbbrevRecord();
1452 continue;
1453 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001454
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001455 // Read a record.
1456 const char *BlobStart;
1457 unsigned BlobLen;
1458 Record.clear();
1459 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1460 default: // Default behavior: ignore.
1461 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001462
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001463 case PP_MACRO_OBJECT_LIKE:
1464 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001465 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001466 break;
1467
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001468 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001469 // Ignore tokens.
1470 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001471 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001472 }
1473 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001474}
1475
Douglas Gregoreee242f2011-10-27 09:33:13 +00001476namespace {
1477 /// \brief Visitor class used to look up identifirs in an AST file.
1478 class IdentifierLookupVisitor {
1479 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001480 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001481 IdentifierInfo *Found;
1482 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001483 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1484 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001485
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001486 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001487 IdentifierLookupVisitor *This
1488 = static_cast<IdentifierLookupVisitor *>(UserData);
1489
Douglas Gregor057df202012-01-18 20:56:22 +00001490 // If we've already searched this module file, skip it now.
1491 if (M.Generation <= This->PriorGeneration)
1492 return true;
1493
Douglas Gregoreee242f2011-10-27 09:33:13 +00001494 ASTIdentifierLookupTable *IdTable
1495 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1496 if (!IdTable)
1497 return false;
1498
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001499 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1500 M, This->Found);
1501
Douglas Gregoreee242f2011-10-27 09:33:13 +00001502 std::pair<const char*, unsigned> Key(This->Name.begin(),
1503 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001504 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001505 if (Pos == IdTable->end())
1506 return false;
1507
1508 // Dereferencing the iterator has the effect of building the
1509 // IdentifierInfo node and populating it with the various
1510 // declarations it needs.
1511 This->Found = *Pos;
1512 return true;
1513 }
1514
1515 // \brief Retrieve the identifier info found within the module
1516 // files.
1517 IdentifierInfo *getIdentifierInfo() const { return Found; }
1518 };
1519}
1520
1521void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001522 // Note that we are loading an identifier.
1523 Deserializing AnIdentifier(this);
1524
Douglas Gregor057df202012-01-18 20:56:22 +00001525 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001526 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001527 PriorGeneration = IdentifierGeneration[&II];
1528
1529 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1530 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1531 markIdentifierUpToDate(&II);
1532}
1533
1534void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1535 if (!II)
1536 return;
1537
1538 II->setOutOfDate(false);
1539
1540 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001541 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001542 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001543}
1544
Douglas Gregora930dc92012-10-22 18:42:04 +00001545llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor38295be2012-10-22 23:51:00 +00001546ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregora930dc92012-10-22 18:42:04 +00001547 // If this ID is bogus, just return an empty input file.
1548 if (ID == 0 || ID > F.InputFilesLoaded.size())
1549 return InputFile();
1550
1551 // If we've already loaded this input file, return it.
1552 if (F.InputFilesLoaded[ID-1].getPointer())
1553 return F.InputFilesLoaded[ID-1];
1554
1555 // Go find this input file.
1556 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1557 SavedStreamPosition SavedPosition(Cursor);
1558 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1559
1560 unsigned Code = Cursor.ReadCode();
1561 RecordData Record;
1562 const char *BlobStart = 0;
1563 unsigned BlobLen = 0;
1564 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1565 &BlobStart, &BlobLen)) {
1566 case INPUT_FILE: {
1567 unsigned StoredID = Record[0];
1568 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi6b8194e2012-10-22 21:50:39 +00001569 (void)StoredID;
Douglas Gregora930dc92012-10-22 18:42:04 +00001570 off_t StoredSize = (off_t)Record[1];
1571 time_t StoredTime = (time_t)Record[2];
1572 bool Overridden = (bool)Record[3];
1573
1574 // Get the file entry for this input file.
1575 StringRef OrigFilename(BlobStart, BlobLen);
1576 std::string Filename = OrigFilename;
1577 MaybeAddSystemRootToFilename(F, Filename);
1578 const FileEntry *File
1579 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1580 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1581
1582 // If we didn't find the file, resolve it relative to the
1583 // original directory from which this AST file was created.
1584 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1585 F.OriginalDir != CurrentDir) {
1586 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1587 F.OriginalDir,
1588 CurrentDir);
1589 if (!Resolved.empty())
1590 File = FileMgr.getFile(Resolved);
1591 }
1592
1593 // For an overridden file, create a virtual file with the stored
1594 // size/timestamp.
1595 if (Overridden && File == 0) {
1596 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1597 }
1598
1599 if (File == 0) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001600 if (Complain) {
1601 std::string ErrorStr = "could not find file '";
1602 ErrorStr += Filename;
1603 ErrorStr += "' referenced by AST file";
1604 Error(ErrorStr.c_str());
1605 }
Douglas Gregora930dc92012-10-22 18:42:04 +00001606 return InputFile();
1607 }
1608
1609 // Note that we've loaded this input file.
1610 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1611
1612 // Check if there was a request to override the contents of the file
1613 // that was part of the precompiled header. Overridding such a file
1614 // can lead to problems when lexing using the source locations from the
1615 // PCH.
1616 SourceManager &SM = getSourceManager();
1617 if (!Overridden && SM.isFileOverridden(File)) {
1618 Error(diag::err_fe_pch_file_overridden, Filename);
1619 // After emitting the diagnostic, recover by disabling the override so
1620 // that the original file will be used.
1621 SM.disableFileContentsOverride(File);
1622 // The FileEntry is a virtual file entry with the size of the contents
1623 // that would override the original contents. Set it to the original's
1624 // size/time.
1625 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1626 StoredSize, StoredTime);
1627 }
1628
1629 // For an overridden file, there is nothing to validate.
1630 if (Overridden)
1631 return InputFile(File, Overridden);
1632
1633 // The stat info from the FileEntry came from the cached stat
1634 // info of the PCH, so we cannot trust it.
1635 struct stat StatBuf;
1636 if (::stat(File->getName(), &StatBuf) != 0) {
1637 StatBuf.st_size = File->getSize();
1638 StatBuf.st_mtime = File->getModificationTime();
1639 }
1640
1641 if ((StoredSize != StatBuf.st_size
1642#if !defined(LLVM_ON_WIN32)
1643 // In our regression testing, the Windows file system seems to
1644 // have inconsistent modification times that sometimes
1645 // erroneously trigger this error-handling path.
1646 || StoredTime != StatBuf.st_mtime
1647#endif
1648 )) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001649 if (Complain)
1650 Error(diag::err_fe_pch_file_modified, Filename);
1651
Douglas Gregora930dc92012-10-22 18:42:04 +00001652 return InputFile();
1653 }
1654
1655 return InputFile(File, Overridden);
1656 }
1657 }
1658
1659 return InputFile();
1660}
1661
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor69e16082012-10-18 21:47:16 +00001663 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001664 std::string Filename = filenameStrRef;
Douglas Gregor69e16082012-10-18 21:47:16 +00001665 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001666 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor69e16082012-10-18 21:47:16 +00001667 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1668 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001669 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor69e16082012-10-18 21:47:16 +00001670 M.OriginalDir,
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001671 CurrentDir);
1672 if (!resolved.empty())
1673 File = FileMgr.getFile(resolved);
1674 }
1675
1676 return File;
1677}
1678
Douglas Gregore650c8c2009-07-07 00:12:59 +00001679/// \brief If we are loading a relocatable PCH file, and the filename is
1680/// not an absolute path, add the system root to the beginning of the file
1681/// name.
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001682void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1683 std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001684 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregorcaed0602012-10-18 21:31:35 +00001685 if (!M.RelocatablePCH)
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001686 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Michael J. Spencer256053b2010-12-17 21:22:22 +00001688 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001689 return;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001690
Douglas Gregor832d6202011-07-22 16:35:34 +00001691 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001692 // If no system root was given, default to '/'
1693 Filename.insert(Filename.begin(), '/');
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001694 return;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001695 }
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Douglas Gregor832d6202011-07-22 16:35:34 +00001697 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001698 if (isysroot[Length - 1] != '/')
1699 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Douglas Gregor832d6202011-07-22 16:35:34 +00001701 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001702}
1703
Douglas Gregor38295be2012-10-22 23:51:00 +00001704ASTReader::ASTReadResult
1705ASTReader::ReadControlBlock(ModuleFile &F,
1706 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
1707 unsigned ClientLoadCapabilities) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001708 llvm::BitstreamCursor &Stream = F.Stream;
1709
1710 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1711 Error("malformed block record in AST file");
1712 return Failure;
1713 }
1714
1715 // Read all of the records and blocks in the control block.
1716 RecordData Record;
1717 while (!Stream.AtEndOfStream()) {
1718 unsigned Code = Stream.ReadCode();
1719 if (Code == llvm::bitc::END_BLOCK) {
1720 if (Stream.ReadBlockEnd()) {
1721 Error("error at end of control block in AST file");
1722 return Failure;
1723 }
1724
Douglas Gregora930dc92012-10-22 18:42:04 +00001725 // Validate all of the input files.
1726 if (!DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001727 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregora930dc92012-10-22 18:42:04 +00001728 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor38295be2012-10-22 23:51:00 +00001729 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001730 return OutOfDate;
Douglas Gregora930dc92012-10-22 18:42:04 +00001731 }
1732
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001733 return Success;
1734 }
1735
1736 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001737 switch (Stream.ReadSubBlockID()) {
1738 case INPUT_FILES_BLOCK_ID:
Douglas Gregora930dc92012-10-22 18:42:04 +00001739 F.InputFilesCursor = Stream;
1740 if (Stream.SkipBlock() || // Skip with the main cursor
1741 // Read the abbreviations
1742 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1743 Error("malformed block record in AST file");
Douglas Gregor745e6f12012-10-19 00:38:02 +00001744 return Failure;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001745 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001746 continue;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001747
1748 default:
1749 if (!Stream.SkipBlock())
1750 continue;
1751 break;
1752 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001753
1754 Error("malformed block record in AST file");
1755 return Failure;
1756 }
1757
1758 if (Code == llvm::bitc::DEFINE_ABBREV) {
1759 Stream.ReadAbbrevRecord();
1760 continue;
1761 }
1762
1763 // Read and process a record.
1764 Record.clear();
1765 const char *BlobStart = 0;
1766 unsigned BlobLen = 0;
1767 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1768 &BlobStart, &BlobLen)) {
1769 case METADATA: {
1770 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001771 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1772 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1773 : diag::warn_pch_version_too_new);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001774 return VersionMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001775 }
1776
1777 bool hasErrors = Record[5];
1778 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1779 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001780 return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001781 }
1782
Douglas Gregorcaed0602012-10-18 21:31:35 +00001783 F.RelocatablePCH = Record[4];
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001784
1785 const std::string &CurBranch = getClangFullRepositoryVersion();
1786 StringRef ASTBranch(BlobStart, BlobLen);
1787 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001788 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1789 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor4825fd72012-10-22 22:50:17 +00001790 return VersionMismatch;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001791 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001792 break;
1793 }
1794
1795 case IMPORTS: {
1796 // Load each of the imported PCH files.
1797 unsigned Idx = 0, N = Record.size();
1798 while (Idx < N) {
1799 // Read information about the AST file.
1800 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1801 unsigned Length = Record[Idx++];
1802 SmallString<128> ImportedFile(Record.begin() + Idx,
1803 Record.begin() + Idx + Length);
1804 Idx += Length;
1805
1806 // Load the AST file.
Douglas Gregor38295be2012-10-22 23:51:00 +00001807 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
1808 ClientLoadCapabilities)) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001809 case Failure: return Failure;
1810 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001811 case OutOfDate: return OutOfDate;
1812 case VersionMismatch: return VersionMismatch;
1813 case ConfigurationMismatch: return ConfigurationMismatch;
1814 case HadErrors: return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001815 case Success: break;
1816 }
1817 }
1818 break;
1819 }
1820
Douglas Gregor38295be2012-10-22 23:51:00 +00001821 case LANGUAGE_OPTIONS: {
1822 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1823 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001824 ParseLanguageOptions(Record, Complain, *Listener) &&
1825 !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001826 return ConfigurationMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001827 break;
Douglas Gregor38295be2012-10-22 23:51:00 +00001828 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001829
Douglas Gregoree097c12012-10-18 17:58:09 +00001830 case TARGET_OPTIONS: {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001831 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1832 if (Listener && &F == *ModuleMgr.begin() &&
1833 ParseTargetOptions(Record, Complain, *Listener) &&
1834 !DisableValidation)
1835 return ConfigurationMismatch;
Douglas Gregoree097c12012-10-18 17:58:09 +00001836 break;
1837 }
1838
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001839 case DIAGNOSTIC_OPTIONS: {
1840 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1841 if (Listener && &F == *ModuleMgr.begin() &&
1842 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1843 !DisableValidation)
1844 return ConfigurationMismatch;
1845 break;
1846 }
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001847
1848 case FILE_SYSTEM_OPTIONS: {
1849 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1850 if (Listener && &F == *ModuleMgr.begin() &&
1851 ParseFileSystemOptions(Record, Complain, *Listener) &&
1852 !DisableValidation)
1853 return ConfigurationMismatch;
1854 break;
1855 }
1856
Douglas Gregorbbf38312012-10-24 16:50:34 +00001857 case HEADER_SEARCH_OPTIONS: {
1858 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1859 if (Listener && &F == *ModuleMgr.begin() &&
1860 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1861 !DisableValidation)
1862 return ConfigurationMismatch;
1863 break;
1864 }
1865
Douglas Gregora71a7d82012-10-24 20:05:57 +00001866 case PREPROCESSOR_OPTIONS: {
1867 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1868 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor87699242012-10-25 00:07:54 +00001869 ParsePreprocessorOptions(Record, Complain, *Listener,
1870 SuggestedPredefines) &&
Douglas Gregora71a7d82012-10-24 20:05:57 +00001871 !DisableValidation)
1872 return ConfigurationMismatch;
1873 break;
1874 }
1875
Douglas Gregor39c497b2012-10-18 18:36:53 +00001876 case ORIGINAL_FILE:
Douglas Gregor69e16082012-10-18 21:47:16 +00001877 F.OriginalSourceFileID = FileID::get(Record[0]);
1878 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
1879 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1880 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001881 break;
1882
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001883 case ORIGINAL_PCH_DIR:
Douglas Gregor69e16082012-10-18 21:47:16 +00001884 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001885 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001886
Douglas Gregora930dc92012-10-22 18:42:04 +00001887 case INPUT_FILE_OFFSETS:
1888 F.InputFileOffsets = (const uint32_t *)BlobStart;
1889 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001890 break;
1891 }
Douglas Gregor745e6f12012-10-19 00:38:02 +00001892 }
1893
1894 Error("premature end of bitstream in AST file");
1895 return Failure;
1896}
1897
Douglas Gregor4825fd72012-10-22 22:50:17 +00001898bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001899 llvm::BitstreamCursor &Stream = F.Stream;
1900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001901 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001902 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001903 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001904 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001905
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001906 // Read all of the records and blocks for the AST file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001907 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001908 while (!Stream.AtEndOfStream()) {
1909 unsigned Code = Stream.ReadCode();
1910 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001911 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001912 Error("error at end of module block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001913 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001914 }
Chris Lattner7356a312009-04-11 21:15:38 +00001915
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001916 DeclContext *DC = Context.getTranslationUnitDecl();
1917 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1918 DC->setMustBuildLookupTable();
1919
Douglas Gregor4825fd72012-10-22 22:50:17 +00001920 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001921 }
1922
1923 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1924 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001926 // We lazily load the decls block, but we want to set up the
1927 // DeclsCursor cursor to point into it. Clone our current bitcode
1928 // cursor to it, enter the block and read the abbrevs in that block.
1929 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001930 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001931 if (Stream.SkipBlock() || // Skip with the main cursor.
1932 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001933 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001934 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001935 return true;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001936 }
1937 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001939 case DECL_UPDATES_BLOCK_ID:
1940 if (Stream.SkipBlock()) {
1941 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001942 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001943 }
1944 break;
1945
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001946 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001947 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001948 if (!PP.getExternalSource())
1949 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001950
Douglas Gregorecdcb882010-10-20 22:00:55 +00001951 if (Stream.SkipBlock() ||
1952 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001953 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001954 return true;
Chris Lattner7356a312009-04-11 21:15:38 +00001955 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001956 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001957 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001958
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001959 case PREPROCESSOR_DETAIL_BLOCK_ID:
1960 F.PreprocessorDetailCursor = Stream;
1961 if (Stream.SkipBlock() ||
1962 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1963 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1964 Error("malformed preprocessor detail record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001965 return true;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001966 }
1967 F.PreprocessorDetailStartOffset
1968 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001969
1970 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001971 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001972 if (!PP.getPreprocessingRecord()->getExternalSource())
1973 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001974 break;
1975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00001977 if (ReadSourceManagerBlock(F))
1978 return true;
Douglas Gregor14f79002009-04-10 03:52:48 +00001979 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001980
1981 case SUBMODULE_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00001982 if (ReadSubmoduleBlock(F))
1983 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001984 break;
1985
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001986 case COMMENTS_BLOCK_ID: {
1987 llvm::BitstreamCursor C = Stream;
1988 if (Stream.SkipBlock() ||
1989 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1990 Error("malformed comments block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001991 return true;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001992 }
1993 CommentsCursors.push_back(std::make_pair(C, &F));
1994 break;
1995 }
1996
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001997 default:
1998 if (!Stream.SkipBlock())
1999 break;
2000 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002001 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002002 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002003 continue;
2004 }
2005
2006 if (Code == llvm::bitc::DEFINE_ABBREV) {
2007 Stream.ReadAbbrevRecord();
2008 continue;
2009 }
2010
2011 // Read and process a record.
2012 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00002013 const char *BlobStart = 0;
2014 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002015 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00002016 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00002017 default: // Default behavior: ignore.
2018 break;
2019
Douglas Gregora119da02011-08-02 16:26:37 +00002020 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002021 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002022 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002023 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002024 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002025 F.TypeOffsets = (const uint32_t *)BlobStart;
2026 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00002027 unsigned LocalBaseTypeIndex = Record[1];
2028 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00002029
Douglas Gregora119da02011-08-02 16:26:37 +00002030 if (F.LocalNumTypes > 0) {
2031 // Introduce the global -> local mapping for types within this module.
2032 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2033
2034 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002035 F.TypeRemap.insertOrReplace(
2036 std::make_pair(LocalBaseTypeIndex,
2037 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00002038
2039 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2040 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002041 break;
Douglas Gregora119da02011-08-02 16:26:37 +00002042 }
2043
Douglas Gregor496c7092011-08-03 15:48:04 +00002044 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002045 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002046 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002047 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002048 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00002049 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002050 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00002051 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002052 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00002053
Douglas Gregor496c7092011-08-03 15:48:04 +00002054 if (F.LocalNumDecls > 0) {
2055 // Introduce the global -> local mapping for declarations within this
2056 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002057 GlobalDeclMap.insert(
2058 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00002059
2060 // Introduce the local -> global mapping for declarations within this
2061 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002062 F.DeclRemap.insertOrReplace(
2063 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00002064
Douglas Gregora1be2782011-12-17 23:38:30 +00002065 // Introduce the global -> local mapping for declarations within this
2066 // module.
2067 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2068
Douglas Gregor496c7092011-08-03 15:48:04 +00002069 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2070 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002071 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00002072 }
2073
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002074 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00002075 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002076 DeclContextInfo &Info = F.DeclContextInfos[TU];
2077 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
2078 Info.NumLexicalDecls
2079 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00002080 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00002081 break;
2082 }
2083
Sebastian Redle1dde812010-08-24 00:50:04 +00002084 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00002085 unsigned Idx = 0;
2086 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00002087 ASTDeclContextNameLookupTable *Table =
2088 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00002089 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00002090 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00002091 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00002092 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2093 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002094 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002095 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00002096 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00002097 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00002098 break;
2099 }
2100
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002101 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002102 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002103 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002104 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002105 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002106 (const unsigned char *)F.IdentifierTableData + Record[0],
2107 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002108 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002109
2110 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002111 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002112 break;
2113
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002114 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002115 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002116 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002117 return true;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002118 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002119 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2120 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002121 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002122 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00002123
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002124 if (F.LocalNumIdentifiers > 0) {
2125 // Introduce the global -> local mapping for identifiers within this
2126 // module.
2127 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2128 &F));
2129
2130 // Introduce the local -> global mapping for identifiers within this
2131 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002132 F.IdentifierRemap.insertOrReplace(
2133 std::make_pair(LocalBaseIdentifierID,
2134 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002135
2136 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2137 + F.LocalNumIdentifiers);
2138 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002139 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002140 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002141
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002142 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002143 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2144 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002145 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002146
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002147 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002148 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2149 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002150 break;
2151
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002152 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002153 TotalNumStatements += Record[0];
2154 TotalNumMacros += Record[1];
2155 TotalLexicalDeclContexts += Record[2];
2156 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002157 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002158
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002159 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002160 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2161 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002162 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002163
Sean Huntebcbe1d2011-05-04 23:29:54 +00002164 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002165 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2166 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002167 break;
2168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002170 if (Record.size() % 4 != 0) {
2171 Error("invalid weak identifiers record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002172 return true;
Douglas Gregor31e37b22011-07-28 18:09:57 +00002173 }
2174
2175 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2176 // files. This isn't the way to do it :)
2177 WeakUndeclaredIdentifiers.clear();
2178
2179 // Translate the weak, undeclared identifiers into global IDs.
2180 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2181 WeakUndeclaredIdentifiers.push_back(
2182 getGlobalIdentifierID(F, Record[I++]));
2183 WeakUndeclaredIdentifiers.push_back(
2184 getGlobalIdentifierID(F, Record[I++]));
2185 WeakUndeclaredIdentifiers.push_back(
2186 ReadSourceLocation(F, Record, I).getRawEncoding());
2187 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2188 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002189 break;
2190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002191 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002192 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2193 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002194 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002195
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002196 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002197 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002198 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002199 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002200 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002201
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002202 if (F.LocalNumSelectors > 0) {
2203 // Introduce the global -> local mapping for selectors within this
2204 // module.
2205 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2206
2207 // Introduce the local -> global mapping for selectors within this
2208 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002209 F.SelectorRemap.insertOrReplace(
2210 std::make_pair(LocalBaseSelectorID,
2211 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002212
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002213 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2214 }
2215 break;
2216 }
2217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002218 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002219 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002220 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002221 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002222 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002223 F.SelectorLookupTableData + Record[0],
2224 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002225 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002226 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002227 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002228
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002229 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002230 if (!Record.empty()) {
2231 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2232 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2233 Record[Idx++]));
2234 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2235 getRawEncoding());
2236 }
2237 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002238 break;
2239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002240 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002241 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002242 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002243 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002244
2245 case FILE_SORTED_DECLS:
2246 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002247 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002248 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002249
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002250 case SOURCE_LOCATION_OFFSETS: {
2251 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002252 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002253 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002254 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002255 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2256 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002257 // Make our entry in the range map. BaseID is negative and growing, so
2258 // we invert it. Because we invert it, though, we need the other end of
2259 // the range.
2260 unsigned RangeStart =
2261 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2262 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2263 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2264
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002265 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2266 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2267 GlobalSLocOffsetMap.insert(
2268 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2269 - SLocSpaceSize,&F));
2270
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002271 // Initialize the remapping table.
2272 // Invalid stays invalid.
2273 F.SLocRemap.insert(std::make_pair(0U, 0));
2274 // This module. Base was 2 when being compiled.
2275 F.SLocRemap.insert(std::make_pair(2U,
2276 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002277
2278 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002279 break;
2280 }
2281
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002282 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002283 // Additional remapping information.
2284 const unsigned char *Data = (const unsigned char*)BlobStart;
2285 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002286
2287 // Continuous range maps we may be updating in our module.
2288 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002289 ContinuousRangeMap<uint32_t, int, 2>::Builder
2290 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002291 ContinuousRangeMap<uint32_t, int, 2>::Builder
2292 MacroRemap(F.MacroRemap);
2293 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002294 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2295 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002296 SubmoduleRemap(F.SubmoduleRemap);
2297 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002298 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002299 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002300 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2301
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002302 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002303 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002304 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002305 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002306 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002307 if (!OM) {
2308 Error("SourceLocation remap refers to unknown module");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002309 return true;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002310 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002311
2312 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2313 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002314 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002315 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002316 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002317 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2318 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002319 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002320
2321 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2322 SLocRemap.insert(std::make_pair(SLocOffset,
2323 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002324 IdentifierRemap.insert(
2325 std::make_pair(IdentifierIDOffset,
2326 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002327 MacroRemap.insert(std::make_pair(MacroIDOffset,
2328 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002329 PreprocessedEntityRemap.insert(
2330 std::make_pair(PreprocessedEntityIDOffset,
2331 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002332 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2333 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002334 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2335 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002336 DeclRemap.insert(std::make_pair(DeclIDOffset,
2337 OM->BaseDeclID - DeclIDOffset));
2338
Douglas Gregora119da02011-08-02 16:26:37 +00002339 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002340 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002341
2342 // Global -> local mappings.
2343 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002344 }
2345 break;
2346 }
2347
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002348 case SOURCE_MANAGER_LINE_TABLE:
2349 if (ParseLineTable(F, Record))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002350 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002351 break;
2352
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002353 case SOURCE_LOCATION_PRELOADS: {
2354 // Need to transform from the local view (1-based IDs) to the global view,
2355 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002356 if (!F.PreloadSLocEntries.empty()) {
2357 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002358 return true;
Douglas Gregorf249bf32011-08-25 21:09:44 +00002359 }
2360
2361 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002362 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002363 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002364
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002365 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002366 if (!DisableStatCache) {
2367 ASTStatCache *MyStatCache =
2368 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2369 (const unsigned char *)BlobStart,
2370 NumStatHits, NumStatMisses);
2371 FileMgr.addStatCache(MyStatCache);
2372 F.StatCache = MyStatCache;
2373 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002374 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002375 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002376
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002377 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002378 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2379 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002380 break;
2381
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002382 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002383 if (Record.size() % 3 != 0) {
2384 Error("Invalid VTABLE_USES record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002385 return true;
Douglas Gregordfe65432011-07-28 19:11:31 +00002386 }
2387
Sebastian Redl40566802010-08-05 18:21:25 +00002388 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002389 // FIXME: Modules will have some trouble with this. This is clearly not
2390 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002391 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002392
2393 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2394 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2395 VTableUses.push_back(
2396 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2397 VTableUses.push_back(Record[Idx++]);
2398 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002399 break;
2400
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002401 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002402 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2403 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002404 break;
2405
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002406 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002407 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002408 Error("Invalid existing PendingInstantiations");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002409 return true;
Axel Naumann39d26c32012-10-02 09:09:43 +00002410 }
2411
2412 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002413 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002414 return true;
Douglas Gregorf2abb522011-07-28 19:26:52 +00002415 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002416
Douglas Gregorf2abb522011-07-28 19:26:52 +00002417 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2418 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2419 PendingInstantiations.push_back(
2420 ReadSourceLocation(F, Record, I).getRawEncoding());
2421 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002422 break;
2423
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002424 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002425 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002426 // FIXME: Modules will have some trouble with this.
2427 SemaDeclRefs.clear();
2428 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2429 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002430 break;
2431
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002432 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002433 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2434 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2435 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002436
2437 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002438
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002439 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002440 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002441 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002442 if (!PP.getPreprocessingRecord()->getExternalSource())
2443 PP.getPreprocessingRecord()->SetExternalSource(*this);
2444 StartingID
2445 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002446 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002447 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002448
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002449 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002450 // Introduce the global -> local mapping for preprocessed entities in
2451 // this module.
2452 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2453
2454 // Introduce the local -> global mapping for preprocessed entities in
2455 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002456 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002457 std::make_pair(LocalBasePreprocessedEntityID,
2458 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2459 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002460
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002461 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002462 }
2463
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002464 case DECL_UPDATE_OFFSETS: {
2465 if (Record.size() % 2 != 0) {
2466 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002467 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002468 }
2469 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002470 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2471 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002472 break;
2473 }
2474
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002476 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002477 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002478 return true;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002479 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002480 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002481 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002482 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002483 break;
2484 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002485
Douglas Gregorcff9f262012-01-27 01:47:08 +00002486 case OBJC_CATEGORIES_MAP: {
2487 if (F.LocalNumObjCCategoriesInMap != 0) {
2488 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002489 return true;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002490 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002491
2492 F.LocalNumObjCCategoriesInMap = Record[0];
2493 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002494 break;
2495 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002496
Douglas Gregorcff9f262012-01-27 01:47:08 +00002497 case OBJC_CATEGORIES:
2498 F.ObjCCategories.swap(Record);
2499 break;
2500
Douglas Gregor7c789c12010-10-29 22:39:52 +00002501 case CXX_BASE_SPECIFIER_OFFSETS: {
2502 if (F.LocalNumCXXBaseSpecifiers != 0) {
2503 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002504 return true;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002505 }
2506
2507 F.LocalNumCXXBaseSpecifiers = Record[0];
2508 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002509 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002510 break;
2511 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002512
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002513 case DIAG_PRAGMA_MAPPINGS:
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002514 if (F.PragmaDiagMappings.empty())
2515 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002516 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002517 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2518 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002519 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002520
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002521 case CUDA_SPECIAL_DECL_REFS:
2522 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002523 // FIXME: Modules will have trouble with this.
2524 CUDASpecialDeclRefs.clear();
2525 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2526 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002527 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002528
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002529 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002530 F.HeaderFileInfoTableData = BlobStart;
2531 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002532 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002533 if (Record[0]) {
2534 F.HeaderFileInfoTable
2535 = HeaderFileInfoLookupTable::Create(
2536 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002537 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002538 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002539 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002540 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002541
2542 PP.getHeaderSearchInfo().SetExternalSource(this);
2543 if (!PP.getHeaderSearchInfo().getExternalLookup())
2544 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002545 }
2546 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002547 }
2548
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002549 case FP_PRAGMA_OPTIONS:
2550 // Later tables overwrite earlier ones.
2551 FPPragmaOptions.swap(Record);
2552 break;
2553
2554 case OPENCL_EXTENSIONS:
2555 // Later tables overwrite earlier ones.
2556 OpenCLExtensions.swap(Record);
2557 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002558
2559 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002560 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2561 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002562 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002563
2564 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002565 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2566 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002567 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002568
2569 case IMPORTED_MODULES: {
2570 if (F.Kind != MK_Module) {
2571 // If we aren't loading a module (which has its own exports), make
2572 // all of the imported modules visible.
2573 // FIXME: Deal with macros-only imports.
2574 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2575 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2576 ImportedModules.push_back(GlobalID);
2577 }
2578 }
2579 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002580 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002581
Douglas Gregora1be2782011-12-17 23:38:30 +00002582 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002583 F.RedeclarationChains.swap(Record);
2584 break;
2585 }
2586
2587 case LOCAL_REDECLARATIONS_MAP: {
2588 if (F.LocalNumRedeclarationsInMap != 0) {
2589 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002590 return true;
Douglas Gregora1be2782011-12-17 23:38:30 +00002591 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002592
Douglas Gregor2171bf12012-01-15 16:58:34 +00002593 F.LocalNumRedeclarationsInMap = Record[0];
2594 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002595 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002596 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002597
2598 case MERGED_DECLARATIONS: {
2599 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2600 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2601 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2602 for (unsigned N = Record[Idx++]; N > 0; --N)
2603 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2604 }
2605 break;
2606 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002607
2608 case MACRO_OFFSET: {
2609 if (F.LocalNumMacros != 0) {
2610 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002611 return true;
Douglas Gregora8235d62012-10-09 23:05:51 +00002612 }
2613 F.MacroOffsets = (const uint32_t *)BlobStart;
2614 F.LocalNumMacros = Record[0];
2615 unsigned LocalBaseMacroID = Record[1];
2616 F.BaseMacroID = getTotalNumMacros();
2617
2618 if (F.LocalNumMacros > 0) {
2619 // Introduce the global -> local mapping for macros within this module.
2620 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2621
2622 // Introduce the local -> global mapping for macros within this module.
2623 F.MacroRemap.insertOrReplace(
2624 std::make_pair(LocalBaseMacroID,
2625 F.BaseMacroID - LocalBaseMacroID));
2626
2627 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2628 }
2629 break;
2630 }
2631
2632 case MACRO_UPDATES: {
2633 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2634 MacroID ID = getGlobalMacroID(F, Record[I++]);
2635 if (I == N)
2636 break;
2637
Douglas Gregor54c8a402012-10-12 00:16:50 +00002638 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2639 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2640 MacroUpdate Update;
2641 Update.UndefLoc = UndefLoc;
2642 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002643 }
2644 break;
2645 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002646 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002647 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002648 Error("premature end of bitstream in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002649 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002650}
2651
Douglas Gregorecc2c092011-12-01 22:20:10 +00002652void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002653 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002654 switch (Names[I].getKind()) {
2655 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002656 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002657 break;
2658
2659 case HiddenName::MacroVisibility: {
2660 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2661 Macro.second->setHidden(!Macro.second->isPublic());
2662 if (Macro.second->isDefined()) {
2663 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2664 }
2665 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002666 }
2667
Douglas Gregor54c8a402012-10-12 00:16:50 +00002668 case HiddenName::MacroUndef: {
2669 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2670 if (Macro.second->isDefined()) {
2671 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2672 if (PPMutationListener *Listener = PP.getPPMutationListener())
2673 Listener->UndefinedMacro(Macro.second);
2674 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2675 }
2676 break;
2677 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002678 }
Douglas Gregor13292642011-12-02 15:45:10 +00002679 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002680}
2681
Douglas Gregor5e356932011-12-01 17:11:21 +00002682void ASTReader::makeModuleVisible(Module *Mod,
2683 Module::NameVisibilityKind NameVisibility) {
2684 llvm::SmallPtrSet<Module *, 4> Visited;
2685 llvm::SmallVector<Module *, 4> Stack;
2686 Stack.push_back(Mod);
2687 while (!Stack.empty()) {
2688 Mod = Stack.back();
2689 Stack.pop_back();
2690
2691 if (NameVisibility <= Mod->NameVisibility) {
2692 // This module already has this level of visibility (or greater), so
2693 // there is nothing more to do.
2694 continue;
2695 }
2696
Douglas Gregor51f564f2011-12-31 04:05:44 +00002697 if (!Mod->isAvailable()) {
2698 // Modules that aren't available cannot be made visible.
2699 continue;
2700 }
2701
Douglas Gregor5e356932011-12-01 17:11:21 +00002702 // Update the module's name visibility.
2703 Mod->NameVisibility = NameVisibility;
2704
Douglas Gregorecc2c092011-12-01 22:20:10 +00002705 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002706 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002707 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2708 if (Hidden != HiddenNamesMap.end()) {
2709 makeNamesVisible(Hidden->second);
2710 HiddenNamesMap.erase(Hidden);
2711 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002712
2713 // Push any non-explicit submodules onto the stack to be marked as
2714 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002715 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2716 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002717 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002718 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2719 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002720 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002721
2722 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002723 bool AnyWildcard = false;
2724 bool UnrestrictedWildcard = false;
2725 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002726 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2727 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002728 if (!Mod->Exports[I].getInt()) {
2729 // Export a named module directly; no wildcards involved.
2730 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002731 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002732
2733 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002734 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002735
2736 // Wildcard export: export all of the imported modules that match
2737 // the given pattern.
2738 AnyWildcard = true;
2739 if (UnrestrictedWildcard)
2740 continue;
2741
2742 if (Module *Restriction = Mod->Exports[I].getPointer())
2743 WildcardRestrictions.push_back(Restriction);
2744 else {
2745 WildcardRestrictions.clear();
2746 UnrestrictedWildcard = true;
2747 }
2748 }
2749
2750 // If there were any wildcards, push any imported modules that were
2751 // re-exported by the wildcard restriction.
2752 if (!AnyWildcard)
2753 continue;
2754
2755 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2756 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002757 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002758 continue;
2759
2760 bool Acceptable = UnrestrictedWildcard;
2761 if (!Acceptable) {
2762 // Check whether this module meets one of the restrictions.
2763 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2764 Module *Restriction = WildcardRestrictions[R];
2765 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2766 Acceptable = true;
2767 break;
2768 }
2769 }
2770 }
2771
2772 if (!Acceptable)
2773 continue;
2774
Douglas Gregor0adaa882011-12-05 17:28:06 +00002775 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002776 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002777 }
2778}
2779
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002780ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor38295be2012-10-22 23:51:00 +00002781 ModuleKind Type,
2782 unsigned ClientLoadCapabilities) {
Douglas Gregor057df202012-01-18 20:56:22 +00002783 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002784 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002785
2786 // Load the core of the AST files.
2787 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor38295be2012-10-22 23:51:00 +00002788 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded,
2789 ClientLoadCapabilities)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002790 case Failure: return Failure;
Douglas Gregor4825fd72012-10-22 22:50:17 +00002791 case OutOfDate: return OutOfDate;
2792 case VersionMismatch: return VersionMismatch;
2793 case ConfigurationMismatch: return ConfigurationMismatch;
2794 case HadErrors: return HadErrors;
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002795 case Success: break;
2796 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002797
2798 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002799
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002800 // Load the AST blocks of all of the modules that we loaded.
2801 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
2802 MEnd = Loaded.end();
2803 M != MEnd; ++M) {
2804 ModuleFile &F = **M;
2805
2806 // Read the AST block.
Douglas Gregor4825fd72012-10-22 22:50:17 +00002807 if (ReadASTBlock(F))
2808 return Failure;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002809
2810 // Once read, set the ModuleFile bit base offset and update the size in
2811 // bits of all files we've seen.
2812 F.GlobalBitOffset = TotalModulesSizeInBits;
2813 TotalModulesSizeInBits += F.SizeInBits;
2814 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2815
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002816 // Preload SLocEntries.
2817 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2818 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor8b53d142012-10-22 22:53:10 +00002819 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002820 // directly because the entry may have already been loaded in which case
Douglas Gregor8b53d142012-10-22 22:53:10 +00002821 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002822 // SourceManager.
2823 SourceMgr.getLoadedSLocEntryByID(Index);
2824 }
2825 }
2826
Douglas Gregoreee242f2011-10-27 09:33:13 +00002827 // Mark all of the identifiers in the identifier table as being out of date,
2828 // so that various accessors know to check the loaded modules when the
2829 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002830 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2831 IdEnd = PP.getIdentifierTable().end();
2832 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002833 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002834
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002835 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002836 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2837 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2838 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002839 Module *ResolvedMod = getSubmodule(GlobalID);
2840
2841 if (Unresolved.IsImport) {
2842 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002843 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002844 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002845 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002846
2847 if (ResolvedMod || Unresolved.IsWildcard)
2848 Unresolved.Mod->Exports.push_back(
2849 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002850 }
Douglas Gregor55988682011-12-05 16:33:54 +00002851 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002852
Douglas Gregor35942772011-09-09 21:34:22 +00002853 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002854
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002855 if (DeserializationListener)
2856 DeserializationListener->ReaderInitialized(this);
2857
Douglas Gregor11407b82012-10-18 21:18:25 +00002858 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2859 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2860 PrimaryModule.OriginalSourceFileID
2861 = FileID::get(PrimaryModule.SLocEntryBaseID
2862 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002863
Douglas Gregor11407b82012-10-18 21:18:25 +00002864 // If this AST file is a precompiled preamble, then set the
2865 // preamble file ID of the source manager to the file source file
2866 // from which the preamble was built.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002867 if (Type == MK_Preamble) {
Douglas Gregor11407b82012-10-18 21:18:25 +00002868 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002869 } else if (Type == MK_MainFile) {
Douglas Gregor11407b82012-10-18 21:18:25 +00002870 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002871 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002872 }
2873
Douglas Gregorcff9f262012-01-27 01:47:08 +00002874 // For any Objective-C class definitions we have already loaded, make sure
2875 // that we load any additional categories.
2876 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2877 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2878 ObjCClassesLoaded[I],
2879 PreviousGeneration);
2880 }
2881
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002882 return Success;
2883}
2884
Douglas Gregor38295be2012-10-22 23:51:00 +00002885ASTReader::ASTReadResult
2886ASTReader::ReadASTCore(StringRef FileName,
2887 ModuleKind Type,
2888 ModuleFile *ImportedBy,
2889 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
2890 unsigned ClientLoadCapabilities) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002891 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002892 bool NewModule;
2893 std::string ErrorStr;
2894 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002895 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002896
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002897 if (!M) {
2898 // We couldn't load the module.
2899 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2900 + ErrorStr;
2901 Error(Msg);
2902 return Failure;
2903 }
2904
2905 if (!NewModule) {
2906 // We've already loaded this module.
2907 return Success;
2908 }
2909
2910 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2911 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002912 if (FileName != "-") {
2913 CurrentDir = llvm::sys::path::parent_path(FileName);
2914 if (CurrentDir.empty()) CurrentDir = ".";
2915 }
2916
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002917 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002918 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002919 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002920 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002921
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002922 // Sniff for the signature.
2923 if (Stream.Read(8) != 'C' ||
2924 Stream.Read(8) != 'P' ||
2925 Stream.Read(8) != 'C' ||
2926 Stream.Read(8) != 'H') {
2927 Diag(diag::err_not_a_pch_file) << FileName;
2928 return Failure;
2929 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002930
Douglas Gregor2cf26342009-04-09 22:27:44 +00002931 while (!Stream.AtEndOfStream()) {
2932 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002933
Douglas Gregore1d918e2009-04-10 23:10:45 +00002934 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002935 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002936 return Failure;
2937 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002938
2939 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002940
Douglas Gregor7ae467f2012-10-18 18:27:37 +00002941 // We only know the control subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002942 switch (BlockID) {
2943 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002944 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002945 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002946 return Failure;
2947 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002948 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002949 case CONTROL_BLOCK_ID:
Douglas Gregor38295be2012-10-22 23:51:00 +00002950 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002951 case Success:
2952 break;
2953
Douglas Gregor4825fd72012-10-22 22:50:17 +00002954 case Failure: return Failure;
2955 case OutOfDate: return OutOfDate;
2956 case VersionMismatch: return VersionMismatch;
2957 case ConfigurationMismatch: return ConfigurationMismatch;
2958 case HadErrors: return HadErrors;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002959 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002960 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002961 case AST_BLOCK_ID:
2962 // Record that we've loaded this module.
2963 Loaded.push_back(M);
2964 return Success;
2965
Douglas Gregor2cf26342009-04-09 22:27:44 +00002966 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002967 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002968 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002969 return Failure;
2970 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002971 break;
2972 }
Mike Stump1eb44332009-09-09 15:08:12 +00002973 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002974
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002975 return Success;
2976}
2977
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002978void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002979 // If there's a listener, notify them that we "read" the translation unit.
2980 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002981 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2982 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002983
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002984 // Make sure we load the declaration update records for the translation unit,
2985 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002986 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2987 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002988
Douglas Gregor5f957282011-08-11 22:18:49 +00002989 // FIXME: Find a better way to deal with collisions between these
2990 // built-in types. Right now, we just ignore the problem.
2991
2992 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002993 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002994 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2995 if (!Context.CFConstantStringTypeDecl)
2996 Context.setCFConstantStringType(GetType(String));
2997 }
2998
2999 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3000 QualType FileType = GetType(File);
3001 if (FileType.isNull()) {
3002 Error("FILE type is NULL");
3003 return;
3004 }
3005
3006 if (!Context.FILEDecl) {
3007 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3008 Context.setFILEDecl(Typedef->getDecl());
3009 else {
3010 const TagType *Tag = FileType->getAs<TagType>();
3011 if (!Tag) {
3012 Error("Invalid FILE type in AST file");
3013 return;
3014 }
3015 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003016 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003017 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003018 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003019
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003020 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003021 QualType Jmp_bufType = GetType(Jmp_buf);
3022 if (Jmp_bufType.isNull()) {
3023 Error("jmp_buf type is NULL");
3024 return;
3025 }
3026
3027 if (!Context.jmp_bufDecl) {
3028 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3029 Context.setjmp_bufDecl(Typedef->getDecl());
3030 else {
3031 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3032 if (!Tag) {
3033 Error("Invalid jmp_buf type in AST file");
3034 return;
3035 }
3036 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003037 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003038 }
Mike Stump782fa302009-07-28 02:25:19 +00003039 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003040
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003041 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003042 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3043 if (Sigjmp_bufType.isNull()) {
3044 Error("sigjmp_buf type is NULL");
3045 return;
3046 }
3047
3048 if (!Context.sigjmp_bufDecl) {
3049 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3050 Context.setsigjmp_bufDecl(Typedef->getDecl());
3051 else {
3052 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3053 assert(Tag && "Invalid sigjmp_buf type in AST file");
3054 Context.setsigjmp_bufDecl(Tag->getDecl());
3055 }
3056 }
3057 }
3058
3059 if (unsigned ObjCIdRedef
3060 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3061 if (Context.ObjCIdRedefinitionType.isNull())
3062 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3063 }
3064
3065 if (unsigned ObjCClassRedef
3066 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3067 if (Context.ObjCClassRedefinitionType.isNull())
3068 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3069 }
3070
3071 if (unsigned ObjCSelRedef
3072 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3073 if (Context.ObjCSelRedefinitionType.isNull())
3074 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3075 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003076
3077 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3078 QualType Ucontext_tType = GetType(Ucontext_t);
3079 if (Ucontext_tType.isNull()) {
3080 Error("ucontext_t type is NULL");
3081 return;
3082 }
3083
3084 if (!Context.ucontext_tDecl) {
3085 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3086 Context.setucontext_tDecl(Typedef->getDecl());
3087 else {
3088 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3089 assert(Tag && "Invalid ucontext_t type in AST file");
3090 Context.setucontext_tDecl(Tag->getDecl());
3091 }
3092 }
3093 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003094 }
3095
Douglas Gregor35942772011-09-09 21:34:22 +00003096 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003097
3098 // If there were any CUDA special declarations, deserialize them.
3099 if (!CUDASpecialDeclRefs.empty()) {
3100 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003101 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003102 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3103 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003104
3105 // Re-export any modules that were imported by a non-module AST file.
3106 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3107 if (Module *Imported = getSubmodule(ImportedModules[I]))
3108 makeModuleVisible(Imported, Module::AllVisible);
3109 }
3110 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003111}
3112
Douglas Gregorecc2c092011-12-01 22:20:10 +00003113void ASTReader::finalizeForWriting() {
3114 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3115 HiddenEnd = HiddenNamesMap.end();
3116 Hidden != HiddenEnd; ++Hidden) {
3117 makeNamesVisible(Hidden->second);
3118 }
3119 HiddenNamesMap.clear();
3120}
3121
Douglas Gregorb64c1932009-05-12 01:31:05 +00003122/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003123/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003124/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003125std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003126 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003127 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003128 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003129 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003130 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003131 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003132 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003133 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003134 return std::string();
3135 }
3136
3137 // Initialize the stream
3138 llvm::BitstreamReader StreamFile;
3139 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003140 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003141 (const unsigned char *)Buffer->getBufferEnd());
3142 Stream.init(StreamFile);
3143
3144 // Sniff for the signature.
3145 if (Stream.Read(8) != 'C' ||
3146 Stream.Read(8) != 'P' ||
3147 Stream.Read(8) != 'C' ||
3148 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003149 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003150 return std::string();
3151 }
3152
3153 RecordData Record;
3154 while (!Stream.AtEndOfStream()) {
3155 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Douglas Gregorb64c1932009-05-12 01:31:05 +00003157 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3158 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003159
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003160 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003161 switch (BlockID) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003162 case CONTROL_BLOCK_ID:
3163 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003164 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003165 return std::string();
3166 }
3167 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003168
Douglas Gregorb64c1932009-05-12 01:31:05 +00003169 default:
3170 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003171 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003172 return std::string();
3173 }
3174 break;
3175 }
3176 continue;
3177 }
3178
3179 if (Code == llvm::bitc::END_BLOCK) {
3180 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003181 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003182 return std::string();
3183 }
3184 continue;
3185 }
3186
3187 if (Code == llvm::bitc::DEFINE_ABBREV) {
3188 Stream.ReadAbbrevRecord();
3189 continue;
3190 }
3191
3192 Record.clear();
3193 const char *BlobStart = 0;
3194 unsigned BlobLen = 0;
Douglas Gregor39c497b2012-10-18 18:36:53 +00003195 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003196 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003197 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003198
3199 return std::string();
3200}
3201
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003202namespace {
3203 class SimplePCHValidator : public ASTReaderListener {
3204 const LangOptions &ExistingLangOpts;
3205 const TargetOptions &ExistingTargetOpts;
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003206 const PreprocessorOptions &ExistingPPOpts;
Douglas Gregor87699242012-10-25 00:07:54 +00003207 FileManager &FileMgr;
3208
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003209 public:
3210 SimplePCHValidator(const LangOptions &ExistingLangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003211 const TargetOptions &ExistingTargetOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003212 const PreprocessorOptions &ExistingPPOpts,
3213 FileManager &FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003214 : ExistingLangOpts(ExistingLangOpts),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003215 ExistingTargetOpts(ExistingTargetOpts),
Douglas Gregor87699242012-10-25 00:07:54 +00003216 ExistingPPOpts(ExistingPPOpts),
3217 FileMgr(FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003218 {
3219 }
3220
3221 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3222 bool Complain) {
3223 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3224 }
3225 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3226 bool Complain) {
3227 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3228 }
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003229 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003230 bool Complain,
3231 std::string &SuggestedPredefines) {
3232 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3233 SuggestedPredefines);
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003234 }
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003235 };
3236}
3237
3238bool ASTReader::isAcceptableASTFile(StringRef Filename,
3239 FileManager &FileMgr,
3240 const LangOptions &LangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003241 const TargetOptions &TargetOpts,
3242 const PreprocessorOptions &PPOpts) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003243 // Open the AST file.
3244 std::string ErrStr;
3245 OwningPtr<llvm::MemoryBuffer> Buffer;
3246 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3247 if (!Buffer) {
3248 return false;
3249 }
3250
3251 // Initialize the stream
3252 llvm::BitstreamReader StreamFile;
3253 llvm::BitstreamCursor Stream;
3254 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3255 (const unsigned char *)Buffer->getBufferEnd());
3256 Stream.init(StreamFile);
3257
3258 // Sniff for the signature.
3259 if (Stream.Read(8) != 'C' ||
3260 Stream.Read(8) != 'P' ||
3261 Stream.Read(8) != 'C' ||
3262 Stream.Read(8) != 'H') {
3263 return false;
3264 }
3265
Douglas Gregor87699242012-10-25 00:07:54 +00003266 SimplePCHValidator Validator(LangOpts, TargetOpts, PPOpts, FileMgr);
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003267 RecordData Record;
3268 bool InControlBlock = false;
3269 while (!Stream.AtEndOfStream()) {
3270 unsigned Code = Stream.ReadCode();
3271
3272 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3273 unsigned BlockID = Stream.ReadSubBlockID();
3274
3275 // We only know the control subblock ID.
3276 switch (BlockID) {
3277 case CONTROL_BLOCK_ID:
3278 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
3279 return false;
3280 } else {
3281 InControlBlock = true;
3282 }
3283 break;
3284
3285 default:
3286 if (Stream.SkipBlock())
3287 return false;
3288 break;
3289 }
3290 continue;
3291 }
3292
3293 if (Code == llvm::bitc::END_BLOCK) {
3294 if (Stream.ReadBlockEnd()) {
3295 return false;
3296 }
3297 InControlBlock = false;
3298 continue;
3299 }
3300
3301 if (Code == llvm::bitc::DEFINE_ABBREV) {
3302 Stream.ReadAbbrevRecord();
3303 continue;
3304 }
3305
3306 Record.clear();
3307 const char *BlobStart = 0;
3308 unsigned BlobLen = 0;
3309 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3310 if (InControlBlock) {
3311 switch ((ControlRecordTypes)RecCode) {
3312 case METADATA: {
3313 if (Record[0] != VERSION_MAJOR) {
3314 return false;
3315 }
3316
3317 const std::string &CurBranch = getClangFullRepositoryVersion();
3318 StringRef ASTBranch(BlobStart, BlobLen);
3319 if (StringRef(CurBranch) != ASTBranch)
3320 return false;
3321
3322 break;
3323 }
3324 case LANGUAGE_OPTIONS:
3325 if (ParseLanguageOptions(Record, false, Validator))
3326 return false;
3327 break;
3328
3329 case TARGET_OPTIONS:
3330 if (ParseTargetOptions(Record, false, Validator))
3331 return false;
3332 break;
3333
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003334 case DIAGNOSTIC_OPTIONS:
3335 if (ParseDiagnosticOptions(Record, false, Validator))
3336 return false;
3337 break;
3338
Douglas Gregorbbf38312012-10-24 16:50:34 +00003339 case FILE_SYSTEM_OPTIONS:
3340 if (ParseFileSystemOptions(Record, false, Validator))
3341 return false;
3342 break;
3343
3344 case HEADER_SEARCH_OPTIONS:
3345 if (ParseHeaderSearchOptions(Record, false, Validator))
3346 return false;
3347 break;
3348
Douglas Gregor87699242012-10-25 00:07:54 +00003349 case PREPROCESSOR_OPTIONS: {
3350 std::string IgnoredSuggestedPredefines;
3351 if (ParsePreprocessorOptions(Record, false, Validator,
3352 IgnoredSuggestedPredefines))
Douglas Gregora71a7d82012-10-24 20:05:57 +00003353 return false;
3354 break;
Douglas Gregor87699242012-10-25 00:07:54 +00003355 }
Douglas Gregora71a7d82012-10-24 20:05:57 +00003356
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003357 default:
3358 // No other validation to perform.
3359 break;
3360 }
3361 }
3362 }
3363
3364 return true;
3365}
3366
Douglas Gregor4825fd72012-10-22 22:50:17 +00003367bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003368 // Enter the submodule block.
3369 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3370 Error("malformed submodule block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003371 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003372 }
3373
3374 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003375 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003376 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003377 RecordData Record;
3378 while (true) {
3379 unsigned Code = F.Stream.ReadCode();
3380 if (Code == llvm::bitc::END_BLOCK) {
3381 if (F.Stream.ReadBlockEnd()) {
3382 Error("error at end of submodule block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003383 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003384 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00003385 return false;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003386 }
3387
3388 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3389 // No known subblocks, always skip them.
3390 F.Stream.ReadSubBlockID();
3391 if (F.Stream.SkipBlock()) {
3392 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003393 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003394 }
3395 continue;
3396 }
3397
3398 if (Code == llvm::bitc::DEFINE_ABBREV) {
3399 F.Stream.ReadAbbrevRecord();
3400 continue;
3401 }
3402
3403 // Read a record.
3404 const char *BlobStart;
3405 unsigned BlobLen;
3406 Record.clear();
3407 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3408 default: // Default behavior: ignore.
3409 break;
3410
3411 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003412 if (First) {
3413 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003414 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003415 }
3416
Douglas Gregore209e502011-12-06 01:10:29 +00003417 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003418 Error("malformed module definition");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003419 return true;
Douglas Gregor1e123682011-12-05 22:27:44 +00003420 }
3421
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003422 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003423 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3424 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3425 bool IsFramework = Record[2];
3426 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003427 bool IsSystem = Record[4];
3428 bool InferSubmodules = Record[5];
3429 bool InferExplicitSubmodules = Record[6];
3430 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003431
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003432 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003433 if (Parent)
3434 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003435
3436 // Retrieve this (sub)module from the module map, creating it if
3437 // necessary.
3438 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3439 IsFramework,
3440 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003441 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3442 if (GlobalIndex >= SubmodulesLoaded.size() ||
3443 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003444 Error("too many submodules");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003445 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003446 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003447
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003448 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003449 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003450 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003451 CurrentModule->InferSubmodules = InferSubmodules;
3452 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3453 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003454 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003455 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003456
Douglas Gregore209e502011-12-06 01:10:29 +00003457 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003458 break;
3459 }
3460
Douglas Gregor77d029f2011-12-08 19:11:24 +00003461 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003462 if (First) {
3463 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003464 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003465 }
3466
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003467 if (!CurrentModule)
3468 break;
3469
3470 StringRef FileName(BlobStart, BlobLen);
3471 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003472 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003473 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003474 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003475 Error("mismatched umbrella headers in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003476 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003477 }
3478 }
3479 break;
3480 }
3481
3482 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003483 if (First) {
3484 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003485 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003486 }
3487
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003488 if (!CurrentModule)
3489 break;
3490
3491 // FIXME: Be more lazy about this!
3492 StringRef FileName(BlobStart, BlobLen);
3493 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3494 if (std::find(CurrentModule->Headers.begin(),
3495 CurrentModule->Headers.end(),
3496 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003497 ModMap.addHeader(CurrentModule, File, false);
3498 }
3499 break;
3500 }
3501
3502 case SUBMODULE_EXCLUDED_HEADER: {
3503 if (First) {
3504 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003505 return true;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003506 }
3507
3508 if (!CurrentModule)
3509 break;
3510
3511 // FIXME: Be more lazy about this!
3512 StringRef FileName(BlobStart, BlobLen);
3513 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3514 if (std::find(CurrentModule->Headers.begin(),
3515 CurrentModule->Headers.end(),
3516 File) == CurrentModule->Headers.end())
3517 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003518 }
3519 break;
3520 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003521
3522 case SUBMODULE_TOPHEADER: {
3523 if (First) {
3524 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003525 return true;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003526 }
3527
3528 if (!CurrentModule)
3529 break;
3530
3531 // FIXME: Be more lazy about this!
3532 StringRef FileName(BlobStart, BlobLen);
3533 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3534 CurrentModule->TopHeaders.insert(File);
3535 break;
3536 }
3537
Douglas Gregor77d029f2011-12-08 19:11:24 +00003538 case SUBMODULE_UMBRELLA_DIR: {
3539 if (First) {
3540 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003541 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003542 }
3543
3544 if (!CurrentModule)
3545 break;
3546
3547 StringRef DirName(BlobStart, BlobLen);
3548 if (const DirectoryEntry *Umbrella
3549 = PP.getFileManager().getDirectory(DirName)) {
3550 if (!CurrentModule->getUmbrellaDir())
3551 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3552 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3553 Error("mismatched umbrella directories in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003554 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003555 }
3556 }
3557 break;
3558 }
3559
Douglas Gregor26ced122011-12-01 00:59:36 +00003560 case SUBMODULE_METADATA: {
3561 if (!First) {
3562 Error("submodule metadata record not at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003563 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003564 }
3565 First = false;
3566
3567 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003568 F.LocalNumSubmodules = Record[0];
3569 unsigned LocalBaseSubmoduleID = Record[1];
3570 if (F.LocalNumSubmodules > 0) {
3571 // Introduce the global -> local mapping for submodules within this
3572 // module.
3573 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3574
3575 // Introduce the local -> global mapping for submodules within this
3576 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003577 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003578 std::make_pair(LocalBaseSubmoduleID,
3579 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3580
3581 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3582 }
3583 break;
3584 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003585
Douglas Gregor55988682011-12-05 16:33:54 +00003586 case SUBMODULE_IMPORTS: {
3587 if (First) {
3588 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003589 return true;
Douglas Gregor55988682011-12-05 16:33:54 +00003590 }
3591
3592 if (!CurrentModule)
3593 break;
3594
3595 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3596 UnresolvedModuleImportExport Unresolved;
3597 Unresolved.File = &F;
3598 Unresolved.Mod = CurrentModule;
3599 Unresolved.ID = Record[Idx];
3600 Unresolved.IsImport = true;
3601 Unresolved.IsWildcard = false;
3602 UnresolvedModuleImportExports.push_back(Unresolved);
3603 }
3604 break;
3605 }
3606
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003607 case SUBMODULE_EXPORTS: {
3608 if (First) {
3609 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003610 return true;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003611 }
3612
3613 if (!CurrentModule)
3614 break;
3615
3616 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003617 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003618 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003619 Unresolved.Mod = CurrentModule;
3620 Unresolved.ID = Record[Idx];
3621 Unresolved.IsImport = false;
3622 Unresolved.IsWildcard = Record[Idx + 1];
3623 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003624 }
3625
3626 // Once we've loaded the set of exports, there's no reason to keep
3627 // the parsed, unresolved exports around.
3628 CurrentModule->UnresolvedExports.clear();
3629 break;
3630 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003631 case SUBMODULE_REQUIRES: {
3632 if (First) {
3633 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003634 return true;
Douglas Gregor51f564f2011-12-31 04:05:44 +00003635 }
3636
3637 if (!CurrentModule)
3638 break;
3639
3640 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003641 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003642 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003643 break;
3644 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003645 }
3646 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003647}
3648
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003649/// \brief Parse the record that corresponds to a LangOptions data
3650/// structure.
3651///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003652/// This routine parses the language options from the AST file and then gives
3653/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003654///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003655/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003656bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3657 bool Complain,
3658 ASTReaderListener &Listener) {
3659 LangOptions LangOpts;
3660 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003661#define LANGOPT(Name, Bits, Default, Description) \
3662 LangOpts.Name = Record[Idx++];
3663#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3664 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3665#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003666
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003667 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3668 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3669 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3670
3671 unsigned Length = Record[Idx++];
3672 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3673 Record.begin() + Idx + Length);
3674 return Listener.ReadLanguageOptions(LangOpts, Complain);
3675}
3676
3677bool ASTReader::ParseTargetOptions(const RecordData &Record,
3678 bool Complain,
3679 ASTReaderListener &Listener) {
3680 unsigned Idx = 0;
3681 TargetOptions TargetOpts;
3682 TargetOpts.Triple = ReadString(Record, Idx);
3683 TargetOpts.CPU = ReadString(Record, Idx);
3684 TargetOpts.ABI = ReadString(Record, Idx);
3685 TargetOpts.CXXABI = ReadString(Record, Idx);
3686 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3687 for (unsigned N = Record[Idx++]; N; --N) {
3688 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3689 }
3690 for (unsigned N = Record[Idx++]; N; --N) {
3691 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003692 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003693
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003694 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003695}
3696
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003697bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3698 ASTReaderListener &Listener) {
3699 DiagnosticOptions DiagOpts;
3700 unsigned Idx = 0;
3701#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3702#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3703 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3704#include "clang/Basic/DiagnosticOptions.def"
3705
3706 for (unsigned N = Record[Idx++]; N; --N) {
3707 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3708 }
3709
3710 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3711}
3712
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00003713bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3714 ASTReaderListener &Listener) {
3715 FileSystemOptions FSOpts;
3716 unsigned Idx = 0;
3717 FSOpts.WorkingDir = ReadString(Record, Idx);
3718 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3719}
3720
Douglas Gregorbbf38312012-10-24 16:50:34 +00003721bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3722 bool Complain,
3723 ASTReaderListener &Listener) {
3724 HeaderSearchOptions HSOpts;
3725 unsigned Idx = 0;
3726 HSOpts.Sysroot = ReadString(Record, Idx);
3727
3728 // Include entries.
3729 for (unsigned N = Record[Idx++]; N; --N) {
3730 std::string Path = ReadString(Record, Idx);
3731 frontend::IncludeDirGroup Group
3732 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3733 bool IsUserSupplied = Record[Idx++];
3734 bool IsFramework = Record[Idx++];
3735 bool IgnoreSysRoot = Record[Idx++];
3736 bool IsInternal = Record[Idx++];
3737 bool ImplicitExternC = Record[Idx++];
3738 HSOpts.UserEntries.push_back(
3739 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3740 IgnoreSysRoot, IsInternal, ImplicitExternC));
3741 }
3742
3743 // System header prefixes.
3744 for (unsigned N = Record[Idx++]; N; --N) {
3745 std::string Prefix = ReadString(Record, Idx);
3746 bool IsSystemHeader = Record[Idx++];
3747 HSOpts.SystemHeaderPrefixes.push_back(
3748 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3749 }
3750
3751 HSOpts.ResourceDir = ReadString(Record, Idx);
3752 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3753 HSOpts.DisableModuleHash = Record[Idx++];
3754 HSOpts.UseBuiltinIncludes = Record[Idx++];
3755 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3756 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3757 HSOpts.UseLibcxx = Record[Idx++];
3758
3759 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3760}
3761
Douglas Gregora71a7d82012-10-24 20:05:57 +00003762bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3763 bool Complain,
Douglas Gregor87699242012-10-25 00:07:54 +00003764 ASTReaderListener &Listener,
3765 std::string &SuggestedPredefines) {
Douglas Gregora71a7d82012-10-24 20:05:57 +00003766 PreprocessorOptions PPOpts;
3767 unsigned Idx = 0;
3768
3769 // Macro definitions/undefs
3770 for (unsigned N = Record[Idx++]; N; --N) {
3771 std::string Macro = ReadString(Record, Idx);
3772 bool IsUndef = Record[Idx++];
3773 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3774 }
3775
3776 // Includes
3777 for (unsigned N = Record[Idx++]; N; --N) {
3778 PPOpts.Includes.push_back(ReadString(Record, Idx));
3779 }
3780
3781 // Macro Includes
3782 for (unsigned N = Record[Idx++]; N; --N) {
3783 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3784 }
3785
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003786 PPOpts.UsePredefines = Record[Idx++];
Douglas Gregora71a7d82012-10-24 20:05:57 +00003787 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3788 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3789 PPOpts.ObjCXXARCStandardLibrary =
3790 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
Douglas Gregor87699242012-10-25 00:07:54 +00003791 SuggestedPredefines.clear();
3792 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3793 SuggestedPredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00003794}
3795
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003796std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003797ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003798 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003799 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003800 assert(I != GlobalPreprocessedEntityMap.end() &&
3801 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003802 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003803 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3804 return std::make_pair(M, LocalIndex);
3805}
3806
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003807std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3808ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3809 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3810 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3811 Mod.NumPreprocessedEntities);
3812
3813 return std::make_pair(PreprocessingRecord::iterator(),
3814 PreprocessingRecord::iterator());
3815}
3816
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003817std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3818ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3819 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3820 ModuleDeclIterator(this, &Mod,
3821 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3822}
3823
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003824PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3825 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003826 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3827 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003828 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003829 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003830
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003831 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003832 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003833
3834 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3835 switch (Code) {
3836 case llvm::bitc::END_BLOCK:
3837 return 0;
3838
3839 case llvm::bitc::ENTER_SUBBLOCK:
3840 Error("unexpected subblock record in preprocessor detail block");
3841 return 0;
3842
3843 case llvm::bitc::DEFINE_ABBREV:
3844 Error("unexpected abbrevation record in preprocessor detail block");
3845 return 0;
3846
3847 default:
3848 break;
3849 }
3850
3851 if (!PP.getPreprocessingRecord()) {
3852 Error("no preprocessing record");
3853 return 0;
3854 }
3855
3856 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003857 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3858 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003859 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3860 const char *BlobStart = 0;
3861 unsigned BlobLen = 0;
3862 RecordData Record;
3863 PreprocessorDetailRecordTypes RecType =
3864 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3865 Code, Record, BlobStart, BlobLen);
3866 switch (RecType) {
3867 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003868 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003869 IdentifierInfo *Name = 0;
3870 MacroDefinition *Def = 0;
3871 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003872 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003873 else {
3874 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003875 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003876 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3877 }
3878
3879 MacroExpansion *ME;
3880 if (isBuiltin)
3881 ME = new (PPRec) MacroExpansion(Name, Range);
3882 else
3883 ME = new (PPRec) MacroExpansion(Def, Range);
3884
3885 return ME;
3886 }
3887
3888 case PPD_MACRO_DEFINITION: {
3889 // Decode the identifier info and then check again; if the macro is
3890 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003891 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003892 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003893 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003894
3895 if (DeserializationListener)
3896 DeserializationListener->MacroDefinitionRead(PPID, MD);
3897
3898 return MD;
3899 }
3900
3901 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003902 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003903 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3904 const FileEntry *File = 0;
3905 if (!FullFileName.empty())
3906 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003907
3908 // FIXME: Stable encoding
3909 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003910 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003911 InclusionDirective *ID
3912 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003913 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003914 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003915 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003916 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003917 return ID;
3918 }
3919 }
David Blaikie7530c032012-01-17 06:56:22 +00003920
3921 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003922}
3923
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003924/// \brief \arg SLocMapI points at a chunk of a module that contains no
3925/// preprocessed entities or the entities it contains are not the ones we are
3926/// looking for. Find the next module that contains entities and return the ID
3927/// of the first entry.
3928PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3929 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3930 ++SLocMapI;
3931 for (GlobalSLocOffsetMapType::const_iterator
3932 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003933 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003934 if (M.NumPreprocessedEntities)
3935 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3936 }
3937
3938 return getTotalNumPreprocessedEntities();
3939}
3940
3941namespace {
3942
3943template <unsigned PPEntityOffset::*PPLoc>
3944struct PPEntityComp {
3945 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003946 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003947
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003948 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003949
Benjamin Kramer88df1252011-09-21 06:42:26 +00003950 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3951 SourceLocation LHS = getLoc(L);
3952 SourceLocation RHS = getLoc(R);
3953 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3954 }
3955
3956 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003957 SourceLocation LHS = getLoc(L);
3958 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3959 }
3960
Benjamin Kramer88df1252011-09-21 06:42:26 +00003961 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003962 SourceLocation RHS = getLoc(R);
3963 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3964 }
3965
3966 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3967 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3968 }
3969};
3970
3971}
3972
3973/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3974PreprocessedEntityID
3975ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3976 if (SourceMgr.isLocalSourceLocation(BLoc))
3977 return getTotalNumPreprocessedEntities();
3978
3979 GlobalSLocOffsetMapType::const_iterator
3980 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3981 BLoc.getOffset());
3982 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3983 "Corrupted global sloc offset map");
3984
3985 if (SLocMapI->second->NumPreprocessedEntities == 0)
3986 return findNextPreprocessedEntity(SLocMapI);
3987
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003988 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003989 typedef const PPEntityOffset *pp_iterator;
3990 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3991 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003992
3993 size_t Count = M.NumPreprocessedEntities;
3994 size_t Half;
3995 pp_iterator First = pp_begin;
3996 pp_iterator PPI;
3997
3998 // Do a binary search manually instead of using std::lower_bound because
3999 // The end locations of entities may be unordered (when a macro expansion
4000 // is inside another macro argument), but for this case it is not important
4001 // whether we get the first macro expansion or its containing macro.
4002 while (Count > 0) {
4003 Half = Count/2;
4004 PPI = First;
4005 std::advance(PPI, Half);
4006 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
4007 BLoc)){
4008 First = PPI;
4009 ++First;
4010 Count = Count - Half - 1;
4011 } else
4012 Count = Half;
4013 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004014
4015 if (PPI == pp_end)
4016 return findNextPreprocessedEntity(SLocMapI);
4017
4018 return getGlobalPreprocessedEntityID(M,
4019 M.BasePreprocessedEntityID + (PPI - pp_begin));
4020}
4021
4022/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
4023PreprocessedEntityID
4024ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
4025 if (SourceMgr.isLocalSourceLocation(ELoc))
4026 return getTotalNumPreprocessedEntities();
4027
4028 GlobalSLocOffsetMapType::const_iterator
4029 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
4030 ELoc.getOffset());
4031 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
4032 "Corrupted global sloc offset map");
4033
4034 if (SLocMapI->second->NumPreprocessedEntities == 0)
4035 return findNextPreprocessedEntity(SLocMapI);
4036
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004037 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00004038 typedef const PPEntityOffset *pp_iterator;
4039 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
4040 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
4041 pp_iterator PPI =
4042 std::upper_bound(pp_begin, pp_end, ELoc,
4043 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
4044
4045 if (PPI == pp_end)
4046 return findNextPreprocessedEntity(SLocMapI);
4047
4048 return getGlobalPreprocessedEntityID(M,
4049 M.BasePreprocessedEntityID + (PPI - pp_begin));
4050}
4051
4052/// \brief Returns a pair of [Begin, End) indices of preallocated
4053/// preprocessed entities that \arg Range encompasses.
4054std::pair<unsigned, unsigned>
4055 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
4056 if (Range.isInvalid())
4057 return std::make_pair(0,0);
4058 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
4059
4060 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
4061 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
4062 return std::make_pair(BeginID, EndID);
4063}
4064
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004065/// \brief Optionally returns true or false if the preallocated preprocessed
4066/// entity with index \arg Index came from file \arg FID.
4067llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
4068 FileID FID) {
4069 if (FID.isInvalid())
4070 return false;
4071
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004072 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
4073 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00004074 unsigned LocalIndex = PPInfo.second;
4075 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
4076
4077 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
4078 if (Loc.isInvalid())
4079 return false;
4080
4081 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
4082 return true;
4083 else
4084 return false;
4085}
4086
Douglas Gregord10a3812011-08-25 18:14:34 +00004087namespace {
4088 /// \brief Visitor used to search for information about a header file.
4089 class HeaderFileInfoVisitor {
4090 ASTReader &Reader;
4091 const FileEntry *FE;
4092
4093 llvm::Optional<HeaderFileInfo> HFI;
4094
4095 public:
4096 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
4097 : Reader(Reader), FE(FE) { }
4098
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004099 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004100 HeaderFileInfoVisitor *This
4101 = static_cast<HeaderFileInfoVisitor *>(UserData);
4102
4103 HeaderFileInfoTrait Trait(This->Reader, M,
4104 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
4105 M.HeaderFileFrameworkStrings,
4106 This->FE->getName());
4107
4108 HeaderFileInfoLookupTable *Table
4109 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4110 if (!Table)
4111 return false;
4112
4113 // Look in the on-disk hash table for an entry for this file name.
4114 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4115 &Trait);
4116 if (Pos == Table->end())
4117 return false;
4118
4119 This->HFI = *Pos;
4120 return true;
4121 }
4122
4123 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4124 };
4125}
4126
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004127HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004128 HeaderFileInfoVisitor Visitor(*this, FE);
4129 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4130 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004131 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00004132 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4133 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004134 }
4135
4136 return HeaderFileInfo();
4137}
4138
David Blaikied6471f72011-09-25 23:23:43 +00004139void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004140 // FIXME: Make it work properly with modules.
4141 llvm::SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004142 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004143 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004144 unsigned Idx = 0;
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004145 DiagStates.clear();
4146 assert(!Diag.DiagStates.empty());
4147 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004148 while (Idx < F.PragmaDiagMappings.size()) {
4149 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004150 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4151 if (DiagStateID != 0) {
4152 Diag.DiagStatePoints.push_back(
4153 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4154 FullSourceLoc(Loc, SourceMgr)));
4155 continue;
4156 }
4157
4158 assert(DiagStateID == 0);
4159 // A new DiagState was created here.
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004160 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004161 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4162 DiagStates.push_back(NewState);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004163 Diag.DiagStatePoints.push_back(
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004164 DiagnosticsEngine::DiagStatePoint(NewState,
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004165 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004166 while (1) {
4167 assert(Idx < F.PragmaDiagMappings.size() &&
4168 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4169 if (Idx >= F.PragmaDiagMappings.size()) {
4170 break; // Something is messed up but at least avoid infinite loop in
4171 // release build.
4172 }
4173 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4174 if (DiagID == (unsigned)-1) {
4175 break; // no more diag/map pairs for this location.
4176 }
4177 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004178 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4179 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004180 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004181 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00004182 }
4183}
4184
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004185/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004186ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00004187 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00004188 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004189 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00004190 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004191}
4192
4193/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00004194///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004195/// The index is the type ID, shifted and minus the number of predefs. This
4196/// routine actually reads the record corresponding to the type at the given
4197/// location. It is a helper routine for GetType, which deals with reading type
4198/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00004199QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004200 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004201 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00004202
Douglas Gregor0b748912009-04-14 21:18:50 +00004203 // Keep track of where we are in the stream, then jump back there
4204 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004205 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00004206
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004207 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00004208
Douglas Gregord89275b2009-07-06 18:54:52 +00004209 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004210 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00004211
Douglas Gregor393f2492011-07-22 00:38:23 +00004212 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00004213 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004214 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004215 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004216 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4217 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004218 if (Record.size() != 2) {
4219 Error("Incorrect encoding of extended qualifier type");
4220 return QualType();
4221 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004222 QualType Base = readType(*Loc.F, Record, Idx);
4223 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00004224 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00004225 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004226
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004227 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004228 if (Record.size() != 1) {
4229 Error("Incorrect encoding of complex type");
4230 return QualType();
4231 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004232 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004233 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004234 }
4235
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004236 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004237 if (Record.size() != 1) {
4238 Error("Incorrect encoding of pointer type");
4239 return QualType();
4240 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004241 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004242 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004243 }
4244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004245 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004246 if (Record.size() != 1) {
4247 Error("Incorrect encoding of block pointer type");
4248 return QualType();
4249 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004250 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004251 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004252 }
4253
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004254 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00004255 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004256 Error("Incorrect encoding of lvalue reference type");
4257 return QualType();
4258 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004259 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004260 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004261 }
4262
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004263 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004264 if (Record.size() != 1) {
4265 Error("Incorrect encoding of rvalue reference type");
4266 return QualType();
4267 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004268 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004269 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004270 }
4271
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004272 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00004273 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004274 Error("Incorrect encoding of member pointer type");
4275 return QualType();
4276 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004277 QualType PointeeType = readType(*Loc.F, Record, Idx);
4278 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004279 if (PointeeType.isNull() || ClassType.isNull())
4280 return QualType();
4281
Douglas Gregor35942772011-09-09 21:34:22 +00004282 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004283 }
4284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004285 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004286 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004287 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4288 unsigned IndexTypeQuals = Record[2];
4289 unsigned Idx = 3;
4290 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004291 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004292 ASM, IndexTypeQuals);
4293 }
4294
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004295 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004296 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004297 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4298 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004299 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004300 }
4301
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004302 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004303 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00004304 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4305 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004306 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4307 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004308 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004309 ASM, IndexTypeQuals,
4310 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004311 }
4312
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004313 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004314 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004315 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004316 return QualType();
4317 }
4318
Douglas Gregor393f2492011-07-22 00:38:23 +00004319 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004320 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004321 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004322 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004323 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004324 }
4325
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004326 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004327 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004328 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004329 return QualType();
4330 }
4331
Douglas Gregor393f2492011-07-22 00:38:23 +00004332 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004333 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004334 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004335 }
4336
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004337 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004338 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004339 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004340 return QualType();
4341 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004342 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004343 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4344 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004345 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004346 }
4347
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004348 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004349 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004350
4351 FunctionProtoType::ExtProtoInfo EPI;
4352 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004353 /*hasregparm*/ Record[2],
4354 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004355 static_cast<CallingConv>(Record[4]),
4356 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004357
John McCallf85e1932011-06-15 23:02:42 +00004358 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004359 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004360 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004361 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004362 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004363
4364 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004365 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004366 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004367 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004368 ExceptionSpecificationType EST =
4369 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4370 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004371 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004372 if (EST == EST_Dynamic) {
4373 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004374 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004375 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004376 EPI.Exceptions = Exceptions.data();
4377 } else if (EST == EST_ComputedNoexcept) {
4378 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004379 } else if (EST == EST_Uninstantiated) {
4380 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4381 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004382 } else if (EST == EST_Unevaluated) {
4383 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004384 }
Douglas Gregor35942772011-09-09 21:34:22 +00004385 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004386 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004387 }
4388
Douglas Gregor409448c2011-07-21 22:35:25 +00004389 case TYPE_UNRESOLVED_USING: {
4390 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004391 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004392 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4393 }
4394
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004395 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004396 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004397 Error("incorrect encoding of typedef type");
4398 return QualType();
4399 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004400 unsigned Idx = 0;
4401 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004402 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004403 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004404 Canonical = Context.getCanonicalType(Canonical);
4405 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004406 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004407
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004408 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004409 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004410
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004411 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004412 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004413 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004414 return QualType();
4415 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004416 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004417 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004418 }
Mike Stump1eb44332009-09-09 15:08:12 +00004419
Douglas Gregorf8af9822012-02-12 18:42:33 +00004420 case TYPE_DECLTYPE: {
4421 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4422 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4423 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004424
Sean Huntca63c202011-05-24 22:41:36 +00004425 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004426 QualType BaseType = readType(*Loc.F, Record, Idx);
4427 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004428 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004429 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004430 }
4431
Richard Smith34b41d92011-02-20 03:19:35 +00004432 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004433 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004434
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004435 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004436 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004437 Error("incorrect encoding of record type");
4438 return QualType();
4439 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004440 unsigned Idx = 0;
4441 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004442 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4443 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4444 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004445 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004446 return T;
4447 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004448
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004449 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004450 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004451 Error("incorrect encoding of enum type");
4452 return QualType();
4453 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004454 unsigned Idx = 0;
4455 bool IsDependent = Record[Idx++];
4456 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004457 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004458 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004459 return T;
4460 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004461
John McCall9d156a72011-01-06 01:58:22 +00004462 case TYPE_ATTRIBUTED: {
4463 if (Record.size() != 3) {
4464 Error("incorrect encoding of attributed type");
4465 return QualType();
4466 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004467 QualType modifiedType = readType(*Loc.F, Record, Idx);
4468 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004469 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004470 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004471 }
4472
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004473 case TYPE_PAREN: {
4474 if (Record.size() != 1) {
4475 Error("incorrect encoding of paren type");
4476 return QualType();
4477 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004478 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004479 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004480 }
4481
Douglas Gregor7536dd52010-12-20 02:24:11 +00004482 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004483 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004484 Error("incorrect encoding of pack expansion type");
4485 return QualType();
4486 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004487 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004488 if (Pattern.isNull())
4489 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004490 llvm::Optional<unsigned> NumExpansions;
4491 if (Record[1])
4492 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004493 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004494 }
4495
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004496 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004497 unsigned Idx = 0;
4498 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004499 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004500 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004501 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004502 }
4503
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004504 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004505 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004506 ObjCInterfaceDecl *ItfD
4507 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004508 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004509 }
4510
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004511 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004512 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004513 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004514 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004515 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004516 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004517 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004518 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004519 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004520
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004521 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004522 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004523 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004524 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004525 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004526
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004527 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004528 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004529 QualType Parm = readType(*Loc.F, Record, Idx);
4530 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004531 return
Douglas Gregor35942772011-09-09 21:34:22 +00004532 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004533 Replacement);
4534 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004535
Douglas Gregorc3069d62011-01-14 02:55:32 +00004536 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4537 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004538 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004539 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004540 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004541 cast<TemplateTypeParmType>(Parm),
4542 ArgPack);
4543 }
4544
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004545 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004546 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004547 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004548 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004549 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004550 return
Douglas Gregor35942772011-09-09 21:34:22 +00004551 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004552 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004553
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004554 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004555 unsigned Idx = 0;
4556 unsigned Depth = Record[Idx++];
4557 unsigned Index = Record[Idx++];
4558 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004559 TemplateTypeParmDecl *D
4560 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004561 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004562 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004563
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004564 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004565 unsigned Idx = 0;
4566 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004567 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004568 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004569 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004570 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004571 Canon = Context.getCanonicalType(Canon);
4572 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004573 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004574
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004575 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004576 unsigned Idx = 0;
4577 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004578 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004579 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004580 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004581 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004582 Args.reserve(NumArgs);
4583 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004584 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004585 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004586 Args.size(), Args.data());
4587 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004588
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004589 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004590 unsigned Idx = 0;
4591
4592 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004593 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004594 ArrayType::ArraySizeModifier ASM
4595 = (ArrayType::ArraySizeModifier)Record[Idx++];
4596 unsigned IndexTypeQuals = Record[Idx++];
4597
4598 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004599 Expr *NumElts = ReadExpr(*Loc.F);
4600 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004601
Douglas Gregor35942772011-09-09 21:34:22 +00004602 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004603 IndexTypeQuals, Brackets);
4604 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004605
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004606 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004607 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004608 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004609 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004610 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004611 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004612 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004613 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004614 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004615 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004616 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004617 else
Douglas Gregor35942772011-09-09 21:34:22 +00004618 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004619 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004620 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004621 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004622 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004623
4624 case TYPE_ATOMIC: {
4625 if (Record.size() != 1) {
4626 Error("Incorrect encoding of atomic type");
4627 return QualType();
4628 }
4629 QualType ValueType = readType(*Loc.F, Record, Idx);
4630 return Context.getAtomicType(ValueType);
4631 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004632 }
David Blaikie7530c032012-01-17 06:56:22 +00004633 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004634}
4635
Sebastian Redlc3632732010-10-05 15:59:54 +00004636class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004637 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004638 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004639 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004640 unsigned &Idx;
4641
Sebastian Redlc3632732010-10-05 15:59:54 +00004642 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4643 unsigned &I) {
4644 return Reader.ReadSourceLocation(F, R, I);
4645 }
4646
Douglas Gregor409448c2011-07-21 22:35:25 +00004647 template<typename T>
4648 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4649 return Reader.ReadDeclAs<T>(F, Record, Idx);
4650 }
4651
John McCalla1ee0c52009-10-16 21:56:05 +00004652public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004653 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004654 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004655 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004656 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004657
John McCall51bd8032009-10-18 01:05:36 +00004658 // We want compile-time assurance that we've enumerated all of
4659 // these, so unfortunately we have to declare them first, then
4660 // define them out-of-line.
4661#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004662#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004663 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004664#include "clang/AST/TypeLocNodes.def"
4665
John McCall51bd8032009-10-18 01:05:36 +00004666 void VisitFunctionTypeLoc(FunctionTypeLoc);
4667 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004668};
4669
John McCall51bd8032009-10-18 01:05:36 +00004670void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004671 // nothing to do
4672}
John McCall51bd8032009-10-18 01:05:36 +00004673void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004674 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004675 if (TL.needsExtraLocalData()) {
4676 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4677 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4678 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4679 TL.setModeAttr(Record[Idx++]);
4680 }
John McCalla1ee0c52009-10-16 21:56:05 +00004681}
John McCall51bd8032009-10-18 01:05:36 +00004682void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004683 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004684}
John McCall51bd8032009-10-18 01:05:36 +00004685void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004686 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004687}
John McCall51bd8032009-10-18 01:05:36 +00004688void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004689 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004690}
John McCall51bd8032009-10-18 01:05:36 +00004691void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004692 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004693}
John McCall51bd8032009-10-18 01:05:36 +00004694void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004695 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004696}
John McCall51bd8032009-10-18 01:05:36 +00004697void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004698 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004699 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004700}
John McCall51bd8032009-10-18 01:05:36 +00004701void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004702 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4703 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004704 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004705 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004706 else
John McCall51bd8032009-10-18 01:05:36 +00004707 TL.setSizeExpr(0);
4708}
4709void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4710 VisitArrayTypeLoc(TL);
4711}
4712void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4713 VisitArrayTypeLoc(TL);
4714}
4715void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4716 VisitArrayTypeLoc(TL);
4717}
4718void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4719 DependentSizedArrayTypeLoc TL) {
4720 VisitArrayTypeLoc(TL);
4721}
4722void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4723 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004724 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004725}
4726void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004727 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004728}
4729void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004730 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004731}
4732void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004733 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004734 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4735 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004736 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004737 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004738 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004739 }
4740}
4741void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4742 VisitFunctionTypeLoc(TL);
4743}
4744void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4745 VisitFunctionTypeLoc(TL);
4746}
John McCalled976492009-12-04 22:46:56 +00004747void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004748 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004749}
John McCall51bd8032009-10-18 01:05:36 +00004750void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004751 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004752}
4753void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004754 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4755 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4756 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004757}
4758void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004759 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4760 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4761 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4762 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004763}
4764void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004765 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004766}
Sean Huntca63c202011-05-24 22:41:36 +00004767void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4768 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4769 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4770 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4771 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4772}
Richard Smith34b41d92011-02-20 03:19:35 +00004773void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4774 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4775}
John McCall51bd8032009-10-18 01:05:36 +00004776void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004777 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004778}
4779void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004780 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004781}
John McCall9d156a72011-01-06 01:58:22 +00004782void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4783 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4784 if (TL.hasAttrOperand()) {
4785 SourceRange range;
4786 range.setBegin(ReadSourceLocation(Record, Idx));
4787 range.setEnd(ReadSourceLocation(Record, Idx));
4788 TL.setAttrOperandParensRange(range);
4789 }
4790 if (TL.hasAttrExprOperand()) {
4791 if (Record[Idx++])
4792 TL.setAttrExprOperand(Reader.ReadExpr(F));
4793 else
4794 TL.setAttrExprOperand(0);
4795 } else if (TL.hasAttrEnumOperand())
4796 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4797}
John McCall51bd8032009-10-18 01:05:36 +00004798void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004799 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004800}
John McCall49a832b2009-10-18 09:09:24 +00004801void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4802 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004803 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004804}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004805void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4806 SubstTemplateTypeParmPackTypeLoc TL) {
4807 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4808}
John McCall51bd8032009-10-18 01:05:36 +00004809void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4810 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004811 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004812 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4813 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4814 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004815 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4816 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004817 Reader.GetTemplateArgumentLocInfo(F,
4818 TL.getTypePtr()->getArg(i).getKind(),
4819 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004820}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004821void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4822 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4823 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4824}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004825void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004826 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004827 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004828}
John McCall3cb0ebd2010-03-10 03:28:59 +00004829void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004830 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004831}
Douglas Gregor4714c122010-03-31 17:34:00 +00004832void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004833 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004834 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004835 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004836}
John McCall33500952010-06-11 00:33:02 +00004837void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4838 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004839 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004840 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004841 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004842 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004843 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4844 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004845 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4846 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004847 Reader.GetTemplateArgumentLocInfo(F,
4848 TL.getTypePtr()->getArg(I).getKind(),
4849 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004850}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004851void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4852 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4853}
John McCall51bd8032009-10-18 01:05:36 +00004854void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004855 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004856}
4857void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4858 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004859 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4860 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004861 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004862 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004863}
John McCall54e14c42009-10-22 22:37:11 +00004864void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004865 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004866}
Eli Friedmanb001de72011-10-06 23:00:33 +00004867void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4868 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4869 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4870 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4871}
John McCalla1ee0c52009-10-16 21:56:05 +00004872
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004873TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004874 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004875 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004876 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004877 if (InfoTy.isNull())
4878 return 0;
4879
Douglas Gregor35942772011-09-09 21:34:22 +00004880 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004881 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004882 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004883 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004884 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004885}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004886
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004887QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004888 unsigned FastQuals = ID & Qualifiers::FastMask;
4889 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004890
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004891 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004892 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004893 switch ((PredefinedTypeIDs)Index) {
4894 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004895 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4896 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004897
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004898 case PREDEF_TYPE_CHAR_U_ID:
4899 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004900 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004901 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004902 break;
4903
Douglas Gregor35942772011-09-09 21:34:22 +00004904 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4905 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4906 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4907 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4908 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4909 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4910 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4911 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4912 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4913 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4914 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4915 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4916 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004917 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004918 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4919 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4920 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4921 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4922 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004923 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004924 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4925 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4926 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4927 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4928 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4929 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4930 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4931 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4932 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004933
4934 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004935 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004936 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004937
4938 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4939 T = Context.ARCUnbridgedCastTy;
4940 break;
4941
Meador Ingefb40e3f2012-07-01 15:57:25 +00004942 case PREDEF_TYPE_VA_LIST_TAG:
4943 T = Context.getVaListTagType();
4944 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004945
4946 case PREDEF_TYPE_BUILTIN_FN:
4947 T = Context.BuiltinFnTy;
4948 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004949 }
4950
4951 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004952 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004953 }
4954
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004955 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004956 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004957 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004958 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004959 if (TypesLoaded[Index].isNull())
4960 return QualType();
4961
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004962 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004963 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004964 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004965 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004966 }
Mike Stump1eb44332009-09-09 15:08:12 +00004967
John McCall0953e762009-09-24 19:53:00 +00004968 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004969}
4970
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004971QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004972 return GetType(getGlobalTypeID(F, LocalID));
4973}
4974
4975serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004976ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004977 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4978 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4979
4980 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4981 return LocalID;
4982
4983 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4984 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4985 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4986
4987 unsigned GlobalIndex = LocalIndex + I->second;
4988 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4989}
4990
John McCall833ca992009-10-29 08:12:44 +00004991TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004992ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004993 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004994 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004995 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004996 switch (Kind) {
4997 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004998 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004999 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00005000 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00005001 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005002 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5003 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00005004 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005005 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00005006 SourceLocation());
5007 }
5008 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005009 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
5010 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00005011 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005012 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00005013 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00005014 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00005015 }
John McCall833ca992009-10-29 08:12:44 +00005016 case TemplateArgument::Null:
5017 case TemplateArgument::Integral:
5018 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005019 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00005020 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00005021 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00005022 return TemplateArgumentLocInfo();
5023 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00005024 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00005025}
5026
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005027TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005028ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005029 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00005030 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005031
5032 if (Arg.getKind() == TemplateArgument::Expression) {
5033 if (Record[Index++]) // bool InfoHasSameExpr.
5034 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
5035 }
Sebastian Redlc3632732010-10-05 15:59:54 +00005036 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00005037 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00005038}
5039
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005040Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00005041 return GetDecl(ID);
5042}
5043
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005044uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00005045 unsigned &Idx){
5046 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00005047 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005048
Douglas Gregore92b8a12011-08-04 00:01:48 +00005049 unsigned LocalID = Record[Idx++];
5050 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005051}
5052
5053CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005054 RecordLocation Loc = getLocalBitOffset(Offset);
5055 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005056 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005057 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005058 ReadingKindTracker ReadingKind(Read_Decl, *this);
5059 RecordData Record;
5060 unsigned Code = Cursor.ReadCode();
5061 unsigned RecCode = Cursor.ReadRecord(Code, Record);
5062 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
5063 Error("Malformed AST file: missing C++ base specifiers");
5064 return 0;
5065 }
5066
5067 unsigned Idx = 0;
5068 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005069 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005070 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
5071 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005072 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005073 return Bases;
5074}
5075
Douglas Gregor409448c2011-07-21 22:35:25 +00005076serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00005077ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005078 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00005079 return LocalID;
5080
5081 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005082 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00005083 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
5084
5085 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00005086}
5087
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005088bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005089 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005090 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
5091 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5092 return &M == I->second;
5093}
5094
Douglas Gregorcff9f262012-01-27 01:47:08 +00005095ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
5096 if (!D->isFromASTFile())
5097 return 0;
5098 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
5099 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5100 return I->second;
5101}
5102
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005103SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
5104 if (ID < NUM_PREDEF_DECL_IDS)
5105 return SourceLocation();
5106
5107 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5108
5109 if (Index > DeclsLoaded.size()) {
5110 Error("declaration ID out-of-range for AST file");
5111 return SourceLocation();
5112 }
5113
5114 if (Decl *D = DeclsLoaded[Index])
5115 return D->getLocation();
5116
5117 unsigned RawLocation = 0;
5118 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5119 return ReadSourceLocation(*Rec.F, RawLocation);
5120}
5121
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005122Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005123 if (ID < NUM_PREDEF_DECL_IDS) {
5124 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005125 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005126 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005127
5128 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005129 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00005130
5131 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005132 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00005133
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005134 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005135 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005136
Douglas Gregor79d67262011-08-12 05:59:41 +00005137 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005138 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005139
Douglas Gregora6ea10e2012-01-17 18:09:05 +00005140 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5141 return Context.getObjCProtocolDecl();
5142
Douglas Gregor772eeae2011-08-12 06:49:56 +00005143 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005144 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005145
5146 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005147 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00005148
5149 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005150 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00005151
5152 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5153 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005154 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005155 }
5156
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005157 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5158
Richard Smith2fbf3732011-12-20 04:39:57 +00005159 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005160 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005161 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005162 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005163 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005164
Douglas Gregorfd002a72011-12-16 22:37:11 +00005165 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00005166 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00005167 if (DeserializationListener)
5168 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5169 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005170
5171 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005172}
5173
Douglas Gregora1be2782011-12-17 23:38:30 +00005174DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5175 DeclID GlobalID) {
5176 if (GlobalID < NUM_PREDEF_DECL_IDS)
5177 return GlobalID;
5178
5179 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5180 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5181 ModuleFile *Owner = I->second;
5182
5183 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5184 = M.GlobalToLocalDeclIDs.find(Owner);
5185 if (Pos == M.GlobalToLocalDeclIDs.end())
5186 return 0;
5187
5188 return GlobalID - Owner->BaseDeclID + Pos->second;
5189}
5190
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005191serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005192 const RecordData &Record,
5193 unsigned &Idx) {
5194 if (Idx >= Record.size()) {
5195 Error("Corrupted AST file");
5196 return 0;
5197 }
5198
5199 return getGlobalDeclID(F, Record[Idx++]);
5200}
5201
Chris Lattner887e2b32009-04-27 05:46:25 +00005202/// \brief Resolve the offset of a statement into a statement.
5203///
5204/// This operation will read a new statement from the external
5205/// source each time it is called, and is meant to be used via a
5206/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005207Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005208 // Switch case IDs are per Decl.
5209 ClearSwitchCaseIDs();
5210
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00005211 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005212 RecordLocation Loc = getLocalBitOffset(Offset);
5213 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5214 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00005215}
5216
Douglas Gregor851c75a2011-08-24 21:27:34 +00005217namespace {
5218 class FindExternalLexicalDeclsVisitor {
5219 ASTReader &Reader;
5220 const DeclContext *DC;
5221 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005222
Douglas Gregor851c75a2011-08-24 21:27:34 +00005223 SmallVectorImpl<Decl*> &Decls;
5224 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5225
5226 public:
5227 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5228 bool (*isKindWeWant)(Decl::Kind),
5229 SmallVectorImpl<Decl*> &Decls)
5230 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5231 {
5232 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5233 PredefsVisited[I] = false;
5234 }
5235
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005236 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00005237 if (Preorder)
5238 return false;
5239
5240 FindExternalLexicalDeclsVisitor *This
5241 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5242
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005243 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00005244 = M.DeclContextInfos.find(This->DC);
5245 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5246 return false;
5247
5248 // Load all of the declaration IDs
5249 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5250 *IDE = ID + Info->second.NumLexicalDecls;
5251 ID != IDE; ++ID) {
5252 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5253 continue;
5254
5255 // Don't add predefined declarations to the lexical context more
5256 // than once.
5257 if (ID->second < NUM_PREDEF_DECL_IDS) {
5258 if (This->PredefsVisited[ID->second])
5259 continue;
5260
5261 This->PredefsVisited[ID->second] = true;
5262 }
5263
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005264 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5265 if (!This->DC->isDeclInLexicalTraversal(D))
5266 This->Decls.push_back(D);
5267 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00005268 }
5269
5270 return false;
5271 }
5272 };
5273}
5274
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005275ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00005276 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00005277 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005278 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00005279 // least. Walk all of the modules in the order they were loaded.
5280 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5281 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005282 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005283 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005284}
5285
Douglas Gregor0d95f772011-08-24 19:03:07 +00005286namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005287
5288class DeclIDComp {
5289 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005290 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005291
5292public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005293 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005294
5295 bool operator()(LocalDeclID L, LocalDeclID R) const {
5296 SourceLocation LHS = getLocation(L);
5297 SourceLocation RHS = getLocation(R);
5298 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5299 }
5300
5301 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5302 SourceLocation RHS = getLocation(R);
5303 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5304 }
5305
5306 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5307 SourceLocation LHS = getLocation(L);
5308 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5309 }
5310
5311 SourceLocation getLocation(LocalDeclID ID) const {
5312 return Reader.getSourceManager().getFileLoc(
5313 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5314 }
5315};
5316
5317}
5318
5319void ASTReader::FindFileRegionDecls(FileID File,
5320 unsigned Offset, unsigned Length,
5321 SmallVectorImpl<Decl *> &Decls) {
5322 SourceManager &SM = getSourceManager();
5323
5324 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5325 if (I == FileDeclIDs.end())
5326 return;
5327
5328 FileDeclsInfo &DInfo = I->second;
5329 if (DInfo.Decls.empty())
5330 return;
5331
5332 SourceLocation
5333 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5334 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5335
5336 DeclIDComp DIDComp(*this, *DInfo.Mod);
5337 ArrayRef<serialization::LocalDeclID>::iterator
5338 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5339 BeginLoc, DIDComp);
5340 if (BeginIt != DInfo.Decls.begin())
5341 --BeginIt;
5342
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005343 // If we are pointing at a top-level decl inside an objc container, we need
5344 // to backtrack until we find it otherwise we will fail to report that the
5345 // region overlaps with an objc container.
5346 while (BeginIt != DInfo.Decls.begin() &&
5347 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5348 ->isTopLevelDeclInObjCContainer())
5349 --BeginIt;
5350
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005351 ArrayRef<serialization::LocalDeclID>::iterator
5352 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5353 EndLoc, DIDComp);
5354 if (EndIt != DInfo.Decls.end())
5355 ++EndIt;
5356
5357 for (ArrayRef<serialization::LocalDeclID>::iterator
5358 DIt = BeginIt; DIt != EndIt; ++DIt)
5359 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5360}
5361
5362namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005363 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005364 /// declaration context.
5365 class DeclContextNameLookupVisitor {
5366 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005367 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005368 DeclarationName Name;
5369 SmallVectorImpl<NamedDecl *> &Decls;
5370
5371 public:
5372 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005373 SmallVectorImpl<const DeclContext *> &Contexts,
5374 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005375 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005376 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005377
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005378 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005379 DeclContextNameLookupVisitor *This
5380 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5381
5382 // Check whether we have any visible declaration information for
5383 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005384 ModuleFile::DeclContextInfosMap::iterator Info;
5385 bool FoundInfo = false;
5386 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5387 Info = M.DeclContextInfos.find(This->Contexts[I]);
5388 if (Info != M.DeclContextInfos.end() &&
5389 Info->second.NameLookupTableData) {
5390 FoundInfo = true;
5391 break;
5392 }
5393 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005394
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005395 if (!FoundInfo)
5396 return false;
5397
Douglas Gregor0d95f772011-08-24 19:03:07 +00005398 // Look for this name within this module.
5399 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005400 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005401 ASTDeclContextNameLookupTable::iterator Pos
5402 = LookupTable->find(This->Name);
5403 if (Pos == LookupTable->end())
5404 return false;
5405
5406 bool FoundAnything = false;
5407 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5408 for (; Data.first != Data.second; ++Data.first) {
5409 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5410 if (!ND)
5411 continue;
5412
5413 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005414 // A name might be null because the decl's redeclarable part is
5415 // currently read before reading its name. The lookup is triggered by
5416 // building that decl (likely indirectly), and so it is later in the
5417 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005418 continue;
5419 }
5420
5421 // Record this declaration.
5422 FoundAnything = true;
5423 This->Decls.push_back(ND);
5424 }
5425
5426 return FoundAnything;
5427 }
5428 };
5429}
5430
John McCall76bd1f32010-06-01 09:23:16 +00005431DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005432ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005433 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005434 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005435 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005436 if (!Name)
5437 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5438 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005439
Chris Lattner5f9e2722011-07-23 10:55:15 +00005440 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005441
5442 // Compute the declaration contexts we need to look into. Multiple such
5443 // declaration contexts occur when two declaration contexts from disjoint
5444 // modules get merged, e.g., when two namespaces with the same name are
5445 // independently defined in separate modules.
5446 SmallVector<const DeclContext *, 2> Contexts;
5447 Contexts.push_back(DC);
5448
5449 if (DC->isNamespace()) {
5450 MergedDeclsMap::iterator Merged
5451 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5452 if (Merged != MergedDecls.end()) {
5453 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5454 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5455 }
5456 }
5457
5458 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005459 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005460 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005461 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005462 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005463}
5464
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005465namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005466 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005467 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005468 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005469 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005470 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005471 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005472
5473 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005474 DeclContextAllNamesVisitor(ASTReader &Reader,
5475 SmallVectorImpl<const DeclContext *> &Contexts,
5476 llvm::DenseMap<DeclarationName,
5477 SmallVector<NamedDecl *, 8> > &Decls)
5478 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005479
5480 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005481 DeclContextAllNamesVisitor *This
5482 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005483
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005484 // Check whether we have any visible declaration information for
5485 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005486 ModuleFile::DeclContextInfosMap::iterator Info;
5487 bool FoundInfo = false;
5488 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5489 Info = M.DeclContextInfos.find(This->Contexts[I]);
5490 if (Info != M.DeclContextInfos.end() &&
5491 Info->second.NameLookupTableData) {
5492 FoundInfo = true;
5493 break;
5494 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005495 }
5496
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005497 if (!FoundInfo)
5498 return false;
5499
5500 ASTDeclContextNameLookupTable *LookupTable =
5501 Info->second.NameLookupTableData;
5502 bool FoundAnything = false;
5503 for (ASTDeclContextNameLookupTable::data_iterator
5504 I = LookupTable->data_begin(), E = LookupTable->data_end();
5505 I != E; ++I) {
5506 ASTDeclContextNameLookupTrait::data_type Data = *I;
5507 for (; Data.first != Data.second; ++Data.first) {
5508 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5509 *Data.first);
5510 if (!ND)
5511 continue;
5512
5513 // Record this declaration.
5514 FoundAnything = true;
5515 This->Decls[ND->getDeclName()].push_back(ND);
5516 }
5517 }
5518
5519 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005520 }
5521 };
5522}
5523
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005524void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005525 if (!DC->hasExternalVisibleStorage())
5526 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005527 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5528
5529 // Compute the declaration contexts we need to look into. Multiple such
5530 // declaration contexts occur when two declaration contexts from disjoint
5531 // modules get merged, e.g., when two namespaces with the same name are
5532 // independently defined in separate modules.
5533 SmallVector<const DeclContext *, 2> Contexts;
5534 Contexts.push_back(DC);
5535
5536 if (DC->isNamespace()) {
5537 MergedDeclsMap::iterator Merged
5538 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5539 if (Merged != MergedDecls.end()) {
5540 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5541 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5542 }
5543 }
5544
5545 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5546 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5547 ++NumVisibleDeclContextsRead;
5548
5549 for (llvm::DenseMap<DeclarationName,
5550 llvm::SmallVector<NamedDecl*, 8> >::iterator
5551 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5552 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5553 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005554 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005555}
5556
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005557/// \brief Under non-PCH compilation the consumer receives the objc methods
5558/// before receiving the implementation, and codegen depends on this.
5559/// We simulate this by deserializing and passing to consumer the methods of the
5560/// implementation before passing the deserialized implementation decl.
5561static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5562 ASTConsumer *Consumer) {
5563 assert(ImplD && Consumer);
5564
5565 for (ObjCImplDecl::method_iterator
5566 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005567 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005568
5569 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5570}
5571
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005572void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005573 assert(Consumer);
5574 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005575 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005576 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005577
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005578 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005579 }
5580}
5581
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005582void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5583 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5584 PassObjCImplDeclToConsumer(ImplD, Consumer);
5585 else
5586 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5587}
5588
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005589void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005590 this->Consumer = Consumer;
5591
Douglas Gregorfdd01722009-04-14 00:24:19 +00005592 if (!Consumer)
5593 return;
5594
5595 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005596 // Force deserialization of this decl, which will cause it to be queued for
5597 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005598 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005599 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005600 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005601
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005602 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005603}
5604
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005605void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005606 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005607
Mike Stump1eb44332009-09-09 15:08:12 +00005608 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005609 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005610 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005611 unsigned NumDeclsLoaded
5612 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5613 (Decl *)0);
5614 unsigned NumIdentifiersLoaded
5615 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5616 IdentifiersLoaded.end(),
5617 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005618 unsigned NumMacrosLoaded
5619 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5620 MacrosLoaded.end(),
5621 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005622 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005623 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5624 SelectorsLoaded.end(),
5625 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005626
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005627 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5628 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005629 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005630 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5631 NumSLocEntriesRead, TotalNumSLocEntries,
5632 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005633 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005634 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005635 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5636 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5637 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005638 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005639 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5640 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005641 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005642 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005643 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5644 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005645 if (!MacrosLoaded.empty())
5646 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5647 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5648 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005649 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005650 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005651 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5652 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005653 if (TotalNumStatements)
5654 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5655 NumStatementsRead, TotalNumStatements,
5656 ((float)NumStatementsRead/TotalNumStatements * 100));
5657 if (TotalNumMacros)
5658 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5659 NumMacrosRead, TotalNumMacros,
5660 ((float)NumMacrosRead/TotalNumMacros * 100));
5661 if (TotalLexicalDeclContexts)
5662 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5663 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5664 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5665 * 100));
5666 if (TotalVisibleDeclContexts)
5667 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5668 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5669 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5670 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005671 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005672 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005673 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5674 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005675 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005676 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005677 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005678 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005679 dump();
5680 std::fprintf(stderr, "\n");
5681}
5682
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005683template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005684static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005685dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005686 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005687 InitialCapacity> &Map) {
5688 if (Map.begin() == Map.end())
5689 return;
5690
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005691 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005692 llvm::errs() << Name << ":\n";
5693 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5694 I != IEnd; ++I) {
5695 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5696 << "\n";
5697 }
5698}
5699
Douglas Gregor23d7df52011-07-21 19:50:14 +00005700void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005701 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005702 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005703 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005704 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005705 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005706 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005707 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005708 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005709 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005710 dumpModuleIDMap("Global preprocessed entity map",
5711 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005712
5713 llvm::errs() << "\n*** PCH/Modules Loaded:";
5714 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5715 MEnd = ModuleMgr.end();
5716 M != MEnd; ++M)
5717 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005718}
5719
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005720/// Return the amount of memory used by memory buffers, breaking down
5721/// by heap-backed versus mmap'ed memory.
5722void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005723 for (ModuleConstIterator I = ModuleMgr.begin(),
5724 E = ModuleMgr.end(); I != E; ++I) {
5725 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005726 size_t bytes = buf->getBufferSize();
5727 switch (buf->getBufferKind()) {
5728 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5729 sizes.malloc_bytes += bytes;
5730 break;
5731 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5732 sizes.mmap_bytes += bytes;
5733 break;
5734 }
5735 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005736 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005737}
5738
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005739void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005740 SemaObj = &S;
Axel Naumann0ec56b72012-10-18 19:05:02 +00005741 S.addExternalSource(this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005742
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005743 // Makes sure any declarations that were deserialized "too early"
5744 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005745 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005746 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5747 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005748 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005749 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005750
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005751 // Load the offsets of the declarations that Sema references.
5752 // They will be lazily deserialized when needed.
5753 if (!SemaDeclRefs.empty()) {
5754 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005755 if (!SemaObj->StdNamespace)
5756 SemaObj->StdNamespace = SemaDeclRefs[0];
5757 if (!SemaObj->StdBadAlloc)
5758 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005759 }
5760
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005761 if (!FPPragmaOptions.empty()) {
5762 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5763 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5764 }
5765
5766 if (!OpenCLExtensions.empty()) {
5767 unsigned I = 0;
5768#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5769#include "clang/Basic/OpenCLExtensions.def"
5770
5771 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5772 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005773}
5774
Douglas Gregor211f6e82011-08-20 04:39:52 +00005775IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005776 // Note that we are loading an identifier.
5777 Deserializing AnIdentifier(this);
5778
Douglas Gregor057df202012-01-18 20:56:22 +00005779 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5780 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005781 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005782 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005783 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005784 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005785}
5786
Douglas Gregor95f42922010-10-14 22:11:03 +00005787namespace clang {
5788 /// \brief An identifier-lookup iterator that enumerates all of the
5789 /// identifiers stored within a set of AST files.
5790 class ASTIdentifierIterator : public IdentifierIterator {
5791 /// \brief The AST reader whose identifiers are being enumerated.
5792 const ASTReader &Reader;
5793
5794 /// \brief The current index into the chain of AST files stored in
5795 /// the AST reader.
5796 unsigned Index;
5797
5798 /// \brief The current position within the identifier lookup table
5799 /// of the current AST file.
5800 ASTIdentifierLookupTable::key_iterator Current;
5801
5802 /// \brief The end position within the identifier lookup table of
5803 /// the current AST file.
5804 ASTIdentifierLookupTable::key_iterator End;
5805
5806 public:
5807 explicit ASTIdentifierIterator(const ASTReader &Reader);
5808
Chris Lattner5f9e2722011-07-23 10:55:15 +00005809 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005810 };
5811}
5812
5813ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005814 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005815 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005816 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005817 Current = IdTable->key_begin();
5818 End = IdTable->key_end();
5819}
5820
Chris Lattner5f9e2722011-07-23 10:55:15 +00005821StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005822 while (Current == End) {
5823 // If we have exhausted all of our AST files, we're done.
5824 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005825 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005826
5827 --Index;
5828 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005829 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5830 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005831 Current = IdTable->key_begin();
5832 End = IdTable->key_end();
5833 }
5834
5835 // We have any identifiers remaining in the current AST file; return
5836 // the next one.
5837 std::pair<const char*, unsigned> Key = *Current;
5838 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005839 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005840}
5841
5842IdentifierIterator *ASTReader::getIdentifiers() const {
5843 return new ASTIdentifierIterator(*this);
5844}
5845
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005846namespace clang { namespace serialization {
5847 class ReadMethodPoolVisitor {
5848 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005849 Selector Sel;
5850 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005851 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5852 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005853
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005854 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005855 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5856 unsigned PriorGeneration)
5857 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005858
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005859 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005860 ReadMethodPoolVisitor *This
5861 = static_cast<ReadMethodPoolVisitor *>(UserData);
5862
5863 if (!M.SelectorLookupTable)
5864 return false;
5865
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005866 // If we've already searched this module file, skip it now.
5867 if (M.Generation <= This->PriorGeneration)
5868 return true;
5869
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005870 ASTSelectorLookupTable *PoolTable
5871 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5872 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5873 if (Pos == PoolTable->end())
5874 return false;
5875
5876 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005877 // FIXME: Not quite happy with the statistics here. We probably should
5878 // disable this tracking when called via LoadSelector.
5879 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005880 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005881 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005882 if (This->Reader.DeserializationListener)
5883 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5884 This->Sel);
5885
5886 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5887 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5888 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005889 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005890
5891 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005892 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5893 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005894 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005895
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005896 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005897 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5898 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005899 }
5900 };
5901} } // end namespace clang::serialization
5902
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005903/// \brief Add the given set of methods to the method list.
5904static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5905 ObjCMethodList &List) {
5906 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5907 S.addMethodToGlobalList(&List, Methods[I]);
5908 }
5909}
5910
5911void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005912 // Get the selector generation and update it to the current generation.
5913 unsigned &Generation = SelectorGeneration[Sel];
5914 unsigned PriorGeneration = Generation;
5915 Generation = CurrentGeneration;
5916
5917 // Search for methods defined with this selector.
5918 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005919 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005920
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005921 if (Visitor.getInstanceMethods().empty() &&
5922 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005923 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005924 return;
5925 }
5926
5927 if (!getSema())
5928 return;
5929
5930 Sema &S = *getSema();
5931 Sema::GlobalMethodPool::iterator Pos
5932 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5933
5934 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5935 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005936}
5937
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005938void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005939 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005940 Namespaces.clear();
5941
5942 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5943 if (NamespaceDecl *Namespace
5944 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5945 Namespaces.push_back(Namespace);
5946 }
5947}
5948
Douglas Gregora8623202011-07-27 20:58:46 +00005949void ASTReader::ReadTentativeDefinitions(
5950 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5951 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5952 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5953 if (Var)
5954 TentativeDefs.push_back(Var);
5955 }
5956 TentativeDefinitions.clear();
5957}
5958
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005959void ASTReader::ReadUnusedFileScopedDecls(
5960 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5961 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5962 DeclaratorDecl *D
5963 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5964 if (D)
5965 Decls.push_back(D);
5966 }
5967 UnusedFileScopedDecls.clear();
5968}
5969
Douglas Gregor0129b562011-07-27 21:57:17 +00005970void ASTReader::ReadDelegatingConstructors(
5971 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5972 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5973 CXXConstructorDecl *D
5974 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5975 if (D)
5976 Decls.push_back(D);
5977 }
5978 DelegatingCtorDecls.clear();
5979}
5980
Douglas Gregord58a0a52011-07-28 00:39:29 +00005981void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5982 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5983 TypedefNameDecl *D
5984 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5985 if (D)
5986 Decls.push_back(D);
5987 }
5988 ExtVectorDecls.clear();
5989}
5990
Douglas Gregora126f172011-07-28 00:53:40 +00005991void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5992 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5993 CXXRecordDecl *D
5994 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5995 if (D)
5996 Decls.push_back(D);
5997 }
5998 DynamicClasses.clear();
5999}
6000
Douglas Gregorec12ce22011-07-28 14:20:37 +00006001void
6002ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
6003 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
6004 NamedDecl *D
6005 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
6006 if (D)
6007 Decls.push_back(D);
6008 }
6009 LocallyScopedExternalDecls.clear();
6010}
6011
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00006012void ASTReader::ReadReferencedSelectors(
6013 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
6014 if (ReferencedSelectorsData.empty())
6015 return;
6016
6017 // If there are @selector references added them to its pool. This is for
6018 // implementation of -Wselector.
6019 unsigned int DataSize = ReferencedSelectorsData.size()-1;
6020 unsigned I = 0;
6021 while (I < DataSize) {
6022 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
6023 SourceLocation SelLoc
6024 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
6025 Sels.push_back(std::make_pair(Sel, SelLoc));
6026 }
6027 ReferencedSelectorsData.clear();
6028}
6029
Douglas Gregor31e37b22011-07-28 18:09:57 +00006030void ASTReader::ReadWeakUndeclaredIdentifiers(
6031 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
6032 if (WeakUndeclaredIdentifiers.empty())
6033 return;
6034
6035 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
6036 IdentifierInfo *WeakId
6037 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6038 IdentifierInfo *AliasId
6039 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
6040 SourceLocation Loc
6041 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
6042 bool Used = WeakUndeclaredIdentifiers[I++];
6043 WeakInfo WI(AliasId, Loc);
6044 WI.setUsed(Used);
6045 WeakIDs.push_back(std::make_pair(WeakId, WI));
6046 }
6047 WeakUndeclaredIdentifiers.clear();
6048}
6049
Douglas Gregordfe65432011-07-28 19:11:31 +00006050void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
6051 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
6052 ExternalVTableUse VT;
6053 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
6054 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
6055 VT.DefinitionRequired = VTableUses[Idx++];
6056 VTables.push_back(VT);
6057 }
6058
6059 VTableUses.clear();
6060}
6061
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006062void ASTReader::ReadPendingInstantiations(
6063 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
6064 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
6065 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
6066 SourceLocation Loc
6067 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00006068
Douglas Gregore5fa3c22012-10-03 18:34:48 +00006069 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00006070 }
6071 PendingInstantiations.clear();
6072}
6073
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006074void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00006075 // It would be complicated to avoid reading the methods anyway. So don't.
6076 ReadMethodPool(Sel);
6077}
6078
Douglas Gregor95eab172011-07-28 20:55:49 +00006079void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00006080 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00006081 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00006082 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006083 if (DeserializationListener)
6084 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00006085}
6086
Douglas Gregord89275b2009-07-06 18:54:52 +00006087/// \brief Set the globally-visible declarations associated with the given
6088/// identifier.
6089///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006090/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00006091/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00006092/// them.
6093///
6094/// \param II an IdentifierInfo that refers to one or more globally-visible
6095/// declarations.
6096///
6097/// \param DeclIDs the set of declaration IDs with the name @p II that are
6098/// visible at global scope.
6099///
6100/// \param Nonrecursive should be true to indicate that the caller knows that
6101/// this call is non-recursive, and therefore the globally-visible declarations
6102/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00006103void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006104ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00006105 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00006106 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006107 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00006108 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
6109 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
6110 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00006111 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00006112 return;
6113 }
Mike Stump1eb44332009-09-09 15:08:12 +00006114
Douglas Gregord89275b2009-07-06 18:54:52 +00006115 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6116 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6117 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00006118 // Introduce this declaration into the translation-unit scope
6119 // and add it to the declaration chain for this identifier, so
6120 // that (unqualified) name lookup will find it.
6121 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00006122 } else {
6123 // Queue this declaration so that it will be added to the
6124 // translation unit scope and identifier's declaration chain
6125 // once a Sema object is known.
6126 PreloadedDecls.push_back(D);
6127 }
6128 }
6129}
6130
Douglas Gregor95eab172011-07-28 20:55:49 +00006131IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00006132 if (ID == 0)
6133 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00006134
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006135 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006136 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00006137 return 0;
6138 }
Mike Stump1eb44332009-09-09 15:08:12 +00006139
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006140 ID -= 1;
6141 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00006142 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6143 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006144 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006145 unsigned Index = ID - M->BaseIdentifierID;
6146 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00006147
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006148 // All of the strings in the AST file are preceded by a 16-bit length.
6149 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00006150 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6151 // unsigned integers. This is important to avoid integer overflow when
6152 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00006153 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00006154 unsigned StrLen = (((unsigned) StrLenPtr[0])
6155 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006156 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006157 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006158 if (DeserializationListener)
6159 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00006160 }
Mike Stump1eb44332009-09-09 15:08:12 +00006161
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006162 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00006163}
6164
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006165IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00006166 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6167}
6168
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006169IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00006170 if (LocalID < NUM_PREDEF_IDENT_IDS)
6171 return LocalID;
6172
6173 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6174 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6175 assert(I != M.IdentifierRemap.end()
6176 && "Invalid index into identifier index remap");
6177
6178 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00006179}
6180
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006181MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00006182 if (ID == 0)
6183 return 0;
6184
6185 if (MacrosLoaded.empty()) {
6186 Error("no macro table in AST file");
6187 return 0;
6188 }
6189
6190 ID -= NUM_PREDEF_MACRO_IDS;
6191 if (!MacrosLoaded[ID]) {
6192 GlobalMacroMapType::iterator I
6193 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6194 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6195 ModuleFile *M = I->second;
6196 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006197 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00006198 }
6199
6200 return MacrosLoaded[ID];
6201}
6202
6203MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6204 if (LocalID < NUM_PREDEF_MACRO_IDS)
6205 return LocalID;
6206
6207 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6208 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6209 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6210
6211 return LocalID + I->second;
6212}
6213
Douglas Gregor26ced122011-12-01 00:59:36 +00006214serialization::SubmoduleID
6215ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6216 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6217 return LocalID;
6218
6219 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6220 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6221 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006222 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00006223
6224 return LocalID + I->second;
6225}
6226
6227Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6228 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6229 assert(GlobalID == 0 && "Unhandled global submodule ID");
6230 return 0;
6231 }
6232
6233 if (GlobalID > SubmodulesLoaded.size()) {
6234 Error("submodule ID out of range in AST file");
6235 return 0;
6236 }
6237
6238 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6239}
6240
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006241Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006242 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6243}
6244
6245Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006246 if (ID == 0)
6247 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00006248
Sebastian Redl725cd962010-08-04 20:40:17 +00006249 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006250 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006251 return Selector();
6252 }
Douglas Gregor83941df2009-04-25 17:48:32 +00006253
Sebastian Redl725cd962010-08-04 20:40:17 +00006254 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00006255 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00006256 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6257 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006258 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006259 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006260 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00006261 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00006262 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00006263 if (DeserializationListener)
6264 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00006265 }
6266
Sebastian Redl725cd962010-08-04 20:40:17 +00006267 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006268}
6269
Douglas Gregor8451ec72011-07-28 14:41:43 +00006270Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00006271 return DecodeSelector(ID);
6272}
6273
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006274uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00006275 // ID 0 (the null selector) is considered an external selector.
6276 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00006277}
6278
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006279serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006280ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006281 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6282 return LocalID;
6283
6284 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6285 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6286 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006287 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006288
6289 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00006290}
6291
Mike Stump1eb44332009-09-09 15:08:12 +00006292DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006293ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00006294 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00006295 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6296 switch (Kind) {
6297 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00006298 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006299
6300 case DeclarationName::ObjCZeroArgSelector:
6301 case DeclarationName::ObjCOneArgSelector:
6302 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006303 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006304
6305 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006306 return Context.DeclarationNames.getCXXConstructorName(
6307 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006308
6309 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006310 return Context.DeclarationNames.getCXXDestructorName(
6311 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006312
6313 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006314 return Context.DeclarationNames.getCXXConversionFunctionName(
6315 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006316
6317 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006318 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006319 (OverloadedOperatorKind)Record[Idx++]);
6320
Sean Hunt3e518bd2009-11-29 07:34:05 +00006321 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006322 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006323 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006324
Douglas Gregor2cf26342009-04-09 22:27:44 +00006325 case DeclarationName::CXXUsingDirective:
6326 return DeclarationName::getUsingDirectiveName();
6327 }
6328
David Blaikie7530c032012-01-17 06:56:22 +00006329 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006330}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006331
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006332void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006333 DeclarationNameLoc &DNLoc,
6334 DeclarationName Name,
6335 const RecordData &Record, unsigned &Idx) {
6336 switch (Name.getNameKind()) {
6337 case DeclarationName::CXXConstructorName:
6338 case DeclarationName::CXXDestructorName:
6339 case DeclarationName::CXXConversionFunctionName:
6340 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6341 break;
6342
6343 case DeclarationName::CXXOperatorName:
6344 DNLoc.CXXOperatorName.BeginOpNameLoc
6345 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6346 DNLoc.CXXOperatorName.EndOpNameLoc
6347 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6348 break;
6349
6350 case DeclarationName::CXXLiteralOperatorName:
6351 DNLoc.CXXLiteralOperatorName.OpNameLoc
6352 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6353 break;
6354
6355 case DeclarationName::Identifier:
6356 case DeclarationName::ObjCZeroArgSelector:
6357 case DeclarationName::ObjCOneArgSelector:
6358 case DeclarationName::ObjCMultiArgSelector:
6359 case DeclarationName::CXXUsingDirective:
6360 break;
6361 }
6362}
6363
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006364void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006365 DeclarationNameInfo &NameInfo,
6366 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006367 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006368 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6369 DeclarationNameLoc DNLoc;
6370 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6371 NameInfo.setInfo(DNLoc);
6372}
6373
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006374void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006375 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006376 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006377 unsigned NumTPLists = Record[Idx++];
6378 Info.NumTemplParamLists = NumTPLists;
6379 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006380 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006381 for (unsigned i=0; i != NumTPLists; ++i)
6382 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6383 }
6384}
6385
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006386TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006387ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006388 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006389 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006390 switch (Kind) {
6391 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006392 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006393
6394 case TemplateName::OverloadedTemplate: {
6395 unsigned size = Record[Idx++];
6396 UnresolvedSet<8> Decls;
6397 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006398 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006399
Douglas Gregor35942772011-09-09 21:34:22 +00006400 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006401 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006402
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006403 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006404 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006405 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006406 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006407 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006408 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006409
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006410 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006411 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006412 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006413 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006414 GetIdentifierInfo(F, Record,
6415 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006416 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006417 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006418 }
John McCall14606042011-06-30 08:33:18 +00006419
6420 case TemplateName::SubstTemplateTemplateParm: {
6421 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006422 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006423 if (!param) return TemplateName();
6424 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006425 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006426 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006427
6428 case TemplateName::SubstTemplateTemplateParmPack: {
6429 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006430 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006431 if (!Param)
6432 return TemplateName();
6433
6434 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6435 if (ArgPack.getKind() != TemplateArgument::Pack)
6436 return TemplateName();
6437
Douglas Gregor35942772011-09-09 21:34:22 +00006438 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006439 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006440 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006441
David Blaikieb219cfc2011-09-23 05:06:16 +00006442 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006443}
6444
6445TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006446ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006447 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006448 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6449 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006450 case TemplateArgument::Null:
6451 return TemplateArgument();
6452 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006453 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006454 case TemplateArgument::Declaration: {
6455 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6456 bool ForReferenceParam = Record[Idx++];
6457 return TemplateArgument(D, ForReferenceParam);
6458 }
6459 case TemplateArgument::NullPtr:
6460 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006461 case TemplateArgument::Integral: {
6462 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006463 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006464 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006465 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006466 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006467 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006468 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006469 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006470 llvm::Optional<unsigned> NumTemplateExpansions;
6471 if (unsigned NumExpansions = Record[Idx++])
6472 NumTemplateExpansions = NumExpansions - 1;
6473 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006474 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006475 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006476 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006477 case TemplateArgument::Pack: {
6478 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006479 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006480 for (unsigned I = 0; I != NumArgs; ++I)
6481 Args[I] = ReadTemplateArgument(F, Record, Idx);
6482 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006483 }
6484 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006485
David Blaikieb219cfc2011-09-23 05:06:16 +00006486 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006487}
6488
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006489TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006490ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006491 const RecordData &Record, unsigned &Idx) {
6492 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6493 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6494 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006495
6496 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006497 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006498 Params.reserve(NumParams);
6499 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006500 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006501
6502 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006503 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006504 Params.data(), Params.size(), RAngleLoc);
6505 return TemplateParams;
6506}
6507
6508void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006509ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006510ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006511 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006512 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006513 unsigned NumTemplateArgs = Record[Idx++];
6514 TemplArgs.reserve(NumTemplateArgs);
6515 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006516 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006517}
6518
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006519/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006520void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006521 const RecordData &Record, unsigned &Idx) {
6522 unsigned NumDecls = Record[Idx++];
6523 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006524 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006525 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6526 Set.addDecl(D, AS);
6527 }
6528}
6529
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006530CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006531ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006532 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006533 bool isVirtual = static_cast<bool>(Record[Idx++]);
6534 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6535 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006536 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006537 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6538 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006539 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006540 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006541 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006542 Result.setInheritConstructors(inheritConstructors);
6543 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006544}
6545
Sean Huntcbb67482011-01-08 20:30:50 +00006546std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006547ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006548 unsigned &Idx) {
6549 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006550 unsigned NumInitializers = Record[Idx++];
6551 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006552 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006553 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006554 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006555 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006556 bool IsBaseVirtual = false;
6557 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006558 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006559
Sean Hunt156b6402011-05-04 01:19:08 +00006560 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6561 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006562 case CTOR_INITIALIZER_BASE:
6563 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006564 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006565 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006566
6567 case CTOR_INITIALIZER_DELEGATING:
6568 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006569 break;
6570
6571 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006572 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006573 break;
6574
6575 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006576 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006577 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006578 }
Sean Hunt156b6402011-05-04 01:19:08 +00006579
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006580 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006581 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006582 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6583 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006584 bool IsWritten = Record[Idx++];
6585 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006586 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006587 if (IsWritten) {
6588 SourceOrderOrNumArrayIndices = Record[Idx++];
6589 } else {
6590 SourceOrderOrNumArrayIndices = Record[Idx++];
6591 Indices.reserve(SourceOrderOrNumArrayIndices);
6592 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006593 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006594 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006595
Sean Huntcbb67482011-01-08 20:30:50 +00006596 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006597 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006598 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006599 LParenLoc, Init, RParenLoc,
6600 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006601 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006602 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6603 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006604 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006605 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006606 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006607 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006608 else
Douglas Gregor35942772011-09-09 21:34:22 +00006609 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006610 MemberOrEllipsisLoc, LParenLoc,
6611 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006612 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006613 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006614 LParenLoc, Init, RParenLoc,
6615 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006616 }
6617
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006618 if (IsWritten)
6619 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006620 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006621 }
6622 }
6623
Sean Huntcbb67482011-01-08 20:30:50 +00006624 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006625}
6626
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006627NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006628ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006629 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006630 unsigned N = Record[Idx++];
6631 NestedNameSpecifier *NNS = 0, *Prev = 0;
6632 for (unsigned I = 0; I != N; ++I) {
6633 NestedNameSpecifier::SpecifierKind Kind
6634 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6635 switch (Kind) {
6636 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006637 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006638 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006639 break;
6640 }
6641
6642 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006643 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006644 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006645 break;
6646 }
6647
Douglas Gregor14aba762011-02-24 02:36:08 +00006648 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006649 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006650 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006651 break;
6652 }
6653
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006654 case NestedNameSpecifier::TypeSpec:
6655 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006656 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006657 if (!T)
6658 return 0;
6659
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006660 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006661 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006662 break;
6663 }
6664
6665 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006666 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006667 // No associated value, and there can't be a prefix.
6668 break;
6669 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006670 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006671 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006672 }
6673 return NNS;
6674}
6675
Douglas Gregordc355712011-02-25 00:36:19 +00006676NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006677ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006678 unsigned &Idx) {
6679 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006680 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006681 for (unsigned I = 0; I != N; ++I) {
6682 NestedNameSpecifier::SpecifierKind Kind
6683 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6684 switch (Kind) {
6685 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006686 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006687 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006688 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006689 break;
6690 }
6691
6692 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006693 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006694 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006695 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006696 break;
6697 }
6698
6699 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006700 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006701 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006702 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006703 break;
6704 }
6705
6706 case NestedNameSpecifier::TypeSpec:
6707 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006708 bool Template = Record[Idx++];
6709 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6710 if (!T)
6711 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006712 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006713
6714 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006715 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006716 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6717 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006718 break;
6719 }
6720
6721 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006722 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006723 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006724 break;
6725 }
6726 }
Douglas Gregordc355712011-02-25 00:36:19 +00006727 }
6728
Douglas Gregor35942772011-09-09 21:34:22 +00006729 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006730}
6731
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006732SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006733ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006734 unsigned &Idx) {
6735 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6736 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006737 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006738}
6739
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006740/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006741llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006742 unsigned BitWidth = Record[Idx++];
6743 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6744 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6745 Idx += NumWords;
6746 return Result;
6747}
6748
6749/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006750llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006751 bool isUnsigned = Record[Idx++];
6752 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6753}
6754
Douglas Gregor17fc2232009-04-14 21:55:33 +00006755/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006756llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006757 return llvm::APFloat(ReadAPInt(Record, Idx));
6758}
6759
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006760// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006761std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006762 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006763 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006764 Idx += Len;
6765 return Result;
6766}
6767
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006768VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6769 unsigned &Idx) {
6770 unsigned Major = Record[Idx++];
6771 unsigned Minor = Record[Idx++];
6772 unsigned Subminor = Record[Idx++];
6773 if (Minor == 0)
6774 return VersionTuple(Major);
6775 if (Subminor == 0)
6776 return VersionTuple(Major, Minor - 1);
6777 return VersionTuple(Major, Minor - 1, Subminor - 1);
6778}
6779
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006780CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006781 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006782 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006783 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006784 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006785}
6786
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006787DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006788 return Diag(SourceLocation(), DiagID);
6789}
6790
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006791DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006792 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006793}
Douglas Gregor025452f2009-04-17 00:04:06 +00006794
Douglas Gregor668c1a42009-04-21 22:25:48 +00006795/// \brief Retrieve the identifier table associated with the
6796/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006797IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006798 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006799}
6800
Douglas Gregor025452f2009-04-17 00:04:06 +00006801/// \brief Record that the given ID maps to the given switch-case
6802/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006803void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006804 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6805 "Already have a SwitchCase with this ID");
6806 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006807}
6808
6809/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006810SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006811 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6812 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006813}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006814
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006815void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006816 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006817}
6818
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006819void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006820 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006821 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6822 serialization::ModuleFile *> >::iterator
6823 I = CommentsCursors.begin(),
6824 E = CommentsCursors.end();
6825 I != E; ++I) {
6826 llvm::BitstreamCursor &Cursor = I->first;
6827 serialization::ModuleFile &F = *I->second;
6828 SavedStreamPosition SavedPosition(Cursor);
6829
6830 RecordData Record;
6831 while (true) {
6832 unsigned Code = Cursor.ReadCode();
6833 if (Code == llvm::bitc::END_BLOCK)
6834 break;
6835
6836 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6837 // No known subblocks, always skip them.
6838 Cursor.ReadSubBlockID();
6839 if (Cursor.SkipBlock()) {
6840 Error("malformed block record in AST file");
6841 return;
6842 }
6843 continue;
6844 }
6845
6846 if (Code == llvm::bitc::DEFINE_ABBREV) {
6847 Cursor.ReadAbbrevRecord();
6848 continue;
6849 }
6850
6851 // Read a record.
6852 Record.clear();
6853 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006854 case COMMENTS_RAW_COMMENT: {
6855 unsigned Idx = 0;
6856 SourceRange SR = ReadSourceRange(F, Record, Idx);
6857 RawComment::CommentKind Kind =
6858 (RawComment::CommentKind) Record[Idx++];
6859 bool IsTrailingComment = Record[Idx++];
6860 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006861 Comments.push_back(new (Context) RawComment(SR, Kind,
6862 IsTrailingComment,
6863 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006864 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006865 }
6866 }
6867 }
6868 }
6869 Context.Comments.addCommentsToFront(Comments);
6870}
6871
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006872void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006873 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6874 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006875 // If any identifiers with corresponding top-level declarations have
6876 // been loaded, load those declarations now.
6877 while (!PendingIdentifierInfos.empty()) {
6878 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6879 PendingIdentifierInfos.front().DeclIDs, true);
6880 PendingIdentifierInfos.pop_front();
6881 }
6882
Douglas Gregora1be2782011-12-17 23:38:30 +00006883 // Load pending declaration chains.
6884 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6885 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006886 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006887 }
6888 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006889
6890 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00006891 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6892 // FIXME: std::move here
6893 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006894 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00006895 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6896 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006897 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00006898 }
6899 }
6900 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006901 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006902
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006903 // If we deserialized any C++ or Objective-C class definitions, any
6904 // Objective-C protocol definitions, or any redeclarable templates, make sure
6905 // that all redeclarations point to the definitions. Note that this can only
6906 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006907 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6908 DEnd = PendingDefinitions.end();
6909 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006910 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6911 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6912 // Make sure that the TagType points at the definition.
6913 const_cast<TagType*>(TagT)->decl = TD;
6914 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006915
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006916 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6917 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6918 REnd = RD->redecls_end();
6919 R != REnd; ++R)
6920 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6921
6922 }
6923
Douglas Gregorfc529f72011-12-19 19:00:47 +00006924 continue;
6925 }
6926
Douglas Gregor1d784b22012-01-01 19:51:50 +00006927 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006928 // Make sure that the ObjCInterfaceType points at the definition.
6929 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6930 ->Decl = ID;
6931
Douglas Gregor1d784b22012-01-01 19:51:50 +00006932 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6933 REnd = ID->redecls_end();
6934 R != REnd; ++R)
6935 R->Data = ID->Data;
6936
6937 continue;
6938 }
6939
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006940 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6941 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6942 REnd = PD->redecls_end();
6943 R != REnd; ++R)
6944 R->Data = PD->Data;
6945
6946 continue;
6947 }
6948
6949 RedeclarableTemplateDecl *RTD
6950 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6951 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6952 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006953 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006954 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006955 }
6956 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006957
6958 // Load the bodies of any functions or methods we've encountered. We do
6959 // this now (delayed) so that we can be sure that the declaration chains
6960 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006961 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6962 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006963 PB != PBEnd; ++PB) {
6964 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6965 // FIXME: Check for =delete/=default?
6966 // FIXME: Complain about ODR violations here?
6967 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6968 FD->setLazyBody(PB->second);
6969 continue;
6970 }
6971
6972 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6973 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6974 MD->setLazyBody(PB->second);
6975 }
6976 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006977}
6978
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006979void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006980 assert(NumCurrentElementsDeserializing &&
6981 "FinishedDeserializing not paired with StartedDeserializing");
6982 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006983 // We decrease NumCurrentElementsDeserializing only after pending actions
6984 // are finished, to avoid recursively re-calling finishPendingActions().
6985 finishPendingActions();
6986 }
6987 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006988
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006989 if (NumCurrentElementsDeserializing == 0 &&
6990 Consumer && !PassingDeclsToConsumer) {
6991 // Guard variable to avoid recursively redoing the process of passing
6992 // decls to consumer.
6993 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6994 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006995
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006996 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006997 // We are not in recursive loading, so it's safe to pass the "interesting"
6998 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006999 Decl *D = InterestingDecls.front();
7000 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00007001 PassInterestingDeclToConsumer(D);
7002 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007003 }
Douglas Gregord89275b2009-07-06 18:54:52 +00007004}
Douglas Gregor501c1032010-08-19 00:28:17 +00007005
Douglas Gregorf8a1e512011-09-02 00:26:20 +00007006ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00007007 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007008 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00007009 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
7010 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00007011 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00007012 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregorcaed0602012-10-18 21:31:35 +00007013 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00007014 DisableStatCache(DisableStatCache),
7015 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00007016 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
7017 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007018 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007019 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
7020 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
7021 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
7022 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007023 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
7024 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00007025 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00007026 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00007027{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00007028 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00007029}
7030
Sebastian Redle1dde812010-08-24 00:50:04 +00007031ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00007032 for (DeclContextVisibleUpdatesPending::iterator
7033 I = PendingVisibleUpdates.begin(),
7034 E = PendingVisibleUpdates.end();
7035 I != E; ++I) {
7036 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
7037 F = I->second.end();
7038 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00007039 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00007040 }
7041}