blob: 22f5aedd4085de56b662c2068d63e0536c01e0c5 [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
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000772/// \brief Read a source manager block
Douglas Gregor4825fd72012-10-22 22:50:17 +0000773bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000774 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000775
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000776 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000777
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000778 // Set the source-location entry cursor to the current position in
779 // the stream. This cursor will be used to read the contents of the
780 // source manager block initially, and then lazily read
781 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000782 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000783
784 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000785 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000786 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000787 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000788 }
789
790 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000791 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000792 Error("malformed source manager block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000793 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000794 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000795
Douglas Gregor14f79002009-04-10 03:52:48 +0000796 RecordData Record;
797 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000798 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000799 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000800 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000801 Error("error at end of Source Manager block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000802 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000803 }
Douglas Gregor4825fd72012-10-22 22:50:17 +0000804 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +0000805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Douglas Gregor14f79002009-04-10 03:52:48 +0000807 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
808 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000809 SLocEntryCursor.ReadSubBlockID();
810 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000811 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000812 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000813 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000814 continue;
815 }
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Douglas Gregor14f79002009-04-10 03:52:48 +0000817 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000818 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000819 continue;
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Douglas Gregor14f79002009-04-10 03:52:48 +0000822 // Read a record.
823 const char *BlobStart;
824 unsigned BlobLen;
825 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000826 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000827 default: // Default behavior: ignore.
828 break;
829
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000830 case SM_SLOC_FILE_ENTRY:
831 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000832 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000833 // Once we hit one of the source location entries, we're done.
Douglas Gregor4825fd72012-10-22 22:50:17 +0000834 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +0000835 }
836 }
837}
838
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000839/// \brief If a header file is not found at the path that we expect it to be
840/// and the PCH file was moved from its original location, try to resolve the
841/// file by assuming that header+PCH were moved together and the header is in
842/// the same place relative to the PCH.
843static std::string
844resolveFileRelativeToOriginalDir(const std::string &Filename,
845 const std::string &OriginalDir,
846 const std::string &CurrDir) {
847 assert(OriginalDir != CurrDir &&
848 "No point trying to resolve the file if the PCH dir didn't change");
849 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000850 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000851 fs::make_absolute(filePath);
852 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000853 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000854
855 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
856 fileDirE = path::end(path::parent_path(filePath));
857 path::const_iterator origDirI = path::begin(OriginalDir),
858 origDirE = path::end(OriginalDir);
859 // Skip the common path components from filePath and OriginalDir.
860 while (fileDirI != fileDirE && origDirI != origDirE &&
861 *fileDirI == *origDirI) {
862 ++fileDirI;
863 ++origDirI;
864 }
865 for (; origDirI != origDirE; ++origDirI)
866 path::append(currPCHPath, "..");
867 path::append(currPCHPath, fileDirI, fileDirE);
868 path::append(currPCHPath, path::filename(Filename));
869 return currPCHPath.str();
870}
871
Douglas Gregor8b53d142012-10-22 22:53:10 +0000872bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000873 if (ID == 0)
Douglas Gregor4825fd72012-10-22 22:50:17 +0000874 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000875
Douglas Gregor0cdd7982011-07-21 18:46:38 +0000876 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000877 Error("source location entry ID out-of-range for AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000878 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000879 }
880
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000881 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000882 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +0000883 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000884 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +0000885
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000886 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000887 unsigned Code = SLocEntryCursor.ReadCode();
888 if (Code == llvm::bitc::END_BLOCK ||
889 Code == llvm::bitc::ENTER_SUBBLOCK ||
890 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000891 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000892 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000893 }
894
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000895 RecordData Record;
896 const char *BlobStart;
897 unsigned BlobLen;
898 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
899 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000900 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000901 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000902
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000903 case SM_SLOC_FILE_ENTRY: {
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +0000904 // We will detect whether a file changed and return 'Failure' for it, but
905 // we will also try to fail gracefully by setting up the SLocEntry.
Douglas Gregora930dc92012-10-22 18:42:04 +0000906 unsigned InputID = Record[4];
907 InputFile IF = getInputFile(*F, InputID);
908 const FileEntry *File = IF.getPointer();
909 bool OverriddenBuffer = IF.getInt();
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +0000910
Douglas Gregora930dc92012-10-22 18:42:04 +0000911 if (!IF.getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +0000912 return true;
Douglas Gregor2d52be52010-03-21 22:49:54 +0000913
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000914 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +0000915 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000916 // This is the module's main file.
917 IncludeLoc = getImportLocation(F);
918 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000919 SrcMgr::CharacteristicKind
920 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
921 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000922 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000923 SrcMgr::FileInfo &FileInfo =
924 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora930dc92012-10-22 18:42:04 +0000925 FileInfo.NumCreatedFIDs = Record[5];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000926 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000927 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +0000928
Douglas Gregora930dc92012-10-22 18:42:04 +0000929 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
930 unsigned NumFileDecls = Record[7];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +0000931 if (NumFileDecls) {
932 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +0000933 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
934 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +0000935 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000936
Douglas Gregor35f9ae62011-11-17 01:44:33 +0000937 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +0000938 = SourceMgr.getOrCreateContentCache(File,
939 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +0000940 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
941 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +0000942 unsigned Code = SLocEntryCursor.ReadCode();
943 Record.clear();
944 unsigned RecCode
945 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
946
947 if (RecCode != SM_SLOC_BUFFER_BLOB) {
948 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000949 return true;
Douglas Gregora081da52011-11-16 20:05:18 +0000950 }
951
952 llvm::MemoryBuffer *Buffer
953 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Douglas Gregora930dc92012-10-22 18:42:04 +0000954 File->getName());
Douglas Gregora081da52011-11-16 20:05:18 +0000955 SourceMgr.overrideFileContents(File, Buffer);
956 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +0000957
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000958 break;
959 }
960
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000961 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000962 const char *Name = BlobStart;
963 unsigned Offset = Record[0];
964 unsigned Code = SLocEntryCursor.ReadCode();
965 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000966 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000967 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000968
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000969 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000970 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000971 return true;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000972 }
973
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000974 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +0000975 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
976 Name);
Douglas Gregor0ca6e272012-10-25 00:30:23 +0000977 SourceMgr.createFileIDForMemBuffer(Buffer, ID, BaseOffset + Offset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000978 break;
979 }
980
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000981 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +0000982 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +0000983 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +0000984 ReadSourceLocation(*F, Record[2]),
985 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000986 Record[4],
987 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000988 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000989 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000990 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000991 }
992
Douglas Gregor4825fd72012-10-22 22:50:17 +0000993 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000994}
995
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000996/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000997SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000998 if (F->ImportLoc.isValid())
999 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001000
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001001 // Otherwise we have a PCH. It's considered to be "imported" at the first
1002 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001003 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001004 // Main file is the importer. We assume that it is the first entry in the
1005 // entry table. We can't ask the manager, because at the time of PCH loading
1006 // the main file entry doesn't exist yet.
1007 // The very first entry is the invalid instantiation loc, which takes up
1008 // offsets 0 and 1.
1009 return SourceLocation::getFromRawEncoding(2U);
1010 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001011 //return F->Loaders[0]->FirstLoc;
1012 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001013}
1014
Chris Lattner6367f6d2009-04-27 01:05:14 +00001015/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1016/// specified cursor. Read the abbreviations that are at the top of the block
1017/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001018bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001019 unsigned BlockID) {
1020 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001021 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001022 return Failure;
1023 }
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattner6367f6d2009-04-27 01:05:14 +00001025 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001026 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001027 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001028
Chris Lattner6367f6d2009-04-27 01:05:14 +00001029 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001030 if (Code != llvm::bitc::DEFINE_ABBREV) {
1031 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001032 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001033 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001034 Cursor.ReadAbbrevRecord();
1035 }
1036}
1037
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00001038void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1039 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001040 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Douglas Gregor37e26842009-04-21 23:56:24 +00001042 // Keep track of where we are in the stream, then jump back there
1043 // after reading this macro.
1044 SavedStreamPosition SavedPosition(Stream);
1045
1046 Stream.JumpToBit(Offset);
1047 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001048 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001049 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Douglas Gregore8219a62012-10-11 21:07:39 +00001051 // RAII object to add the loaded macro information once we're done
1052 // adding tokens.
1053 struct AddLoadedMacroInfoRAII {
1054 Preprocessor &PP;
1055 MacroInfo *Hint;
1056 MacroInfo *MI;
1057 IdentifierInfo *II;
1058
1059 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1060 : PP(PP), Hint(Hint), MI(), II() { }
1061 ~AddLoadedMacroInfoRAII( ) {
1062 if (MI) {
1063 // Finally, install the macro.
1064 PP.addLoadedMacroInfo(II, MI, Hint);
1065 }
1066 }
1067 } AddLoadedMacroInfo(PP, Hint);
1068
Douglas Gregor37e26842009-04-21 23:56:24 +00001069 while (true) {
1070 unsigned Code = Stream.ReadCode();
1071 switch (Code) {
1072 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001073 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001074
1075 case llvm::bitc::ENTER_SUBBLOCK:
1076 // No known subblocks, always skip them.
1077 Stream.ReadSubBlockID();
1078 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001079 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001080 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001081 }
1082 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Douglas Gregor37e26842009-04-21 23:56:24 +00001084 case llvm::bitc::DEFINE_ABBREV:
1085 Stream.ReadAbbrevRecord();
1086 continue;
1087 default: break;
1088 }
1089
1090 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001091 const char *BlobStart = 0;
1092 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001093 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001094 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001095 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001096 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001097 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001098 case PP_MACRO_OBJECT_LIKE:
1099 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001100 // If we already have a macro, that means that we've hit the end
1101 // of the definition of the macro we were looking for. We're
1102 // done.
1103 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001104 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001105
Douglas Gregor95eab172011-07-28 20:55:49 +00001106 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001107 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001108 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001109 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Douglas Gregora8235d62012-10-09 23:05:51 +00001112 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1113
1114 // If this macro has already been loaded, don't do so again.
1115 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1116 return;
1117
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001118 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1119 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001120 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001121 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001122
Douglas Gregora8235d62012-10-09 23:05:51 +00001123 // Record this macro.
1124 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1125
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001126 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1127 if (UndefLoc.isValid())
1128 MI->setUndefLoc(UndefLoc);
1129
1130 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001131 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001133 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001134 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001135
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001136 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001137 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001138 bool isC99VarArgs = Record[NextIndex++];
1139 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001140 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001141 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001142 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001143 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001144
1145 // Install function-like macro info.
1146 MI->setIsFunctionLike();
1147 if (isC99VarArgs) MI->setIsC99Varargs();
1148 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001149 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001150 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001151 }
1152
Douglas Gregora8235d62012-10-09 23:05:51 +00001153 if (DeserializationListener)
1154 DeserializationListener->MacroRead(GlobalID, MI);
1155
1156 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001157 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001158 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1159 if (Update != MacroUpdates.end()) {
1160 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001161 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1162 bool Hidden = false;
1163 if (unsigned SubmoduleID = Update->second[I].first) {
1164 if (Module *Owner = getSubmodule(SubmoduleID)) {
1165 if (Owner->NameVisibility == Module::Hidden) {
1166 // Note that this #undef is hidden.
1167 Hidden = true;
1168
1169 // Record this hiding for later.
1170 HiddenNamesMap[Owner].push_back(
1171 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1172 }
1173 }
1174 }
1175
1176 if (!Hidden) {
1177 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1178 if (PPMutationListener *Listener = PP.getPPMutationListener())
1179 Listener->UndefinedMacro(MI);
1180 break;
1181 }
1182 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001183 }
1184 MacroUpdates.erase(Update);
1185 }
1186
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001187 // Determine whether this macro definition is visible.
1188 bool Hidden = !MI->isPublic();
1189 if (!Hidden && GlobalSubmoduleID) {
1190 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1191 if (Owner->NameVisibility == Module::Hidden) {
1192 // The owning module is not visible, and this macro definition
1193 // should not be, either.
1194 Hidden = true;
1195
1196 // Note that this macro definition was hidden because its owning
1197 // module is not yet visible.
1198 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1199 }
1200 }
1201 }
1202 MI->setHidden(Hidden);
1203
Douglas Gregore8219a62012-10-11 21:07:39 +00001204 // Make sure we install the macro once we're done.
1205 AddLoadedMacroInfo.MI = MI;
1206 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001207
1208 // Remember that we saw this macro last so that we add the tokens that
1209 // form its body to it.
1210 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001211
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001212 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1213 Record[NextIndex]) {
1214 // We have a macro definition. Register the association
1215 PreprocessedEntityID
1216 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1217 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1218 PPRec.RegisterMacroDefinition(Macro,
1219 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001220 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001221
Douglas Gregor37e26842009-04-21 23:56:24 +00001222 ++NumMacrosRead;
1223 break;
1224 }
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001226 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001227 // If we see a TOKEN before a PP_MACRO_*, then the file is
1228 // erroneous, just pretend we didn't see this.
1229 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Douglas Gregor37e26842009-04-21 23:56:24 +00001231 Token Tok;
1232 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001233 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001234 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001235 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001236 Tok.setIdentifierInfo(II);
1237 Tok.setKind((tok::TokenKind)Record[3]);
1238 Tok.setFlag((Token::TokenFlags)Record[4]);
1239 Macro->AddTokenToBody(Tok);
1240 break;
1241 }
David Blaikie7530c032012-01-17 06:56:22 +00001242 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001243 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001244}
1245
Douglas Gregor86c67d82011-07-28 22:39:26 +00001246PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001247ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001248 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001249 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1250 assert(I != M.PreprocessedEntityRemap.end()
1251 && "Invalid index into preprocessed entity index remap");
1252
1253 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001254}
1255
Douglas Gregor98339b92011-08-25 20:47:51 +00001256unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1257 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001258}
Douglas Gregor98339b92011-08-25 20:47:51 +00001259
1260HeaderFileInfoTrait::internal_key_type
1261HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1262
1263bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1264 if (strcmp(a, b) == 0)
1265 return true;
1266
1267 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1268 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001269
1270 // Determine whether the actual files are equivalent.
1271 bool Result = false;
1272 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001273 return false;
1274
Douglas Gregor99a922b2011-12-09 16:22:07 +00001275 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001276}
1277
1278std::pair<unsigned, unsigned>
1279HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1280 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1281 unsigned DataLen = (unsigned) *d++;
1282 return std::make_pair(KeyLen + 1, DataLen);
1283}
1284
1285HeaderFileInfoTrait::data_type
1286HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1287 unsigned DataLen) {
1288 const unsigned char *End = d + DataLen;
1289 using namespace clang::io;
1290 HeaderFileInfo HFI;
1291 unsigned Flags = *d++;
1292 HFI.isImport = (Flags >> 5) & 0x01;
1293 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1294 HFI.DirInfo = (Flags >> 2) & 0x03;
1295 HFI.Resolved = (Flags >> 1) & 0x01;
1296 HFI.IndexHeaderMapHeader = Flags & 0x01;
1297 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001298 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1299 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001300 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1301 // The framework offset is 1 greater than the actual offset,
1302 // since 0 is used as an indicator for "no framework name".
1303 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1304 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1305 }
1306
1307 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1308 (void)End;
1309
1310 // This HeaderFileInfo was externally loaded.
1311 HFI.External = true;
1312 return HFI;
1313}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001314
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001315void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1316 II->setHadMacroDefinition(true);
1317 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1318 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001319}
1320
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001321void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001322 // Note that we are loading defined macros.
1323 Deserializing Macros(this);
1324
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001325 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1326 E = ModuleMgr.rend(); I != E; ++I) {
1327 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001328
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001329 // If there was no preprocessor block, skip this file.
1330 if (!MacroCursor.getBitStreamReader())
1331 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001332
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001333 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001334 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001335
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001336 RecordData Record;
1337 while (true) {
1338 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001339 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001340 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001341
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001342 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1343 // No known subblocks, always skip them.
1344 Cursor.ReadSubBlockID();
1345 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001346 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001347 return;
1348 }
1349 continue;
1350 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001351
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001352 if (Code == llvm::bitc::DEFINE_ABBREV) {
1353 Cursor.ReadAbbrevRecord();
1354 continue;
1355 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001356
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001357 // Read a record.
1358 const char *BlobStart;
1359 unsigned BlobLen;
1360 Record.clear();
1361 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1362 default: // Default behavior: ignore.
1363 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001364
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001365 case PP_MACRO_OBJECT_LIKE:
1366 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001367 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001368 break;
1369
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001370 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001371 // Ignore tokens.
1372 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001373 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001374 }
1375 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001376}
1377
Douglas Gregoreee242f2011-10-27 09:33:13 +00001378namespace {
1379 /// \brief Visitor class used to look up identifirs in an AST file.
1380 class IdentifierLookupVisitor {
1381 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001382 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001383 IdentifierInfo *Found;
1384 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001385 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1386 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001387
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001388 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001389 IdentifierLookupVisitor *This
1390 = static_cast<IdentifierLookupVisitor *>(UserData);
1391
Douglas Gregor057df202012-01-18 20:56:22 +00001392 // If we've already searched this module file, skip it now.
1393 if (M.Generation <= This->PriorGeneration)
1394 return true;
1395
Douglas Gregoreee242f2011-10-27 09:33:13 +00001396 ASTIdentifierLookupTable *IdTable
1397 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1398 if (!IdTable)
1399 return false;
1400
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001401 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1402 M, This->Found);
1403
Douglas Gregoreee242f2011-10-27 09:33:13 +00001404 std::pair<const char*, unsigned> Key(This->Name.begin(),
1405 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001406 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001407 if (Pos == IdTable->end())
1408 return false;
1409
1410 // Dereferencing the iterator has the effect of building the
1411 // IdentifierInfo node and populating it with the various
1412 // declarations it needs.
1413 This->Found = *Pos;
1414 return true;
1415 }
1416
1417 // \brief Retrieve the identifier info found within the module
1418 // files.
1419 IdentifierInfo *getIdentifierInfo() const { return Found; }
1420 };
1421}
1422
1423void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001424 // Note that we are loading an identifier.
1425 Deserializing AnIdentifier(this);
1426
Douglas Gregor057df202012-01-18 20:56:22 +00001427 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001428 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001429 PriorGeneration = IdentifierGeneration[&II];
1430
1431 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1432 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1433 markIdentifierUpToDate(&II);
1434}
1435
1436void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1437 if (!II)
1438 return;
1439
1440 II->setOutOfDate(false);
1441
1442 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001443 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001444 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001445}
1446
Douglas Gregora930dc92012-10-22 18:42:04 +00001447llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor38295be2012-10-22 23:51:00 +00001448ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregora930dc92012-10-22 18:42:04 +00001449 // If this ID is bogus, just return an empty input file.
1450 if (ID == 0 || ID > F.InputFilesLoaded.size())
1451 return InputFile();
1452
1453 // If we've already loaded this input file, return it.
1454 if (F.InputFilesLoaded[ID-1].getPointer())
1455 return F.InputFilesLoaded[ID-1];
1456
1457 // Go find this input file.
1458 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1459 SavedStreamPosition SavedPosition(Cursor);
1460 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1461
1462 unsigned Code = Cursor.ReadCode();
1463 RecordData Record;
1464 const char *BlobStart = 0;
1465 unsigned BlobLen = 0;
1466 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1467 &BlobStart, &BlobLen)) {
1468 case INPUT_FILE: {
1469 unsigned StoredID = Record[0];
1470 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi6b8194e2012-10-22 21:50:39 +00001471 (void)StoredID;
Douglas Gregora930dc92012-10-22 18:42:04 +00001472 off_t StoredSize = (off_t)Record[1];
1473 time_t StoredTime = (time_t)Record[2];
1474 bool Overridden = (bool)Record[3];
1475
1476 // Get the file entry for this input file.
1477 StringRef OrigFilename(BlobStart, BlobLen);
1478 std::string Filename = OrigFilename;
1479 MaybeAddSystemRootToFilename(F, Filename);
1480 const FileEntry *File
1481 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1482 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1483
1484 // If we didn't find the file, resolve it relative to the
1485 // original directory from which this AST file was created.
1486 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1487 F.OriginalDir != CurrentDir) {
1488 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1489 F.OriginalDir,
1490 CurrentDir);
1491 if (!Resolved.empty())
1492 File = FileMgr.getFile(Resolved);
1493 }
1494
1495 // For an overridden file, create a virtual file with the stored
1496 // size/timestamp.
1497 if (Overridden && File == 0) {
1498 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1499 }
1500
1501 if (File == 0) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001502 if (Complain) {
1503 std::string ErrorStr = "could not find file '";
1504 ErrorStr += Filename;
1505 ErrorStr += "' referenced by AST file";
1506 Error(ErrorStr.c_str());
1507 }
Douglas Gregora930dc92012-10-22 18:42:04 +00001508 return InputFile();
1509 }
1510
1511 // Note that we've loaded this input file.
1512 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1513
1514 // Check if there was a request to override the contents of the file
1515 // that was part of the precompiled header. Overridding such a file
1516 // can lead to problems when lexing using the source locations from the
1517 // PCH.
1518 SourceManager &SM = getSourceManager();
1519 if (!Overridden && SM.isFileOverridden(File)) {
1520 Error(diag::err_fe_pch_file_overridden, Filename);
1521 // After emitting the diagnostic, recover by disabling the override so
1522 // that the original file will be used.
1523 SM.disableFileContentsOverride(File);
1524 // The FileEntry is a virtual file entry with the size of the contents
1525 // that would override the original contents. Set it to the original's
1526 // size/time.
1527 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1528 StoredSize, StoredTime);
1529 }
1530
1531 // For an overridden file, there is nothing to validate.
1532 if (Overridden)
1533 return InputFile(File, Overridden);
1534
1535 // The stat info from the FileEntry came from the cached stat
1536 // info of the PCH, so we cannot trust it.
1537 struct stat StatBuf;
1538 if (::stat(File->getName(), &StatBuf) != 0) {
1539 StatBuf.st_size = File->getSize();
1540 StatBuf.st_mtime = File->getModificationTime();
1541 }
1542
1543 if ((StoredSize != StatBuf.st_size
1544#if !defined(LLVM_ON_WIN32)
1545 // In our regression testing, the Windows file system seems to
1546 // have inconsistent modification times that sometimes
1547 // erroneously trigger this error-handling path.
1548 || StoredTime != StatBuf.st_mtime
1549#endif
1550 )) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001551 if (Complain)
1552 Error(diag::err_fe_pch_file_modified, Filename);
1553
Douglas Gregora930dc92012-10-22 18:42:04 +00001554 return InputFile();
1555 }
1556
1557 return InputFile(File, Overridden);
1558 }
1559 }
1560
1561 return InputFile();
1562}
1563
Chris Lattner5f9e2722011-07-23 10:55:15 +00001564const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor69e16082012-10-18 21:47:16 +00001565 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001566 std::string Filename = filenameStrRef;
Douglas Gregor69e16082012-10-18 21:47:16 +00001567 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001568 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor69e16082012-10-18 21:47:16 +00001569 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1570 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001571 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor69e16082012-10-18 21:47:16 +00001572 M.OriginalDir,
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001573 CurrentDir);
1574 if (!resolved.empty())
1575 File = FileMgr.getFile(resolved);
1576 }
1577
1578 return File;
1579}
1580
Douglas Gregore650c8c2009-07-07 00:12:59 +00001581/// \brief If we are loading a relocatable PCH file, and the filename is
1582/// not an absolute path, add the system root to the beginning of the file
1583/// name.
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001584void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1585 std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001586 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregorcaed0602012-10-18 21:31:35 +00001587 if (!M.RelocatablePCH)
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001588 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Michael J. Spencer256053b2010-12-17 21:22:22 +00001590 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001591 return;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001592
Douglas Gregor832d6202011-07-22 16:35:34 +00001593 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001594 // If no system root was given, default to '/'
1595 Filename.insert(Filename.begin(), '/');
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001596 return;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Douglas Gregor832d6202011-07-22 16:35:34 +00001599 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001600 if (isysroot[Length - 1] != '/')
1601 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Douglas Gregor832d6202011-07-22 16:35:34 +00001603 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001604}
1605
Douglas Gregor38295be2012-10-22 23:51:00 +00001606ASTReader::ASTReadResult
1607ASTReader::ReadControlBlock(ModuleFile &F,
1608 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
1609 unsigned ClientLoadCapabilities) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001610 llvm::BitstreamCursor &Stream = F.Stream;
1611
1612 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1613 Error("malformed block record in AST file");
1614 return Failure;
1615 }
1616
1617 // Read all of the records and blocks in the control block.
1618 RecordData Record;
1619 while (!Stream.AtEndOfStream()) {
1620 unsigned Code = Stream.ReadCode();
1621 if (Code == llvm::bitc::END_BLOCK) {
1622 if (Stream.ReadBlockEnd()) {
1623 Error("error at end of control block in AST file");
1624 return Failure;
1625 }
1626
Douglas Gregora930dc92012-10-22 18:42:04 +00001627 // Validate all of the input files.
1628 if (!DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001629 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregora930dc92012-10-22 18:42:04 +00001630 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor38295be2012-10-22 23:51:00 +00001631 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001632 return OutOfDate;
Douglas Gregora930dc92012-10-22 18:42:04 +00001633 }
1634
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001635 return Success;
1636 }
1637
1638 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001639 switch (Stream.ReadSubBlockID()) {
1640 case INPUT_FILES_BLOCK_ID:
Douglas Gregora930dc92012-10-22 18:42:04 +00001641 F.InputFilesCursor = Stream;
1642 if (Stream.SkipBlock() || // Skip with the main cursor
1643 // Read the abbreviations
1644 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1645 Error("malformed block record in AST file");
Douglas Gregor745e6f12012-10-19 00:38:02 +00001646 return Failure;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001647 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001648 continue;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001649
1650 default:
1651 if (!Stream.SkipBlock())
1652 continue;
1653 break;
1654 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001655
1656 Error("malformed block record in AST file");
1657 return Failure;
1658 }
1659
1660 if (Code == llvm::bitc::DEFINE_ABBREV) {
1661 Stream.ReadAbbrevRecord();
1662 continue;
1663 }
1664
1665 // Read and process a record.
1666 Record.clear();
1667 const char *BlobStart = 0;
1668 unsigned BlobLen = 0;
1669 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1670 &BlobStart, &BlobLen)) {
1671 case METADATA: {
1672 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001673 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1674 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1675 : diag::warn_pch_version_too_new);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001676 return VersionMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001677 }
1678
1679 bool hasErrors = Record[5];
1680 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1681 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001682 return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001683 }
1684
Douglas Gregorcaed0602012-10-18 21:31:35 +00001685 F.RelocatablePCH = Record[4];
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001686
1687 const std::string &CurBranch = getClangFullRepositoryVersion();
1688 StringRef ASTBranch(BlobStart, BlobLen);
1689 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001690 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1691 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor4825fd72012-10-22 22:50:17 +00001692 return VersionMismatch;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001693 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001694 break;
1695 }
1696
1697 case IMPORTS: {
1698 // Load each of the imported PCH files.
1699 unsigned Idx = 0, N = Record.size();
1700 while (Idx < N) {
1701 // Read information about the AST file.
1702 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1703 unsigned Length = Record[Idx++];
1704 SmallString<128> ImportedFile(Record.begin() + Idx,
1705 Record.begin() + Idx + Length);
1706 Idx += Length;
1707
1708 // Load the AST file.
Douglas Gregor38295be2012-10-22 23:51:00 +00001709 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
1710 ClientLoadCapabilities)) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001711 case Failure: return Failure;
1712 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001713 case OutOfDate: return OutOfDate;
1714 case VersionMismatch: return VersionMismatch;
1715 case ConfigurationMismatch: return ConfigurationMismatch;
1716 case HadErrors: return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001717 case Success: break;
1718 }
1719 }
1720 break;
1721 }
1722
Douglas Gregor38295be2012-10-22 23:51:00 +00001723 case LANGUAGE_OPTIONS: {
1724 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1725 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001726 ParseLanguageOptions(Record, Complain, *Listener) &&
1727 !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001728 return ConfigurationMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001729 break;
Douglas Gregor38295be2012-10-22 23:51:00 +00001730 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001731
Douglas Gregoree097c12012-10-18 17:58:09 +00001732 case TARGET_OPTIONS: {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001733 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1734 if (Listener && &F == *ModuleMgr.begin() &&
1735 ParseTargetOptions(Record, Complain, *Listener) &&
1736 !DisableValidation)
1737 return ConfigurationMismatch;
Douglas Gregoree097c12012-10-18 17:58:09 +00001738 break;
1739 }
1740
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001741 case DIAGNOSTIC_OPTIONS: {
1742 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1743 if (Listener && &F == *ModuleMgr.begin() &&
1744 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1745 !DisableValidation)
1746 return ConfigurationMismatch;
1747 break;
1748 }
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001749
1750 case FILE_SYSTEM_OPTIONS: {
1751 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1752 if (Listener && &F == *ModuleMgr.begin() &&
1753 ParseFileSystemOptions(Record, Complain, *Listener) &&
1754 !DisableValidation)
1755 return ConfigurationMismatch;
1756 break;
1757 }
1758
Douglas Gregorbbf38312012-10-24 16:50:34 +00001759 case HEADER_SEARCH_OPTIONS: {
1760 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1761 if (Listener && &F == *ModuleMgr.begin() &&
1762 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1763 !DisableValidation)
1764 return ConfigurationMismatch;
1765 break;
1766 }
1767
Douglas Gregora71a7d82012-10-24 20:05:57 +00001768 case PREPROCESSOR_OPTIONS: {
1769 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1770 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor87699242012-10-25 00:07:54 +00001771 ParsePreprocessorOptions(Record, Complain, *Listener,
1772 SuggestedPredefines) &&
Douglas Gregora71a7d82012-10-24 20:05:57 +00001773 !DisableValidation)
1774 return ConfigurationMismatch;
1775 break;
1776 }
1777
Douglas Gregor39c497b2012-10-18 18:36:53 +00001778 case ORIGINAL_FILE:
Douglas Gregor69e16082012-10-18 21:47:16 +00001779 F.OriginalSourceFileID = FileID::get(Record[0]);
1780 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
1781 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1782 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001783 break;
1784
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001785 case ORIGINAL_PCH_DIR:
Douglas Gregor69e16082012-10-18 21:47:16 +00001786 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001787 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001788
Douglas Gregora930dc92012-10-22 18:42:04 +00001789 case INPUT_FILE_OFFSETS:
1790 F.InputFileOffsets = (const uint32_t *)BlobStart;
1791 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001792 break;
1793 }
Douglas Gregor745e6f12012-10-19 00:38:02 +00001794 }
1795
1796 Error("premature end of bitstream in AST file");
1797 return Failure;
1798}
1799
Douglas Gregor4825fd72012-10-22 22:50:17 +00001800bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001801 llvm::BitstreamCursor &Stream = F.Stream;
1802
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001803 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001804 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001805 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001806 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001807
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001808 // Read all of the records and blocks for the AST file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001809 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001810 while (!Stream.AtEndOfStream()) {
1811 unsigned Code = Stream.ReadCode();
1812 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001813 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001814 Error("error at end of module block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001815 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001816 }
Chris Lattner7356a312009-04-11 21:15:38 +00001817
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001818 DeclContext *DC = Context.getTranslationUnitDecl();
1819 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1820 DC->setMustBuildLookupTable();
1821
Douglas Gregor4825fd72012-10-22 22:50:17 +00001822 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001823 }
1824
1825 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1826 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001827 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001828 // We lazily load the decls block, but we want to set up the
1829 // DeclsCursor cursor to point into it. Clone our current bitcode
1830 // cursor to it, enter the block and read the abbrevs in that block.
1831 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001832 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001833 if (Stream.SkipBlock() || // Skip with the main cursor.
1834 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001835 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001836 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001837 return true;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001838 }
1839 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001841 case DECL_UPDATES_BLOCK_ID:
1842 if (Stream.SkipBlock()) {
1843 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001844 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001845 }
1846 break;
1847
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001848 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001849 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001850 if (!PP.getExternalSource())
1851 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001852
Douglas Gregorecdcb882010-10-20 22:00:55 +00001853 if (Stream.SkipBlock() ||
1854 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001855 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001856 return true;
Chris Lattner7356a312009-04-11 21:15:38 +00001857 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001858 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001859 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001860
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001861 case PREPROCESSOR_DETAIL_BLOCK_ID:
1862 F.PreprocessorDetailCursor = Stream;
1863 if (Stream.SkipBlock() ||
1864 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1865 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1866 Error("malformed preprocessor detail record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001867 return true;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001868 }
1869 F.PreprocessorDetailStartOffset
1870 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001871
1872 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001873 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001874 if (!PP.getPreprocessingRecord()->getExternalSource())
1875 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001876 break;
1877
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001878 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00001879 if (ReadSourceManagerBlock(F))
1880 return true;
Douglas Gregor14f79002009-04-10 03:52:48 +00001881 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001882
1883 case SUBMODULE_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00001884 if (ReadSubmoduleBlock(F))
1885 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001886 break;
1887
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001888 case COMMENTS_BLOCK_ID: {
1889 llvm::BitstreamCursor C = Stream;
1890 if (Stream.SkipBlock() ||
1891 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1892 Error("malformed comments block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001893 return true;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001894 }
1895 CommentsCursors.push_back(std::make_pair(C, &F));
1896 break;
1897 }
1898
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001899 default:
1900 if (!Stream.SkipBlock())
1901 break;
1902 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001903 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001904 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001905 continue;
1906 }
1907
1908 if (Code == llvm::bitc::DEFINE_ABBREV) {
1909 Stream.ReadAbbrevRecord();
1910 continue;
1911 }
1912
1913 // Read and process a record.
1914 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001915 const char *BlobStart = 0;
1916 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001917 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001918 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001919 default: // Default behavior: ignore.
1920 break;
1921
Douglas Gregora119da02011-08-02 16:26:37 +00001922 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001923 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001924 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001925 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001926 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001927 F.TypeOffsets = (const uint32_t *)BlobStart;
1928 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001929 unsigned LocalBaseTypeIndex = Record[1];
1930 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001931
Douglas Gregora119da02011-08-02 16:26:37 +00001932 if (F.LocalNumTypes > 0) {
1933 // Introduce the global -> local mapping for types within this module.
1934 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1935
1936 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001937 F.TypeRemap.insertOrReplace(
1938 std::make_pair(LocalBaseTypeIndex,
1939 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001940
1941 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1942 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001943 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001944 }
1945
Douglas Gregor496c7092011-08-03 15:48:04 +00001946 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001947 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001948 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001949 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001950 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001951 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001952 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001953 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001954 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001955
Douglas Gregor496c7092011-08-03 15:48:04 +00001956 if (F.LocalNumDecls > 0) {
1957 // Introduce the global -> local mapping for declarations within this
1958 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001959 GlobalDeclMap.insert(
1960 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001961
1962 // Introduce the local -> global mapping for declarations within this
1963 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001964 F.DeclRemap.insertOrReplace(
1965 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001966
Douglas Gregora1be2782011-12-17 23:38:30 +00001967 // Introduce the global -> local mapping for declarations within this
1968 // module.
1969 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1970
Douglas Gregor496c7092011-08-03 15:48:04 +00001971 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1972 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001973 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001974 }
1975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001977 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001978 DeclContextInfo &Info = F.DeclContextInfos[TU];
1979 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1980 Info.NumLexicalDecls
1981 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001982 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001983 break;
1984 }
1985
Sebastian Redle1dde812010-08-24 00:50:04 +00001986 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001987 unsigned Idx = 0;
1988 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001989 ASTDeclContextNameLookupTable *Table =
1990 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001991 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001992 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001993 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001994 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1995 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001996 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001997 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001998 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001999 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00002000 break;
2001 }
2002
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002003 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002004 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002005 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002006 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002007 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002008 (const unsigned char *)F.IdentifierTableData + Record[0],
2009 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002010 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002011
2012 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002013 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002014 break;
2015
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002016 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002017 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002018 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002019 return true;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002020 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002021 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2022 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002023 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002024 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00002025
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002026 if (F.LocalNumIdentifiers > 0) {
2027 // Introduce the global -> local mapping for identifiers within this
2028 // module.
2029 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2030 &F));
2031
2032 // Introduce the local -> global mapping for identifiers within this
2033 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002034 F.IdentifierRemap.insertOrReplace(
2035 std::make_pair(LocalBaseIdentifierID,
2036 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002037
2038 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2039 + F.LocalNumIdentifiers);
2040 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002041 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002042 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002043
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002044 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002045 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2046 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002047 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002048
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002049 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002050 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2051 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002052 break;
2053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002054 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002055 TotalNumStatements += Record[0];
2056 TotalNumMacros += Record[1];
2057 TotalLexicalDeclContexts += Record[2];
2058 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002059 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002060
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002061 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002062 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2063 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002064 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002065
Sean Huntebcbe1d2011-05-04 23:29:54 +00002066 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002067 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2068 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002069 break;
2070
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002071 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002072 if (Record.size() % 4 != 0) {
2073 Error("invalid weak identifiers record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002074 return true;
Douglas Gregor31e37b22011-07-28 18:09:57 +00002075 }
2076
2077 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2078 // files. This isn't the way to do it :)
2079 WeakUndeclaredIdentifiers.clear();
2080
2081 // Translate the weak, undeclared identifiers into global IDs.
2082 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2083 WeakUndeclaredIdentifiers.push_back(
2084 getGlobalIdentifierID(F, Record[I++]));
2085 WeakUndeclaredIdentifiers.push_back(
2086 getGlobalIdentifierID(F, Record[I++]));
2087 WeakUndeclaredIdentifiers.push_back(
2088 ReadSourceLocation(F, Record, I).getRawEncoding());
2089 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2090 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002091 break;
2092
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002093 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002094 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2095 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002096 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002097
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002098 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002099 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002100 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002101 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002102 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002103
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002104 if (F.LocalNumSelectors > 0) {
2105 // Introduce the global -> local mapping for selectors within this
2106 // module.
2107 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2108
2109 // Introduce the local -> global mapping for selectors within this
2110 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002111 F.SelectorRemap.insertOrReplace(
2112 std::make_pair(LocalBaseSelectorID,
2113 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002114
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002115 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2116 }
2117 break;
2118 }
2119
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002120 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002121 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002122 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002123 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002124 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002125 F.SelectorLookupTableData + Record[0],
2126 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002127 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002128 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002129 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002130
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002131 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002132 if (!Record.empty()) {
2133 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2134 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2135 Record[Idx++]));
2136 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2137 getRawEncoding());
2138 }
2139 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002140 break;
2141
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002142 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002143 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002144 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002145 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002146
2147 case FILE_SORTED_DECLS:
2148 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002149 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002150 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002151
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002152 case SOURCE_LOCATION_OFFSETS: {
2153 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002154 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002155 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002156 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002157 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2158 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002159 // Make our entry in the range map. BaseID is negative and growing, so
2160 // we invert it. Because we invert it, though, we need the other end of
2161 // the range.
2162 unsigned RangeStart =
2163 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2164 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2165 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2166
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002167 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2168 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2169 GlobalSLocOffsetMap.insert(
2170 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2171 - SLocSpaceSize,&F));
2172
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002173 // Initialize the remapping table.
2174 // Invalid stays invalid.
2175 F.SLocRemap.insert(std::make_pair(0U, 0));
2176 // This module. Base was 2 when being compiled.
2177 F.SLocRemap.insert(std::make_pair(2U,
2178 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002179
2180 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002181 break;
2182 }
2183
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002184 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002185 // Additional remapping information.
2186 const unsigned char *Data = (const unsigned char*)BlobStart;
2187 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002188
2189 // Continuous range maps we may be updating in our module.
2190 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002191 ContinuousRangeMap<uint32_t, int, 2>::Builder
2192 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002193 ContinuousRangeMap<uint32_t, int, 2>::Builder
2194 MacroRemap(F.MacroRemap);
2195 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002196 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2197 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002198 SubmoduleRemap(F.SubmoduleRemap);
2199 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002200 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002201 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002202 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2203
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002204 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002205 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002206 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002207 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002208 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002209 if (!OM) {
2210 Error("SourceLocation remap refers to unknown module");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002211 return true;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002212 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002213
2214 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2215 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002216 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002217 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002218 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002219 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2220 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002221 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002222
2223 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2224 SLocRemap.insert(std::make_pair(SLocOffset,
2225 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002226 IdentifierRemap.insert(
2227 std::make_pair(IdentifierIDOffset,
2228 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002229 MacroRemap.insert(std::make_pair(MacroIDOffset,
2230 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002231 PreprocessedEntityRemap.insert(
2232 std::make_pair(PreprocessedEntityIDOffset,
2233 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002234 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2235 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002236 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2237 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002238 DeclRemap.insert(std::make_pair(DeclIDOffset,
2239 OM->BaseDeclID - DeclIDOffset));
2240
Douglas Gregora119da02011-08-02 16:26:37 +00002241 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002242 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002243
2244 // Global -> local mappings.
2245 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002246 }
2247 break;
2248 }
2249
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002250 case SOURCE_MANAGER_LINE_TABLE:
2251 if (ParseLineTable(F, Record))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002252 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002253 break;
2254
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002255 case SOURCE_LOCATION_PRELOADS: {
2256 // Need to transform from the local view (1-based IDs) to the global view,
2257 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002258 if (!F.PreloadSLocEntries.empty()) {
2259 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002260 return true;
Douglas Gregorf249bf32011-08-25 21:09:44 +00002261 }
2262
2263 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002264 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002265 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002266
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002267 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002268 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2269 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002270 break;
2271
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002272 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002273 if (Record.size() % 3 != 0) {
2274 Error("Invalid VTABLE_USES record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002275 return true;
Douglas Gregordfe65432011-07-28 19:11:31 +00002276 }
2277
Sebastian Redl40566802010-08-05 18:21:25 +00002278 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002279 // FIXME: Modules will have some trouble with this. This is clearly not
2280 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002281 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002282
2283 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2284 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2285 VTableUses.push_back(
2286 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2287 VTableUses.push_back(Record[Idx++]);
2288 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002289 break;
2290
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002291 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002292 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2293 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002294 break;
2295
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002296 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002297 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002298 Error("Invalid existing PendingInstantiations");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002299 return true;
Axel Naumann39d26c32012-10-02 09:09:43 +00002300 }
2301
2302 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002303 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002304 return true;
Douglas Gregorf2abb522011-07-28 19:26:52 +00002305 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002306
Douglas Gregorf2abb522011-07-28 19:26:52 +00002307 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2308 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2309 PendingInstantiations.push_back(
2310 ReadSourceLocation(F, Record, I).getRawEncoding());
2311 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002312 break;
2313
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002314 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002315 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002316 // FIXME: Modules will have some trouble with this.
2317 SemaDeclRefs.clear();
2318 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2319 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002320 break;
2321
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002322 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002323 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2324 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2325 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002326
2327 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002328
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002329 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002330 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002331 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002332 if (!PP.getPreprocessingRecord()->getExternalSource())
2333 PP.getPreprocessingRecord()->SetExternalSource(*this);
2334 StartingID
2335 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002336 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002337 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002338
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002339 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002340 // Introduce the global -> local mapping for preprocessed entities in
2341 // this module.
2342 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2343
2344 // Introduce the local -> global mapping for preprocessed entities in
2345 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002346 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002347 std::make_pair(LocalBasePreprocessedEntityID,
2348 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2349 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002350
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002351 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002352 }
2353
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002354 case DECL_UPDATE_OFFSETS: {
2355 if (Record.size() % 2 != 0) {
2356 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002357 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002358 }
2359 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002360 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2361 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002362 break;
2363 }
2364
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002365 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002366 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002367 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002368 return true;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002369 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002370 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002371 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002372 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002373 break;
2374 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002375
Douglas Gregorcff9f262012-01-27 01:47:08 +00002376 case OBJC_CATEGORIES_MAP: {
2377 if (F.LocalNumObjCCategoriesInMap != 0) {
2378 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002379 return true;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002380 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002381
2382 F.LocalNumObjCCategoriesInMap = Record[0];
2383 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002384 break;
2385 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002386
Douglas Gregorcff9f262012-01-27 01:47:08 +00002387 case OBJC_CATEGORIES:
2388 F.ObjCCategories.swap(Record);
2389 break;
2390
Douglas Gregor7c789c12010-10-29 22:39:52 +00002391 case CXX_BASE_SPECIFIER_OFFSETS: {
2392 if (F.LocalNumCXXBaseSpecifiers != 0) {
2393 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002394 return true;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002395 }
2396
2397 F.LocalNumCXXBaseSpecifiers = Record[0];
2398 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002399 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002400 break;
2401 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002402
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002403 case DIAG_PRAGMA_MAPPINGS:
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002404 if (F.PragmaDiagMappings.empty())
2405 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002406 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002407 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2408 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002409 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002410
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002411 case CUDA_SPECIAL_DECL_REFS:
2412 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002413 // FIXME: Modules will have trouble with this.
2414 CUDASpecialDeclRefs.clear();
2415 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2416 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002417 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002418
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002419 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002420 F.HeaderFileInfoTableData = BlobStart;
2421 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002422 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002423 if (Record[0]) {
2424 F.HeaderFileInfoTable
2425 = HeaderFileInfoLookupTable::Create(
2426 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002427 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002428 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002429 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002430 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002431
2432 PP.getHeaderSearchInfo().SetExternalSource(this);
2433 if (!PP.getHeaderSearchInfo().getExternalLookup())
2434 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002435 }
2436 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002437 }
2438
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002439 case FP_PRAGMA_OPTIONS:
2440 // Later tables overwrite earlier ones.
2441 FPPragmaOptions.swap(Record);
2442 break;
2443
2444 case OPENCL_EXTENSIONS:
2445 // Later tables overwrite earlier ones.
2446 OpenCLExtensions.swap(Record);
2447 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002448
2449 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002450 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2451 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002452 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002453
2454 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002455 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2456 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002457 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002458
2459 case IMPORTED_MODULES: {
2460 if (F.Kind != MK_Module) {
2461 // If we aren't loading a module (which has its own exports), make
2462 // all of the imported modules visible.
2463 // FIXME: Deal with macros-only imports.
2464 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2465 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2466 ImportedModules.push_back(GlobalID);
2467 }
2468 }
2469 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002470 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002471
Douglas Gregora1be2782011-12-17 23:38:30 +00002472 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002473 F.RedeclarationChains.swap(Record);
2474 break;
2475 }
2476
2477 case LOCAL_REDECLARATIONS_MAP: {
2478 if (F.LocalNumRedeclarationsInMap != 0) {
2479 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002480 return true;
Douglas Gregora1be2782011-12-17 23:38:30 +00002481 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002482
Douglas Gregor2171bf12012-01-15 16:58:34 +00002483 F.LocalNumRedeclarationsInMap = Record[0];
2484 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002485 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002486 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002487
2488 case MERGED_DECLARATIONS: {
2489 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2490 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2491 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2492 for (unsigned N = Record[Idx++]; N > 0; --N)
2493 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2494 }
2495 break;
2496 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002497
2498 case MACRO_OFFSET: {
2499 if (F.LocalNumMacros != 0) {
2500 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002501 return true;
Douglas Gregora8235d62012-10-09 23:05:51 +00002502 }
2503 F.MacroOffsets = (const uint32_t *)BlobStart;
2504 F.LocalNumMacros = Record[0];
2505 unsigned LocalBaseMacroID = Record[1];
2506 F.BaseMacroID = getTotalNumMacros();
2507
2508 if (F.LocalNumMacros > 0) {
2509 // Introduce the global -> local mapping for macros within this module.
2510 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2511
2512 // Introduce the local -> global mapping for macros within this module.
2513 F.MacroRemap.insertOrReplace(
2514 std::make_pair(LocalBaseMacroID,
2515 F.BaseMacroID - LocalBaseMacroID));
2516
2517 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2518 }
2519 break;
2520 }
2521
2522 case MACRO_UPDATES: {
2523 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2524 MacroID ID = getGlobalMacroID(F, Record[I++]);
2525 if (I == N)
2526 break;
2527
Douglas Gregor54c8a402012-10-12 00:16:50 +00002528 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2529 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2530 MacroUpdate Update;
2531 Update.UndefLoc = UndefLoc;
2532 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002533 }
2534 break;
2535 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002536 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002537 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002538 Error("premature end of bitstream in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002539 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002540}
2541
Douglas Gregorecc2c092011-12-01 22:20:10 +00002542void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002543 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002544 switch (Names[I].getKind()) {
2545 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002546 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002547 break;
2548
2549 case HiddenName::MacroVisibility: {
2550 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2551 Macro.second->setHidden(!Macro.second->isPublic());
2552 if (Macro.second->isDefined()) {
2553 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2554 }
2555 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002556 }
2557
Douglas Gregor54c8a402012-10-12 00:16:50 +00002558 case HiddenName::MacroUndef: {
2559 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2560 if (Macro.second->isDefined()) {
2561 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2562 if (PPMutationListener *Listener = PP.getPPMutationListener())
2563 Listener->UndefinedMacro(Macro.second);
2564 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2565 }
2566 break;
2567 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002568 }
Douglas Gregor13292642011-12-02 15:45:10 +00002569 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002570}
2571
Douglas Gregor5e356932011-12-01 17:11:21 +00002572void ASTReader::makeModuleVisible(Module *Mod,
2573 Module::NameVisibilityKind NameVisibility) {
2574 llvm::SmallPtrSet<Module *, 4> Visited;
2575 llvm::SmallVector<Module *, 4> Stack;
2576 Stack.push_back(Mod);
2577 while (!Stack.empty()) {
2578 Mod = Stack.back();
2579 Stack.pop_back();
2580
2581 if (NameVisibility <= Mod->NameVisibility) {
2582 // This module already has this level of visibility (or greater), so
2583 // there is nothing more to do.
2584 continue;
2585 }
2586
Douglas Gregor51f564f2011-12-31 04:05:44 +00002587 if (!Mod->isAvailable()) {
2588 // Modules that aren't available cannot be made visible.
2589 continue;
2590 }
2591
Douglas Gregor5e356932011-12-01 17:11:21 +00002592 // Update the module's name visibility.
2593 Mod->NameVisibility = NameVisibility;
2594
Douglas Gregorecc2c092011-12-01 22:20:10 +00002595 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002596 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002597 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2598 if (Hidden != HiddenNamesMap.end()) {
2599 makeNamesVisible(Hidden->second);
2600 HiddenNamesMap.erase(Hidden);
2601 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002602
2603 // Push any non-explicit submodules onto the stack to be marked as
2604 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002605 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2606 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002607 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002608 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2609 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002610 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002611
2612 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002613 bool AnyWildcard = false;
2614 bool UnrestrictedWildcard = false;
2615 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002616 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2617 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002618 if (!Mod->Exports[I].getInt()) {
2619 // Export a named module directly; no wildcards involved.
2620 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002621 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002622
2623 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002624 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002625
2626 // Wildcard export: export all of the imported modules that match
2627 // the given pattern.
2628 AnyWildcard = true;
2629 if (UnrestrictedWildcard)
2630 continue;
2631
2632 if (Module *Restriction = Mod->Exports[I].getPointer())
2633 WildcardRestrictions.push_back(Restriction);
2634 else {
2635 WildcardRestrictions.clear();
2636 UnrestrictedWildcard = true;
2637 }
2638 }
2639
2640 // If there were any wildcards, push any imported modules that were
2641 // re-exported by the wildcard restriction.
2642 if (!AnyWildcard)
2643 continue;
2644
2645 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2646 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002647 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002648 continue;
2649
2650 bool Acceptable = UnrestrictedWildcard;
2651 if (!Acceptable) {
2652 // Check whether this module meets one of the restrictions.
2653 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2654 Module *Restriction = WildcardRestrictions[R];
2655 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2656 Acceptable = true;
2657 break;
2658 }
2659 }
2660 }
2661
2662 if (!Acceptable)
2663 continue;
2664
Douglas Gregor0adaa882011-12-05 17:28:06 +00002665 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002666 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002667 }
2668}
2669
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002670ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor38295be2012-10-22 23:51:00 +00002671 ModuleKind Type,
2672 unsigned ClientLoadCapabilities) {
Douglas Gregor057df202012-01-18 20:56:22 +00002673 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002674 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002675
2676 // Load the core of the AST files.
2677 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor38295be2012-10-22 23:51:00 +00002678 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded,
2679 ClientLoadCapabilities)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002680 case Failure: return Failure;
Douglas Gregor4825fd72012-10-22 22:50:17 +00002681 case OutOfDate: return OutOfDate;
2682 case VersionMismatch: return VersionMismatch;
2683 case ConfigurationMismatch: return ConfigurationMismatch;
2684 case HadErrors: return HadErrors;
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002685 case Success: break;
2686 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002687
2688 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002689
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002690 // Load the AST blocks of all of the modules that we loaded.
2691 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
2692 MEnd = Loaded.end();
2693 M != MEnd; ++M) {
2694 ModuleFile &F = **M;
2695
2696 // Read the AST block.
Douglas Gregor4825fd72012-10-22 22:50:17 +00002697 if (ReadASTBlock(F))
2698 return Failure;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002699
2700 // Once read, set the ModuleFile bit base offset and update the size in
2701 // bits of all files we've seen.
2702 F.GlobalBitOffset = TotalModulesSizeInBits;
2703 TotalModulesSizeInBits += F.SizeInBits;
2704 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2705
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002706 // Preload SLocEntries.
2707 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2708 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor8b53d142012-10-22 22:53:10 +00002709 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002710 // directly because the entry may have already been loaded in which case
Douglas Gregor8b53d142012-10-22 22:53:10 +00002711 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002712 // SourceManager.
2713 SourceMgr.getLoadedSLocEntryByID(Index);
2714 }
2715 }
2716
Douglas Gregoreee242f2011-10-27 09:33:13 +00002717 // Mark all of the identifiers in the identifier table as being out of date,
2718 // so that various accessors know to check the loaded modules when the
2719 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002720 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2721 IdEnd = PP.getIdentifierTable().end();
2722 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002723 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002724
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002725 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002726 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2727 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2728 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002729 Module *ResolvedMod = getSubmodule(GlobalID);
2730
2731 if (Unresolved.IsImport) {
2732 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002733 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002734 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002735 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002736
2737 if (ResolvedMod || Unresolved.IsWildcard)
2738 Unresolved.Mod->Exports.push_back(
2739 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002740 }
Douglas Gregor55988682011-12-05 16:33:54 +00002741 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002742
Douglas Gregor35942772011-09-09 21:34:22 +00002743 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002744
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002745 if (DeserializationListener)
2746 DeserializationListener->ReaderInitialized(this);
2747
Douglas Gregor11407b82012-10-18 21:18:25 +00002748 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2749 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2750 PrimaryModule.OriginalSourceFileID
2751 = FileID::get(PrimaryModule.SLocEntryBaseID
2752 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002753
Douglas Gregor11407b82012-10-18 21:18:25 +00002754 // If this AST file is a precompiled preamble, then set the
2755 // preamble file ID of the source manager to the file source file
2756 // from which the preamble was built.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002757 if (Type == MK_Preamble) {
Douglas Gregor11407b82012-10-18 21:18:25 +00002758 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002759 } else if (Type == MK_MainFile) {
Douglas Gregor11407b82012-10-18 21:18:25 +00002760 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002761 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002762 }
2763
Douglas Gregorcff9f262012-01-27 01:47:08 +00002764 // For any Objective-C class definitions we have already loaded, make sure
2765 // that we load any additional categories.
2766 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2767 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2768 ObjCClassesLoaded[I],
2769 PreviousGeneration);
2770 }
2771
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002772 return Success;
2773}
2774
Douglas Gregor38295be2012-10-22 23:51:00 +00002775ASTReader::ASTReadResult
2776ASTReader::ReadASTCore(StringRef FileName,
2777 ModuleKind Type,
2778 ModuleFile *ImportedBy,
2779 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
2780 unsigned ClientLoadCapabilities) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002781 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002782 bool NewModule;
2783 std::string ErrorStr;
2784 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002785 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002786
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002787 if (!M) {
2788 // We couldn't load the module.
2789 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2790 + ErrorStr;
2791 Error(Msg);
2792 return Failure;
2793 }
2794
2795 if (!NewModule) {
2796 // We've already loaded this module.
2797 return Success;
2798 }
2799
2800 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2801 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002802 if (FileName != "-") {
2803 CurrentDir = llvm::sys::path::parent_path(FileName);
2804 if (CurrentDir.empty()) CurrentDir = ".";
2805 }
2806
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002807 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002808 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002809 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002810 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002811
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002812 // Sniff for the signature.
2813 if (Stream.Read(8) != 'C' ||
2814 Stream.Read(8) != 'P' ||
2815 Stream.Read(8) != 'C' ||
2816 Stream.Read(8) != 'H') {
2817 Diag(diag::err_not_a_pch_file) << FileName;
2818 return Failure;
2819 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002820
Douglas Gregor2cf26342009-04-09 22:27:44 +00002821 while (!Stream.AtEndOfStream()) {
2822 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002823
Douglas Gregore1d918e2009-04-10 23:10:45 +00002824 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002825 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002826 return Failure;
2827 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002828
2829 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002830
Douglas Gregor7ae467f2012-10-18 18:27:37 +00002831 // We only know the control subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002832 switch (BlockID) {
2833 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002834 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002835 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002836 return Failure;
2837 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002838 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002839 case CONTROL_BLOCK_ID:
Douglas Gregor38295be2012-10-22 23:51:00 +00002840 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002841 case Success:
2842 break;
2843
Douglas Gregor4825fd72012-10-22 22:50:17 +00002844 case Failure: return Failure;
2845 case OutOfDate: return OutOfDate;
2846 case VersionMismatch: return VersionMismatch;
2847 case ConfigurationMismatch: return ConfigurationMismatch;
2848 case HadErrors: return HadErrors;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002849 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002850 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002851 case AST_BLOCK_ID:
2852 // Record that we've loaded this module.
2853 Loaded.push_back(M);
2854 return Success;
2855
Douglas Gregor2cf26342009-04-09 22:27:44 +00002856 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002857 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002858 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002859 return Failure;
2860 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002861 break;
2862 }
Mike Stump1eb44332009-09-09 15:08:12 +00002863 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002864
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002865 return Success;
2866}
2867
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002868void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002869 // If there's a listener, notify them that we "read" the translation unit.
2870 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002871 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2872 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002873
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002874 // Make sure we load the declaration update records for the translation unit,
2875 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002876 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2877 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002878
Douglas Gregor5f957282011-08-11 22:18:49 +00002879 // FIXME: Find a better way to deal with collisions between these
2880 // built-in types. Right now, we just ignore the problem.
2881
2882 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002883 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002884 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2885 if (!Context.CFConstantStringTypeDecl)
2886 Context.setCFConstantStringType(GetType(String));
2887 }
2888
2889 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2890 QualType FileType = GetType(File);
2891 if (FileType.isNull()) {
2892 Error("FILE type is NULL");
2893 return;
2894 }
2895
2896 if (!Context.FILEDecl) {
2897 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2898 Context.setFILEDecl(Typedef->getDecl());
2899 else {
2900 const TagType *Tag = FileType->getAs<TagType>();
2901 if (!Tag) {
2902 Error("Invalid FILE type in AST file");
2903 return;
2904 }
2905 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002906 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002907 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002908 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002909
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002910 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002911 QualType Jmp_bufType = GetType(Jmp_buf);
2912 if (Jmp_bufType.isNull()) {
2913 Error("jmp_buf type is NULL");
2914 return;
2915 }
2916
2917 if (!Context.jmp_bufDecl) {
2918 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2919 Context.setjmp_bufDecl(Typedef->getDecl());
2920 else {
2921 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2922 if (!Tag) {
2923 Error("Invalid jmp_buf type in AST file");
2924 return;
2925 }
2926 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002927 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002928 }
Mike Stump782fa302009-07-28 02:25:19 +00002929 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002930
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002931 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002932 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2933 if (Sigjmp_bufType.isNull()) {
2934 Error("sigjmp_buf type is NULL");
2935 return;
2936 }
2937
2938 if (!Context.sigjmp_bufDecl) {
2939 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2940 Context.setsigjmp_bufDecl(Typedef->getDecl());
2941 else {
2942 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2943 assert(Tag && "Invalid sigjmp_buf type in AST file");
2944 Context.setsigjmp_bufDecl(Tag->getDecl());
2945 }
2946 }
2947 }
2948
2949 if (unsigned ObjCIdRedef
2950 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2951 if (Context.ObjCIdRedefinitionType.isNull())
2952 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2953 }
2954
2955 if (unsigned ObjCClassRedef
2956 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2957 if (Context.ObjCClassRedefinitionType.isNull())
2958 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2959 }
2960
2961 if (unsigned ObjCSelRedef
2962 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2963 if (Context.ObjCSelRedefinitionType.isNull())
2964 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2965 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002966
2967 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2968 QualType Ucontext_tType = GetType(Ucontext_t);
2969 if (Ucontext_tType.isNull()) {
2970 Error("ucontext_t type is NULL");
2971 return;
2972 }
2973
2974 if (!Context.ucontext_tDecl) {
2975 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2976 Context.setucontext_tDecl(Typedef->getDecl());
2977 else {
2978 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2979 assert(Tag && "Invalid ucontext_t type in AST file");
2980 Context.setucontext_tDecl(Tag->getDecl());
2981 }
2982 }
2983 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002984 }
2985
Douglas Gregor35942772011-09-09 21:34:22 +00002986 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002987
2988 // If there were any CUDA special declarations, deserialize them.
2989 if (!CUDASpecialDeclRefs.empty()) {
2990 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002991 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002992 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2993 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002994
2995 // Re-export any modules that were imported by a non-module AST file.
2996 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
2997 if (Module *Imported = getSubmodule(ImportedModules[I]))
2998 makeModuleVisible(Imported, Module::AllVisible);
2999 }
3000 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003001}
3002
Douglas Gregorecc2c092011-12-01 22:20:10 +00003003void ASTReader::finalizeForWriting() {
3004 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3005 HiddenEnd = HiddenNamesMap.end();
3006 Hidden != HiddenEnd; ++Hidden) {
3007 makeNamesVisible(Hidden->second);
3008 }
3009 HiddenNamesMap.clear();
3010}
3011
Douglas Gregorb64c1932009-05-12 01:31:05 +00003012/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003013/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003014/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003015std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003016 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003017 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003018 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003019 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003020 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003021 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003022 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003023 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003024 return std::string();
3025 }
3026
3027 // Initialize the stream
3028 llvm::BitstreamReader StreamFile;
3029 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003030 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003031 (const unsigned char *)Buffer->getBufferEnd());
3032 Stream.init(StreamFile);
3033
3034 // Sniff for the signature.
3035 if (Stream.Read(8) != 'C' ||
3036 Stream.Read(8) != 'P' ||
3037 Stream.Read(8) != 'C' ||
3038 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003039 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003040 return std::string();
3041 }
3042
3043 RecordData Record;
3044 while (!Stream.AtEndOfStream()) {
3045 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003046
Douglas Gregorb64c1932009-05-12 01:31:05 +00003047 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3048 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003049
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003050 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003051 switch (BlockID) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003052 case CONTROL_BLOCK_ID:
3053 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003054 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003055 return std::string();
3056 }
3057 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003058
Douglas Gregorb64c1932009-05-12 01:31:05 +00003059 default:
3060 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003061 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003062 return std::string();
3063 }
3064 break;
3065 }
3066 continue;
3067 }
3068
3069 if (Code == llvm::bitc::END_BLOCK) {
3070 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003071 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003072 return std::string();
3073 }
3074 continue;
3075 }
3076
3077 if (Code == llvm::bitc::DEFINE_ABBREV) {
3078 Stream.ReadAbbrevRecord();
3079 continue;
3080 }
3081
3082 Record.clear();
3083 const char *BlobStart = 0;
3084 unsigned BlobLen = 0;
Douglas Gregor39c497b2012-10-18 18:36:53 +00003085 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003086 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003087 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003088
3089 return std::string();
3090}
3091
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003092namespace {
3093 class SimplePCHValidator : public ASTReaderListener {
3094 const LangOptions &ExistingLangOpts;
3095 const TargetOptions &ExistingTargetOpts;
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003096 const PreprocessorOptions &ExistingPPOpts;
Douglas Gregor87699242012-10-25 00:07:54 +00003097 FileManager &FileMgr;
3098
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003099 public:
3100 SimplePCHValidator(const LangOptions &ExistingLangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003101 const TargetOptions &ExistingTargetOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003102 const PreprocessorOptions &ExistingPPOpts,
3103 FileManager &FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003104 : ExistingLangOpts(ExistingLangOpts),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003105 ExistingTargetOpts(ExistingTargetOpts),
Douglas Gregor87699242012-10-25 00:07:54 +00003106 ExistingPPOpts(ExistingPPOpts),
3107 FileMgr(FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003108 {
3109 }
3110
3111 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3112 bool Complain) {
3113 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3114 }
3115 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3116 bool Complain) {
3117 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3118 }
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003119 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003120 bool Complain,
3121 std::string &SuggestedPredefines) {
3122 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3123 SuggestedPredefines);
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003124 }
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003125 };
3126}
3127
3128bool ASTReader::isAcceptableASTFile(StringRef Filename,
3129 FileManager &FileMgr,
3130 const LangOptions &LangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003131 const TargetOptions &TargetOpts,
3132 const PreprocessorOptions &PPOpts) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003133 // Open the AST file.
3134 std::string ErrStr;
3135 OwningPtr<llvm::MemoryBuffer> Buffer;
3136 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3137 if (!Buffer) {
3138 return false;
3139 }
3140
3141 // Initialize the stream
3142 llvm::BitstreamReader StreamFile;
3143 llvm::BitstreamCursor Stream;
3144 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3145 (const unsigned char *)Buffer->getBufferEnd());
3146 Stream.init(StreamFile);
3147
3148 // Sniff for the signature.
3149 if (Stream.Read(8) != 'C' ||
3150 Stream.Read(8) != 'P' ||
3151 Stream.Read(8) != 'C' ||
3152 Stream.Read(8) != 'H') {
3153 return false;
3154 }
3155
Douglas Gregor87699242012-10-25 00:07:54 +00003156 SimplePCHValidator Validator(LangOpts, TargetOpts, PPOpts, FileMgr);
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003157 RecordData Record;
3158 bool InControlBlock = false;
3159 while (!Stream.AtEndOfStream()) {
3160 unsigned Code = Stream.ReadCode();
3161
3162 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3163 unsigned BlockID = Stream.ReadSubBlockID();
3164
3165 // We only know the control subblock ID.
3166 switch (BlockID) {
3167 case CONTROL_BLOCK_ID:
3168 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
3169 return false;
3170 } else {
3171 InControlBlock = true;
3172 }
3173 break;
3174
3175 default:
3176 if (Stream.SkipBlock())
3177 return false;
3178 break;
3179 }
3180 continue;
3181 }
3182
3183 if (Code == llvm::bitc::END_BLOCK) {
3184 if (Stream.ReadBlockEnd()) {
3185 return false;
3186 }
3187 InControlBlock = false;
3188 continue;
3189 }
3190
3191 if (Code == llvm::bitc::DEFINE_ABBREV) {
3192 Stream.ReadAbbrevRecord();
3193 continue;
3194 }
3195
3196 Record.clear();
3197 const char *BlobStart = 0;
3198 unsigned BlobLen = 0;
3199 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3200 if (InControlBlock) {
3201 switch ((ControlRecordTypes)RecCode) {
3202 case METADATA: {
3203 if (Record[0] != VERSION_MAJOR) {
3204 return false;
3205 }
3206
3207 const std::string &CurBranch = getClangFullRepositoryVersion();
3208 StringRef ASTBranch(BlobStart, BlobLen);
3209 if (StringRef(CurBranch) != ASTBranch)
3210 return false;
3211
3212 break;
3213 }
3214 case LANGUAGE_OPTIONS:
3215 if (ParseLanguageOptions(Record, false, Validator))
3216 return false;
3217 break;
3218
3219 case TARGET_OPTIONS:
3220 if (ParseTargetOptions(Record, false, Validator))
3221 return false;
3222 break;
3223
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003224 case DIAGNOSTIC_OPTIONS:
3225 if (ParseDiagnosticOptions(Record, false, Validator))
3226 return false;
3227 break;
3228
Douglas Gregorbbf38312012-10-24 16:50:34 +00003229 case FILE_SYSTEM_OPTIONS:
3230 if (ParseFileSystemOptions(Record, false, Validator))
3231 return false;
3232 break;
3233
3234 case HEADER_SEARCH_OPTIONS:
3235 if (ParseHeaderSearchOptions(Record, false, Validator))
3236 return false;
3237 break;
3238
Douglas Gregor87699242012-10-25 00:07:54 +00003239 case PREPROCESSOR_OPTIONS: {
3240 std::string IgnoredSuggestedPredefines;
3241 if (ParsePreprocessorOptions(Record, false, Validator,
3242 IgnoredSuggestedPredefines))
Douglas Gregora71a7d82012-10-24 20:05:57 +00003243 return false;
3244 break;
Douglas Gregor87699242012-10-25 00:07:54 +00003245 }
Douglas Gregora71a7d82012-10-24 20:05:57 +00003246
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003247 default:
3248 // No other validation to perform.
3249 break;
3250 }
3251 }
3252 }
3253
3254 return true;
3255}
3256
Douglas Gregor4825fd72012-10-22 22:50:17 +00003257bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003258 // Enter the submodule block.
3259 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3260 Error("malformed submodule block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003261 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003262 }
3263
3264 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003265 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003266 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003267 RecordData Record;
3268 while (true) {
3269 unsigned Code = F.Stream.ReadCode();
3270 if (Code == llvm::bitc::END_BLOCK) {
3271 if (F.Stream.ReadBlockEnd()) {
3272 Error("error at end of submodule block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003273 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003274 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00003275 return false;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003276 }
3277
3278 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3279 // No known subblocks, always skip them.
3280 F.Stream.ReadSubBlockID();
3281 if (F.Stream.SkipBlock()) {
3282 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003283 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003284 }
3285 continue;
3286 }
3287
3288 if (Code == llvm::bitc::DEFINE_ABBREV) {
3289 F.Stream.ReadAbbrevRecord();
3290 continue;
3291 }
3292
3293 // Read a record.
3294 const char *BlobStart;
3295 unsigned BlobLen;
3296 Record.clear();
3297 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3298 default: // Default behavior: ignore.
3299 break;
3300
3301 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003302 if (First) {
3303 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003304 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003305 }
3306
Douglas Gregore209e502011-12-06 01:10:29 +00003307 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003308 Error("malformed module definition");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003309 return true;
Douglas Gregor1e123682011-12-05 22:27:44 +00003310 }
3311
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003312 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003313 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3314 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3315 bool IsFramework = Record[2];
3316 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003317 bool IsSystem = Record[4];
3318 bool InferSubmodules = Record[5];
3319 bool InferExplicitSubmodules = Record[6];
3320 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003321
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003322 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003323 if (Parent)
3324 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003325
3326 // Retrieve this (sub)module from the module map, creating it if
3327 // necessary.
3328 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3329 IsFramework,
3330 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003331 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3332 if (GlobalIndex >= SubmodulesLoaded.size() ||
3333 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003334 Error("too many submodules");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003335 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003336 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003337
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003338 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003339 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003340 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003341 CurrentModule->InferSubmodules = InferSubmodules;
3342 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3343 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003344 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003345 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003346
Douglas Gregore209e502011-12-06 01:10:29 +00003347 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003348 break;
3349 }
3350
Douglas Gregor77d029f2011-12-08 19:11:24 +00003351 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003352 if (First) {
3353 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003354 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003355 }
3356
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003357 if (!CurrentModule)
3358 break;
3359
3360 StringRef FileName(BlobStart, BlobLen);
3361 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003362 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003363 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003364 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003365 Error("mismatched umbrella headers in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003366 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003367 }
3368 }
3369 break;
3370 }
3371
3372 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003373 if (First) {
3374 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003375 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003376 }
3377
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003378 if (!CurrentModule)
3379 break;
3380
3381 // FIXME: Be more lazy about this!
3382 StringRef FileName(BlobStart, BlobLen);
3383 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3384 if (std::find(CurrentModule->Headers.begin(),
3385 CurrentModule->Headers.end(),
3386 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003387 ModMap.addHeader(CurrentModule, File, false);
3388 }
3389 break;
3390 }
3391
3392 case SUBMODULE_EXCLUDED_HEADER: {
3393 if (First) {
3394 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003395 return true;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003396 }
3397
3398 if (!CurrentModule)
3399 break;
3400
3401 // FIXME: Be more lazy about this!
3402 StringRef FileName(BlobStart, BlobLen);
3403 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3404 if (std::find(CurrentModule->Headers.begin(),
3405 CurrentModule->Headers.end(),
3406 File) == CurrentModule->Headers.end())
3407 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003408 }
3409 break;
3410 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003411
3412 case SUBMODULE_TOPHEADER: {
3413 if (First) {
3414 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003415 return true;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003416 }
3417
3418 if (!CurrentModule)
3419 break;
3420
3421 // FIXME: Be more lazy about this!
3422 StringRef FileName(BlobStart, BlobLen);
3423 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3424 CurrentModule->TopHeaders.insert(File);
3425 break;
3426 }
3427
Douglas Gregor77d029f2011-12-08 19:11:24 +00003428 case SUBMODULE_UMBRELLA_DIR: {
3429 if (First) {
3430 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003431 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003432 }
3433
3434 if (!CurrentModule)
3435 break;
3436
3437 StringRef DirName(BlobStart, BlobLen);
3438 if (const DirectoryEntry *Umbrella
3439 = PP.getFileManager().getDirectory(DirName)) {
3440 if (!CurrentModule->getUmbrellaDir())
3441 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3442 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3443 Error("mismatched umbrella directories in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003444 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003445 }
3446 }
3447 break;
3448 }
3449
Douglas Gregor26ced122011-12-01 00:59:36 +00003450 case SUBMODULE_METADATA: {
3451 if (!First) {
3452 Error("submodule metadata record not at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003453 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003454 }
3455 First = false;
3456
3457 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003458 F.LocalNumSubmodules = Record[0];
3459 unsigned LocalBaseSubmoduleID = Record[1];
3460 if (F.LocalNumSubmodules > 0) {
3461 // Introduce the global -> local mapping for submodules within this
3462 // module.
3463 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3464
3465 // Introduce the local -> global mapping for submodules within this
3466 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003467 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003468 std::make_pair(LocalBaseSubmoduleID,
3469 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3470
3471 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3472 }
3473 break;
3474 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003475
Douglas Gregor55988682011-12-05 16:33:54 +00003476 case SUBMODULE_IMPORTS: {
3477 if (First) {
3478 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003479 return true;
Douglas Gregor55988682011-12-05 16:33:54 +00003480 }
3481
3482 if (!CurrentModule)
3483 break;
3484
3485 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3486 UnresolvedModuleImportExport Unresolved;
3487 Unresolved.File = &F;
3488 Unresolved.Mod = CurrentModule;
3489 Unresolved.ID = Record[Idx];
3490 Unresolved.IsImport = true;
3491 Unresolved.IsWildcard = false;
3492 UnresolvedModuleImportExports.push_back(Unresolved);
3493 }
3494 break;
3495 }
3496
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003497 case SUBMODULE_EXPORTS: {
3498 if (First) {
3499 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003500 return true;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003501 }
3502
3503 if (!CurrentModule)
3504 break;
3505
3506 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003507 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003508 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003509 Unresolved.Mod = CurrentModule;
3510 Unresolved.ID = Record[Idx];
3511 Unresolved.IsImport = false;
3512 Unresolved.IsWildcard = Record[Idx + 1];
3513 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003514 }
3515
3516 // Once we've loaded the set of exports, there's no reason to keep
3517 // the parsed, unresolved exports around.
3518 CurrentModule->UnresolvedExports.clear();
3519 break;
3520 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003521 case SUBMODULE_REQUIRES: {
3522 if (First) {
3523 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003524 return true;
Douglas Gregor51f564f2011-12-31 04:05:44 +00003525 }
3526
3527 if (!CurrentModule)
3528 break;
3529
3530 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003531 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003532 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003533 break;
3534 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003535 }
3536 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003537}
3538
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003539/// \brief Parse the record that corresponds to a LangOptions data
3540/// structure.
3541///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003542/// This routine parses the language options from the AST file and then gives
3543/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003544///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003545/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003546bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3547 bool Complain,
3548 ASTReaderListener &Listener) {
3549 LangOptions LangOpts;
3550 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003551#define LANGOPT(Name, Bits, Default, Description) \
3552 LangOpts.Name = Record[Idx++];
3553#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3554 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3555#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003556
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003557 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3558 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3559 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3560
3561 unsigned Length = Record[Idx++];
3562 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3563 Record.begin() + Idx + Length);
3564 return Listener.ReadLanguageOptions(LangOpts, Complain);
3565}
3566
3567bool ASTReader::ParseTargetOptions(const RecordData &Record,
3568 bool Complain,
3569 ASTReaderListener &Listener) {
3570 unsigned Idx = 0;
3571 TargetOptions TargetOpts;
3572 TargetOpts.Triple = ReadString(Record, Idx);
3573 TargetOpts.CPU = ReadString(Record, Idx);
3574 TargetOpts.ABI = ReadString(Record, Idx);
3575 TargetOpts.CXXABI = ReadString(Record, Idx);
3576 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3577 for (unsigned N = Record[Idx++]; N; --N) {
3578 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3579 }
3580 for (unsigned N = Record[Idx++]; N; --N) {
3581 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003582 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003583
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003584 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003585}
3586
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003587bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3588 ASTReaderListener &Listener) {
3589 DiagnosticOptions DiagOpts;
3590 unsigned Idx = 0;
3591#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3592#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3593 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3594#include "clang/Basic/DiagnosticOptions.def"
3595
3596 for (unsigned N = Record[Idx++]; N; --N) {
3597 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3598 }
3599
3600 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3601}
3602
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00003603bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3604 ASTReaderListener &Listener) {
3605 FileSystemOptions FSOpts;
3606 unsigned Idx = 0;
3607 FSOpts.WorkingDir = ReadString(Record, Idx);
3608 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3609}
3610
Douglas Gregorbbf38312012-10-24 16:50:34 +00003611bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3612 bool Complain,
3613 ASTReaderListener &Listener) {
3614 HeaderSearchOptions HSOpts;
3615 unsigned Idx = 0;
3616 HSOpts.Sysroot = ReadString(Record, Idx);
3617
3618 // Include entries.
3619 for (unsigned N = Record[Idx++]; N; --N) {
3620 std::string Path = ReadString(Record, Idx);
3621 frontend::IncludeDirGroup Group
3622 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3623 bool IsUserSupplied = Record[Idx++];
3624 bool IsFramework = Record[Idx++];
3625 bool IgnoreSysRoot = Record[Idx++];
3626 bool IsInternal = Record[Idx++];
3627 bool ImplicitExternC = Record[Idx++];
3628 HSOpts.UserEntries.push_back(
3629 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3630 IgnoreSysRoot, IsInternal, ImplicitExternC));
3631 }
3632
3633 // System header prefixes.
3634 for (unsigned N = Record[Idx++]; N; --N) {
3635 std::string Prefix = ReadString(Record, Idx);
3636 bool IsSystemHeader = Record[Idx++];
3637 HSOpts.SystemHeaderPrefixes.push_back(
3638 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3639 }
3640
3641 HSOpts.ResourceDir = ReadString(Record, Idx);
3642 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3643 HSOpts.DisableModuleHash = Record[Idx++];
3644 HSOpts.UseBuiltinIncludes = Record[Idx++];
3645 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3646 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3647 HSOpts.UseLibcxx = Record[Idx++];
3648
3649 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3650}
3651
Douglas Gregora71a7d82012-10-24 20:05:57 +00003652bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3653 bool Complain,
Douglas Gregor87699242012-10-25 00:07:54 +00003654 ASTReaderListener &Listener,
3655 std::string &SuggestedPredefines) {
Douglas Gregora71a7d82012-10-24 20:05:57 +00003656 PreprocessorOptions PPOpts;
3657 unsigned Idx = 0;
3658
3659 // Macro definitions/undefs
3660 for (unsigned N = Record[Idx++]; N; --N) {
3661 std::string Macro = ReadString(Record, Idx);
3662 bool IsUndef = Record[Idx++];
3663 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3664 }
3665
3666 // Includes
3667 for (unsigned N = Record[Idx++]; N; --N) {
3668 PPOpts.Includes.push_back(ReadString(Record, Idx));
3669 }
3670
3671 // Macro Includes
3672 for (unsigned N = Record[Idx++]; N; --N) {
3673 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3674 }
3675
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003676 PPOpts.UsePredefines = Record[Idx++];
Douglas Gregora71a7d82012-10-24 20:05:57 +00003677 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3678 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3679 PPOpts.ObjCXXARCStandardLibrary =
3680 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
Douglas Gregor87699242012-10-25 00:07:54 +00003681 SuggestedPredefines.clear();
3682 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3683 SuggestedPredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00003684}
3685
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003686std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003687ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003688 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003689 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003690 assert(I != GlobalPreprocessedEntityMap.end() &&
3691 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003692 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003693 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3694 return std::make_pair(M, LocalIndex);
3695}
3696
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003697std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3698ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3699 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3700 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3701 Mod.NumPreprocessedEntities);
3702
3703 return std::make_pair(PreprocessingRecord::iterator(),
3704 PreprocessingRecord::iterator());
3705}
3706
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003707std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3708ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3709 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3710 ModuleDeclIterator(this, &Mod,
3711 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3712}
3713
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003714PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3715 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003716 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3717 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003718 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003719 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003720
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003721 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003722 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003723
3724 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3725 switch (Code) {
3726 case llvm::bitc::END_BLOCK:
3727 return 0;
3728
3729 case llvm::bitc::ENTER_SUBBLOCK:
3730 Error("unexpected subblock record in preprocessor detail block");
3731 return 0;
3732
3733 case llvm::bitc::DEFINE_ABBREV:
3734 Error("unexpected abbrevation record in preprocessor detail block");
3735 return 0;
3736
3737 default:
3738 break;
3739 }
3740
3741 if (!PP.getPreprocessingRecord()) {
3742 Error("no preprocessing record");
3743 return 0;
3744 }
3745
3746 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003747 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3748 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003749 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3750 const char *BlobStart = 0;
3751 unsigned BlobLen = 0;
3752 RecordData Record;
3753 PreprocessorDetailRecordTypes RecType =
3754 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3755 Code, Record, BlobStart, BlobLen);
3756 switch (RecType) {
3757 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003758 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003759 IdentifierInfo *Name = 0;
3760 MacroDefinition *Def = 0;
3761 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003762 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003763 else {
3764 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003765 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003766 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3767 }
3768
3769 MacroExpansion *ME;
3770 if (isBuiltin)
3771 ME = new (PPRec) MacroExpansion(Name, Range);
3772 else
3773 ME = new (PPRec) MacroExpansion(Def, Range);
3774
3775 return ME;
3776 }
3777
3778 case PPD_MACRO_DEFINITION: {
3779 // Decode the identifier info and then check again; if the macro is
3780 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003781 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003782 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003783 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003784
3785 if (DeserializationListener)
3786 DeserializationListener->MacroDefinitionRead(PPID, MD);
3787
3788 return MD;
3789 }
3790
3791 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003792 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003793 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3794 const FileEntry *File = 0;
3795 if (!FullFileName.empty())
3796 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003797
3798 // FIXME: Stable encoding
3799 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003800 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003801 InclusionDirective *ID
3802 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003803 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003804 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003805 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003806 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003807 return ID;
3808 }
3809 }
David Blaikie7530c032012-01-17 06:56:22 +00003810
3811 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003812}
3813
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003814/// \brief \arg SLocMapI points at a chunk of a module that contains no
3815/// preprocessed entities or the entities it contains are not the ones we are
3816/// looking for. Find the next module that contains entities and return the ID
3817/// of the first entry.
3818PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3819 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3820 ++SLocMapI;
3821 for (GlobalSLocOffsetMapType::const_iterator
3822 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003823 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003824 if (M.NumPreprocessedEntities)
Argyrios Kyrtzidisce254922012-11-02 02:31:22 +00003825 return M.BasePreprocessedEntityID;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003826 }
3827
3828 return getTotalNumPreprocessedEntities();
3829}
3830
3831namespace {
3832
3833template <unsigned PPEntityOffset::*PPLoc>
3834struct PPEntityComp {
3835 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003836 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003837
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003838 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003839
Benjamin Kramer88df1252011-09-21 06:42:26 +00003840 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3841 SourceLocation LHS = getLoc(L);
3842 SourceLocation RHS = getLoc(R);
3843 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3844 }
3845
3846 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003847 SourceLocation LHS = getLoc(L);
3848 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3849 }
3850
Benjamin Kramer88df1252011-09-21 06:42:26 +00003851 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003852 SourceLocation RHS = getLoc(R);
3853 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3854 }
3855
3856 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3857 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3858 }
3859};
3860
3861}
3862
3863/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3864PreprocessedEntityID
3865ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3866 if (SourceMgr.isLocalSourceLocation(BLoc))
3867 return getTotalNumPreprocessedEntities();
3868
3869 GlobalSLocOffsetMapType::const_iterator
3870 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3871 BLoc.getOffset());
3872 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3873 "Corrupted global sloc offset map");
3874
3875 if (SLocMapI->second->NumPreprocessedEntities == 0)
3876 return findNextPreprocessedEntity(SLocMapI);
3877
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003878 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003879 typedef const PPEntityOffset *pp_iterator;
3880 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3881 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003882
3883 size_t Count = M.NumPreprocessedEntities;
3884 size_t Half;
3885 pp_iterator First = pp_begin;
3886 pp_iterator PPI;
3887
3888 // Do a binary search manually instead of using std::lower_bound because
3889 // The end locations of entities may be unordered (when a macro expansion
3890 // is inside another macro argument), but for this case it is not important
3891 // whether we get the first macro expansion or its containing macro.
3892 while (Count > 0) {
3893 Half = Count/2;
3894 PPI = First;
3895 std::advance(PPI, Half);
3896 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3897 BLoc)){
3898 First = PPI;
3899 ++First;
3900 Count = Count - Half - 1;
3901 } else
3902 Count = Half;
3903 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003904
3905 if (PPI == pp_end)
3906 return findNextPreprocessedEntity(SLocMapI);
3907
Argyrios Kyrtzidisce254922012-11-02 02:31:22 +00003908 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003909}
3910
3911/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3912PreprocessedEntityID
3913ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3914 if (SourceMgr.isLocalSourceLocation(ELoc))
3915 return getTotalNumPreprocessedEntities();
3916
3917 GlobalSLocOffsetMapType::const_iterator
3918 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3919 ELoc.getOffset());
3920 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3921 "Corrupted global sloc offset map");
3922
3923 if (SLocMapI->second->NumPreprocessedEntities == 0)
3924 return findNextPreprocessedEntity(SLocMapI);
3925
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003926 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003927 typedef const PPEntityOffset *pp_iterator;
3928 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3929 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3930 pp_iterator PPI =
3931 std::upper_bound(pp_begin, pp_end, ELoc,
3932 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3933
3934 if (PPI == pp_end)
3935 return findNextPreprocessedEntity(SLocMapI);
3936
Argyrios Kyrtzidisce254922012-11-02 02:31:22 +00003937 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003938}
3939
3940/// \brief Returns a pair of [Begin, End) indices of preallocated
3941/// preprocessed entities that \arg Range encompasses.
3942std::pair<unsigned, unsigned>
3943 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3944 if (Range.isInvalid())
3945 return std::make_pair(0,0);
3946 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3947
3948 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3949 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3950 return std::make_pair(BeginID, EndID);
3951}
3952
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003953/// \brief Optionally returns true or false if the preallocated preprocessed
3954/// entity with index \arg Index came from file \arg FID.
3955llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3956 FileID FID) {
3957 if (FID.isInvalid())
3958 return false;
3959
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003960 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3961 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003962 unsigned LocalIndex = PPInfo.second;
3963 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3964
3965 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3966 if (Loc.isInvalid())
3967 return false;
3968
3969 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3970 return true;
3971 else
3972 return false;
3973}
3974
Douglas Gregord10a3812011-08-25 18:14:34 +00003975namespace {
3976 /// \brief Visitor used to search for information about a header file.
3977 class HeaderFileInfoVisitor {
3978 ASTReader &Reader;
3979 const FileEntry *FE;
3980
3981 llvm::Optional<HeaderFileInfo> HFI;
3982
3983 public:
3984 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3985 : Reader(Reader), FE(FE) { }
3986
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003987 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003988 HeaderFileInfoVisitor *This
3989 = static_cast<HeaderFileInfoVisitor *>(UserData);
3990
3991 HeaderFileInfoTrait Trait(This->Reader, M,
3992 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3993 M.HeaderFileFrameworkStrings,
3994 This->FE->getName());
3995
3996 HeaderFileInfoLookupTable *Table
3997 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3998 if (!Table)
3999 return false;
4000
4001 // Look in the on-disk hash table for an entry for this file name.
4002 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4003 &Trait);
4004 if (Pos == Table->end())
4005 return false;
4006
4007 This->HFI = *Pos;
4008 return true;
4009 }
4010
4011 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4012 };
4013}
4014
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004015HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004016 HeaderFileInfoVisitor Visitor(*this, FE);
4017 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4018 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004019 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00004020 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4021 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004022 }
4023
4024 return HeaderFileInfo();
4025}
4026
David Blaikied6471f72011-09-25 23:23:43 +00004027void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004028 // FIXME: Make it work properly with modules.
4029 llvm::SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004030 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004031 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004032 unsigned Idx = 0;
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004033 DiagStates.clear();
4034 assert(!Diag.DiagStates.empty());
4035 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004036 while (Idx < F.PragmaDiagMappings.size()) {
4037 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004038 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4039 if (DiagStateID != 0) {
4040 Diag.DiagStatePoints.push_back(
4041 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4042 FullSourceLoc(Loc, SourceMgr)));
4043 continue;
4044 }
4045
4046 assert(DiagStateID == 0);
4047 // A new DiagState was created here.
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004048 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004049 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4050 DiagStates.push_back(NewState);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004051 Diag.DiagStatePoints.push_back(
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004052 DiagnosticsEngine::DiagStatePoint(NewState,
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004053 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004054 while (1) {
4055 assert(Idx < F.PragmaDiagMappings.size() &&
4056 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4057 if (Idx >= F.PragmaDiagMappings.size()) {
4058 break; // Something is messed up but at least avoid infinite loop in
4059 // release build.
4060 }
4061 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4062 if (DiagID == (unsigned)-1) {
4063 break; // no more diag/map pairs for this location.
4064 }
4065 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004066 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4067 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004068 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004069 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00004070 }
4071}
4072
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004073/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004074ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00004075 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00004076 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004077 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00004078 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004079}
4080
4081/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00004082///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004083/// The index is the type ID, shifted and minus the number of predefs. This
4084/// routine actually reads the record corresponding to the type at the given
4085/// location. It is a helper routine for GetType, which deals with reading type
4086/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00004087QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004088 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004089 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00004090
Douglas Gregor0b748912009-04-14 21:18:50 +00004091 // Keep track of where we are in the stream, then jump back there
4092 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004093 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00004094
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004095 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00004096
Douglas Gregord89275b2009-07-06 18:54:52 +00004097 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004098 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00004099
Douglas Gregor393f2492011-07-22 00:38:23 +00004100 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00004101 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004102 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004103 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004104 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4105 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004106 if (Record.size() != 2) {
4107 Error("Incorrect encoding of extended qualifier type");
4108 return QualType();
4109 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004110 QualType Base = readType(*Loc.F, Record, Idx);
4111 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00004112 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00004113 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004114
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004115 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004116 if (Record.size() != 1) {
4117 Error("Incorrect encoding of complex type");
4118 return QualType();
4119 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004120 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004121 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004122 }
4123
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004124 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004125 if (Record.size() != 1) {
4126 Error("Incorrect encoding of pointer type");
4127 return QualType();
4128 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004129 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004130 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004131 }
4132
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004133 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004134 if (Record.size() != 1) {
4135 Error("Incorrect encoding of block pointer type");
4136 return QualType();
4137 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004138 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004139 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004140 }
4141
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004142 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00004143 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004144 Error("Incorrect encoding of lvalue reference type");
4145 return QualType();
4146 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004147 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004148 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004149 }
4150
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004151 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004152 if (Record.size() != 1) {
4153 Error("Incorrect encoding of rvalue reference type");
4154 return QualType();
4155 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004156 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004157 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004158 }
4159
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004160 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00004161 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004162 Error("Incorrect encoding of member pointer type");
4163 return QualType();
4164 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004165 QualType PointeeType = readType(*Loc.F, Record, Idx);
4166 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004167 if (PointeeType.isNull() || ClassType.isNull())
4168 return QualType();
4169
Douglas Gregor35942772011-09-09 21:34:22 +00004170 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004171 }
4172
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004173 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004174 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004175 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4176 unsigned IndexTypeQuals = Record[2];
4177 unsigned Idx = 3;
4178 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004179 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004180 ASM, IndexTypeQuals);
4181 }
4182
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004183 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004184 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004185 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4186 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004187 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004188 }
4189
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004190 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004191 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00004192 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4193 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004194 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4195 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004196 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004197 ASM, IndexTypeQuals,
4198 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004199 }
4200
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004201 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004202 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004203 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004204 return QualType();
4205 }
4206
Douglas Gregor393f2492011-07-22 00:38:23 +00004207 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004208 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004209 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004210 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004211 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004212 }
4213
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004214 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004215 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004216 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004217 return QualType();
4218 }
4219
Douglas Gregor393f2492011-07-22 00:38:23 +00004220 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004221 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004222 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004223 }
4224
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004225 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004226 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004227 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004228 return QualType();
4229 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004230 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004231 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4232 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004233 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004234 }
4235
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004236 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004237 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004238
4239 FunctionProtoType::ExtProtoInfo EPI;
4240 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004241 /*hasregparm*/ Record[2],
4242 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004243 static_cast<CallingConv>(Record[4]),
4244 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004245
John McCallf85e1932011-06-15 23:02:42 +00004246 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004247 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004248 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004249 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004250 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004251
4252 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004253 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004254 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004255 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004256 ExceptionSpecificationType EST =
4257 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4258 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004259 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004260 if (EST == EST_Dynamic) {
4261 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004262 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004263 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004264 EPI.Exceptions = Exceptions.data();
4265 } else if (EST == EST_ComputedNoexcept) {
4266 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004267 } else if (EST == EST_Uninstantiated) {
4268 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4269 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004270 } else if (EST == EST_Unevaluated) {
4271 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004272 }
Douglas Gregor35942772011-09-09 21:34:22 +00004273 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004274 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004275 }
4276
Douglas Gregor409448c2011-07-21 22:35:25 +00004277 case TYPE_UNRESOLVED_USING: {
4278 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004279 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004280 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4281 }
4282
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004283 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004284 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004285 Error("incorrect encoding of typedef type");
4286 return QualType();
4287 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004288 unsigned Idx = 0;
4289 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004290 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004291 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004292 Canonical = Context.getCanonicalType(Canonical);
4293 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004294 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004295
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004296 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004297 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004298
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004299 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004300 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004301 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004302 return QualType();
4303 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004304 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004305 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004306 }
Mike Stump1eb44332009-09-09 15:08:12 +00004307
Douglas Gregorf8af9822012-02-12 18:42:33 +00004308 case TYPE_DECLTYPE: {
4309 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4310 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4311 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004312
Sean Huntca63c202011-05-24 22:41:36 +00004313 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004314 QualType BaseType = readType(*Loc.F, Record, Idx);
4315 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004316 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004317 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004318 }
4319
Richard Smith34b41d92011-02-20 03:19:35 +00004320 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004321 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004322
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004323 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004324 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004325 Error("incorrect encoding of record type");
4326 return QualType();
4327 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004328 unsigned Idx = 0;
4329 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004330 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4331 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4332 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004333 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004334 return T;
4335 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004336
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004337 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004338 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004339 Error("incorrect encoding of enum type");
4340 return QualType();
4341 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004342 unsigned Idx = 0;
4343 bool IsDependent = Record[Idx++];
4344 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004345 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004346 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004347 return T;
4348 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004349
John McCall9d156a72011-01-06 01:58:22 +00004350 case TYPE_ATTRIBUTED: {
4351 if (Record.size() != 3) {
4352 Error("incorrect encoding of attributed type");
4353 return QualType();
4354 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004355 QualType modifiedType = readType(*Loc.F, Record, Idx);
4356 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004357 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004358 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004359 }
4360
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004361 case TYPE_PAREN: {
4362 if (Record.size() != 1) {
4363 Error("incorrect encoding of paren type");
4364 return QualType();
4365 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004366 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004367 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004368 }
4369
Douglas Gregor7536dd52010-12-20 02:24:11 +00004370 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004371 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004372 Error("incorrect encoding of pack expansion type");
4373 return QualType();
4374 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004375 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004376 if (Pattern.isNull())
4377 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004378 llvm::Optional<unsigned> NumExpansions;
4379 if (Record[1])
4380 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004381 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004382 }
4383
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004384 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004385 unsigned Idx = 0;
4386 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004387 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004388 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004389 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004390 }
4391
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004392 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004393 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004394 ObjCInterfaceDecl *ItfD
4395 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004396 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004397 }
4398
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004399 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004400 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004401 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004402 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004403 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004404 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004405 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004406 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004407 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004408
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004409 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004410 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004411 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004412 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004413 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004414
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004415 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004416 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004417 QualType Parm = readType(*Loc.F, Record, Idx);
4418 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004419 return
Douglas Gregor35942772011-09-09 21:34:22 +00004420 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004421 Replacement);
4422 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004423
Douglas Gregorc3069d62011-01-14 02:55:32 +00004424 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4425 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004426 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004427 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004428 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004429 cast<TemplateTypeParmType>(Parm),
4430 ArgPack);
4431 }
4432
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004433 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004434 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004435 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004436 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004437 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004438 return
Douglas Gregor35942772011-09-09 21:34:22 +00004439 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004440 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004441
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004442 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004443 unsigned Idx = 0;
4444 unsigned Depth = Record[Idx++];
4445 unsigned Index = Record[Idx++];
4446 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004447 TemplateTypeParmDecl *D
4448 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004449 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004450 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004451
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004452 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004453 unsigned Idx = 0;
4454 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004455 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004456 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004457 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004458 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004459 Canon = Context.getCanonicalType(Canon);
4460 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004461 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004462
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004463 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004464 unsigned Idx = 0;
4465 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004466 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004467 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004468 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004469 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004470 Args.reserve(NumArgs);
4471 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004472 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004473 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004474 Args.size(), Args.data());
4475 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004476
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004477 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004478 unsigned Idx = 0;
4479
4480 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004481 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004482 ArrayType::ArraySizeModifier ASM
4483 = (ArrayType::ArraySizeModifier)Record[Idx++];
4484 unsigned IndexTypeQuals = Record[Idx++];
4485
4486 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004487 Expr *NumElts = ReadExpr(*Loc.F);
4488 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004489
Douglas Gregor35942772011-09-09 21:34:22 +00004490 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004491 IndexTypeQuals, Brackets);
4492 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004493
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004494 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004495 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004496 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004497 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004498 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004499 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004500 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004501 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004502 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004503 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004504 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004505 else
Douglas Gregor35942772011-09-09 21:34:22 +00004506 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004507 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004508 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004509 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004510 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004511
4512 case TYPE_ATOMIC: {
4513 if (Record.size() != 1) {
4514 Error("Incorrect encoding of atomic type");
4515 return QualType();
4516 }
4517 QualType ValueType = readType(*Loc.F, Record, Idx);
4518 return Context.getAtomicType(ValueType);
4519 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004520 }
David Blaikie7530c032012-01-17 06:56:22 +00004521 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004522}
4523
Sebastian Redlc3632732010-10-05 15:59:54 +00004524class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004525 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004526 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004527 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004528 unsigned &Idx;
4529
Sebastian Redlc3632732010-10-05 15:59:54 +00004530 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4531 unsigned &I) {
4532 return Reader.ReadSourceLocation(F, R, I);
4533 }
4534
Douglas Gregor409448c2011-07-21 22:35:25 +00004535 template<typename T>
4536 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4537 return Reader.ReadDeclAs<T>(F, Record, Idx);
4538 }
4539
John McCalla1ee0c52009-10-16 21:56:05 +00004540public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004541 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004542 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004543 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004544 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004545
John McCall51bd8032009-10-18 01:05:36 +00004546 // We want compile-time assurance that we've enumerated all of
4547 // these, so unfortunately we have to declare them first, then
4548 // define them out-of-line.
4549#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004550#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004551 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004552#include "clang/AST/TypeLocNodes.def"
4553
John McCall51bd8032009-10-18 01:05:36 +00004554 void VisitFunctionTypeLoc(FunctionTypeLoc);
4555 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004556};
4557
John McCall51bd8032009-10-18 01:05:36 +00004558void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004559 // nothing to do
4560}
John McCall51bd8032009-10-18 01:05:36 +00004561void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004562 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004563 if (TL.needsExtraLocalData()) {
4564 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4565 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4566 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4567 TL.setModeAttr(Record[Idx++]);
4568 }
John McCalla1ee0c52009-10-16 21:56:05 +00004569}
John McCall51bd8032009-10-18 01:05:36 +00004570void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004571 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004572}
John McCall51bd8032009-10-18 01:05:36 +00004573void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004574 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004575}
John McCall51bd8032009-10-18 01:05:36 +00004576void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004577 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004578}
John McCall51bd8032009-10-18 01:05:36 +00004579void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004580 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004581}
John McCall51bd8032009-10-18 01:05:36 +00004582void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004583 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004584}
John McCall51bd8032009-10-18 01:05:36 +00004585void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004586 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004587 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004588}
John McCall51bd8032009-10-18 01:05:36 +00004589void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004590 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4591 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004592 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004593 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004594 else
John McCall51bd8032009-10-18 01:05:36 +00004595 TL.setSizeExpr(0);
4596}
4597void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4598 VisitArrayTypeLoc(TL);
4599}
4600void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4601 VisitArrayTypeLoc(TL);
4602}
4603void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4604 VisitArrayTypeLoc(TL);
4605}
4606void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4607 DependentSizedArrayTypeLoc TL) {
4608 VisitArrayTypeLoc(TL);
4609}
4610void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4611 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004612 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004613}
4614void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004615 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004616}
4617void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004618 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004619}
4620void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004621 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004622 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4623 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004624 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004625 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004626 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004627 }
4628}
4629void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4630 VisitFunctionTypeLoc(TL);
4631}
4632void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4633 VisitFunctionTypeLoc(TL);
4634}
John McCalled976492009-12-04 22:46:56 +00004635void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004636 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004637}
John McCall51bd8032009-10-18 01:05:36 +00004638void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004639 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004640}
4641void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004642 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4643 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4644 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004645}
4646void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004647 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4648 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4649 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4650 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004651}
4652void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004653 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004654}
Sean Huntca63c202011-05-24 22:41:36 +00004655void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4656 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4657 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4658 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4659 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4660}
Richard Smith34b41d92011-02-20 03:19:35 +00004661void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4662 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4663}
John McCall51bd8032009-10-18 01:05:36 +00004664void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004665 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004666}
4667void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004668 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004669}
John McCall9d156a72011-01-06 01:58:22 +00004670void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4671 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4672 if (TL.hasAttrOperand()) {
4673 SourceRange range;
4674 range.setBegin(ReadSourceLocation(Record, Idx));
4675 range.setEnd(ReadSourceLocation(Record, Idx));
4676 TL.setAttrOperandParensRange(range);
4677 }
4678 if (TL.hasAttrExprOperand()) {
4679 if (Record[Idx++])
4680 TL.setAttrExprOperand(Reader.ReadExpr(F));
4681 else
4682 TL.setAttrExprOperand(0);
4683 } else if (TL.hasAttrEnumOperand())
4684 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4685}
John McCall51bd8032009-10-18 01:05:36 +00004686void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004687 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004688}
John McCall49a832b2009-10-18 09:09:24 +00004689void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4690 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004691 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004692}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004693void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4694 SubstTemplateTypeParmPackTypeLoc TL) {
4695 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4696}
John McCall51bd8032009-10-18 01:05:36 +00004697void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4698 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004699 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004700 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4701 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4702 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004703 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4704 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004705 Reader.GetTemplateArgumentLocInfo(F,
4706 TL.getTypePtr()->getArg(i).getKind(),
4707 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004708}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004709void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4710 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4711 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4712}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004713void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004714 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004715 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004716}
John McCall3cb0ebd2010-03-10 03:28:59 +00004717void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004718 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004719}
Douglas Gregor4714c122010-03-31 17:34:00 +00004720void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004721 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004722 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004723 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004724}
John McCall33500952010-06-11 00:33:02 +00004725void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4726 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004727 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004728 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004729 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004730 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004731 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4732 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004733 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4734 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004735 Reader.GetTemplateArgumentLocInfo(F,
4736 TL.getTypePtr()->getArg(I).getKind(),
4737 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004738}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004739void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4740 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4741}
John McCall51bd8032009-10-18 01:05:36 +00004742void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004743 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004744}
4745void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4746 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004747 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4748 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004749 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004750 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004751}
John McCall54e14c42009-10-22 22:37:11 +00004752void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004753 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004754}
Eli Friedmanb001de72011-10-06 23:00:33 +00004755void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4756 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4757 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4758 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4759}
John McCalla1ee0c52009-10-16 21:56:05 +00004760
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004761TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004762 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004763 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004764 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004765 if (InfoTy.isNull())
4766 return 0;
4767
Douglas Gregor35942772011-09-09 21:34:22 +00004768 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004769 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004770 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004771 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004772 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004773}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004774
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004775QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004776 unsigned FastQuals = ID & Qualifiers::FastMask;
4777 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004778
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004779 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004780 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004781 switch ((PredefinedTypeIDs)Index) {
4782 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004783 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4784 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004785
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004786 case PREDEF_TYPE_CHAR_U_ID:
4787 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004788 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004789 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004790 break;
4791
Douglas Gregor35942772011-09-09 21:34:22 +00004792 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4793 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4794 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4795 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4796 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4797 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4798 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4799 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4800 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4801 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4802 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4803 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4804 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004805 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004806 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4807 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4808 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4809 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4810 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004811 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004812 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4813 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4814 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4815 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4816 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4817 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4818 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4819 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4820 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004821
4822 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004823 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004824 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004825
4826 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4827 T = Context.ARCUnbridgedCastTy;
4828 break;
4829
Meador Ingefb40e3f2012-07-01 15:57:25 +00004830 case PREDEF_TYPE_VA_LIST_TAG:
4831 T = Context.getVaListTagType();
4832 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004833
4834 case PREDEF_TYPE_BUILTIN_FN:
4835 T = Context.BuiltinFnTy;
4836 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004837 }
4838
4839 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004840 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004841 }
4842
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004843 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004844 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004845 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004846 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004847 if (TypesLoaded[Index].isNull())
4848 return QualType();
4849
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004850 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004851 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004852 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004853 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004854 }
Mike Stump1eb44332009-09-09 15:08:12 +00004855
John McCall0953e762009-09-24 19:53:00 +00004856 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004857}
4858
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004859QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004860 return GetType(getGlobalTypeID(F, LocalID));
4861}
4862
4863serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004864ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004865 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4866 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4867
4868 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4869 return LocalID;
4870
4871 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4872 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4873 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4874
4875 unsigned GlobalIndex = LocalIndex + I->second;
4876 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4877}
4878
John McCall833ca992009-10-29 08:12:44 +00004879TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004880ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004881 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004882 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004883 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004884 switch (Kind) {
4885 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004886 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004887 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004888 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004889 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004890 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4891 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004892 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004893 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004894 SourceLocation());
4895 }
4896 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004897 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4898 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004899 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004900 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004901 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004902 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004903 }
John McCall833ca992009-10-29 08:12:44 +00004904 case TemplateArgument::Null:
4905 case TemplateArgument::Integral:
4906 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004907 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004908 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004909 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004910 return TemplateArgumentLocInfo();
4911 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004912 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004913}
4914
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004915TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004916ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004917 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004918 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004919
4920 if (Arg.getKind() == TemplateArgument::Expression) {
4921 if (Record[Index++]) // bool InfoHasSameExpr.
4922 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4923 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004924 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004925 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004926}
4927
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004928Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004929 return GetDecl(ID);
4930}
4931
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004932uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004933 unsigned &Idx){
4934 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004935 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004936
Douglas Gregore92b8a12011-08-04 00:01:48 +00004937 unsigned LocalID = Record[Idx++];
4938 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004939}
4940
4941CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004942 RecordLocation Loc = getLocalBitOffset(Offset);
4943 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004944 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004945 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004946 ReadingKindTracker ReadingKind(Read_Decl, *this);
4947 RecordData Record;
4948 unsigned Code = Cursor.ReadCode();
4949 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4950 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4951 Error("Malformed AST file: missing C++ base specifiers");
4952 return 0;
4953 }
4954
4955 unsigned Idx = 0;
4956 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004957 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004958 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4959 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004960 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004961 return Bases;
4962}
4963
Douglas Gregor409448c2011-07-21 22:35:25 +00004964serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004965ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004966 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004967 return LocalID;
4968
4969 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004970 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004971 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4972
4973 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004974}
4975
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004976bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004977 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004978 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4979 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4980 return &M == I->second;
4981}
4982
Douglas Gregorcff9f262012-01-27 01:47:08 +00004983ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4984 if (!D->isFromASTFile())
4985 return 0;
4986 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4987 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4988 return I->second;
4989}
4990
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004991SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4992 if (ID < NUM_PREDEF_DECL_IDS)
4993 return SourceLocation();
4994
4995 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4996
4997 if (Index > DeclsLoaded.size()) {
4998 Error("declaration ID out-of-range for AST file");
4999 return SourceLocation();
5000 }
5001
5002 if (Decl *D = DeclsLoaded[Index])
5003 return D->getLocation();
5004
5005 unsigned RawLocation = 0;
5006 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5007 return ReadSourceLocation(*Rec.F, RawLocation);
5008}
5009
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005010Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005011 if (ID < NUM_PREDEF_DECL_IDS) {
5012 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005013 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005014 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005015
5016 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005017 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00005018
5019 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005020 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00005021
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005022 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005023 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005024
Douglas Gregor79d67262011-08-12 05:59:41 +00005025 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005026 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005027
Douglas Gregora6ea10e2012-01-17 18:09:05 +00005028 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5029 return Context.getObjCProtocolDecl();
5030
Douglas Gregor772eeae2011-08-12 06:49:56 +00005031 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005032 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005033
5034 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005035 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00005036
5037 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005038 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00005039
5040 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5041 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005042 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005043 }
5044
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005045 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5046
Richard Smith2fbf3732011-12-20 04:39:57 +00005047 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005048 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005049 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005050 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005051 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005052
Douglas Gregorfd002a72011-12-16 22:37:11 +00005053 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00005054 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00005055 if (DeserializationListener)
5056 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5057 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005058
5059 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005060}
5061
Douglas Gregora1be2782011-12-17 23:38:30 +00005062DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5063 DeclID GlobalID) {
5064 if (GlobalID < NUM_PREDEF_DECL_IDS)
5065 return GlobalID;
5066
5067 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5068 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5069 ModuleFile *Owner = I->second;
5070
5071 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5072 = M.GlobalToLocalDeclIDs.find(Owner);
5073 if (Pos == M.GlobalToLocalDeclIDs.end())
5074 return 0;
5075
5076 return GlobalID - Owner->BaseDeclID + Pos->second;
5077}
5078
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005079serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005080 const RecordData &Record,
5081 unsigned &Idx) {
5082 if (Idx >= Record.size()) {
5083 Error("Corrupted AST file");
5084 return 0;
5085 }
5086
5087 return getGlobalDeclID(F, Record[Idx++]);
5088}
5089
Chris Lattner887e2b32009-04-27 05:46:25 +00005090/// \brief Resolve the offset of a statement into a statement.
5091///
5092/// This operation will read a new statement from the external
5093/// source each time it is called, and is meant to be used via a
5094/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005095Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005096 // Switch case IDs are per Decl.
5097 ClearSwitchCaseIDs();
5098
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00005099 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005100 RecordLocation Loc = getLocalBitOffset(Offset);
5101 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5102 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00005103}
5104
Douglas Gregor851c75a2011-08-24 21:27:34 +00005105namespace {
5106 class FindExternalLexicalDeclsVisitor {
5107 ASTReader &Reader;
5108 const DeclContext *DC;
5109 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005110
Douglas Gregor851c75a2011-08-24 21:27:34 +00005111 SmallVectorImpl<Decl*> &Decls;
5112 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5113
5114 public:
5115 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5116 bool (*isKindWeWant)(Decl::Kind),
5117 SmallVectorImpl<Decl*> &Decls)
5118 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5119 {
5120 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5121 PredefsVisited[I] = false;
5122 }
5123
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005124 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00005125 if (Preorder)
5126 return false;
5127
5128 FindExternalLexicalDeclsVisitor *This
5129 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5130
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005131 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00005132 = M.DeclContextInfos.find(This->DC);
5133 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5134 return false;
5135
5136 // Load all of the declaration IDs
5137 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5138 *IDE = ID + Info->second.NumLexicalDecls;
5139 ID != IDE; ++ID) {
5140 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5141 continue;
5142
5143 // Don't add predefined declarations to the lexical context more
5144 // than once.
5145 if (ID->second < NUM_PREDEF_DECL_IDS) {
5146 if (This->PredefsVisited[ID->second])
5147 continue;
5148
5149 This->PredefsVisited[ID->second] = true;
5150 }
5151
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005152 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5153 if (!This->DC->isDeclInLexicalTraversal(D))
5154 This->Decls.push_back(D);
5155 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00005156 }
5157
5158 return false;
5159 }
5160 };
5161}
5162
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005163ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00005164 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00005165 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005166 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00005167 // least. Walk all of the modules in the order they were loaded.
5168 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5169 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005170 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005171 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005172}
5173
Douglas Gregor0d95f772011-08-24 19:03:07 +00005174namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005175
5176class DeclIDComp {
5177 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005178 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005179
5180public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005181 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005182
5183 bool operator()(LocalDeclID L, LocalDeclID R) const {
5184 SourceLocation LHS = getLocation(L);
5185 SourceLocation RHS = getLocation(R);
5186 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5187 }
5188
5189 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5190 SourceLocation RHS = getLocation(R);
5191 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5192 }
5193
5194 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5195 SourceLocation LHS = getLocation(L);
5196 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5197 }
5198
5199 SourceLocation getLocation(LocalDeclID ID) const {
5200 return Reader.getSourceManager().getFileLoc(
5201 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5202 }
5203};
5204
5205}
5206
5207void ASTReader::FindFileRegionDecls(FileID File,
5208 unsigned Offset, unsigned Length,
5209 SmallVectorImpl<Decl *> &Decls) {
5210 SourceManager &SM = getSourceManager();
5211
5212 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5213 if (I == FileDeclIDs.end())
5214 return;
5215
5216 FileDeclsInfo &DInfo = I->second;
5217 if (DInfo.Decls.empty())
5218 return;
5219
5220 SourceLocation
5221 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5222 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5223
5224 DeclIDComp DIDComp(*this, *DInfo.Mod);
5225 ArrayRef<serialization::LocalDeclID>::iterator
5226 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5227 BeginLoc, DIDComp);
5228 if (BeginIt != DInfo.Decls.begin())
5229 --BeginIt;
5230
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005231 // If we are pointing at a top-level decl inside an objc container, we need
5232 // to backtrack until we find it otherwise we will fail to report that the
5233 // region overlaps with an objc container.
5234 while (BeginIt != DInfo.Decls.begin() &&
5235 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5236 ->isTopLevelDeclInObjCContainer())
5237 --BeginIt;
5238
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005239 ArrayRef<serialization::LocalDeclID>::iterator
5240 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5241 EndLoc, DIDComp);
5242 if (EndIt != DInfo.Decls.end())
5243 ++EndIt;
5244
5245 for (ArrayRef<serialization::LocalDeclID>::iterator
5246 DIt = BeginIt; DIt != EndIt; ++DIt)
5247 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5248}
5249
5250namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005251 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005252 /// declaration context.
5253 class DeclContextNameLookupVisitor {
5254 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005255 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005256 DeclarationName Name;
5257 SmallVectorImpl<NamedDecl *> &Decls;
5258
5259 public:
5260 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005261 SmallVectorImpl<const DeclContext *> &Contexts,
5262 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005263 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005264 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005265
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005266 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005267 DeclContextNameLookupVisitor *This
5268 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5269
5270 // Check whether we have any visible declaration information for
5271 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005272 ModuleFile::DeclContextInfosMap::iterator Info;
5273 bool FoundInfo = false;
5274 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5275 Info = M.DeclContextInfos.find(This->Contexts[I]);
5276 if (Info != M.DeclContextInfos.end() &&
5277 Info->second.NameLookupTableData) {
5278 FoundInfo = true;
5279 break;
5280 }
5281 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005282
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005283 if (!FoundInfo)
5284 return false;
5285
Douglas Gregor0d95f772011-08-24 19:03:07 +00005286 // Look for this name within this module.
5287 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005288 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005289 ASTDeclContextNameLookupTable::iterator Pos
5290 = LookupTable->find(This->Name);
5291 if (Pos == LookupTable->end())
5292 return false;
5293
5294 bool FoundAnything = false;
5295 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5296 for (; Data.first != Data.second; ++Data.first) {
5297 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5298 if (!ND)
5299 continue;
5300
5301 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005302 // A name might be null because the decl's redeclarable part is
5303 // currently read before reading its name. The lookup is triggered by
5304 // building that decl (likely indirectly), and so it is later in the
5305 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005306 continue;
5307 }
5308
5309 // Record this declaration.
5310 FoundAnything = true;
5311 This->Decls.push_back(ND);
5312 }
5313
5314 return FoundAnything;
5315 }
5316 };
5317}
5318
John McCall76bd1f32010-06-01 09:23:16 +00005319DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005320ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005321 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005322 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005323 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005324 if (!Name)
5325 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5326 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005327
Chris Lattner5f9e2722011-07-23 10:55:15 +00005328 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005329
5330 // Compute the declaration contexts we need to look into. Multiple such
5331 // declaration contexts occur when two declaration contexts from disjoint
5332 // modules get merged, e.g., when two namespaces with the same name are
5333 // independently defined in separate modules.
5334 SmallVector<const DeclContext *, 2> Contexts;
5335 Contexts.push_back(DC);
5336
5337 if (DC->isNamespace()) {
5338 MergedDeclsMap::iterator Merged
5339 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5340 if (Merged != MergedDecls.end()) {
5341 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5342 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5343 }
5344 }
5345
5346 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005347 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005348 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005349 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005350 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005351}
5352
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005353namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005354 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005355 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005356 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005357 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005358 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005359 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005360
5361 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005362 DeclContextAllNamesVisitor(ASTReader &Reader,
5363 SmallVectorImpl<const DeclContext *> &Contexts,
5364 llvm::DenseMap<DeclarationName,
5365 SmallVector<NamedDecl *, 8> > &Decls)
5366 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005367
5368 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005369 DeclContextAllNamesVisitor *This
5370 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005371
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005372 // Check whether we have any visible declaration information for
5373 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005374 ModuleFile::DeclContextInfosMap::iterator Info;
5375 bool FoundInfo = false;
5376 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5377 Info = M.DeclContextInfos.find(This->Contexts[I]);
5378 if (Info != M.DeclContextInfos.end() &&
5379 Info->second.NameLookupTableData) {
5380 FoundInfo = true;
5381 break;
5382 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005383 }
5384
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005385 if (!FoundInfo)
5386 return false;
5387
5388 ASTDeclContextNameLookupTable *LookupTable =
5389 Info->second.NameLookupTableData;
5390 bool FoundAnything = false;
5391 for (ASTDeclContextNameLookupTable::data_iterator
5392 I = LookupTable->data_begin(), E = LookupTable->data_end();
5393 I != E; ++I) {
5394 ASTDeclContextNameLookupTrait::data_type Data = *I;
5395 for (; Data.first != Data.second; ++Data.first) {
5396 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5397 *Data.first);
5398 if (!ND)
5399 continue;
5400
5401 // Record this declaration.
5402 FoundAnything = true;
5403 This->Decls[ND->getDeclName()].push_back(ND);
5404 }
5405 }
5406
5407 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005408 }
5409 };
5410}
5411
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005412void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005413 if (!DC->hasExternalVisibleStorage())
5414 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005415 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5416
5417 // Compute the declaration contexts we need to look into. Multiple such
5418 // declaration contexts occur when two declaration contexts from disjoint
5419 // modules get merged, e.g., when two namespaces with the same name are
5420 // independently defined in separate modules.
5421 SmallVector<const DeclContext *, 2> Contexts;
5422 Contexts.push_back(DC);
5423
5424 if (DC->isNamespace()) {
5425 MergedDeclsMap::iterator Merged
5426 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5427 if (Merged != MergedDecls.end()) {
5428 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5429 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5430 }
5431 }
5432
5433 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5434 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5435 ++NumVisibleDeclContextsRead;
5436
5437 for (llvm::DenseMap<DeclarationName,
5438 llvm::SmallVector<NamedDecl*, 8> >::iterator
5439 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5440 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5441 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005442 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005443}
5444
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005445/// \brief Under non-PCH compilation the consumer receives the objc methods
5446/// before receiving the implementation, and codegen depends on this.
5447/// We simulate this by deserializing and passing to consumer the methods of the
5448/// implementation before passing the deserialized implementation decl.
5449static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5450 ASTConsumer *Consumer) {
5451 assert(ImplD && Consumer);
5452
5453 for (ObjCImplDecl::method_iterator
5454 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005455 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005456
5457 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5458}
5459
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005460void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005461 assert(Consumer);
5462 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005463 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005464 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005465
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005466 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005467 }
5468}
5469
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005470void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5471 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5472 PassObjCImplDeclToConsumer(ImplD, Consumer);
5473 else
5474 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5475}
5476
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005477void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005478 this->Consumer = Consumer;
5479
Douglas Gregorfdd01722009-04-14 00:24:19 +00005480 if (!Consumer)
5481 return;
5482
5483 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005484 // Force deserialization of this decl, which will cause it to be queued for
5485 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005486 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005487 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005488 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005489
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005490 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005491}
5492
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005493void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005494 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005495
Mike Stump1eb44332009-09-09 15:08:12 +00005496 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005497 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005498 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005499 unsigned NumDeclsLoaded
5500 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5501 (Decl *)0);
5502 unsigned NumIdentifiersLoaded
5503 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5504 IdentifiersLoaded.end(),
5505 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005506 unsigned NumMacrosLoaded
5507 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5508 MacrosLoaded.end(),
5509 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005510 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005511 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5512 SelectorsLoaded.end(),
5513 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005514
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005515 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005516 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5517 NumSLocEntriesRead, TotalNumSLocEntries,
5518 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005519 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005520 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005521 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5522 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5523 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005524 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005525 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5526 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005527 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005528 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005529 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5530 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005531 if (!MacrosLoaded.empty())
5532 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5533 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5534 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005535 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005536 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005537 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5538 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005539 if (TotalNumStatements)
5540 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5541 NumStatementsRead, TotalNumStatements,
5542 ((float)NumStatementsRead/TotalNumStatements * 100));
5543 if (TotalNumMacros)
5544 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5545 NumMacrosRead, TotalNumMacros,
5546 ((float)NumMacrosRead/TotalNumMacros * 100));
5547 if (TotalLexicalDeclContexts)
5548 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5549 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5550 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5551 * 100));
5552 if (TotalVisibleDeclContexts)
5553 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5554 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5555 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5556 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005557 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005558 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005559 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5560 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005561 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005562 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005563 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005564 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005565 dump();
5566 std::fprintf(stderr, "\n");
5567}
5568
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005569template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005570static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005571dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005572 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005573 InitialCapacity> &Map) {
5574 if (Map.begin() == Map.end())
5575 return;
5576
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005577 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005578 llvm::errs() << Name << ":\n";
5579 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5580 I != IEnd; ++I) {
5581 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5582 << "\n";
5583 }
5584}
5585
Douglas Gregor23d7df52011-07-21 19:50:14 +00005586void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005587 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005588 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005589 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005590 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005591 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005592 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005593 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005594 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005595 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005596 dumpModuleIDMap("Global preprocessed entity map",
5597 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005598
5599 llvm::errs() << "\n*** PCH/Modules Loaded:";
5600 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5601 MEnd = ModuleMgr.end();
5602 M != MEnd; ++M)
5603 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005604}
5605
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005606/// Return the amount of memory used by memory buffers, breaking down
5607/// by heap-backed versus mmap'ed memory.
5608void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005609 for (ModuleConstIterator I = ModuleMgr.begin(),
5610 E = ModuleMgr.end(); I != E; ++I) {
5611 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005612 size_t bytes = buf->getBufferSize();
5613 switch (buf->getBufferKind()) {
5614 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5615 sizes.malloc_bytes += bytes;
5616 break;
5617 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5618 sizes.mmap_bytes += bytes;
5619 break;
5620 }
5621 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005622 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005623}
5624
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005625void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005626 SemaObj = &S;
Axel Naumann0ec56b72012-10-18 19:05:02 +00005627 S.addExternalSource(this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005628
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005629 // Makes sure any declarations that were deserialized "too early"
5630 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005631 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005632 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5633 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005634 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005635 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005636
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005637 // Load the offsets of the declarations that Sema references.
5638 // They will be lazily deserialized when needed.
5639 if (!SemaDeclRefs.empty()) {
5640 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005641 if (!SemaObj->StdNamespace)
5642 SemaObj->StdNamespace = SemaDeclRefs[0];
5643 if (!SemaObj->StdBadAlloc)
5644 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005645 }
5646
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005647 if (!FPPragmaOptions.empty()) {
5648 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5649 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5650 }
5651
5652 if (!OpenCLExtensions.empty()) {
5653 unsigned I = 0;
5654#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5655#include "clang/Basic/OpenCLExtensions.def"
5656
5657 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5658 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005659}
5660
Douglas Gregor211f6e82011-08-20 04:39:52 +00005661IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005662 // Note that we are loading an identifier.
5663 Deserializing AnIdentifier(this);
5664
Douglas Gregor057df202012-01-18 20:56:22 +00005665 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5666 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005667 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005668 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005669 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005670 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005671}
5672
Douglas Gregor95f42922010-10-14 22:11:03 +00005673namespace clang {
5674 /// \brief An identifier-lookup iterator that enumerates all of the
5675 /// identifiers stored within a set of AST files.
5676 class ASTIdentifierIterator : public IdentifierIterator {
5677 /// \brief The AST reader whose identifiers are being enumerated.
5678 const ASTReader &Reader;
5679
5680 /// \brief The current index into the chain of AST files stored in
5681 /// the AST reader.
5682 unsigned Index;
5683
5684 /// \brief The current position within the identifier lookup table
5685 /// of the current AST file.
5686 ASTIdentifierLookupTable::key_iterator Current;
5687
5688 /// \brief The end position within the identifier lookup table of
5689 /// the current AST file.
5690 ASTIdentifierLookupTable::key_iterator End;
5691
5692 public:
5693 explicit ASTIdentifierIterator(const ASTReader &Reader);
5694
Chris Lattner5f9e2722011-07-23 10:55:15 +00005695 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005696 };
5697}
5698
5699ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005700 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005701 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005702 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005703 Current = IdTable->key_begin();
5704 End = IdTable->key_end();
5705}
5706
Chris Lattner5f9e2722011-07-23 10:55:15 +00005707StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005708 while (Current == End) {
5709 // If we have exhausted all of our AST files, we're done.
5710 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005711 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005712
5713 --Index;
5714 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005715 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5716 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005717 Current = IdTable->key_begin();
5718 End = IdTable->key_end();
5719 }
5720
5721 // We have any identifiers remaining in the current AST file; return
5722 // the next one.
5723 std::pair<const char*, unsigned> Key = *Current;
5724 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005725 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005726}
5727
5728IdentifierIterator *ASTReader::getIdentifiers() const {
5729 return new ASTIdentifierIterator(*this);
5730}
5731
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005732namespace clang { namespace serialization {
5733 class ReadMethodPoolVisitor {
5734 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005735 Selector Sel;
5736 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005737 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5738 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005739
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005740 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005741 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5742 unsigned PriorGeneration)
5743 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005744
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005745 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005746 ReadMethodPoolVisitor *This
5747 = static_cast<ReadMethodPoolVisitor *>(UserData);
5748
5749 if (!M.SelectorLookupTable)
5750 return false;
5751
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005752 // If we've already searched this module file, skip it now.
5753 if (M.Generation <= This->PriorGeneration)
5754 return true;
5755
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005756 ASTSelectorLookupTable *PoolTable
5757 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5758 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5759 if (Pos == PoolTable->end())
5760 return false;
5761
5762 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005763 // FIXME: Not quite happy with the statistics here. We probably should
5764 // disable this tracking when called via LoadSelector.
5765 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005766 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005767 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005768 if (This->Reader.DeserializationListener)
5769 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5770 This->Sel);
5771
5772 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5773 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5774 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005775 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005776
5777 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005778 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5779 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005780 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005781
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005782 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005783 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5784 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005785 }
5786 };
5787} } // end namespace clang::serialization
5788
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005789/// \brief Add the given set of methods to the method list.
5790static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5791 ObjCMethodList &List) {
5792 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5793 S.addMethodToGlobalList(&List, Methods[I]);
5794 }
5795}
5796
5797void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005798 // Get the selector generation and update it to the current generation.
5799 unsigned &Generation = SelectorGeneration[Sel];
5800 unsigned PriorGeneration = Generation;
5801 Generation = CurrentGeneration;
5802
5803 // Search for methods defined with this selector.
5804 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005805 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005806
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005807 if (Visitor.getInstanceMethods().empty() &&
5808 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005809 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005810 return;
5811 }
5812
5813 if (!getSema())
5814 return;
5815
5816 Sema &S = *getSema();
5817 Sema::GlobalMethodPool::iterator Pos
5818 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5819
5820 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5821 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005822}
5823
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005824void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005825 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005826 Namespaces.clear();
5827
5828 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5829 if (NamespaceDecl *Namespace
5830 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5831 Namespaces.push_back(Namespace);
5832 }
5833}
5834
Douglas Gregora8623202011-07-27 20:58:46 +00005835void ASTReader::ReadTentativeDefinitions(
5836 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5837 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5838 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5839 if (Var)
5840 TentativeDefs.push_back(Var);
5841 }
5842 TentativeDefinitions.clear();
5843}
5844
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005845void ASTReader::ReadUnusedFileScopedDecls(
5846 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5847 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5848 DeclaratorDecl *D
5849 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5850 if (D)
5851 Decls.push_back(D);
5852 }
5853 UnusedFileScopedDecls.clear();
5854}
5855
Douglas Gregor0129b562011-07-27 21:57:17 +00005856void ASTReader::ReadDelegatingConstructors(
5857 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5858 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5859 CXXConstructorDecl *D
5860 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5861 if (D)
5862 Decls.push_back(D);
5863 }
5864 DelegatingCtorDecls.clear();
5865}
5866
Douglas Gregord58a0a52011-07-28 00:39:29 +00005867void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5868 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5869 TypedefNameDecl *D
5870 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5871 if (D)
5872 Decls.push_back(D);
5873 }
5874 ExtVectorDecls.clear();
5875}
5876
Douglas Gregora126f172011-07-28 00:53:40 +00005877void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5878 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5879 CXXRecordDecl *D
5880 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5881 if (D)
5882 Decls.push_back(D);
5883 }
5884 DynamicClasses.clear();
5885}
5886
Douglas Gregorec12ce22011-07-28 14:20:37 +00005887void
5888ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5889 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5890 NamedDecl *D
5891 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5892 if (D)
5893 Decls.push_back(D);
5894 }
5895 LocallyScopedExternalDecls.clear();
5896}
5897
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005898void ASTReader::ReadReferencedSelectors(
5899 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5900 if (ReferencedSelectorsData.empty())
5901 return;
5902
5903 // If there are @selector references added them to its pool. This is for
5904 // implementation of -Wselector.
5905 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5906 unsigned I = 0;
5907 while (I < DataSize) {
5908 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5909 SourceLocation SelLoc
5910 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5911 Sels.push_back(std::make_pair(Sel, SelLoc));
5912 }
5913 ReferencedSelectorsData.clear();
5914}
5915
Douglas Gregor31e37b22011-07-28 18:09:57 +00005916void ASTReader::ReadWeakUndeclaredIdentifiers(
5917 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5918 if (WeakUndeclaredIdentifiers.empty())
5919 return;
5920
5921 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5922 IdentifierInfo *WeakId
5923 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5924 IdentifierInfo *AliasId
5925 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5926 SourceLocation Loc
5927 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5928 bool Used = WeakUndeclaredIdentifiers[I++];
5929 WeakInfo WI(AliasId, Loc);
5930 WI.setUsed(Used);
5931 WeakIDs.push_back(std::make_pair(WeakId, WI));
5932 }
5933 WeakUndeclaredIdentifiers.clear();
5934}
5935
Douglas Gregordfe65432011-07-28 19:11:31 +00005936void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5937 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5938 ExternalVTableUse VT;
5939 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5940 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5941 VT.DefinitionRequired = VTableUses[Idx++];
5942 VTables.push_back(VT);
5943 }
5944
5945 VTableUses.clear();
5946}
5947
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005948void ASTReader::ReadPendingInstantiations(
5949 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5950 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5951 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5952 SourceLocation Loc
5953 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005954
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005955 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005956 }
5957 PendingInstantiations.clear();
5958}
5959
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005960void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005961 // It would be complicated to avoid reading the methods anyway. So don't.
5962 ReadMethodPool(Sel);
5963}
5964
Douglas Gregor95eab172011-07-28 20:55:49 +00005965void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005966 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005967 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005968 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005969 if (DeserializationListener)
5970 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005971}
5972
Douglas Gregord89275b2009-07-06 18:54:52 +00005973/// \brief Set the globally-visible declarations associated with the given
5974/// identifier.
5975///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005976/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005977/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005978/// them.
5979///
5980/// \param II an IdentifierInfo that refers to one or more globally-visible
5981/// declarations.
5982///
5983/// \param DeclIDs the set of declaration IDs with the name @p II that are
5984/// visible at global scope.
5985///
5986/// \param Nonrecursive should be true to indicate that the caller knows that
5987/// this call is non-recursive, and therefore the globally-visible declarations
5988/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005989void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005990ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005991 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005992 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005993 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005994 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5995 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5996 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005997 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005998 return;
5999 }
Mike Stump1eb44332009-09-09 15:08:12 +00006000
Douglas Gregord89275b2009-07-06 18:54:52 +00006001 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6002 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6003 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00006004 // Introduce this declaration into the translation-unit scope
6005 // and add it to the declaration chain for this identifier, so
6006 // that (unqualified) name lookup will find it.
6007 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00006008 } else {
6009 // Queue this declaration so that it will be added to the
6010 // translation unit scope and identifier's declaration chain
6011 // once a Sema object is known.
6012 PreloadedDecls.push_back(D);
6013 }
6014 }
6015}
6016
Douglas Gregor95eab172011-07-28 20:55:49 +00006017IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00006018 if (ID == 0)
6019 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00006020
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006021 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006022 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00006023 return 0;
6024 }
Mike Stump1eb44332009-09-09 15:08:12 +00006025
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006026 ID -= 1;
6027 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00006028 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6029 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006030 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006031 unsigned Index = ID - M->BaseIdentifierID;
6032 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00006033
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006034 // All of the strings in the AST file are preceded by a 16-bit length.
6035 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00006036 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6037 // unsigned integers. This is important to avoid integer overflow when
6038 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00006039 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00006040 unsigned StrLen = (((unsigned) StrLenPtr[0])
6041 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006042 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006043 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006044 if (DeserializationListener)
6045 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00006046 }
Mike Stump1eb44332009-09-09 15:08:12 +00006047
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006048 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00006049}
6050
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006051IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00006052 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6053}
6054
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006055IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00006056 if (LocalID < NUM_PREDEF_IDENT_IDS)
6057 return LocalID;
6058
6059 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6060 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6061 assert(I != M.IdentifierRemap.end()
6062 && "Invalid index into identifier index remap");
6063
6064 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00006065}
6066
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006067MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00006068 if (ID == 0)
6069 return 0;
6070
6071 if (MacrosLoaded.empty()) {
6072 Error("no macro table in AST file");
6073 return 0;
6074 }
6075
6076 ID -= NUM_PREDEF_MACRO_IDS;
6077 if (!MacrosLoaded[ID]) {
6078 GlobalMacroMapType::iterator I
6079 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6080 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6081 ModuleFile *M = I->second;
6082 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006083 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00006084 }
6085
6086 return MacrosLoaded[ID];
6087}
6088
6089MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6090 if (LocalID < NUM_PREDEF_MACRO_IDS)
6091 return LocalID;
6092
6093 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6094 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6095 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6096
6097 return LocalID + I->second;
6098}
6099
Douglas Gregor26ced122011-12-01 00:59:36 +00006100serialization::SubmoduleID
6101ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6102 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6103 return LocalID;
6104
6105 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6106 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6107 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006108 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00006109
6110 return LocalID + I->second;
6111}
6112
6113Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6114 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6115 assert(GlobalID == 0 && "Unhandled global submodule ID");
6116 return 0;
6117 }
6118
6119 if (GlobalID > SubmodulesLoaded.size()) {
6120 Error("submodule ID out of range in AST file");
6121 return 0;
6122 }
6123
6124 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6125}
6126
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006127Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006128 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6129}
6130
6131Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006132 if (ID == 0)
6133 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00006134
Sebastian Redl725cd962010-08-04 20:40:17 +00006135 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006136 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006137 return Selector();
6138 }
Douglas Gregor83941df2009-04-25 17:48:32 +00006139
Sebastian Redl725cd962010-08-04 20:40:17 +00006140 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00006141 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00006142 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6143 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006144 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006145 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006146 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00006147 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00006148 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00006149 if (DeserializationListener)
6150 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00006151 }
6152
Sebastian Redl725cd962010-08-04 20:40:17 +00006153 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006154}
6155
Douglas Gregor8451ec72011-07-28 14:41:43 +00006156Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00006157 return DecodeSelector(ID);
6158}
6159
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006160uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00006161 // ID 0 (the null selector) is considered an external selector.
6162 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00006163}
6164
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006165serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006166ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006167 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6168 return LocalID;
6169
6170 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6171 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6172 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006173 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006174
6175 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00006176}
6177
Mike Stump1eb44332009-09-09 15:08:12 +00006178DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006179ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00006180 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00006181 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6182 switch (Kind) {
6183 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00006184 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006185
6186 case DeclarationName::ObjCZeroArgSelector:
6187 case DeclarationName::ObjCOneArgSelector:
6188 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006189 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006190
6191 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006192 return Context.DeclarationNames.getCXXConstructorName(
6193 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006194
6195 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006196 return Context.DeclarationNames.getCXXDestructorName(
6197 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006198
6199 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006200 return Context.DeclarationNames.getCXXConversionFunctionName(
6201 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006202
6203 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006204 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006205 (OverloadedOperatorKind)Record[Idx++]);
6206
Sean Hunt3e518bd2009-11-29 07:34:05 +00006207 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006208 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006209 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006210
Douglas Gregor2cf26342009-04-09 22:27:44 +00006211 case DeclarationName::CXXUsingDirective:
6212 return DeclarationName::getUsingDirectiveName();
6213 }
6214
David Blaikie7530c032012-01-17 06:56:22 +00006215 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006216}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006217
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006218void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006219 DeclarationNameLoc &DNLoc,
6220 DeclarationName Name,
6221 const RecordData &Record, unsigned &Idx) {
6222 switch (Name.getNameKind()) {
6223 case DeclarationName::CXXConstructorName:
6224 case DeclarationName::CXXDestructorName:
6225 case DeclarationName::CXXConversionFunctionName:
6226 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6227 break;
6228
6229 case DeclarationName::CXXOperatorName:
6230 DNLoc.CXXOperatorName.BeginOpNameLoc
6231 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6232 DNLoc.CXXOperatorName.EndOpNameLoc
6233 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6234 break;
6235
6236 case DeclarationName::CXXLiteralOperatorName:
6237 DNLoc.CXXLiteralOperatorName.OpNameLoc
6238 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6239 break;
6240
6241 case DeclarationName::Identifier:
6242 case DeclarationName::ObjCZeroArgSelector:
6243 case DeclarationName::ObjCOneArgSelector:
6244 case DeclarationName::ObjCMultiArgSelector:
6245 case DeclarationName::CXXUsingDirective:
6246 break;
6247 }
6248}
6249
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006250void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006251 DeclarationNameInfo &NameInfo,
6252 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006253 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006254 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6255 DeclarationNameLoc DNLoc;
6256 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6257 NameInfo.setInfo(DNLoc);
6258}
6259
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006260void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006261 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006262 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006263 unsigned NumTPLists = Record[Idx++];
6264 Info.NumTemplParamLists = NumTPLists;
6265 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006266 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006267 for (unsigned i=0; i != NumTPLists; ++i)
6268 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6269 }
6270}
6271
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006272TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006273ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006274 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006275 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006276 switch (Kind) {
6277 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006278 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006279
6280 case TemplateName::OverloadedTemplate: {
6281 unsigned size = Record[Idx++];
6282 UnresolvedSet<8> Decls;
6283 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006284 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006285
Douglas Gregor35942772011-09-09 21:34:22 +00006286 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006287 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006288
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006289 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006290 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006291 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006292 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006293 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006294 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006295
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006296 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006297 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006298 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006299 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006300 GetIdentifierInfo(F, Record,
6301 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006302 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006303 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006304 }
John McCall14606042011-06-30 08:33:18 +00006305
6306 case TemplateName::SubstTemplateTemplateParm: {
6307 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006308 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006309 if (!param) return TemplateName();
6310 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006311 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006312 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006313
6314 case TemplateName::SubstTemplateTemplateParmPack: {
6315 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006316 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006317 if (!Param)
6318 return TemplateName();
6319
6320 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6321 if (ArgPack.getKind() != TemplateArgument::Pack)
6322 return TemplateName();
6323
Douglas Gregor35942772011-09-09 21:34:22 +00006324 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006325 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006326 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006327
David Blaikieb219cfc2011-09-23 05:06:16 +00006328 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006329}
6330
6331TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006332ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006333 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006334 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6335 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006336 case TemplateArgument::Null:
6337 return TemplateArgument();
6338 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006339 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006340 case TemplateArgument::Declaration: {
6341 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6342 bool ForReferenceParam = Record[Idx++];
6343 return TemplateArgument(D, ForReferenceParam);
6344 }
6345 case TemplateArgument::NullPtr:
6346 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006347 case TemplateArgument::Integral: {
6348 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006349 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006350 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006351 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006352 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006353 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006354 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006355 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006356 llvm::Optional<unsigned> NumTemplateExpansions;
6357 if (unsigned NumExpansions = Record[Idx++])
6358 NumTemplateExpansions = NumExpansions - 1;
6359 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006360 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006361 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006362 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006363 case TemplateArgument::Pack: {
6364 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006365 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006366 for (unsigned I = 0; I != NumArgs; ++I)
6367 Args[I] = ReadTemplateArgument(F, Record, Idx);
6368 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006369 }
6370 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006371
David Blaikieb219cfc2011-09-23 05:06:16 +00006372 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006373}
6374
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006375TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006376ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006377 const RecordData &Record, unsigned &Idx) {
6378 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6379 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6380 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006381
6382 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006383 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006384 Params.reserve(NumParams);
6385 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006386 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006387
6388 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006389 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006390 Params.data(), Params.size(), RAngleLoc);
6391 return TemplateParams;
6392}
6393
6394void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006395ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006396ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006397 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006398 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006399 unsigned NumTemplateArgs = Record[Idx++];
6400 TemplArgs.reserve(NumTemplateArgs);
6401 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006402 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006403}
6404
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006405/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006406void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006407 const RecordData &Record, unsigned &Idx) {
6408 unsigned NumDecls = Record[Idx++];
6409 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006410 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006411 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6412 Set.addDecl(D, AS);
6413 }
6414}
6415
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006416CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006417ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006418 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006419 bool isVirtual = static_cast<bool>(Record[Idx++]);
6420 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6421 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006422 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006423 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6424 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006425 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006426 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006427 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006428 Result.setInheritConstructors(inheritConstructors);
6429 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006430}
6431
Sean Huntcbb67482011-01-08 20:30:50 +00006432std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006433ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006434 unsigned &Idx) {
6435 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006436 unsigned NumInitializers = Record[Idx++];
6437 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006438 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006439 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006440 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006441 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006442 bool IsBaseVirtual = false;
6443 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006444 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006445
Sean Hunt156b6402011-05-04 01:19:08 +00006446 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6447 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006448 case CTOR_INITIALIZER_BASE:
6449 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006450 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006451 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006452
6453 case CTOR_INITIALIZER_DELEGATING:
6454 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006455 break;
6456
6457 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006458 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006459 break;
6460
6461 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006462 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006463 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006464 }
Sean Hunt156b6402011-05-04 01:19:08 +00006465
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006466 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006467 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006468 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6469 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006470 bool IsWritten = Record[Idx++];
6471 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006472 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006473 if (IsWritten) {
6474 SourceOrderOrNumArrayIndices = Record[Idx++];
6475 } else {
6476 SourceOrderOrNumArrayIndices = Record[Idx++];
6477 Indices.reserve(SourceOrderOrNumArrayIndices);
6478 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006479 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006480 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006481
Sean Huntcbb67482011-01-08 20:30:50 +00006482 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006483 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006484 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006485 LParenLoc, Init, RParenLoc,
6486 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006487 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006488 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6489 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006490 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006491 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006492 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006493 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006494 else
Douglas Gregor35942772011-09-09 21:34:22 +00006495 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006496 MemberOrEllipsisLoc, LParenLoc,
6497 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006498 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006499 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006500 LParenLoc, Init, RParenLoc,
6501 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006502 }
6503
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006504 if (IsWritten)
6505 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006506 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006507 }
6508 }
6509
Sean Huntcbb67482011-01-08 20:30:50 +00006510 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006511}
6512
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006513NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006514ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006515 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006516 unsigned N = Record[Idx++];
6517 NestedNameSpecifier *NNS = 0, *Prev = 0;
6518 for (unsigned I = 0; I != N; ++I) {
6519 NestedNameSpecifier::SpecifierKind Kind
6520 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6521 switch (Kind) {
6522 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006523 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006524 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006525 break;
6526 }
6527
6528 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006529 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006530 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006531 break;
6532 }
6533
Douglas Gregor14aba762011-02-24 02:36:08 +00006534 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006535 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006536 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006537 break;
6538 }
6539
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006540 case NestedNameSpecifier::TypeSpec:
6541 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006542 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006543 if (!T)
6544 return 0;
6545
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006546 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006547 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006548 break;
6549 }
6550
6551 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006552 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006553 // No associated value, and there can't be a prefix.
6554 break;
6555 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006556 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006557 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006558 }
6559 return NNS;
6560}
6561
Douglas Gregordc355712011-02-25 00:36:19 +00006562NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006563ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006564 unsigned &Idx) {
6565 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006566 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006567 for (unsigned I = 0; I != N; ++I) {
6568 NestedNameSpecifier::SpecifierKind Kind
6569 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6570 switch (Kind) {
6571 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006572 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006573 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006574 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006575 break;
6576 }
6577
6578 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006579 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006580 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006581 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006582 break;
6583 }
6584
6585 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006586 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006587 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006588 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006589 break;
6590 }
6591
6592 case NestedNameSpecifier::TypeSpec:
6593 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006594 bool Template = Record[Idx++];
6595 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6596 if (!T)
6597 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006598 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006599
6600 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006601 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006602 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6603 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006604 break;
6605 }
6606
6607 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006608 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006609 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006610 break;
6611 }
6612 }
Douglas Gregordc355712011-02-25 00:36:19 +00006613 }
6614
Douglas Gregor35942772011-09-09 21:34:22 +00006615 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006616}
6617
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006618SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006619ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006620 unsigned &Idx) {
6621 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6622 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006623 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006624}
6625
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006626/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006627llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006628 unsigned BitWidth = Record[Idx++];
6629 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6630 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6631 Idx += NumWords;
6632 return Result;
6633}
6634
6635/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006636llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006637 bool isUnsigned = Record[Idx++];
6638 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6639}
6640
Douglas Gregor17fc2232009-04-14 21:55:33 +00006641/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006642llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006643 return llvm::APFloat(ReadAPInt(Record, Idx));
6644}
6645
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006646// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006647std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006648 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006649 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006650 Idx += Len;
6651 return Result;
6652}
6653
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006654VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6655 unsigned &Idx) {
6656 unsigned Major = Record[Idx++];
6657 unsigned Minor = Record[Idx++];
6658 unsigned Subminor = Record[Idx++];
6659 if (Minor == 0)
6660 return VersionTuple(Major);
6661 if (Subminor == 0)
6662 return VersionTuple(Major, Minor - 1);
6663 return VersionTuple(Major, Minor - 1, Subminor - 1);
6664}
6665
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006666CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006667 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006668 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006669 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006670 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006671}
6672
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006673DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006674 return Diag(SourceLocation(), DiagID);
6675}
6676
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006677DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006678 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006679}
Douglas Gregor025452f2009-04-17 00:04:06 +00006680
Douglas Gregor668c1a42009-04-21 22:25:48 +00006681/// \brief Retrieve the identifier table associated with the
6682/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006683IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006684 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006685}
6686
Douglas Gregor025452f2009-04-17 00:04:06 +00006687/// \brief Record that the given ID maps to the given switch-case
6688/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006689void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006690 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6691 "Already have a SwitchCase with this ID");
6692 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006693}
6694
6695/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006696SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006697 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6698 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006699}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006700
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006701void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006702 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006703}
6704
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006705void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006706 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006707 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6708 serialization::ModuleFile *> >::iterator
6709 I = CommentsCursors.begin(),
6710 E = CommentsCursors.end();
6711 I != E; ++I) {
6712 llvm::BitstreamCursor &Cursor = I->first;
6713 serialization::ModuleFile &F = *I->second;
6714 SavedStreamPosition SavedPosition(Cursor);
6715
6716 RecordData Record;
6717 while (true) {
6718 unsigned Code = Cursor.ReadCode();
6719 if (Code == llvm::bitc::END_BLOCK)
6720 break;
6721
6722 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6723 // No known subblocks, always skip them.
6724 Cursor.ReadSubBlockID();
6725 if (Cursor.SkipBlock()) {
6726 Error("malformed block record in AST file");
6727 return;
6728 }
6729 continue;
6730 }
6731
6732 if (Code == llvm::bitc::DEFINE_ABBREV) {
6733 Cursor.ReadAbbrevRecord();
6734 continue;
6735 }
6736
6737 // Read a record.
6738 Record.clear();
6739 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006740 case COMMENTS_RAW_COMMENT: {
6741 unsigned Idx = 0;
6742 SourceRange SR = ReadSourceRange(F, Record, Idx);
6743 RawComment::CommentKind Kind =
6744 (RawComment::CommentKind) Record[Idx++];
6745 bool IsTrailingComment = Record[Idx++];
6746 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006747 Comments.push_back(new (Context) RawComment(SR, Kind,
6748 IsTrailingComment,
6749 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006750 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006751 }
6752 }
6753 }
6754 }
6755 Context.Comments.addCommentsToFront(Comments);
6756}
6757
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006758void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006759 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6760 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006761 // If any identifiers with corresponding top-level declarations have
6762 // been loaded, load those declarations now.
6763 while (!PendingIdentifierInfos.empty()) {
6764 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6765 PendingIdentifierInfos.front().DeclIDs, true);
6766 PendingIdentifierInfos.pop_front();
6767 }
6768
Douglas Gregora1be2782011-12-17 23:38:30 +00006769 // Load pending declaration chains.
6770 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6771 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006772 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006773 }
6774 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006775
6776 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00006777 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6778 // FIXME: std::move here
6779 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006780 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00006781 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6782 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006783 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00006784 }
6785 }
6786 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006787 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006788
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006789 // If we deserialized any C++ or Objective-C class definitions, any
6790 // Objective-C protocol definitions, or any redeclarable templates, make sure
6791 // that all redeclarations point to the definitions. Note that this can only
6792 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006793 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6794 DEnd = PendingDefinitions.end();
6795 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006796 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6797 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6798 // Make sure that the TagType points at the definition.
6799 const_cast<TagType*>(TagT)->decl = TD;
6800 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006801
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006802 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6803 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6804 REnd = RD->redecls_end();
6805 R != REnd; ++R)
6806 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6807
6808 }
6809
Douglas Gregorfc529f72011-12-19 19:00:47 +00006810 continue;
6811 }
6812
Douglas Gregor1d784b22012-01-01 19:51:50 +00006813 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006814 // Make sure that the ObjCInterfaceType points at the definition.
6815 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6816 ->Decl = ID;
6817
Douglas Gregor1d784b22012-01-01 19:51:50 +00006818 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6819 REnd = ID->redecls_end();
6820 R != REnd; ++R)
6821 R->Data = ID->Data;
6822
6823 continue;
6824 }
6825
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006826 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6827 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6828 REnd = PD->redecls_end();
6829 R != REnd; ++R)
6830 R->Data = PD->Data;
6831
6832 continue;
6833 }
6834
6835 RedeclarableTemplateDecl *RTD
6836 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6837 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6838 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006839 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006840 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006841 }
6842 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006843
6844 // Load the bodies of any functions or methods we've encountered. We do
6845 // this now (delayed) so that we can be sure that the declaration chains
6846 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006847 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6848 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006849 PB != PBEnd; ++PB) {
6850 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6851 // FIXME: Check for =delete/=default?
6852 // FIXME: Complain about ODR violations here?
6853 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6854 FD->setLazyBody(PB->second);
6855 continue;
6856 }
6857
6858 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6859 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6860 MD->setLazyBody(PB->second);
6861 }
6862 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006863}
6864
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006865void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006866 assert(NumCurrentElementsDeserializing &&
6867 "FinishedDeserializing not paired with StartedDeserializing");
6868 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006869 // We decrease NumCurrentElementsDeserializing only after pending actions
6870 // are finished, to avoid recursively re-calling finishPendingActions().
6871 finishPendingActions();
6872 }
6873 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006874
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006875 if (NumCurrentElementsDeserializing == 0 &&
6876 Consumer && !PassingDeclsToConsumer) {
6877 // Guard variable to avoid recursively redoing the process of passing
6878 // decls to consumer.
6879 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6880 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006881
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006882 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006883 // We are not in recursive loading, so it's safe to pass the "interesting"
6884 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006885 Decl *D = InterestingDecls.front();
6886 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006887 PassInterestingDeclToConsumer(D);
6888 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006889 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006890}
Douglas Gregor501c1032010-08-19 00:28:17 +00006891
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006892ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006893 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00006894 bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006895 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6896 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006897 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006898 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregorcaed0602012-10-18 21:31:35 +00006899 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006900 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006901 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006902 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006903 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6904 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6905 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6906 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006907 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6908 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006909 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006910 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006911{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006912 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006913}
6914
Sebastian Redle1dde812010-08-24 00:50:04 +00006915ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006916 for (DeclContextVisibleUpdatesPending::iterator
6917 I = PendingVisibleUpdates.begin(),
6918 E = PendingVisibleUpdates.end();
6919 I != E; ++I) {
6920 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6921 F = I->second.end();
6922 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006923 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006924 }
6925}