blob: 783049e6f78144623ed6679bad4559191fee0fc1 [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];
Argyrios Kyrtzidis23de7ce2012-11-06 00:35:04 +0000964 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000965 unsigned Code = SLocEntryCursor.ReadCode();
966 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000967 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000968 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000969
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000970 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000971 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +0000972 return true;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000973 }
974
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000975 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +0000976 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
977 Name);
Argyrios Kyrtzidis23de7ce2012-11-06 00:35:04 +0000978 SourceMgr.createFileIDForMemBuffer(Buffer, ID, BaseOffset + Offset,
979 IncludeLoc);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000980 break;
981 }
982
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000983 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +0000984 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +0000985 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +0000986 ReadSourceLocation(*F, Record[2]),
987 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000988 Record[4],
989 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000990 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000991 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000992 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000993 }
994
Douglas Gregor4825fd72012-10-22 22:50:17 +0000995 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000996}
997
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000998/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000999SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001000 if (F->ImportLoc.isValid())
1001 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001002
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001003 // Otherwise we have a PCH. It's considered to be "imported" at the first
1004 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001005 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001006 // Main file is the importer. We assume that it is the first entry in the
1007 // entry table. We can't ask the manager, because at the time of PCH loading
1008 // the main file entry doesn't exist yet.
1009 // The very first entry is the invalid instantiation loc, which takes up
1010 // offsets 0 and 1.
1011 return SourceLocation::getFromRawEncoding(2U);
1012 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001013 //return F->Loaders[0]->FirstLoc;
1014 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001015}
1016
Chris Lattner6367f6d2009-04-27 01:05:14 +00001017/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1018/// specified cursor. Read the abbreviations that are at the top of the block
1019/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001020bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001021 unsigned BlockID) {
1022 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001023 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001024 return Failure;
1025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattner6367f6d2009-04-27 01:05:14 +00001027 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001028 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001029 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001030
Chris Lattner6367f6d2009-04-27 01:05:14 +00001031 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001032 if (Code != llvm::bitc::DEFINE_ABBREV) {
1033 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001034 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001035 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001036 Cursor.ReadAbbrevRecord();
1037 }
1038}
1039
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00001040void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1041 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001042 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Douglas Gregor37e26842009-04-21 23:56:24 +00001044 // Keep track of where we are in the stream, then jump back there
1045 // after reading this macro.
1046 SavedStreamPosition SavedPosition(Stream);
1047
1048 Stream.JumpToBit(Offset);
1049 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001050 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001051 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Douglas Gregore8219a62012-10-11 21:07:39 +00001053 // RAII object to add the loaded macro information once we're done
1054 // adding tokens.
1055 struct AddLoadedMacroInfoRAII {
1056 Preprocessor &PP;
1057 MacroInfo *Hint;
1058 MacroInfo *MI;
1059 IdentifierInfo *II;
1060
1061 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1062 : PP(PP), Hint(Hint), MI(), II() { }
1063 ~AddLoadedMacroInfoRAII( ) {
1064 if (MI) {
1065 // Finally, install the macro.
1066 PP.addLoadedMacroInfo(II, MI, Hint);
1067 }
1068 }
1069 } AddLoadedMacroInfo(PP, Hint);
1070
Douglas Gregor37e26842009-04-21 23:56:24 +00001071 while (true) {
1072 unsigned Code = Stream.ReadCode();
1073 switch (Code) {
1074 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001075 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001076
1077 case llvm::bitc::ENTER_SUBBLOCK:
1078 // No known subblocks, always skip them.
1079 Stream.ReadSubBlockID();
1080 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001081 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001082 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001083 }
1084 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Douglas Gregor37e26842009-04-21 23:56:24 +00001086 case llvm::bitc::DEFINE_ABBREV:
1087 Stream.ReadAbbrevRecord();
1088 continue;
1089 default: break;
1090 }
1091
1092 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001093 const char *BlobStart = 0;
1094 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001095 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001096 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001097 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001098 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001099 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001100 case PP_MACRO_OBJECT_LIKE:
1101 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001102 // If we already have a macro, that means that we've hit the end
1103 // of the definition of the macro we were looking for. We're
1104 // done.
1105 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001106 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001107
Douglas Gregor95eab172011-07-28 20:55:49 +00001108 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001109 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001110 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001111 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Douglas Gregora8235d62012-10-09 23:05:51 +00001114 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1115
1116 // If this macro has already been loaded, don't do so again.
1117 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1118 return;
1119
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001120 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1121 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001122 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001123 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001124
Douglas Gregora8235d62012-10-09 23:05:51 +00001125 // Record this macro.
1126 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1127
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001128 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1129 if (UndefLoc.isValid())
1130 MI->setUndefLoc(UndefLoc);
1131
1132 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001133 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001135 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001136 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001138 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001139 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001140 bool isC99VarArgs = Record[NextIndex++];
1141 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001142 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001143 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001144 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001145 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001146
1147 // Install function-like macro info.
1148 MI->setIsFunctionLike();
1149 if (isC99VarArgs) MI->setIsC99Varargs();
1150 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001151 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001152 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001153 }
1154
Douglas Gregora8235d62012-10-09 23:05:51 +00001155 if (DeserializationListener)
1156 DeserializationListener->MacroRead(GlobalID, MI);
1157
1158 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001159 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001160 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1161 if (Update != MacroUpdates.end()) {
1162 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001163 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1164 bool Hidden = false;
1165 if (unsigned SubmoduleID = Update->second[I].first) {
1166 if (Module *Owner = getSubmodule(SubmoduleID)) {
1167 if (Owner->NameVisibility == Module::Hidden) {
1168 // Note that this #undef is hidden.
1169 Hidden = true;
1170
1171 // Record this hiding for later.
1172 HiddenNamesMap[Owner].push_back(
1173 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1174 }
1175 }
1176 }
1177
1178 if (!Hidden) {
1179 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1180 if (PPMutationListener *Listener = PP.getPPMutationListener())
1181 Listener->UndefinedMacro(MI);
1182 break;
1183 }
1184 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001185 }
1186 MacroUpdates.erase(Update);
1187 }
1188
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001189 // Determine whether this macro definition is visible.
1190 bool Hidden = !MI->isPublic();
1191 if (!Hidden && GlobalSubmoduleID) {
1192 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1193 if (Owner->NameVisibility == Module::Hidden) {
1194 // The owning module is not visible, and this macro definition
1195 // should not be, either.
1196 Hidden = true;
1197
1198 // Note that this macro definition was hidden because its owning
1199 // module is not yet visible.
1200 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1201 }
1202 }
1203 }
1204 MI->setHidden(Hidden);
1205
Douglas Gregore8219a62012-10-11 21:07:39 +00001206 // Make sure we install the macro once we're done.
1207 AddLoadedMacroInfo.MI = MI;
1208 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001209
1210 // Remember that we saw this macro last so that we add the tokens that
1211 // form its body to it.
1212 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001213
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001214 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1215 Record[NextIndex]) {
1216 // We have a macro definition. Register the association
1217 PreprocessedEntityID
1218 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1219 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1220 PPRec.RegisterMacroDefinition(Macro,
1221 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001222 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001223
Douglas Gregor37e26842009-04-21 23:56:24 +00001224 ++NumMacrosRead;
1225 break;
1226 }
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001228 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001229 // If we see a TOKEN before a PP_MACRO_*, then the file is
1230 // erroneous, just pretend we didn't see this.
1231 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Douglas Gregor37e26842009-04-21 23:56:24 +00001233 Token Tok;
1234 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001235 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001236 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001237 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001238 Tok.setIdentifierInfo(II);
1239 Tok.setKind((tok::TokenKind)Record[3]);
1240 Tok.setFlag((Token::TokenFlags)Record[4]);
1241 Macro->AddTokenToBody(Tok);
1242 break;
1243 }
David Blaikie7530c032012-01-17 06:56:22 +00001244 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001245 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001246}
1247
Douglas Gregor86c67d82011-07-28 22:39:26 +00001248PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001249ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001250 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001251 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1252 assert(I != M.PreprocessedEntityRemap.end()
1253 && "Invalid index into preprocessed entity index remap");
1254
1255 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001256}
1257
Douglas Gregor98339b92011-08-25 20:47:51 +00001258unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1259 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001260}
Douglas Gregor98339b92011-08-25 20:47:51 +00001261
1262HeaderFileInfoTrait::internal_key_type
1263HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1264
1265bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1266 if (strcmp(a, b) == 0)
1267 return true;
1268
1269 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1270 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001271
1272 // Determine whether the actual files are equivalent.
1273 bool Result = false;
1274 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001275 return false;
1276
Douglas Gregor99a922b2011-12-09 16:22:07 +00001277 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001278}
1279
1280std::pair<unsigned, unsigned>
1281HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1282 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1283 unsigned DataLen = (unsigned) *d++;
1284 return std::make_pair(KeyLen + 1, DataLen);
1285}
1286
1287HeaderFileInfoTrait::data_type
1288HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1289 unsigned DataLen) {
1290 const unsigned char *End = d + DataLen;
1291 using namespace clang::io;
1292 HeaderFileInfo HFI;
1293 unsigned Flags = *d++;
1294 HFI.isImport = (Flags >> 5) & 0x01;
1295 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1296 HFI.DirInfo = (Flags >> 2) & 0x03;
1297 HFI.Resolved = (Flags >> 1) & 0x01;
1298 HFI.IndexHeaderMapHeader = Flags & 0x01;
1299 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001300 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1301 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001302 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1303 // The framework offset is 1 greater than the actual offset,
1304 // since 0 is used as an indicator for "no framework name".
1305 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1306 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1307 }
1308
1309 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1310 (void)End;
1311
1312 // This HeaderFileInfo was externally loaded.
1313 HFI.External = true;
1314 return HFI;
1315}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001316
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001317void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1318 II->setHadMacroDefinition(true);
1319 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1320 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001321}
1322
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001323void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001324 // Note that we are loading defined macros.
1325 Deserializing Macros(this);
1326
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001327 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1328 E = ModuleMgr.rend(); I != E; ++I) {
1329 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001330
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001331 // If there was no preprocessor block, skip this file.
1332 if (!MacroCursor.getBitStreamReader())
1333 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001334
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001335 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001336 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001337
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001338 RecordData Record;
1339 while (true) {
1340 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001341 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001342 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001343
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001344 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1345 // No known subblocks, always skip them.
1346 Cursor.ReadSubBlockID();
1347 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001348 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001349 return;
1350 }
1351 continue;
1352 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001353
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001354 if (Code == llvm::bitc::DEFINE_ABBREV) {
1355 Cursor.ReadAbbrevRecord();
1356 continue;
1357 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001358
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001359 // Read a record.
1360 const char *BlobStart;
1361 unsigned BlobLen;
1362 Record.clear();
1363 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1364 default: // Default behavior: ignore.
1365 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001366
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001367 case PP_MACRO_OBJECT_LIKE:
1368 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001369 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001370 break;
1371
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001372 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001373 // Ignore tokens.
1374 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001375 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001376 }
1377 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001378}
1379
Douglas Gregoreee242f2011-10-27 09:33:13 +00001380namespace {
1381 /// \brief Visitor class used to look up identifirs in an AST file.
1382 class IdentifierLookupVisitor {
1383 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001384 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001385 IdentifierInfo *Found;
1386 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001387 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1388 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001389
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001390 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001391 IdentifierLookupVisitor *This
1392 = static_cast<IdentifierLookupVisitor *>(UserData);
1393
Douglas Gregor057df202012-01-18 20:56:22 +00001394 // If we've already searched this module file, skip it now.
1395 if (M.Generation <= This->PriorGeneration)
1396 return true;
1397
Douglas Gregoreee242f2011-10-27 09:33:13 +00001398 ASTIdentifierLookupTable *IdTable
1399 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1400 if (!IdTable)
1401 return false;
1402
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001403 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1404 M, This->Found);
1405
Douglas Gregoreee242f2011-10-27 09:33:13 +00001406 std::pair<const char*, unsigned> Key(This->Name.begin(),
1407 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001408 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001409 if (Pos == IdTable->end())
1410 return false;
1411
1412 // Dereferencing the iterator has the effect of building the
1413 // IdentifierInfo node and populating it with the various
1414 // declarations it needs.
1415 This->Found = *Pos;
1416 return true;
1417 }
1418
1419 // \brief Retrieve the identifier info found within the module
1420 // files.
1421 IdentifierInfo *getIdentifierInfo() const { return Found; }
1422 };
1423}
1424
1425void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001426 // Note that we are loading an identifier.
1427 Deserializing AnIdentifier(this);
1428
Douglas Gregor057df202012-01-18 20:56:22 +00001429 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001430 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001431 PriorGeneration = IdentifierGeneration[&II];
1432
1433 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1434 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1435 markIdentifierUpToDate(&II);
1436}
1437
1438void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1439 if (!II)
1440 return;
1441
1442 II->setOutOfDate(false);
1443
1444 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001445 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001446 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001447}
1448
Douglas Gregora930dc92012-10-22 18:42:04 +00001449llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor38295be2012-10-22 23:51:00 +00001450ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregora930dc92012-10-22 18:42:04 +00001451 // If this ID is bogus, just return an empty input file.
1452 if (ID == 0 || ID > F.InputFilesLoaded.size())
1453 return InputFile();
1454
1455 // If we've already loaded this input file, return it.
1456 if (F.InputFilesLoaded[ID-1].getPointer())
1457 return F.InputFilesLoaded[ID-1];
1458
1459 // Go find this input file.
1460 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1461 SavedStreamPosition SavedPosition(Cursor);
1462 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1463
1464 unsigned Code = Cursor.ReadCode();
1465 RecordData Record;
1466 const char *BlobStart = 0;
1467 unsigned BlobLen = 0;
1468 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1469 &BlobStart, &BlobLen)) {
1470 case INPUT_FILE: {
1471 unsigned StoredID = Record[0];
1472 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi6b8194e2012-10-22 21:50:39 +00001473 (void)StoredID;
Douglas Gregora930dc92012-10-22 18:42:04 +00001474 off_t StoredSize = (off_t)Record[1];
1475 time_t StoredTime = (time_t)Record[2];
1476 bool Overridden = (bool)Record[3];
1477
1478 // Get the file entry for this input file.
1479 StringRef OrigFilename(BlobStart, BlobLen);
1480 std::string Filename = OrigFilename;
1481 MaybeAddSystemRootToFilename(F, Filename);
1482 const FileEntry *File
1483 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1484 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1485
1486 // If we didn't find the file, resolve it relative to the
1487 // original directory from which this AST file was created.
1488 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1489 F.OriginalDir != CurrentDir) {
1490 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1491 F.OriginalDir,
1492 CurrentDir);
1493 if (!Resolved.empty())
1494 File = FileMgr.getFile(Resolved);
1495 }
1496
1497 // For an overridden file, create a virtual file with the stored
1498 // size/timestamp.
1499 if (Overridden && File == 0) {
1500 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1501 }
1502
1503 if (File == 0) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001504 if (Complain) {
1505 std::string ErrorStr = "could not find file '";
1506 ErrorStr += Filename;
1507 ErrorStr += "' referenced by AST file";
1508 Error(ErrorStr.c_str());
1509 }
Douglas Gregora930dc92012-10-22 18:42:04 +00001510 return InputFile();
1511 }
1512
1513 // Note that we've loaded this input file.
1514 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1515
1516 // Check if there was a request to override the contents of the file
1517 // that was part of the precompiled header. Overridding such a file
1518 // can lead to problems when lexing using the source locations from the
1519 // PCH.
1520 SourceManager &SM = getSourceManager();
1521 if (!Overridden && SM.isFileOverridden(File)) {
1522 Error(diag::err_fe_pch_file_overridden, Filename);
1523 // After emitting the diagnostic, recover by disabling the override so
1524 // that the original file will be used.
1525 SM.disableFileContentsOverride(File);
1526 // The FileEntry is a virtual file entry with the size of the contents
1527 // that would override the original contents. Set it to the original's
1528 // size/time.
1529 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1530 StoredSize, StoredTime);
1531 }
1532
1533 // For an overridden file, there is nothing to validate.
1534 if (Overridden)
1535 return InputFile(File, Overridden);
1536
1537 // The stat info from the FileEntry came from the cached stat
1538 // info of the PCH, so we cannot trust it.
1539 struct stat StatBuf;
1540 if (::stat(File->getName(), &StatBuf) != 0) {
1541 StatBuf.st_size = File->getSize();
1542 StatBuf.st_mtime = File->getModificationTime();
1543 }
1544
1545 if ((StoredSize != StatBuf.st_size
1546#if !defined(LLVM_ON_WIN32)
1547 // In our regression testing, the Windows file system seems to
1548 // have inconsistent modification times that sometimes
1549 // erroneously trigger this error-handling path.
1550 || StoredTime != StatBuf.st_mtime
1551#endif
1552 )) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001553 if (Complain)
1554 Error(diag::err_fe_pch_file_modified, Filename);
1555
Douglas Gregora930dc92012-10-22 18:42:04 +00001556 return InputFile();
1557 }
1558
1559 return InputFile(File, Overridden);
1560 }
1561 }
1562
1563 return InputFile();
1564}
1565
Chris Lattner5f9e2722011-07-23 10:55:15 +00001566const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor69e16082012-10-18 21:47:16 +00001567 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001568 std::string Filename = filenameStrRef;
Douglas Gregor69e16082012-10-18 21:47:16 +00001569 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001570 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor69e16082012-10-18 21:47:16 +00001571 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1572 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001573 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor69e16082012-10-18 21:47:16 +00001574 M.OriginalDir,
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001575 CurrentDir);
1576 if (!resolved.empty())
1577 File = FileMgr.getFile(resolved);
1578 }
1579
1580 return File;
1581}
1582
Douglas Gregore650c8c2009-07-07 00:12:59 +00001583/// \brief If we are loading a relocatable PCH file, and the filename is
1584/// not an absolute path, add the system root to the beginning of the file
1585/// name.
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001586void ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1587 std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001588 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregorcaed0602012-10-18 21:31:35 +00001589 if (!M.RelocatablePCH)
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001590 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Michael J. Spencer256053b2010-12-17 21:22:22 +00001592 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001593 return;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001594
Douglas Gregor832d6202011-07-22 16:35:34 +00001595 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001596 // If no system root was given, default to '/'
1597 Filename.insert(Filename.begin(), '/');
Rafael Espindola848bc3a2012-10-30 00:38:13 +00001598 return;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Douglas Gregor832d6202011-07-22 16:35:34 +00001601 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001602 if (isysroot[Length - 1] != '/')
1603 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Douglas Gregor832d6202011-07-22 16:35:34 +00001605 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001606}
1607
Douglas Gregor38295be2012-10-22 23:51:00 +00001608ASTReader::ASTReadResult
1609ASTReader::ReadControlBlock(ModuleFile &F,
1610 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
1611 unsigned ClientLoadCapabilities) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001612 llvm::BitstreamCursor &Stream = F.Stream;
1613
1614 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1615 Error("malformed block record in AST file");
1616 return Failure;
1617 }
1618
1619 // Read all of the records and blocks in the control block.
1620 RecordData Record;
1621 while (!Stream.AtEndOfStream()) {
1622 unsigned Code = Stream.ReadCode();
1623 if (Code == llvm::bitc::END_BLOCK) {
1624 if (Stream.ReadBlockEnd()) {
1625 Error("error at end of control block in AST file");
1626 return Failure;
1627 }
1628
Douglas Gregora930dc92012-10-22 18:42:04 +00001629 // Validate all of the input files.
1630 if (!DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001631 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregora930dc92012-10-22 18:42:04 +00001632 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor38295be2012-10-22 23:51:00 +00001633 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001634 return OutOfDate;
Douglas Gregora930dc92012-10-22 18:42:04 +00001635 }
1636
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001637 return Success;
1638 }
1639
1640 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001641 switch (Stream.ReadSubBlockID()) {
1642 case INPUT_FILES_BLOCK_ID:
Douglas Gregora930dc92012-10-22 18:42:04 +00001643 F.InputFilesCursor = Stream;
1644 if (Stream.SkipBlock() || // Skip with the main cursor
1645 // Read the abbreviations
1646 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1647 Error("malformed block record in AST file");
Douglas Gregor745e6f12012-10-19 00:38:02 +00001648 return Failure;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001649 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001650 continue;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001651
1652 default:
1653 if (!Stream.SkipBlock())
1654 continue;
1655 break;
1656 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001657
1658 Error("malformed block record in AST file");
1659 return Failure;
1660 }
1661
1662 if (Code == llvm::bitc::DEFINE_ABBREV) {
1663 Stream.ReadAbbrevRecord();
1664 continue;
1665 }
1666
1667 // Read and process a record.
1668 Record.clear();
1669 const char *BlobStart = 0;
1670 unsigned BlobLen = 0;
1671 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1672 &BlobStart, &BlobLen)) {
1673 case METADATA: {
1674 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001675 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1676 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1677 : diag::warn_pch_version_too_new);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001678 return VersionMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001679 }
1680
1681 bool hasErrors = Record[5];
1682 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1683 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001684 return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001685 }
1686
Douglas Gregorcaed0602012-10-18 21:31:35 +00001687 F.RelocatablePCH = Record[4];
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001688
1689 const std::string &CurBranch = getClangFullRepositoryVersion();
1690 StringRef ASTBranch(BlobStart, BlobLen);
1691 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001692 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1693 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor4825fd72012-10-22 22:50:17 +00001694 return VersionMismatch;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001695 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001696 break;
1697 }
1698
1699 case IMPORTS: {
1700 // Load each of the imported PCH files.
1701 unsigned Idx = 0, N = Record.size();
1702 while (Idx < N) {
1703 // Read information about the AST file.
1704 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1705 unsigned Length = Record[Idx++];
1706 SmallString<128> ImportedFile(Record.begin() + Idx,
1707 Record.begin() + Idx + Length);
1708 Idx += Length;
1709
1710 // Load the AST file.
Douglas Gregor38295be2012-10-22 23:51:00 +00001711 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
1712 ClientLoadCapabilities)) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001713 case Failure: return Failure;
1714 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001715 case OutOfDate: return OutOfDate;
1716 case VersionMismatch: return VersionMismatch;
1717 case ConfigurationMismatch: return ConfigurationMismatch;
1718 case HadErrors: return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001719 case Success: break;
1720 }
1721 }
1722 break;
1723 }
1724
Douglas Gregor38295be2012-10-22 23:51:00 +00001725 case LANGUAGE_OPTIONS: {
1726 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1727 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001728 ParseLanguageOptions(Record, Complain, *Listener) &&
1729 !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001730 return ConfigurationMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001731 break;
Douglas Gregor38295be2012-10-22 23:51:00 +00001732 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001733
Douglas Gregoree097c12012-10-18 17:58:09 +00001734 case TARGET_OPTIONS: {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00001735 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1736 if (Listener && &F == *ModuleMgr.begin() &&
1737 ParseTargetOptions(Record, Complain, *Listener) &&
1738 !DisableValidation)
1739 return ConfigurationMismatch;
Douglas Gregoree097c12012-10-18 17:58:09 +00001740 break;
1741 }
1742
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001743 case DIAGNOSTIC_OPTIONS: {
1744 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1745 if (Listener && &F == *ModuleMgr.begin() &&
1746 ParseDiagnosticOptions(Record, Complain, *Listener) &&
1747 !DisableValidation)
1748 return ConfigurationMismatch;
1749 break;
1750 }
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001751
1752 case FILE_SYSTEM_OPTIONS: {
1753 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1754 if (Listener && &F == *ModuleMgr.begin() &&
1755 ParseFileSystemOptions(Record, Complain, *Listener) &&
1756 !DisableValidation)
1757 return ConfigurationMismatch;
1758 break;
1759 }
1760
Douglas Gregorbbf38312012-10-24 16:50:34 +00001761 case HEADER_SEARCH_OPTIONS: {
1762 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1763 if (Listener && &F == *ModuleMgr.begin() &&
1764 ParseHeaderSearchOptions(Record, Complain, *Listener) &&
1765 !DisableValidation)
1766 return ConfigurationMismatch;
1767 break;
1768 }
1769
Douglas Gregora71a7d82012-10-24 20:05:57 +00001770 case PREPROCESSOR_OPTIONS: {
1771 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1772 if (Listener && &F == *ModuleMgr.begin() &&
Douglas Gregor87699242012-10-25 00:07:54 +00001773 ParsePreprocessorOptions(Record, Complain, *Listener,
1774 SuggestedPredefines) &&
Douglas Gregora71a7d82012-10-24 20:05:57 +00001775 !DisableValidation)
1776 return ConfigurationMismatch;
1777 break;
1778 }
1779
Douglas Gregor39c497b2012-10-18 18:36:53 +00001780 case ORIGINAL_FILE:
Douglas Gregor69e16082012-10-18 21:47:16 +00001781 F.OriginalSourceFileID = FileID::get(Record[0]);
1782 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
1783 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
1784 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001785 break;
1786
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001787 case ORIGINAL_PCH_DIR:
Douglas Gregor69e16082012-10-18 21:47:16 +00001788 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001789 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001790
Douglas Gregora930dc92012-10-22 18:42:04 +00001791 case INPUT_FILE_OFFSETS:
1792 F.InputFileOffsets = (const uint32_t *)BlobStart;
1793 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001794 break;
1795 }
Douglas Gregor745e6f12012-10-19 00:38:02 +00001796 }
1797
1798 Error("premature end of bitstream in AST file");
1799 return Failure;
1800}
1801
Douglas Gregor4825fd72012-10-22 22:50:17 +00001802bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001803 llvm::BitstreamCursor &Stream = F.Stream;
1804
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001805 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001806 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001807 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001808 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001809
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001810 // Read all of the records and blocks for the AST file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001811 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001812 while (!Stream.AtEndOfStream()) {
1813 unsigned Code = Stream.ReadCode();
1814 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001815 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001816 Error("error at end of module block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001817 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001818 }
Chris Lattner7356a312009-04-11 21:15:38 +00001819
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001820 DeclContext *DC = Context.getTranslationUnitDecl();
1821 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1822 DC->setMustBuildLookupTable();
1823
Douglas Gregor4825fd72012-10-22 22:50:17 +00001824 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001825 }
1826
1827 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1828 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001829 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001830 // We lazily load the decls block, but we want to set up the
1831 // DeclsCursor cursor to point into it. Clone our current bitcode
1832 // cursor to it, enter the block and read the abbrevs in that block.
1833 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001834 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001835 if (Stream.SkipBlock() || // Skip with the main cursor.
1836 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001837 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001838 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001839 return true;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001840 }
1841 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001843 case DECL_UPDATES_BLOCK_ID:
1844 if (Stream.SkipBlock()) {
1845 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001846 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001847 }
1848 break;
1849
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001850 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001851 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001852 if (!PP.getExternalSource())
1853 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001854
Douglas Gregorecdcb882010-10-20 22:00:55 +00001855 if (Stream.SkipBlock() ||
1856 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001857 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001858 return true;
Chris Lattner7356a312009-04-11 21:15:38 +00001859 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001860 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001861 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001862
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001863 case PREPROCESSOR_DETAIL_BLOCK_ID:
1864 F.PreprocessorDetailCursor = Stream;
1865 if (Stream.SkipBlock() ||
1866 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1867 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1868 Error("malformed preprocessor detail record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001869 return true;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001870 }
1871 F.PreprocessorDetailStartOffset
1872 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001873
1874 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001875 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001876 if (!PP.getPreprocessingRecord()->getExternalSource())
1877 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001878 break;
1879
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001880 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00001881 if (ReadSourceManagerBlock(F))
1882 return true;
Douglas Gregor14f79002009-04-10 03:52:48 +00001883 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001884
1885 case SUBMODULE_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00001886 if (ReadSubmoduleBlock(F))
1887 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001888 break;
1889
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001890 case COMMENTS_BLOCK_ID: {
1891 llvm::BitstreamCursor C = Stream;
1892 if (Stream.SkipBlock() ||
1893 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1894 Error("malformed comments block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001895 return true;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001896 }
1897 CommentsCursors.push_back(std::make_pair(C, &F));
1898 break;
1899 }
1900
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001901 default:
1902 if (!Stream.SkipBlock())
1903 break;
1904 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001905 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001906 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001907 continue;
1908 }
1909
1910 if (Code == llvm::bitc::DEFINE_ABBREV) {
1911 Stream.ReadAbbrevRecord();
1912 continue;
1913 }
1914
1915 // Read and process a record.
1916 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001917 const char *BlobStart = 0;
1918 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001919 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001920 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001921 default: // Default behavior: ignore.
1922 break;
1923
Douglas Gregora119da02011-08-02 16:26:37 +00001924 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001925 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001926 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001927 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001928 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001929 F.TypeOffsets = (const uint32_t *)BlobStart;
1930 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001931 unsigned LocalBaseTypeIndex = Record[1];
1932 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001933
Douglas Gregora119da02011-08-02 16:26:37 +00001934 if (F.LocalNumTypes > 0) {
1935 // Introduce the global -> local mapping for types within this module.
1936 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1937
1938 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001939 F.TypeRemap.insertOrReplace(
1940 std::make_pair(LocalBaseTypeIndex,
1941 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001942
1943 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1944 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001945 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001946 }
1947
Douglas Gregor496c7092011-08-03 15:48:04 +00001948 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001949 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001950 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001951 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001952 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001953 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001954 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001955 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001956 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001957
Douglas Gregor496c7092011-08-03 15:48:04 +00001958 if (F.LocalNumDecls > 0) {
1959 // Introduce the global -> local mapping for declarations within this
1960 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001961 GlobalDeclMap.insert(
1962 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001963
1964 // Introduce the local -> global mapping for declarations within this
1965 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001966 F.DeclRemap.insertOrReplace(
1967 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001968
Douglas Gregora1be2782011-12-17 23:38:30 +00001969 // Introduce the global -> local mapping for declarations within this
1970 // module.
1971 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1972
Douglas Gregor496c7092011-08-03 15:48:04 +00001973 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1974 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001975 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001976 }
1977
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001978 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001979 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001980 DeclContextInfo &Info = F.DeclContextInfos[TU];
1981 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1982 Info.NumLexicalDecls
1983 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001984 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001985 break;
1986 }
1987
Sebastian Redle1dde812010-08-24 00:50:04 +00001988 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001989 unsigned Idx = 0;
1990 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001991 ASTDeclContextNameLookupTable *Table =
1992 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001993 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001994 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001995 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001996 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1997 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001998 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001999 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00002000 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00002001 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00002002 break;
2003 }
2004
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002005 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002006 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002007 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002008 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002009 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002010 (const unsigned char *)F.IdentifierTableData + Record[0],
2011 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002012 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002013
2014 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002015 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002016 break;
2017
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002018 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002019 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002020 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002021 return true;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002022 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002023 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2024 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002025 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002026 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00002027
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002028 if (F.LocalNumIdentifiers > 0) {
2029 // Introduce the global -> local mapping for identifiers within this
2030 // module.
2031 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2032 &F));
2033
2034 // Introduce the local -> global mapping for identifiers within this
2035 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002036 F.IdentifierRemap.insertOrReplace(
2037 std::make_pair(LocalBaseIdentifierID,
2038 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002039
2040 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2041 + F.LocalNumIdentifiers);
2042 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002043 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002044 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002045
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002046 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002047 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2048 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002049 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002051 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002052 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2053 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002054 break;
2055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002056 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002057 TotalNumStatements += Record[0];
2058 TotalNumMacros += Record[1];
2059 TotalLexicalDeclContexts += Record[2];
2060 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002061 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002062
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002063 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002064 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2065 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002066 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002067
Sean Huntebcbe1d2011-05-04 23:29:54 +00002068 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002069 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2070 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002071 break;
2072
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002073 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002074 if (Record.size() % 4 != 0) {
2075 Error("invalid weak identifiers record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002076 return true;
Douglas Gregor31e37b22011-07-28 18:09:57 +00002077 }
2078
2079 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2080 // files. This isn't the way to do it :)
2081 WeakUndeclaredIdentifiers.clear();
2082
2083 // Translate the weak, undeclared identifiers into global IDs.
2084 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2085 WeakUndeclaredIdentifiers.push_back(
2086 getGlobalIdentifierID(F, Record[I++]));
2087 WeakUndeclaredIdentifiers.push_back(
2088 getGlobalIdentifierID(F, Record[I++]));
2089 WeakUndeclaredIdentifiers.push_back(
2090 ReadSourceLocation(F, Record, I).getRawEncoding());
2091 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2092 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002093 break;
2094
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002095 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002096 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2097 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002098 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002099
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002100 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002101 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002102 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002103 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002104 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002105
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002106 if (F.LocalNumSelectors > 0) {
2107 // Introduce the global -> local mapping for selectors within this
2108 // module.
2109 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2110
2111 // Introduce the local -> global mapping for selectors within this
2112 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002113 F.SelectorRemap.insertOrReplace(
2114 std::make_pair(LocalBaseSelectorID,
2115 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002116
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002117 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2118 }
2119 break;
2120 }
2121
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002122 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002123 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002124 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002125 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002126 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002127 F.SelectorLookupTableData + Record[0],
2128 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002129 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002130 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002131 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002132
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002133 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002134 if (!Record.empty()) {
2135 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2136 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2137 Record[Idx++]));
2138 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2139 getRawEncoding());
2140 }
2141 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002142 break;
2143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002144 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002145 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002146 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002147 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002148
2149 case FILE_SORTED_DECLS:
2150 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002151 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002152 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002153
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002154 case SOURCE_LOCATION_OFFSETS: {
2155 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002156 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002157 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002158 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002159 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2160 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002161 // Make our entry in the range map. BaseID is negative and growing, so
2162 // we invert it. Because we invert it, though, we need the other end of
2163 // the range.
2164 unsigned RangeStart =
2165 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2166 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2167 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2168
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002169 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2170 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2171 GlobalSLocOffsetMap.insert(
2172 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2173 - SLocSpaceSize,&F));
2174
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002175 // Initialize the remapping table.
2176 // Invalid stays invalid.
2177 F.SLocRemap.insert(std::make_pair(0U, 0));
2178 // This module. Base was 2 when being compiled.
2179 F.SLocRemap.insert(std::make_pair(2U,
2180 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002181
2182 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002183 break;
2184 }
2185
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002186 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002187 // Additional remapping information.
2188 const unsigned char *Data = (const unsigned char*)BlobStart;
2189 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002190
2191 // Continuous range maps we may be updating in our module.
2192 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002193 ContinuousRangeMap<uint32_t, int, 2>::Builder
2194 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002195 ContinuousRangeMap<uint32_t, int, 2>::Builder
2196 MacroRemap(F.MacroRemap);
2197 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002198 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2199 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002200 SubmoduleRemap(F.SubmoduleRemap);
2201 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002202 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002203 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002204 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2205
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002206 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002207 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002208 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002209 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002210 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002211 if (!OM) {
2212 Error("SourceLocation remap refers to unknown module");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002213 return true;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002214 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002215
2216 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2217 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002218 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002219 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002220 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002221 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2222 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002223 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002224
2225 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2226 SLocRemap.insert(std::make_pair(SLocOffset,
2227 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002228 IdentifierRemap.insert(
2229 std::make_pair(IdentifierIDOffset,
2230 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002231 MacroRemap.insert(std::make_pair(MacroIDOffset,
2232 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002233 PreprocessedEntityRemap.insert(
2234 std::make_pair(PreprocessedEntityIDOffset,
2235 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002236 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2237 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002238 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2239 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002240 DeclRemap.insert(std::make_pair(DeclIDOffset,
2241 OM->BaseDeclID - DeclIDOffset));
2242
Douglas Gregora119da02011-08-02 16:26:37 +00002243 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002244 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002245
2246 // Global -> local mappings.
2247 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002248 }
2249 break;
2250 }
2251
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002252 case SOURCE_MANAGER_LINE_TABLE:
2253 if (ParseLineTable(F, Record))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002254 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002255 break;
2256
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002257 case SOURCE_LOCATION_PRELOADS: {
2258 // Need to transform from the local view (1-based IDs) to the global view,
2259 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002260 if (!F.PreloadSLocEntries.empty()) {
2261 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002262 return true;
Douglas Gregorf249bf32011-08-25 21:09:44 +00002263 }
2264
2265 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002266 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002267 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002269 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002270 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2271 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002272 break;
2273
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002274 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002275 if (Record.size() % 3 != 0) {
2276 Error("Invalid VTABLE_USES record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002277 return true;
Douglas Gregordfe65432011-07-28 19:11:31 +00002278 }
2279
Sebastian Redl40566802010-08-05 18:21:25 +00002280 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002281 // FIXME: Modules will have some trouble with this. This is clearly not
2282 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002283 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002284
2285 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2286 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2287 VTableUses.push_back(
2288 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2289 VTableUses.push_back(Record[Idx++]);
2290 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002291 break;
2292
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002293 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002294 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2295 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002296 break;
2297
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002298 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002299 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002300 Error("Invalid existing PendingInstantiations");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002301 return true;
Axel Naumann39d26c32012-10-02 09:09:43 +00002302 }
2303
2304 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002305 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002306 return true;
Douglas Gregorf2abb522011-07-28 19:26:52 +00002307 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002308
Douglas Gregorf2abb522011-07-28 19:26:52 +00002309 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2310 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2311 PendingInstantiations.push_back(
2312 ReadSourceLocation(F, Record, I).getRawEncoding());
2313 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002314 break;
2315
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002316 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002317 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002318 // FIXME: Modules will have some trouble with this.
2319 SemaDeclRefs.clear();
2320 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2321 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002322 break;
2323
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002324 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002325 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2326 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2327 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002328
2329 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002330
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002331 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002332 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002333 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002334 if (!PP.getPreprocessingRecord()->getExternalSource())
2335 PP.getPreprocessingRecord()->SetExternalSource(*this);
2336 StartingID
2337 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002338 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002339 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002340
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002341 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002342 // Introduce the global -> local mapping for preprocessed entities in
2343 // this module.
2344 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2345
2346 // Introduce the local -> global mapping for preprocessed entities in
2347 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002348 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002349 std::make_pair(LocalBasePreprocessedEntityID,
2350 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2351 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002352
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002353 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002354 }
2355
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002356 case DECL_UPDATE_OFFSETS: {
2357 if (Record.size() % 2 != 0) {
2358 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002359 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002360 }
2361 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002362 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2363 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002364 break;
2365 }
2366
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002367 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002368 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002369 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002370 return true;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002371 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002372 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002373 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002374 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002375 break;
2376 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002377
Douglas Gregorcff9f262012-01-27 01:47:08 +00002378 case OBJC_CATEGORIES_MAP: {
2379 if (F.LocalNumObjCCategoriesInMap != 0) {
2380 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002381 return true;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002382 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002383
2384 F.LocalNumObjCCategoriesInMap = Record[0];
2385 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002386 break;
2387 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002388
Douglas Gregorcff9f262012-01-27 01:47:08 +00002389 case OBJC_CATEGORIES:
2390 F.ObjCCategories.swap(Record);
2391 break;
2392
Douglas Gregor7c789c12010-10-29 22:39:52 +00002393 case CXX_BASE_SPECIFIER_OFFSETS: {
2394 if (F.LocalNumCXXBaseSpecifiers != 0) {
2395 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002396 return true;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002397 }
2398
2399 F.LocalNumCXXBaseSpecifiers = Record[0];
2400 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002401 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002402 break;
2403 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002404
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002405 case DIAG_PRAGMA_MAPPINGS:
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002406 if (F.PragmaDiagMappings.empty())
2407 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002408 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002409 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2410 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002411 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002412
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002413 case CUDA_SPECIAL_DECL_REFS:
2414 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002415 // FIXME: Modules will have trouble with this.
2416 CUDASpecialDeclRefs.clear();
2417 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2418 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002419 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002420
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002421 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002422 F.HeaderFileInfoTableData = BlobStart;
2423 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002424 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002425 if (Record[0]) {
2426 F.HeaderFileInfoTable
2427 = HeaderFileInfoLookupTable::Create(
2428 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002429 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002430 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002431 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002432 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002433
2434 PP.getHeaderSearchInfo().SetExternalSource(this);
2435 if (!PP.getHeaderSearchInfo().getExternalLookup())
2436 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002437 }
2438 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002439 }
2440
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002441 case FP_PRAGMA_OPTIONS:
2442 // Later tables overwrite earlier ones.
2443 FPPragmaOptions.swap(Record);
2444 break;
2445
2446 case OPENCL_EXTENSIONS:
2447 // Later tables overwrite earlier ones.
2448 OpenCLExtensions.swap(Record);
2449 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002450
2451 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002452 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2453 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002454 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002455
2456 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002457 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2458 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002459 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002460
2461 case IMPORTED_MODULES: {
2462 if (F.Kind != MK_Module) {
2463 // If we aren't loading a module (which has its own exports), make
2464 // all of the imported modules visible.
2465 // FIXME: Deal with macros-only imports.
2466 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2467 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2468 ImportedModules.push_back(GlobalID);
2469 }
2470 }
2471 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002472 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002473
Douglas Gregora1be2782011-12-17 23:38:30 +00002474 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002475 F.RedeclarationChains.swap(Record);
2476 break;
2477 }
2478
2479 case LOCAL_REDECLARATIONS_MAP: {
2480 if (F.LocalNumRedeclarationsInMap != 0) {
2481 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002482 return true;
Douglas Gregora1be2782011-12-17 23:38:30 +00002483 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002484
Douglas Gregor2171bf12012-01-15 16:58:34 +00002485 F.LocalNumRedeclarationsInMap = Record[0];
2486 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002487 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002488 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002489
2490 case MERGED_DECLARATIONS: {
2491 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2492 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2493 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2494 for (unsigned N = Record[Idx++]; N > 0; --N)
2495 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2496 }
2497 break;
2498 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002499
2500 case MACRO_OFFSET: {
2501 if (F.LocalNumMacros != 0) {
2502 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002503 return true;
Douglas Gregora8235d62012-10-09 23:05:51 +00002504 }
2505 F.MacroOffsets = (const uint32_t *)BlobStart;
2506 F.LocalNumMacros = Record[0];
2507 unsigned LocalBaseMacroID = Record[1];
2508 F.BaseMacroID = getTotalNumMacros();
2509
2510 if (F.LocalNumMacros > 0) {
2511 // Introduce the global -> local mapping for macros within this module.
2512 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2513
2514 // Introduce the local -> global mapping for macros within this module.
2515 F.MacroRemap.insertOrReplace(
2516 std::make_pair(LocalBaseMacroID,
2517 F.BaseMacroID - LocalBaseMacroID));
2518
2519 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2520 }
2521 break;
2522 }
2523
2524 case MACRO_UPDATES: {
2525 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2526 MacroID ID = getGlobalMacroID(F, Record[I++]);
2527 if (I == N)
2528 break;
2529
Douglas Gregor54c8a402012-10-12 00:16:50 +00002530 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2531 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2532 MacroUpdate Update;
2533 Update.UndefLoc = UndefLoc;
2534 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002535 }
2536 break;
2537 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002538 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002539 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002540 Error("premature end of bitstream in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002541 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002542}
2543
Douglas Gregorecc2c092011-12-01 22:20:10 +00002544void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002545 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002546 switch (Names[I].getKind()) {
2547 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002548 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002549 break;
2550
2551 case HiddenName::MacroVisibility: {
2552 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2553 Macro.second->setHidden(!Macro.second->isPublic());
2554 if (Macro.second->isDefined()) {
2555 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2556 }
2557 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002558 }
2559
Douglas Gregor54c8a402012-10-12 00:16:50 +00002560 case HiddenName::MacroUndef: {
2561 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2562 if (Macro.second->isDefined()) {
2563 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2564 if (PPMutationListener *Listener = PP.getPPMutationListener())
2565 Listener->UndefinedMacro(Macro.second);
2566 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2567 }
2568 break;
2569 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002570 }
Douglas Gregor13292642011-12-02 15:45:10 +00002571 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002572}
2573
Douglas Gregor5e356932011-12-01 17:11:21 +00002574void ASTReader::makeModuleVisible(Module *Mod,
2575 Module::NameVisibilityKind NameVisibility) {
2576 llvm::SmallPtrSet<Module *, 4> Visited;
2577 llvm::SmallVector<Module *, 4> Stack;
2578 Stack.push_back(Mod);
2579 while (!Stack.empty()) {
2580 Mod = Stack.back();
2581 Stack.pop_back();
2582
2583 if (NameVisibility <= Mod->NameVisibility) {
2584 // This module already has this level of visibility (or greater), so
2585 // there is nothing more to do.
2586 continue;
2587 }
2588
Douglas Gregor51f564f2011-12-31 04:05:44 +00002589 if (!Mod->isAvailable()) {
2590 // Modules that aren't available cannot be made visible.
2591 continue;
2592 }
2593
Douglas Gregor5e356932011-12-01 17:11:21 +00002594 // Update the module's name visibility.
2595 Mod->NameVisibility = NameVisibility;
2596
Douglas Gregorecc2c092011-12-01 22:20:10 +00002597 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002598 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002599 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2600 if (Hidden != HiddenNamesMap.end()) {
2601 makeNamesVisible(Hidden->second);
2602 HiddenNamesMap.erase(Hidden);
2603 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002604
2605 // Push any non-explicit submodules onto the stack to be marked as
2606 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002607 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2608 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002609 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002610 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2611 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002612 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002613
2614 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002615 bool AnyWildcard = false;
2616 bool UnrestrictedWildcard = false;
2617 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002618 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2619 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002620 if (!Mod->Exports[I].getInt()) {
2621 // Export a named module directly; no wildcards involved.
2622 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002623 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002624
2625 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002626 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002627
2628 // Wildcard export: export all of the imported modules that match
2629 // the given pattern.
2630 AnyWildcard = true;
2631 if (UnrestrictedWildcard)
2632 continue;
2633
2634 if (Module *Restriction = Mod->Exports[I].getPointer())
2635 WildcardRestrictions.push_back(Restriction);
2636 else {
2637 WildcardRestrictions.clear();
2638 UnrestrictedWildcard = true;
2639 }
2640 }
2641
2642 // If there were any wildcards, push any imported modules that were
2643 // re-exported by the wildcard restriction.
2644 if (!AnyWildcard)
2645 continue;
2646
2647 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2648 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002649 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002650 continue;
2651
2652 bool Acceptable = UnrestrictedWildcard;
2653 if (!Acceptable) {
2654 // Check whether this module meets one of the restrictions.
2655 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2656 Module *Restriction = WildcardRestrictions[R];
2657 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2658 Acceptable = true;
2659 break;
2660 }
2661 }
2662 }
2663
2664 if (!Acceptable)
2665 continue;
2666
Douglas Gregor0adaa882011-12-05 17:28:06 +00002667 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002668 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002669 }
2670}
2671
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002672ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor38295be2012-10-22 23:51:00 +00002673 ModuleKind Type,
2674 unsigned ClientLoadCapabilities) {
Douglas Gregor057df202012-01-18 20:56:22 +00002675 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002676 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002677
2678 // Load the core of the AST files.
2679 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor38295be2012-10-22 23:51:00 +00002680 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded,
2681 ClientLoadCapabilities)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002682 case Failure: return Failure;
Douglas Gregor4825fd72012-10-22 22:50:17 +00002683 case OutOfDate: return OutOfDate;
2684 case VersionMismatch: return VersionMismatch;
2685 case ConfigurationMismatch: return ConfigurationMismatch;
2686 case HadErrors: return HadErrors;
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002687 case Success: break;
2688 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002689
2690 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002691
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002692 // Load the AST blocks of all of the modules that we loaded.
2693 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
2694 MEnd = Loaded.end();
2695 M != MEnd; ++M) {
2696 ModuleFile &F = **M;
2697
2698 // Read the AST block.
Douglas Gregor4825fd72012-10-22 22:50:17 +00002699 if (ReadASTBlock(F))
2700 return Failure;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002701
2702 // Once read, set the ModuleFile bit base offset and update the size in
2703 // bits of all files we've seen.
2704 F.GlobalBitOffset = TotalModulesSizeInBits;
2705 TotalModulesSizeInBits += F.SizeInBits;
2706 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2707
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002708 // Preload SLocEntries.
2709 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2710 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor8b53d142012-10-22 22:53:10 +00002711 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002712 // directly because the entry may have already been loaded in which case
Douglas Gregor8b53d142012-10-22 22:53:10 +00002713 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002714 // SourceManager.
2715 SourceMgr.getLoadedSLocEntryByID(Index);
2716 }
2717 }
2718
Douglas Gregoreee242f2011-10-27 09:33:13 +00002719 // Mark all of the identifiers in the identifier table as being out of date,
2720 // so that various accessors know to check the loaded modules when the
2721 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002722 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2723 IdEnd = PP.getIdentifierTable().end();
2724 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002725 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002726
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002727 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002728 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2729 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2730 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002731 Module *ResolvedMod = getSubmodule(GlobalID);
2732
2733 if (Unresolved.IsImport) {
2734 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002735 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002736 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002737 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002738
2739 if (ResolvedMod || Unresolved.IsWildcard)
2740 Unresolved.Mod->Exports.push_back(
2741 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002742 }
Douglas Gregor55988682011-12-05 16:33:54 +00002743 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002744
Douglas Gregor35942772011-09-09 21:34:22 +00002745 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002746
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002747 if (DeserializationListener)
2748 DeserializationListener->ReaderInitialized(this);
2749
Douglas Gregor11407b82012-10-18 21:18:25 +00002750 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2751 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
2752 PrimaryModule.OriginalSourceFileID
2753 = FileID::get(PrimaryModule.SLocEntryBaseID
2754 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002755
Douglas Gregor11407b82012-10-18 21:18:25 +00002756 // If this AST file is a precompiled preamble, then set the
2757 // preamble file ID of the source manager to the file source file
2758 // from which the preamble was built.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002759 if (Type == MK_Preamble) {
Douglas Gregor11407b82012-10-18 21:18:25 +00002760 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002761 } else if (Type == MK_MainFile) {
Douglas Gregor11407b82012-10-18 21:18:25 +00002762 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002763 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002764 }
2765
Douglas Gregorcff9f262012-01-27 01:47:08 +00002766 // For any Objective-C class definitions we have already loaded, make sure
2767 // that we load any additional categories.
2768 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2769 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2770 ObjCClassesLoaded[I],
2771 PreviousGeneration);
2772 }
2773
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002774 return Success;
2775}
2776
Douglas Gregor38295be2012-10-22 23:51:00 +00002777ASTReader::ASTReadResult
2778ASTReader::ReadASTCore(StringRef FileName,
2779 ModuleKind Type,
2780 ModuleFile *ImportedBy,
2781 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
2782 unsigned ClientLoadCapabilities) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002783 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002784 bool NewModule;
2785 std::string ErrorStr;
2786 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002787 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002788
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002789 if (!M) {
2790 // We couldn't load the module.
2791 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2792 + ErrorStr;
2793 Error(Msg);
2794 return Failure;
2795 }
2796
2797 if (!NewModule) {
2798 // We've already loaded this module.
2799 return Success;
2800 }
2801
2802 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2803 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002804 if (FileName != "-") {
2805 CurrentDir = llvm::sys::path::parent_path(FileName);
2806 if (CurrentDir.empty()) CurrentDir = ".";
2807 }
2808
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002809 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002810 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002811 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002812 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002813
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002814 // Sniff for the signature.
2815 if (Stream.Read(8) != 'C' ||
2816 Stream.Read(8) != 'P' ||
2817 Stream.Read(8) != 'C' ||
2818 Stream.Read(8) != 'H') {
2819 Diag(diag::err_not_a_pch_file) << FileName;
2820 return Failure;
2821 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002822
Douglas Gregor2cf26342009-04-09 22:27:44 +00002823 while (!Stream.AtEndOfStream()) {
2824 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Douglas Gregore1d918e2009-04-10 23:10:45 +00002826 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002827 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002828 return Failure;
2829 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002830
2831 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002832
Douglas Gregor7ae467f2012-10-18 18:27:37 +00002833 // We only know the control subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002834 switch (BlockID) {
2835 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002836 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002837 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002838 return Failure;
2839 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002840 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002841 case CONTROL_BLOCK_ID:
Douglas Gregor38295be2012-10-22 23:51:00 +00002842 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002843 case Success:
2844 break;
2845
Douglas Gregor4825fd72012-10-22 22:50:17 +00002846 case Failure: return Failure;
2847 case OutOfDate: return OutOfDate;
2848 case VersionMismatch: return VersionMismatch;
2849 case ConfigurationMismatch: return ConfigurationMismatch;
2850 case HadErrors: return HadErrors;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002851 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002852 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002853 case AST_BLOCK_ID:
2854 // Record that we've loaded this module.
2855 Loaded.push_back(M);
2856 return Success;
2857
Douglas Gregor2cf26342009-04-09 22:27:44 +00002858 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002859 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002860 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002861 return Failure;
2862 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002863 break;
2864 }
Mike Stump1eb44332009-09-09 15:08:12 +00002865 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002866
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002867 return Success;
2868}
2869
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002870void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002871 // If there's a listener, notify them that we "read" the translation unit.
2872 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002873 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2874 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002875
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002876 // Make sure we load the declaration update records for the translation unit,
2877 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002878 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2879 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002880
Douglas Gregor5f957282011-08-11 22:18:49 +00002881 // FIXME: Find a better way to deal with collisions between these
2882 // built-in types. Right now, we just ignore the problem.
2883
2884 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002885 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002886 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2887 if (!Context.CFConstantStringTypeDecl)
2888 Context.setCFConstantStringType(GetType(String));
2889 }
2890
2891 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2892 QualType FileType = GetType(File);
2893 if (FileType.isNull()) {
2894 Error("FILE type is NULL");
2895 return;
2896 }
2897
2898 if (!Context.FILEDecl) {
2899 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2900 Context.setFILEDecl(Typedef->getDecl());
2901 else {
2902 const TagType *Tag = FileType->getAs<TagType>();
2903 if (!Tag) {
2904 Error("Invalid FILE type in AST file");
2905 return;
2906 }
2907 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002908 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002909 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002910 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002911
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002912 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002913 QualType Jmp_bufType = GetType(Jmp_buf);
2914 if (Jmp_bufType.isNull()) {
2915 Error("jmp_buf type is NULL");
2916 return;
2917 }
2918
2919 if (!Context.jmp_bufDecl) {
2920 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2921 Context.setjmp_bufDecl(Typedef->getDecl());
2922 else {
2923 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2924 if (!Tag) {
2925 Error("Invalid jmp_buf type in AST file");
2926 return;
2927 }
2928 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002929 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002930 }
Mike Stump782fa302009-07-28 02:25:19 +00002931 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002932
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002933 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002934 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2935 if (Sigjmp_bufType.isNull()) {
2936 Error("sigjmp_buf type is NULL");
2937 return;
2938 }
2939
2940 if (!Context.sigjmp_bufDecl) {
2941 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2942 Context.setsigjmp_bufDecl(Typedef->getDecl());
2943 else {
2944 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2945 assert(Tag && "Invalid sigjmp_buf type in AST file");
2946 Context.setsigjmp_bufDecl(Tag->getDecl());
2947 }
2948 }
2949 }
2950
2951 if (unsigned ObjCIdRedef
2952 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2953 if (Context.ObjCIdRedefinitionType.isNull())
2954 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2955 }
2956
2957 if (unsigned ObjCClassRedef
2958 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2959 if (Context.ObjCClassRedefinitionType.isNull())
2960 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2961 }
2962
2963 if (unsigned ObjCSelRedef
2964 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2965 if (Context.ObjCSelRedefinitionType.isNull())
2966 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2967 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002968
2969 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2970 QualType Ucontext_tType = GetType(Ucontext_t);
2971 if (Ucontext_tType.isNull()) {
2972 Error("ucontext_t type is NULL");
2973 return;
2974 }
2975
2976 if (!Context.ucontext_tDecl) {
2977 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2978 Context.setucontext_tDecl(Typedef->getDecl());
2979 else {
2980 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2981 assert(Tag && "Invalid ucontext_t type in AST file");
2982 Context.setucontext_tDecl(Tag->getDecl());
2983 }
2984 }
2985 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002986 }
2987
Douglas Gregor35942772011-09-09 21:34:22 +00002988 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002989
2990 // If there were any CUDA special declarations, deserialize them.
2991 if (!CUDASpecialDeclRefs.empty()) {
2992 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002993 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002994 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2995 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002996
2997 // Re-export any modules that were imported by a non-module AST file.
2998 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
2999 if (Module *Imported = getSubmodule(ImportedModules[I]))
3000 makeModuleVisible(Imported, Module::AllVisible);
3001 }
3002 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003003}
3004
Douglas Gregorecc2c092011-12-01 22:20:10 +00003005void ASTReader::finalizeForWriting() {
3006 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3007 HiddenEnd = HiddenNamesMap.end();
3008 Hidden != HiddenEnd; ++Hidden) {
3009 makeNamesVisible(Hidden->second);
3010 }
3011 HiddenNamesMap.clear();
3012}
3013
Douglas Gregorb64c1932009-05-12 01:31:05 +00003014/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003015/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003016/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003017std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003018 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003019 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003020 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003021 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003022 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003023 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003024 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003025 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003026 return std::string();
3027 }
3028
3029 // Initialize the stream
3030 llvm::BitstreamReader StreamFile;
3031 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003032 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003033 (const unsigned char *)Buffer->getBufferEnd());
3034 Stream.init(StreamFile);
3035
3036 // Sniff for the signature.
3037 if (Stream.Read(8) != 'C' ||
3038 Stream.Read(8) != 'P' ||
3039 Stream.Read(8) != 'C' ||
3040 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003041 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003042 return std::string();
3043 }
3044
3045 RecordData Record;
3046 while (!Stream.AtEndOfStream()) {
3047 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003048
Douglas Gregorb64c1932009-05-12 01:31:05 +00003049 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3050 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003051
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003052 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003053 switch (BlockID) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003054 case CONTROL_BLOCK_ID:
3055 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003056 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003057 return std::string();
3058 }
3059 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003060
Douglas Gregorb64c1932009-05-12 01:31:05 +00003061 default:
3062 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003063 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003064 return std::string();
3065 }
3066 break;
3067 }
3068 continue;
3069 }
3070
3071 if (Code == llvm::bitc::END_BLOCK) {
3072 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003073 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003074 return std::string();
3075 }
3076 continue;
3077 }
3078
3079 if (Code == llvm::bitc::DEFINE_ABBREV) {
3080 Stream.ReadAbbrevRecord();
3081 continue;
3082 }
3083
3084 Record.clear();
3085 const char *BlobStart = 0;
3086 unsigned BlobLen = 0;
Douglas Gregor39c497b2012-10-18 18:36:53 +00003087 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003088 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003089 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003090
3091 return std::string();
3092}
3093
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003094namespace {
3095 class SimplePCHValidator : public ASTReaderListener {
3096 const LangOptions &ExistingLangOpts;
3097 const TargetOptions &ExistingTargetOpts;
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003098 const PreprocessorOptions &ExistingPPOpts;
Douglas Gregor87699242012-10-25 00:07:54 +00003099 FileManager &FileMgr;
3100
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003101 public:
3102 SimplePCHValidator(const LangOptions &ExistingLangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003103 const TargetOptions &ExistingTargetOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003104 const PreprocessorOptions &ExistingPPOpts,
3105 FileManager &FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003106 : ExistingLangOpts(ExistingLangOpts),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003107 ExistingTargetOpts(ExistingTargetOpts),
Douglas Gregor87699242012-10-25 00:07:54 +00003108 ExistingPPOpts(ExistingPPOpts),
3109 FileMgr(FileMgr)
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003110 {
3111 }
3112
3113 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
3114 bool Complain) {
3115 return checkLanguageOptions(ExistingLangOpts, LangOpts, 0);
3116 }
3117 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
3118 bool Complain) {
3119 return checkTargetOptions(ExistingTargetOpts, TargetOpts, 0);
3120 }
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003121 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
Douglas Gregor87699242012-10-25 00:07:54 +00003122 bool Complain,
3123 std::string &SuggestedPredefines) {
3124 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, 0, FileMgr,
3125 SuggestedPredefines);
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003126 }
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003127 };
3128}
3129
3130bool ASTReader::isAcceptableASTFile(StringRef Filename,
3131 FileManager &FileMgr,
3132 const LangOptions &LangOpts,
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003133 const TargetOptions &TargetOpts,
3134 const PreprocessorOptions &PPOpts) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003135 // Open the AST file.
3136 std::string ErrStr;
3137 OwningPtr<llvm::MemoryBuffer> Buffer;
3138 Buffer.reset(FileMgr.getBufferForFile(Filename, &ErrStr));
3139 if (!Buffer) {
3140 return false;
3141 }
3142
3143 // Initialize the stream
3144 llvm::BitstreamReader StreamFile;
3145 llvm::BitstreamCursor Stream;
3146 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
3147 (const unsigned char *)Buffer->getBufferEnd());
3148 Stream.init(StreamFile);
3149
3150 // Sniff for the signature.
3151 if (Stream.Read(8) != 'C' ||
3152 Stream.Read(8) != 'P' ||
3153 Stream.Read(8) != 'C' ||
3154 Stream.Read(8) != 'H') {
3155 return false;
3156 }
3157
Douglas Gregor87699242012-10-25 00:07:54 +00003158 SimplePCHValidator Validator(LangOpts, TargetOpts, PPOpts, FileMgr);
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003159 RecordData Record;
3160 bool InControlBlock = false;
3161 while (!Stream.AtEndOfStream()) {
3162 unsigned Code = Stream.ReadCode();
3163
3164 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3165 unsigned BlockID = Stream.ReadSubBlockID();
3166
3167 // We only know the control subblock ID.
3168 switch (BlockID) {
3169 case CONTROL_BLOCK_ID:
3170 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
3171 return false;
3172 } else {
3173 InControlBlock = true;
3174 }
3175 break;
3176
3177 default:
3178 if (Stream.SkipBlock())
3179 return false;
3180 break;
3181 }
3182 continue;
3183 }
3184
3185 if (Code == llvm::bitc::END_BLOCK) {
3186 if (Stream.ReadBlockEnd()) {
3187 return false;
3188 }
3189 InControlBlock = false;
3190 continue;
3191 }
3192
3193 if (Code == llvm::bitc::DEFINE_ABBREV) {
3194 Stream.ReadAbbrevRecord();
3195 continue;
3196 }
3197
3198 Record.clear();
3199 const char *BlobStart = 0;
3200 unsigned BlobLen = 0;
3201 unsigned RecCode = Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen);
3202 if (InControlBlock) {
3203 switch ((ControlRecordTypes)RecCode) {
3204 case METADATA: {
3205 if (Record[0] != VERSION_MAJOR) {
3206 return false;
3207 }
3208
3209 const std::string &CurBranch = getClangFullRepositoryVersion();
3210 StringRef ASTBranch(BlobStart, BlobLen);
3211 if (StringRef(CurBranch) != ASTBranch)
3212 return false;
3213
3214 break;
3215 }
3216 case LANGUAGE_OPTIONS:
3217 if (ParseLanguageOptions(Record, false, Validator))
3218 return false;
3219 break;
3220
3221 case TARGET_OPTIONS:
3222 if (ParseTargetOptions(Record, false, Validator))
3223 return false;
3224 break;
3225
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003226 case DIAGNOSTIC_OPTIONS:
3227 if (ParseDiagnosticOptions(Record, false, Validator))
3228 return false;
3229 break;
3230
Douglas Gregorbbf38312012-10-24 16:50:34 +00003231 case FILE_SYSTEM_OPTIONS:
3232 if (ParseFileSystemOptions(Record, false, Validator))
3233 return false;
3234 break;
3235
3236 case HEADER_SEARCH_OPTIONS:
3237 if (ParseHeaderSearchOptions(Record, false, Validator))
3238 return false;
3239 break;
3240
Douglas Gregor87699242012-10-25 00:07:54 +00003241 case PREPROCESSOR_OPTIONS: {
3242 std::string IgnoredSuggestedPredefines;
3243 if (ParsePreprocessorOptions(Record, false, Validator,
3244 IgnoredSuggestedPredefines))
Douglas Gregora71a7d82012-10-24 20:05:57 +00003245 return false;
3246 break;
Douglas Gregor87699242012-10-25 00:07:54 +00003247 }
Douglas Gregora71a7d82012-10-24 20:05:57 +00003248
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003249 default:
3250 // No other validation to perform.
3251 break;
3252 }
3253 }
3254 }
3255
3256 return true;
3257}
3258
Douglas Gregor4825fd72012-10-22 22:50:17 +00003259bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003260 // Enter the submodule block.
3261 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3262 Error("malformed submodule block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003263 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003264 }
3265
3266 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003267 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003268 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003269 RecordData Record;
3270 while (true) {
3271 unsigned Code = F.Stream.ReadCode();
3272 if (Code == llvm::bitc::END_BLOCK) {
3273 if (F.Stream.ReadBlockEnd()) {
3274 Error("error at end of submodule block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003275 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003276 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00003277 return false;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003278 }
3279
3280 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3281 // No known subblocks, always skip them.
3282 F.Stream.ReadSubBlockID();
3283 if (F.Stream.SkipBlock()) {
3284 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003285 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003286 }
3287 continue;
3288 }
3289
3290 if (Code == llvm::bitc::DEFINE_ABBREV) {
3291 F.Stream.ReadAbbrevRecord();
3292 continue;
3293 }
3294
3295 // Read a record.
3296 const char *BlobStart;
3297 unsigned BlobLen;
3298 Record.clear();
3299 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3300 default: // Default behavior: ignore.
3301 break;
3302
3303 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003304 if (First) {
3305 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003306 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003307 }
3308
Douglas Gregore209e502011-12-06 01:10:29 +00003309 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003310 Error("malformed module definition");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003311 return true;
Douglas Gregor1e123682011-12-05 22:27:44 +00003312 }
3313
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003314 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003315 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3316 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3317 bool IsFramework = Record[2];
3318 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003319 bool IsSystem = Record[4];
3320 bool InferSubmodules = Record[5];
3321 bool InferExplicitSubmodules = Record[6];
3322 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003323
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003324 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003325 if (Parent)
3326 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003327
3328 // Retrieve this (sub)module from the module map, creating it if
3329 // necessary.
3330 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3331 IsFramework,
3332 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003333 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3334 if (GlobalIndex >= SubmodulesLoaded.size() ||
3335 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003336 Error("too many submodules");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003337 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003338 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003339
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003340 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003341 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003342 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003343 CurrentModule->InferSubmodules = InferSubmodules;
3344 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3345 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003346 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003347 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003348
Douglas Gregore209e502011-12-06 01:10:29 +00003349 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003350 break;
3351 }
3352
Douglas Gregor77d029f2011-12-08 19:11:24 +00003353 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003354 if (First) {
3355 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003356 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003357 }
3358
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003359 if (!CurrentModule)
3360 break;
3361
3362 StringRef FileName(BlobStart, BlobLen);
3363 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003364 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003365 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003366 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003367 Error("mismatched umbrella headers in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003368 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003369 }
3370 }
3371 break;
3372 }
3373
3374 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003375 if (First) {
3376 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003377 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003378 }
3379
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003380 if (!CurrentModule)
3381 break;
3382
3383 // FIXME: Be more lazy about this!
3384 StringRef FileName(BlobStart, BlobLen);
3385 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3386 if (std::find(CurrentModule->Headers.begin(),
3387 CurrentModule->Headers.end(),
3388 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003389 ModMap.addHeader(CurrentModule, File, false);
3390 }
3391 break;
3392 }
3393
3394 case SUBMODULE_EXCLUDED_HEADER: {
3395 if (First) {
3396 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003397 return true;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003398 }
3399
3400 if (!CurrentModule)
3401 break;
3402
3403 // FIXME: Be more lazy about this!
3404 StringRef FileName(BlobStart, BlobLen);
3405 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3406 if (std::find(CurrentModule->Headers.begin(),
3407 CurrentModule->Headers.end(),
3408 File) == CurrentModule->Headers.end())
3409 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003410 }
3411 break;
3412 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003413
3414 case SUBMODULE_TOPHEADER: {
3415 if (First) {
3416 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003417 return true;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003418 }
3419
3420 if (!CurrentModule)
3421 break;
3422
3423 // FIXME: Be more lazy about this!
3424 StringRef FileName(BlobStart, BlobLen);
3425 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3426 CurrentModule->TopHeaders.insert(File);
3427 break;
3428 }
3429
Douglas Gregor77d029f2011-12-08 19:11:24 +00003430 case SUBMODULE_UMBRELLA_DIR: {
3431 if (First) {
3432 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003433 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003434 }
3435
3436 if (!CurrentModule)
3437 break;
3438
3439 StringRef DirName(BlobStart, BlobLen);
3440 if (const DirectoryEntry *Umbrella
3441 = PP.getFileManager().getDirectory(DirName)) {
3442 if (!CurrentModule->getUmbrellaDir())
3443 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3444 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3445 Error("mismatched umbrella directories in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003446 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003447 }
3448 }
3449 break;
3450 }
3451
Douglas Gregor26ced122011-12-01 00:59:36 +00003452 case SUBMODULE_METADATA: {
3453 if (!First) {
3454 Error("submodule metadata record not at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003455 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003456 }
3457 First = false;
3458
3459 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003460 F.LocalNumSubmodules = Record[0];
3461 unsigned LocalBaseSubmoduleID = Record[1];
3462 if (F.LocalNumSubmodules > 0) {
3463 // Introduce the global -> local mapping for submodules within this
3464 // module.
3465 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3466
3467 // Introduce the local -> global mapping for submodules within this
3468 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003469 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003470 std::make_pair(LocalBaseSubmoduleID,
3471 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3472
3473 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3474 }
3475 break;
3476 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003477
Douglas Gregor55988682011-12-05 16:33:54 +00003478 case SUBMODULE_IMPORTS: {
3479 if (First) {
3480 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003481 return true;
Douglas Gregor55988682011-12-05 16:33:54 +00003482 }
3483
3484 if (!CurrentModule)
3485 break;
3486
3487 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3488 UnresolvedModuleImportExport Unresolved;
3489 Unresolved.File = &F;
3490 Unresolved.Mod = CurrentModule;
3491 Unresolved.ID = Record[Idx];
3492 Unresolved.IsImport = true;
3493 Unresolved.IsWildcard = false;
3494 UnresolvedModuleImportExports.push_back(Unresolved);
3495 }
3496 break;
3497 }
3498
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003499 case SUBMODULE_EXPORTS: {
3500 if (First) {
3501 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003502 return true;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003503 }
3504
3505 if (!CurrentModule)
3506 break;
3507
3508 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003509 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003510 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003511 Unresolved.Mod = CurrentModule;
3512 Unresolved.ID = Record[Idx];
3513 Unresolved.IsImport = false;
3514 Unresolved.IsWildcard = Record[Idx + 1];
3515 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003516 }
3517
3518 // Once we've loaded the set of exports, there's no reason to keep
3519 // the parsed, unresolved exports around.
3520 CurrentModule->UnresolvedExports.clear();
3521 break;
3522 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003523 case SUBMODULE_REQUIRES: {
3524 if (First) {
3525 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003526 return true;
Douglas Gregor51f564f2011-12-31 04:05:44 +00003527 }
3528
3529 if (!CurrentModule)
3530 break;
3531
3532 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003533 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003534 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003535 break;
3536 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003537 }
3538 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003539}
3540
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003541/// \brief Parse the record that corresponds to a LangOptions data
3542/// structure.
3543///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003544/// This routine parses the language options from the AST file and then gives
3545/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003546///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003547/// \returns true if the listener deems the file unacceptable, false otherwise.
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003548bool ASTReader::ParseLanguageOptions(const RecordData &Record,
3549 bool Complain,
3550 ASTReaderListener &Listener) {
3551 LangOptions LangOpts;
3552 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003553#define LANGOPT(Name, Bits, Default, Description) \
3554 LangOpts.Name = Record[Idx++];
3555#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3556 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3557#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003558
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003559 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3560 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3561 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
3562
3563 unsigned Length = Record[Idx++];
3564 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3565 Record.begin() + Idx + Length);
3566 return Listener.ReadLanguageOptions(LangOpts, Complain);
3567}
3568
3569bool ASTReader::ParseTargetOptions(const RecordData &Record,
3570 bool Complain,
3571 ASTReaderListener &Listener) {
3572 unsigned Idx = 0;
3573 TargetOptions TargetOpts;
3574 TargetOpts.Triple = ReadString(Record, Idx);
3575 TargetOpts.CPU = ReadString(Record, Idx);
3576 TargetOpts.ABI = ReadString(Record, Idx);
3577 TargetOpts.CXXABI = ReadString(Record, Idx);
3578 TargetOpts.LinkerVersion = ReadString(Record, Idx);
3579 for (unsigned N = Record[Idx++]; N; --N) {
3580 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
3581 }
3582 for (unsigned N = Record[Idx++]; N; --N) {
3583 TargetOpts.Features.push_back(ReadString(Record, Idx));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003584 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003585
Douglas Gregor27ffa6c2012-10-23 06:18:24 +00003586 return Listener.ReadTargetOptions(TargetOpts, Complain);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003587}
3588
Douglas Gregor5f3d8222012-10-24 15:17:15 +00003589bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
3590 ASTReaderListener &Listener) {
3591 DiagnosticOptions DiagOpts;
3592 unsigned Idx = 0;
3593#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];
3594#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
3595 DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));
3596#include "clang/Basic/DiagnosticOptions.def"
3597
3598 for (unsigned N = Record[Idx++]; N; --N) {
3599 DiagOpts.Warnings.push_back(ReadString(Record, Idx));
3600 }
3601
3602 return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
3603}
3604
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00003605bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
3606 ASTReaderListener &Listener) {
3607 FileSystemOptions FSOpts;
3608 unsigned Idx = 0;
3609 FSOpts.WorkingDir = ReadString(Record, Idx);
3610 return Listener.ReadFileSystemOptions(FSOpts, Complain);
3611}
3612
Douglas Gregorbbf38312012-10-24 16:50:34 +00003613bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
3614 bool Complain,
3615 ASTReaderListener &Listener) {
3616 HeaderSearchOptions HSOpts;
3617 unsigned Idx = 0;
3618 HSOpts.Sysroot = ReadString(Record, Idx);
3619
3620 // Include entries.
3621 for (unsigned N = Record[Idx++]; N; --N) {
3622 std::string Path = ReadString(Record, Idx);
3623 frontend::IncludeDirGroup Group
3624 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
3625 bool IsUserSupplied = Record[Idx++];
3626 bool IsFramework = Record[Idx++];
3627 bool IgnoreSysRoot = Record[Idx++];
3628 bool IsInternal = Record[Idx++];
3629 bool ImplicitExternC = Record[Idx++];
3630 HSOpts.UserEntries.push_back(
3631 HeaderSearchOptions::Entry(Path, Group, IsUserSupplied, IsFramework,
3632 IgnoreSysRoot, IsInternal, ImplicitExternC));
3633 }
3634
3635 // System header prefixes.
3636 for (unsigned N = Record[Idx++]; N; --N) {
3637 std::string Prefix = ReadString(Record, Idx);
3638 bool IsSystemHeader = Record[Idx++];
3639 HSOpts.SystemHeaderPrefixes.push_back(
3640 HeaderSearchOptions::SystemHeaderPrefix(Prefix, IsSystemHeader));
3641 }
3642
3643 HSOpts.ResourceDir = ReadString(Record, Idx);
3644 HSOpts.ModuleCachePath = ReadString(Record, Idx);
3645 HSOpts.DisableModuleHash = Record[Idx++];
3646 HSOpts.UseBuiltinIncludes = Record[Idx++];
3647 HSOpts.UseStandardSystemIncludes = Record[Idx++];
3648 HSOpts.UseStandardCXXIncludes = Record[Idx++];
3649 HSOpts.UseLibcxx = Record[Idx++];
3650
3651 return Listener.ReadHeaderSearchOptions(HSOpts, Complain);
3652}
3653
Douglas Gregora71a7d82012-10-24 20:05:57 +00003654bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
3655 bool Complain,
Douglas Gregor87699242012-10-25 00:07:54 +00003656 ASTReaderListener &Listener,
3657 std::string &SuggestedPredefines) {
Douglas Gregora71a7d82012-10-24 20:05:57 +00003658 PreprocessorOptions PPOpts;
3659 unsigned Idx = 0;
3660
3661 // Macro definitions/undefs
3662 for (unsigned N = Record[Idx++]; N; --N) {
3663 std::string Macro = ReadString(Record, Idx);
3664 bool IsUndef = Record[Idx++];
3665 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
3666 }
3667
3668 // Includes
3669 for (unsigned N = Record[Idx++]; N; --N) {
3670 PPOpts.Includes.push_back(ReadString(Record, Idx));
3671 }
3672
3673 // Macro Includes
3674 for (unsigned N = Record[Idx++]; N; --N) {
3675 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
3676 }
3677
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00003678 PPOpts.UsePredefines = Record[Idx++];
Douglas Gregora71a7d82012-10-24 20:05:57 +00003679 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
3680 PPOpts.ImplicitPTHInclude = ReadString(Record, Idx);
3681 PPOpts.ObjCXXARCStandardLibrary =
3682 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
Douglas Gregor87699242012-10-25 00:07:54 +00003683 SuggestedPredefines.clear();
3684 return Listener.ReadPreprocessorOptions(PPOpts, Complain,
3685 SuggestedPredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00003686}
3687
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003688std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003689ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003690 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003691 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003692 assert(I != GlobalPreprocessedEntityMap.end() &&
3693 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003694 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003695 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3696 return std::make_pair(M, LocalIndex);
3697}
3698
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003699std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3700ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3701 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3702 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3703 Mod.NumPreprocessedEntities);
3704
3705 return std::make_pair(PreprocessingRecord::iterator(),
3706 PreprocessingRecord::iterator());
3707}
3708
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003709std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3710ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3711 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3712 ModuleDeclIterator(this, &Mod,
3713 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3714}
3715
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003716PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3717 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003718 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3719 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003720 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003721 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003722
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003723 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003724 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003725
3726 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3727 switch (Code) {
3728 case llvm::bitc::END_BLOCK:
3729 return 0;
3730
3731 case llvm::bitc::ENTER_SUBBLOCK:
3732 Error("unexpected subblock record in preprocessor detail block");
3733 return 0;
3734
3735 case llvm::bitc::DEFINE_ABBREV:
3736 Error("unexpected abbrevation record in preprocessor detail block");
3737 return 0;
3738
3739 default:
3740 break;
3741 }
3742
3743 if (!PP.getPreprocessingRecord()) {
3744 Error("no preprocessing record");
3745 return 0;
3746 }
3747
3748 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003749 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3750 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003751 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3752 const char *BlobStart = 0;
3753 unsigned BlobLen = 0;
3754 RecordData Record;
3755 PreprocessorDetailRecordTypes RecType =
3756 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3757 Code, Record, BlobStart, BlobLen);
3758 switch (RecType) {
3759 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003760 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003761 IdentifierInfo *Name = 0;
3762 MacroDefinition *Def = 0;
3763 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003764 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003765 else {
3766 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003767 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003768 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3769 }
3770
3771 MacroExpansion *ME;
3772 if (isBuiltin)
3773 ME = new (PPRec) MacroExpansion(Name, Range);
3774 else
3775 ME = new (PPRec) MacroExpansion(Def, Range);
3776
3777 return ME;
3778 }
3779
3780 case PPD_MACRO_DEFINITION: {
3781 // Decode the identifier info and then check again; if the macro is
3782 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003783 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003784 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003785 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003786
3787 if (DeserializationListener)
3788 DeserializationListener->MacroDefinitionRead(PPID, MD);
3789
3790 return MD;
3791 }
3792
3793 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003794 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003795 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3796 const FileEntry *File = 0;
3797 if (!FullFileName.empty())
3798 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003799
3800 // FIXME: Stable encoding
3801 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003802 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003803 InclusionDirective *ID
3804 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003805 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003806 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003807 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003808 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003809 return ID;
3810 }
3811 }
David Blaikie7530c032012-01-17 06:56:22 +00003812
3813 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003814}
3815
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003816/// \brief \arg SLocMapI points at a chunk of a module that contains no
3817/// preprocessed entities or the entities it contains are not the ones we are
3818/// looking for. Find the next module that contains entities and return the ID
3819/// of the first entry.
3820PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3821 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3822 ++SLocMapI;
3823 for (GlobalSLocOffsetMapType::const_iterator
3824 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003825 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003826 if (M.NumPreprocessedEntities)
Argyrios Kyrtzidisce254922012-11-02 02:31:22 +00003827 return M.BasePreprocessedEntityID;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003828 }
3829
3830 return getTotalNumPreprocessedEntities();
3831}
3832
3833namespace {
3834
3835template <unsigned PPEntityOffset::*PPLoc>
3836struct PPEntityComp {
3837 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003838 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003839
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003840 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003841
Benjamin Kramer88df1252011-09-21 06:42:26 +00003842 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3843 SourceLocation LHS = getLoc(L);
3844 SourceLocation RHS = getLoc(R);
3845 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3846 }
3847
3848 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003849 SourceLocation LHS = getLoc(L);
3850 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3851 }
3852
Benjamin Kramer88df1252011-09-21 06:42:26 +00003853 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003854 SourceLocation RHS = getLoc(R);
3855 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3856 }
3857
3858 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3859 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3860 }
3861};
3862
3863}
3864
3865/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3866PreprocessedEntityID
3867ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3868 if (SourceMgr.isLocalSourceLocation(BLoc))
3869 return getTotalNumPreprocessedEntities();
3870
3871 GlobalSLocOffsetMapType::const_iterator
3872 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3873 BLoc.getOffset());
3874 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3875 "Corrupted global sloc offset map");
3876
3877 if (SLocMapI->second->NumPreprocessedEntities == 0)
3878 return findNextPreprocessedEntity(SLocMapI);
3879
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003880 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003881 typedef const PPEntityOffset *pp_iterator;
3882 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3883 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003884
3885 size_t Count = M.NumPreprocessedEntities;
3886 size_t Half;
3887 pp_iterator First = pp_begin;
3888 pp_iterator PPI;
3889
3890 // Do a binary search manually instead of using std::lower_bound because
3891 // The end locations of entities may be unordered (when a macro expansion
3892 // is inside another macro argument), but for this case it is not important
3893 // whether we get the first macro expansion or its containing macro.
3894 while (Count > 0) {
3895 Half = Count/2;
3896 PPI = First;
3897 std::advance(PPI, Half);
3898 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3899 BLoc)){
3900 First = PPI;
3901 ++First;
3902 Count = Count - Half - 1;
3903 } else
3904 Count = Half;
3905 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003906
3907 if (PPI == pp_end)
3908 return findNextPreprocessedEntity(SLocMapI);
3909
Argyrios Kyrtzidisce254922012-11-02 02:31:22 +00003910 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003911}
3912
3913/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3914PreprocessedEntityID
3915ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3916 if (SourceMgr.isLocalSourceLocation(ELoc))
3917 return getTotalNumPreprocessedEntities();
3918
3919 GlobalSLocOffsetMapType::const_iterator
3920 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3921 ELoc.getOffset());
3922 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3923 "Corrupted global sloc offset map");
3924
3925 if (SLocMapI->second->NumPreprocessedEntities == 0)
3926 return findNextPreprocessedEntity(SLocMapI);
3927
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003928 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003929 typedef const PPEntityOffset *pp_iterator;
3930 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3931 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3932 pp_iterator PPI =
3933 std::upper_bound(pp_begin, pp_end, ELoc,
3934 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3935
3936 if (PPI == pp_end)
3937 return findNextPreprocessedEntity(SLocMapI);
3938
Argyrios Kyrtzidisce254922012-11-02 02:31:22 +00003939 return M.BasePreprocessedEntityID + (PPI - pp_begin);
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003940}
3941
3942/// \brief Returns a pair of [Begin, End) indices of preallocated
3943/// preprocessed entities that \arg Range encompasses.
3944std::pair<unsigned, unsigned>
3945 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3946 if (Range.isInvalid())
3947 return std::make_pair(0,0);
3948 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3949
3950 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3951 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3952 return std::make_pair(BeginID, EndID);
3953}
3954
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003955/// \brief Optionally returns true or false if the preallocated preprocessed
3956/// entity with index \arg Index came from file \arg FID.
3957llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3958 FileID FID) {
3959 if (FID.isInvalid())
3960 return false;
3961
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003962 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3963 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003964 unsigned LocalIndex = PPInfo.second;
3965 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3966
3967 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3968 if (Loc.isInvalid())
3969 return false;
3970
3971 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3972 return true;
3973 else
3974 return false;
3975}
3976
Douglas Gregord10a3812011-08-25 18:14:34 +00003977namespace {
3978 /// \brief Visitor used to search for information about a header file.
3979 class HeaderFileInfoVisitor {
3980 ASTReader &Reader;
3981 const FileEntry *FE;
3982
3983 llvm::Optional<HeaderFileInfo> HFI;
3984
3985 public:
3986 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3987 : Reader(Reader), FE(FE) { }
3988
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003989 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003990 HeaderFileInfoVisitor *This
3991 = static_cast<HeaderFileInfoVisitor *>(UserData);
3992
3993 HeaderFileInfoTrait Trait(This->Reader, M,
3994 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3995 M.HeaderFileFrameworkStrings,
3996 This->FE->getName());
3997
3998 HeaderFileInfoLookupTable *Table
3999 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
4000 if (!Table)
4001 return false;
4002
4003 // Look in the on-disk hash table for an entry for this file name.
4004 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
4005 &Trait);
4006 if (Pos == Table->end())
4007 return false;
4008
4009 This->HFI = *Pos;
4010 return true;
4011 }
4012
4013 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
4014 };
4015}
4016
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004017HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00004018 HeaderFileInfoVisitor Visitor(*this, FE);
4019 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
4020 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004021 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00004022 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
4023 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004024 }
4025
4026 return HeaderFileInfo();
4027}
4028
David Blaikied6471f72011-09-25 23:23:43 +00004029void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004030 // FIXME: Make it work properly with modules.
4031 llvm::SmallVector<DiagnosticsEngine::DiagState *, 32> DiagStates;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004032 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004033 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004034 unsigned Idx = 0;
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004035 DiagStates.clear();
4036 assert(!Diag.DiagStates.empty());
4037 DiagStates.push_back(&Diag.DiagStates.front()); // the command-line one.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004038 while (Idx < F.PragmaDiagMappings.size()) {
4039 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004040 unsigned DiagStateID = F.PragmaDiagMappings[Idx++];
4041 if (DiagStateID != 0) {
4042 Diag.DiagStatePoints.push_back(
4043 DiagnosticsEngine::DiagStatePoint(DiagStates[DiagStateID-1],
4044 FullSourceLoc(Loc, SourceMgr)));
4045 continue;
4046 }
4047
4048 assert(DiagStateID == 0);
4049 // A new DiagState was created here.
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004050 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004051 DiagnosticsEngine::DiagState *NewState = &Diag.DiagStates.back();
4052 DiagStates.push_back(NewState);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004053 Diag.DiagStatePoints.push_back(
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00004054 DiagnosticsEngine::DiagStatePoint(NewState,
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004055 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004056 while (1) {
4057 assert(Idx < F.PragmaDiagMappings.size() &&
4058 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4059 if (Idx >= F.PragmaDiagMappings.size()) {
4060 break; // Something is messed up but at least avoid infinite loop in
4061 // release build.
4062 }
4063 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4064 if (DiagID == (unsigned)-1) {
4065 break; // no more diag/map pairs for this location.
4066 }
4067 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004068 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4069 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004070 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004071 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00004072 }
4073}
4074
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004075/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004076ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00004077 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00004078 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004079 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00004080 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004081}
4082
4083/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00004084///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004085/// The index is the type ID, shifted and minus the number of predefs. This
4086/// routine actually reads the record corresponding to the type at the given
4087/// location. It is a helper routine for GetType, which deals with reading type
4088/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00004089QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004090 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004091 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00004092
Douglas Gregor0b748912009-04-14 21:18:50 +00004093 // Keep track of where we are in the stream, then jump back there
4094 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004095 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00004096
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004097 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00004098
Douglas Gregord89275b2009-07-06 18:54:52 +00004099 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004100 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00004101
Douglas Gregor393f2492011-07-22 00:38:23 +00004102 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00004103 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004104 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004105 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004106 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4107 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004108 if (Record.size() != 2) {
4109 Error("Incorrect encoding of extended qualifier type");
4110 return QualType();
4111 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004112 QualType Base = readType(*Loc.F, Record, Idx);
4113 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00004114 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00004115 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004116
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004117 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004118 if (Record.size() != 1) {
4119 Error("Incorrect encoding of complex type");
4120 return QualType();
4121 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004122 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004123 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004124 }
4125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004126 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004127 if (Record.size() != 1) {
4128 Error("Incorrect encoding of pointer type");
4129 return QualType();
4130 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004131 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004132 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004133 }
4134
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004135 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004136 if (Record.size() != 1) {
4137 Error("Incorrect encoding of block pointer type");
4138 return QualType();
4139 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004140 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004141 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004142 }
4143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004144 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00004145 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004146 Error("Incorrect encoding of lvalue reference type");
4147 return QualType();
4148 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004149 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004150 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004151 }
4152
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004153 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004154 if (Record.size() != 1) {
4155 Error("Incorrect encoding of rvalue reference type");
4156 return QualType();
4157 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004158 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004159 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004160 }
4161
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004162 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00004163 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004164 Error("Incorrect encoding of member pointer type");
4165 return QualType();
4166 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004167 QualType PointeeType = readType(*Loc.F, Record, Idx);
4168 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004169 if (PointeeType.isNull() || ClassType.isNull())
4170 return QualType();
4171
Douglas Gregor35942772011-09-09 21:34:22 +00004172 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004173 }
4174
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004175 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004176 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004177 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4178 unsigned IndexTypeQuals = Record[2];
4179 unsigned Idx = 3;
4180 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004181 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004182 ASM, IndexTypeQuals);
4183 }
4184
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004185 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004186 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004187 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4188 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004189 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004190 }
4191
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004192 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004193 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00004194 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4195 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004196 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4197 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004198 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004199 ASM, IndexTypeQuals,
4200 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004201 }
4202
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004203 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004204 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004205 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004206 return QualType();
4207 }
4208
Douglas Gregor393f2492011-07-22 00:38:23 +00004209 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004210 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004211 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004212 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004213 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004214 }
4215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004216 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004217 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004218 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004219 return QualType();
4220 }
4221
Douglas Gregor393f2492011-07-22 00:38:23 +00004222 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004223 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004224 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004225 }
4226
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004227 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004228 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004229 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004230 return QualType();
4231 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004232 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004233 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4234 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004235 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004236 }
4237
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004238 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004239 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004240
4241 FunctionProtoType::ExtProtoInfo EPI;
4242 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004243 /*hasregparm*/ Record[2],
4244 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004245 static_cast<CallingConv>(Record[4]),
4246 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004247
John McCallf85e1932011-06-15 23:02:42 +00004248 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004249 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004250 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004251 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004252 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004253
4254 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004255 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004256 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004257 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004258 ExceptionSpecificationType EST =
4259 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4260 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004261 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004262 if (EST == EST_Dynamic) {
4263 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004264 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004265 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004266 EPI.Exceptions = Exceptions.data();
4267 } else if (EST == EST_ComputedNoexcept) {
4268 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004269 } else if (EST == EST_Uninstantiated) {
4270 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4271 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004272 } else if (EST == EST_Unevaluated) {
4273 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004274 }
Douglas Gregor35942772011-09-09 21:34:22 +00004275 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004276 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004277 }
4278
Douglas Gregor409448c2011-07-21 22:35:25 +00004279 case TYPE_UNRESOLVED_USING: {
4280 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004281 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004282 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4283 }
4284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004285 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004286 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004287 Error("incorrect encoding of typedef type");
4288 return QualType();
4289 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004290 unsigned Idx = 0;
4291 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004292 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004293 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004294 Canonical = Context.getCanonicalType(Canonical);
4295 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004296 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004297
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004298 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004299 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004300
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004301 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004302 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004303 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004304 return QualType();
4305 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004306 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004307 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004308 }
Mike Stump1eb44332009-09-09 15:08:12 +00004309
Douglas Gregorf8af9822012-02-12 18:42:33 +00004310 case TYPE_DECLTYPE: {
4311 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4312 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4313 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004314
Sean Huntca63c202011-05-24 22:41:36 +00004315 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004316 QualType BaseType = readType(*Loc.F, Record, Idx);
4317 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004318 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004319 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004320 }
4321
Richard Smith34b41d92011-02-20 03:19:35 +00004322 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004323 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004324
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004325 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004326 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004327 Error("incorrect encoding of record type");
4328 return QualType();
4329 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004330 unsigned Idx = 0;
4331 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004332 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4333 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4334 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004335 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004336 return T;
4337 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004338
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004339 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004340 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004341 Error("incorrect encoding of enum type");
4342 return QualType();
4343 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004344 unsigned Idx = 0;
4345 bool IsDependent = Record[Idx++];
4346 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004347 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004348 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004349 return T;
4350 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004351
John McCall9d156a72011-01-06 01:58:22 +00004352 case TYPE_ATTRIBUTED: {
4353 if (Record.size() != 3) {
4354 Error("incorrect encoding of attributed type");
4355 return QualType();
4356 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004357 QualType modifiedType = readType(*Loc.F, Record, Idx);
4358 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004359 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004360 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004361 }
4362
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004363 case TYPE_PAREN: {
4364 if (Record.size() != 1) {
4365 Error("incorrect encoding of paren type");
4366 return QualType();
4367 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004368 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004369 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004370 }
4371
Douglas Gregor7536dd52010-12-20 02:24:11 +00004372 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004373 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004374 Error("incorrect encoding of pack expansion type");
4375 return QualType();
4376 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004377 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004378 if (Pattern.isNull())
4379 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004380 llvm::Optional<unsigned> NumExpansions;
4381 if (Record[1])
4382 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004383 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004384 }
4385
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004386 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004387 unsigned Idx = 0;
4388 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004389 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004390 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004391 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004392 }
4393
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004394 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004395 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004396 ObjCInterfaceDecl *ItfD
4397 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004398 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004399 }
4400
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004401 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004402 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004403 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004404 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004405 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004406 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004407 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004408 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004409 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004410
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004411 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004412 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004413 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004414 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004415 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004416
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004417 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004418 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004419 QualType Parm = readType(*Loc.F, Record, Idx);
4420 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004421 return
Douglas Gregor35942772011-09-09 21:34:22 +00004422 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004423 Replacement);
4424 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004425
Douglas Gregorc3069d62011-01-14 02:55:32 +00004426 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4427 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004428 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004429 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004430 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004431 cast<TemplateTypeParmType>(Parm),
4432 ArgPack);
4433 }
4434
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004435 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004436 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004437 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004438 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004439 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004440 return
Douglas Gregor35942772011-09-09 21:34:22 +00004441 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004442 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004443
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004444 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004445 unsigned Idx = 0;
4446 unsigned Depth = Record[Idx++];
4447 unsigned Index = Record[Idx++];
4448 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004449 TemplateTypeParmDecl *D
4450 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004451 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004452 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004453
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004454 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004455 unsigned Idx = 0;
4456 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004457 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004458 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004459 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004460 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004461 Canon = Context.getCanonicalType(Canon);
4462 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004463 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004464
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004465 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004466 unsigned Idx = 0;
4467 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004468 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004469 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004470 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004471 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004472 Args.reserve(NumArgs);
4473 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004474 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004475 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004476 Args.size(), Args.data());
4477 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004478
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004479 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004480 unsigned Idx = 0;
4481
4482 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004483 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004484 ArrayType::ArraySizeModifier ASM
4485 = (ArrayType::ArraySizeModifier)Record[Idx++];
4486 unsigned IndexTypeQuals = Record[Idx++];
4487
4488 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004489 Expr *NumElts = ReadExpr(*Loc.F);
4490 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004491
Douglas Gregor35942772011-09-09 21:34:22 +00004492 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004493 IndexTypeQuals, Brackets);
4494 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004495
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004496 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004497 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004498 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004499 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004500 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004501 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004502 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004503 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004504 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004505 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004506 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004507 else
Douglas Gregor35942772011-09-09 21:34:22 +00004508 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004509 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004510 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004511 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004512 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004513
4514 case TYPE_ATOMIC: {
4515 if (Record.size() != 1) {
4516 Error("Incorrect encoding of atomic type");
4517 return QualType();
4518 }
4519 QualType ValueType = readType(*Loc.F, Record, Idx);
4520 return Context.getAtomicType(ValueType);
4521 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004522 }
David Blaikie7530c032012-01-17 06:56:22 +00004523 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004524}
4525
Sebastian Redlc3632732010-10-05 15:59:54 +00004526class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004527 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004528 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004529 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004530 unsigned &Idx;
4531
Sebastian Redlc3632732010-10-05 15:59:54 +00004532 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4533 unsigned &I) {
4534 return Reader.ReadSourceLocation(F, R, I);
4535 }
4536
Douglas Gregor409448c2011-07-21 22:35:25 +00004537 template<typename T>
4538 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4539 return Reader.ReadDeclAs<T>(F, Record, Idx);
4540 }
4541
John McCalla1ee0c52009-10-16 21:56:05 +00004542public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004543 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004544 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004545 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004546 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004547
John McCall51bd8032009-10-18 01:05:36 +00004548 // We want compile-time assurance that we've enumerated all of
4549 // these, so unfortunately we have to declare them first, then
4550 // define them out-of-line.
4551#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004552#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004553 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004554#include "clang/AST/TypeLocNodes.def"
4555
John McCall51bd8032009-10-18 01:05:36 +00004556 void VisitFunctionTypeLoc(FunctionTypeLoc);
4557 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004558};
4559
John McCall51bd8032009-10-18 01:05:36 +00004560void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004561 // nothing to do
4562}
John McCall51bd8032009-10-18 01:05:36 +00004563void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004564 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004565 if (TL.needsExtraLocalData()) {
4566 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4567 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4568 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4569 TL.setModeAttr(Record[Idx++]);
4570 }
John McCalla1ee0c52009-10-16 21:56:05 +00004571}
John McCall51bd8032009-10-18 01:05:36 +00004572void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004573 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004574}
John McCall51bd8032009-10-18 01:05:36 +00004575void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004576 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004577}
John McCall51bd8032009-10-18 01:05:36 +00004578void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004579 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004580}
John McCall51bd8032009-10-18 01:05:36 +00004581void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004582 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004583}
John McCall51bd8032009-10-18 01:05:36 +00004584void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004585 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004586}
John McCall51bd8032009-10-18 01:05:36 +00004587void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004588 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004589 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004590}
John McCall51bd8032009-10-18 01:05:36 +00004591void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004592 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4593 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004594 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004595 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004596 else
John McCall51bd8032009-10-18 01:05:36 +00004597 TL.setSizeExpr(0);
4598}
4599void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4600 VisitArrayTypeLoc(TL);
4601}
4602void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4603 VisitArrayTypeLoc(TL);
4604}
4605void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4606 VisitArrayTypeLoc(TL);
4607}
4608void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4609 DependentSizedArrayTypeLoc TL) {
4610 VisitArrayTypeLoc(TL);
4611}
4612void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4613 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004614 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004615}
4616void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004617 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004618}
4619void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004620 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004621}
4622void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004623 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004624 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4625 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004626 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004627 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004628 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004629 }
4630}
4631void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4632 VisitFunctionTypeLoc(TL);
4633}
4634void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4635 VisitFunctionTypeLoc(TL);
4636}
John McCalled976492009-12-04 22:46:56 +00004637void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004638 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004639}
John McCall51bd8032009-10-18 01:05:36 +00004640void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004641 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004642}
4643void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004644 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4645 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4646 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004647}
4648void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004649 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4650 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4651 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4652 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004653}
4654void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004655 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004656}
Sean Huntca63c202011-05-24 22:41:36 +00004657void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4658 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4659 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4660 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4661 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4662}
Richard Smith34b41d92011-02-20 03:19:35 +00004663void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4664 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4665}
John McCall51bd8032009-10-18 01:05:36 +00004666void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004667 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004668}
4669void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004670 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004671}
John McCall9d156a72011-01-06 01:58:22 +00004672void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4673 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4674 if (TL.hasAttrOperand()) {
4675 SourceRange range;
4676 range.setBegin(ReadSourceLocation(Record, Idx));
4677 range.setEnd(ReadSourceLocation(Record, Idx));
4678 TL.setAttrOperandParensRange(range);
4679 }
4680 if (TL.hasAttrExprOperand()) {
4681 if (Record[Idx++])
4682 TL.setAttrExprOperand(Reader.ReadExpr(F));
4683 else
4684 TL.setAttrExprOperand(0);
4685 } else if (TL.hasAttrEnumOperand())
4686 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4687}
John McCall51bd8032009-10-18 01:05:36 +00004688void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004689 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004690}
John McCall49a832b2009-10-18 09:09:24 +00004691void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4692 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004693 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004694}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004695void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4696 SubstTemplateTypeParmPackTypeLoc TL) {
4697 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4698}
John McCall51bd8032009-10-18 01:05:36 +00004699void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4700 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004701 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004702 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4703 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4704 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004705 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4706 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004707 Reader.GetTemplateArgumentLocInfo(F,
4708 TL.getTypePtr()->getArg(i).getKind(),
4709 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004710}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004711void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4712 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4713 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4714}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004715void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004716 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004717 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004718}
John McCall3cb0ebd2010-03-10 03:28:59 +00004719void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004720 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004721}
Douglas Gregor4714c122010-03-31 17:34:00 +00004722void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004723 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004724 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004725 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004726}
John McCall33500952010-06-11 00:33:02 +00004727void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4728 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004729 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004730 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004731 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004732 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004733 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4734 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004735 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4736 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004737 Reader.GetTemplateArgumentLocInfo(F,
4738 TL.getTypePtr()->getArg(I).getKind(),
4739 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004740}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004741void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4742 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4743}
John McCall51bd8032009-10-18 01:05:36 +00004744void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004745 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004746}
4747void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4748 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004749 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4750 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004751 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004752 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004753}
John McCall54e14c42009-10-22 22:37:11 +00004754void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004755 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004756}
Eli Friedmanb001de72011-10-06 23:00:33 +00004757void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4758 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4759 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4760 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4761}
John McCalla1ee0c52009-10-16 21:56:05 +00004762
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004763TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004764 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004765 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004766 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004767 if (InfoTy.isNull())
4768 return 0;
4769
Douglas Gregor35942772011-09-09 21:34:22 +00004770 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004771 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004772 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004773 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004774 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004775}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004776
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004777QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004778 unsigned FastQuals = ID & Qualifiers::FastMask;
4779 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004780
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004781 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004782 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004783 switch ((PredefinedTypeIDs)Index) {
4784 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004785 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4786 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004787
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004788 case PREDEF_TYPE_CHAR_U_ID:
4789 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004790 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004791 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004792 break;
4793
Douglas Gregor35942772011-09-09 21:34:22 +00004794 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4795 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4796 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4797 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4798 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4799 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4800 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4801 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4802 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4803 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4804 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4805 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4806 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004807 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004808 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4809 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4810 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4811 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4812 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004813 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004814 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4815 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4816 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4817 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4818 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4819 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4820 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4821 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4822 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004823
4824 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004825 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004826 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004827
4828 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4829 T = Context.ARCUnbridgedCastTy;
4830 break;
4831
Meador Ingefb40e3f2012-07-01 15:57:25 +00004832 case PREDEF_TYPE_VA_LIST_TAG:
4833 T = Context.getVaListTagType();
4834 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004835
4836 case PREDEF_TYPE_BUILTIN_FN:
4837 T = Context.BuiltinFnTy;
4838 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004839 }
4840
4841 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004842 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004843 }
4844
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004845 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004846 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004847 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004848 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004849 if (TypesLoaded[Index].isNull())
4850 return QualType();
4851
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004852 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004853 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004854 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004855 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004856 }
Mike Stump1eb44332009-09-09 15:08:12 +00004857
John McCall0953e762009-09-24 19:53:00 +00004858 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004859}
4860
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004861QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004862 return GetType(getGlobalTypeID(F, LocalID));
4863}
4864
4865serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004866ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004867 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4868 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4869
4870 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4871 return LocalID;
4872
4873 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4874 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4875 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4876
4877 unsigned GlobalIndex = LocalIndex + I->second;
4878 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4879}
4880
John McCall833ca992009-10-29 08:12:44 +00004881TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004882ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004883 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004884 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004885 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004886 switch (Kind) {
4887 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004888 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004889 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004890 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004891 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004892 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4893 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004894 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004895 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004896 SourceLocation());
4897 }
4898 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004899 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4900 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004901 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004902 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004903 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004904 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004905 }
John McCall833ca992009-10-29 08:12:44 +00004906 case TemplateArgument::Null:
4907 case TemplateArgument::Integral:
4908 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004909 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004910 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004911 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004912 return TemplateArgumentLocInfo();
4913 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004914 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004915}
4916
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004917TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004918ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004919 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004920 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004921
4922 if (Arg.getKind() == TemplateArgument::Expression) {
4923 if (Record[Index++]) // bool InfoHasSameExpr.
4924 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4925 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004926 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004927 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004928}
4929
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004930Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004931 return GetDecl(ID);
4932}
4933
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004934uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004935 unsigned &Idx){
4936 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004937 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004938
Douglas Gregore92b8a12011-08-04 00:01:48 +00004939 unsigned LocalID = Record[Idx++];
4940 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004941}
4942
4943CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004944 RecordLocation Loc = getLocalBitOffset(Offset);
4945 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004946 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004947 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004948 ReadingKindTracker ReadingKind(Read_Decl, *this);
4949 RecordData Record;
4950 unsigned Code = Cursor.ReadCode();
4951 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4952 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4953 Error("Malformed AST file: missing C++ base specifiers");
4954 return 0;
4955 }
4956
4957 unsigned Idx = 0;
4958 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004959 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004960 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4961 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004962 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004963 return Bases;
4964}
4965
Douglas Gregor409448c2011-07-21 22:35:25 +00004966serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004967ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004968 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004969 return LocalID;
4970
4971 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004972 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004973 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4974
4975 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004976}
4977
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004978bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004979 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004980 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4981 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4982 return &M == I->second;
4983}
4984
Douglas Gregorcff9f262012-01-27 01:47:08 +00004985ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4986 if (!D->isFromASTFile())
4987 return 0;
4988 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4989 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4990 return I->second;
4991}
4992
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004993SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4994 if (ID < NUM_PREDEF_DECL_IDS)
4995 return SourceLocation();
4996
4997 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4998
4999 if (Index > DeclsLoaded.size()) {
5000 Error("declaration ID out-of-range for AST file");
5001 return SourceLocation();
5002 }
5003
5004 if (Decl *D = DeclsLoaded[Index])
5005 return D->getLocation();
5006
5007 unsigned RawLocation = 0;
5008 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
5009 return ReadSourceLocation(*Rec.F, RawLocation);
5010}
5011
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005012Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005013 if (ID < NUM_PREDEF_DECL_IDS) {
5014 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005015 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005016 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005017
5018 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005019 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00005020
5021 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005022 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00005023
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005024 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005025 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00005026
Douglas Gregor79d67262011-08-12 05:59:41 +00005027 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005028 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005029
Douglas Gregora6ea10e2012-01-17 18:09:05 +00005030 case PREDEF_DECL_OBJC_PROTOCOL_ID:
5031 return Context.getObjCProtocolDecl();
5032
Douglas Gregor772eeae2011-08-12 06:49:56 +00005033 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005034 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00005035
5036 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005037 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00005038
5039 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00005040 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00005041
5042 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
5043 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005044 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00005045 }
5046
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005047 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5048
Richard Smith2fbf3732011-12-20 04:39:57 +00005049 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005050 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005051 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005052 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005053 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005054
Douglas Gregorfd002a72011-12-16 22:37:11 +00005055 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00005056 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00005057 if (DeserializationListener)
5058 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5059 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005060
5061 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005062}
5063
Douglas Gregora1be2782011-12-17 23:38:30 +00005064DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5065 DeclID GlobalID) {
5066 if (GlobalID < NUM_PREDEF_DECL_IDS)
5067 return GlobalID;
5068
5069 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5070 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5071 ModuleFile *Owner = I->second;
5072
5073 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5074 = M.GlobalToLocalDeclIDs.find(Owner);
5075 if (Pos == M.GlobalToLocalDeclIDs.end())
5076 return 0;
5077
5078 return GlobalID - Owner->BaseDeclID + Pos->second;
5079}
5080
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005081serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005082 const RecordData &Record,
5083 unsigned &Idx) {
5084 if (Idx >= Record.size()) {
5085 Error("Corrupted AST file");
5086 return 0;
5087 }
5088
5089 return getGlobalDeclID(F, Record[Idx++]);
5090}
5091
Chris Lattner887e2b32009-04-27 05:46:25 +00005092/// \brief Resolve the offset of a statement into a statement.
5093///
5094/// This operation will read a new statement from the external
5095/// source each time it is called, and is meant to be used via a
5096/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005097Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005098 // Switch case IDs are per Decl.
5099 ClearSwitchCaseIDs();
5100
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00005101 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005102 RecordLocation Loc = getLocalBitOffset(Offset);
5103 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5104 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00005105}
5106
Douglas Gregor851c75a2011-08-24 21:27:34 +00005107namespace {
5108 class FindExternalLexicalDeclsVisitor {
5109 ASTReader &Reader;
5110 const DeclContext *DC;
5111 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005112
Douglas Gregor851c75a2011-08-24 21:27:34 +00005113 SmallVectorImpl<Decl*> &Decls;
5114 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5115
5116 public:
5117 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5118 bool (*isKindWeWant)(Decl::Kind),
5119 SmallVectorImpl<Decl*> &Decls)
5120 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5121 {
5122 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5123 PredefsVisited[I] = false;
5124 }
5125
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005126 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00005127 if (Preorder)
5128 return false;
5129
5130 FindExternalLexicalDeclsVisitor *This
5131 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5132
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005133 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00005134 = M.DeclContextInfos.find(This->DC);
5135 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5136 return false;
5137
5138 // Load all of the declaration IDs
5139 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5140 *IDE = ID + Info->second.NumLexicalDecls;
5141 ID != IDE; ++ID) {
5142 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5143 continue;
5144
5145 // Don't add predefined declarations to the lexical context more
5146 // than once.
5147 if (ID->second < NUM_PREDEF_DECL_IDS) {
5148 if (This->PredefsVisited[ID->second])
5149 continue;
5150
5151 This->PredefsVisited[ID->second] = true;
5152 }
5153
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005154 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5155 if (!This->DC->isDeclInLexicalTraversal(D))
5156 This->Decls.push_back(D);
5157 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00005158 }
5159
5160 return false;
5161 }
5162 };
5163}
5164
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005165ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00005166 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00005167 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005168 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00005169 // least. Walk all of the modules in the order they were loaded.
5170 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5171 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005172 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005173 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005174}
5175
Douglas Gregor0d95f772011-08-24 19:03:07 +00005176namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005177
5178class DeclIDComp {
5179 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005180 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005181
5182public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005183 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005184
5185 bool operator()(LocalDeclID L, LocalDeclID R) const {
5186 SourceLocation LHS = getLocation(L);
5187 SourceLocation RHS = getLocation(R);
5188 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5189 }
5190
5191 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5192 SourceLocation RHS = getLocation(R);
5193 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5194 }
5195
5196 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5197 SourceLocation LHS = getLocation(L);
5198 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5199 }
5200
5201 SourceLocation getLocation(LocalDeclID ID) const {
5202 return Reader.getSourceManager().getFileLoc(
5203 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5204 }
5205};
5206
5207}
5208
5209void ASTReader::FindFileRegionDecls(FileID File,
5210 unsigned Offset, unsigned Length,
5211 SmallVectorImpl<Decl *> &Decls) {
5212 SourceManager &SM = getSourceManager();
5213
5214 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5215 if (I == FileDeclIDs.end())
5216 return;
5217
5218 FileDeclsInfo &DInfo = I->second;
5219 if (DInfo.Decls.empty())
5220 return;
5221
5222 SourceLocation
5223 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5224 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5225
5226 DeclIDComp DIDComp(*this, *DInfo.Mod);
5227 ArrayRef<serialization::LocalDeclID>::iterator
5228 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5229 BeginLoc, DIDComp);
5230 if (BeginIt != DInfo.Decls.begin())
5231 --BeginIt;
5232
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005233 // If we are pointing at a top-level decl inside an objc container, we need
5234 // to backtrack until we find it otherwise we will fail to report that the
5235 // region overlaps with an objc container.
5236 while (BeginIt != DInfo.Decls.begin() &&
5237 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5238 ->isTopLevelDeclInObjCContainer())
5239 --BeginIt;
5240
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005241 ArrayRef<serialization::LocalDeclID>::iterator
5242 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5243 EndLoc, DIDComp);
5244 if (EndIt != DInfo.Decls.end())
5245 ++EndIt;
5246
5247 for (ArrayRef<serialization::LocalDeclID>::iterator
5248 DIt = BeginIt; DIt != EndIt; ++DIt)
5249 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5250}
5251
5252namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005253 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005254 /// declaration context.
5255 class DeclContextNameLookupVisitor {
5256 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005257 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005258 DeclarationName Name;
5259 SmallVectorImpl<NamedDecl *> &Decls;
5260
5261 public:
5262 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005263 SmallVectorImpl<const DeclContext *> &Contexts,
5264 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005265 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005266 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005267
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005268 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005269 DeclContextNameLookupVisitor *This
5270 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5271
5272 // Check whether we have any visible declaration information for
5273 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005274 ModuleFile::DeclContextInfosMap::iterator Info;
5275 bool FoundInfo = false;
5276 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5277 Info = M.DeclContextInfos.find(This->Contexts[I]);
5278 if (Info != M.DeclContextInfos.end() &&
5279 Info->second.NameLookupTableData) {
5280 FoundInfo = true;
5281 break;
5282 }
5283 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005284
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005285 if (!FoundInfo)
5286 return false;
5287
Douglas Gregor0d95f772011-08-24 19:03:07 +00005288 // Look for this name within this module.
5289 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005290 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005291 ASTDeclContextNameLookupTable::iterator Pos
5292 = LookupTable->find(This->Name);
5293 if (Pos == LookupTable->end())
5294 return false;
5295
5296 bool FoundAnything = false;
5297 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5298 for (; Data.first != Data.second; ++Data.first) {
5299 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5300 if (!ND)
5301 continue;
5302
5303 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005304 // A name might be null because the decl's redeclarable part is
5305 // currently read before reading its name. The lookup is triggered by
5306 // building that decl (likely indirectly), and so it is later in the
5307 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005308 continue;
5309 }
5310
5311 // Record this declaration.
5312 FoundAnything = true;
5313 This->Decls.push_back(ND);
5314 }
5315
5316 return FoundAnything;
5317 }
5318 };
5319}
5320
John McCall76bd1f32010-06-01 09:23:16 +00005321DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005322ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005323 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005324 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005325 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005326 if (!Name)
5327 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5328 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005329
Chris Lattner5f9e2722011-07-23 10:55:15 +00005330 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005331
5332 // Compute the declaration contexts we need to look into. Multiple such
5333 // declaration contexts occur when two declaration contexts from disjoint
5334 // modules get merged, e.g., when two namespaces with the same name are
5335 // independently defined in separate modules.
5336 SmallVector<const DeclContext *, 2> Contexts;
5337 Contexts.push_back(DC);
5338
5339 if (DC->isNamespace()) {
5340 MergedDeclsMap::iterator Merged
5341 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5342 if (Merged != MergedDecls.end()) {
5343 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5344 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5345 }
5346 }
5347
5348 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005349 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005350 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005351 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005352 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005353}
5354
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005355namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005356 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005357 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005358 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005359 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005360 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005361 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005362
5363 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005364 DeclContextAllNamesVisitor(ASTReader &Reader,
5365 SmallVectorImpl<const DeclContext *> &Contexts,
5366 llvm::DenseMap<DeclarationName,
5367 SmallVector<NamedDecl *, 8> > &Decls)
5368 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005369
5370 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005371 DeclContextAllNamesVisitor *This
5372 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005373
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005374 // Check whether we have any visible declaration information for
5375 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005376 ModuleFile::DeclContextInfosMap::iterator Info;
5377 bool FoundInfo = false;
5378 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5379 Info = M.DeclContextInfos.find(This->Contexts[I]);
5380 if (Info != M.DeclContextInfos.end() &&
5381 Info->second.NameLookupTableData) {
5382 FoundInfo = true;
5383 break;
5384 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005385 }
5386
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005387 if (!FoundInfo)
5388 return false;
5389
5390 ASTDeclContextNameLookupTable *LookupTable =
5391 Info->second.NameLookupTableData;
5392 bool FoundAnything = false;
5393 for (ASTDeclContextNameLookupTable::data_iterator
5394 I = LookupTable->data_begin(), E = LookupTable->data_end();
5395 I != E; ++I) {
5396 ASTDeclContextNameLookupTrait::data_type Data = *I;
5397 for (; Data.first != Data.second; ++Data.first) {
5398 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5399 *Data.first);
5400 if (!ND)
5401 continue;
5402
5403 // Record this declaration.
5404 FoundAnything = true;
5405 This->Decls[ND->getDeclName()].push_back(ND);
5406 }
5407 }
5408
5409 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005410 }
5411 };
5412}
5413
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005414void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005415 if (!DC->hasExternalVisibleStorage())
5416 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005417 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5418
5419 // Compute the declaration contexts we need to look into. Multiple such
5420 // declaration contexts occur when two declaration contexts from disjoint
5421 // modules get merged, e.g., when two namespaces with the same name are
5422 // independently defined in separate modules.
5423 SmallVector<const DeclContext *, 2> Contexts;
5424 Contexts.push_back(DC);
5425
5426 if (DC->isNamespace()) {
5427 MergedDeclsMap::iterator Merged
5428 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5429 if (Merged != MergedDecls.end()) {
5430 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5431 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5432 }
5433 }
5434
5435 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5436 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5437 ++NumVisibleDeclContextsRead;
5438
5439 for (llvm::DenseMap<DeclarationName,
5440 llvm::SmallVector<NamedDecl*, 8> >::iterator
5441 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5442 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5443 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005444 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005445}
5446
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005447/// \brief Under non-PCH compilation the consumer receives the objc methods
5448/// before receiving the implementation, and codegen depends on this.
5449/// We simulate this by deserializing and passing to consumer the methods of the
5450/// implementation before passing the deserialized implementation decl.
5451static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5452 ASTConsumer *Consumer) {
5453 assert(ImplD && Consumer);
5454
5455 for (ObjCImplDecl::method_iterator
5456 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005457 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005458
5459 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5460}
5461
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005462void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005463 assert(Consumer);
5464 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005465 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005466 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005467
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005468 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005469 }
5470}
5471
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005472void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5473 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5474 PassObjCImplDeclToConsumer(ImplD, Consumer);
5475 else
5476 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5477}
5478
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005479void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005480 this->Consumer = Consumer;
5481
Douglas Gregorfdd01722009-04-14 00:24:19 +00005482 if (!Consumer)
5483 return;
5484
5485 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005486 // Force deserialization of this decl, which will cause it to be queued for
5487 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005488 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005489 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005490 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005491
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005492 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005493}
5494
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005495void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005496 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005497
Mike Stump1eb44332009-09-09 15:08:12 +00005498 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005499 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005500 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005501 unsigned NumDeclsLoaded
5502 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5503 (Decl *)0);
5504 unsigned NumIdentifiersLoaded
5505 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5506 IdentifiersLoaded.end(),
5507 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005508 unsigned NumMacrosLoaded
5509 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5510 MacrosLoaded.end(),
5511 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005512 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005513 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5514 SelectorsLoaded.end(),
5515 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005516
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005517 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005518 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5519 NumSLocEntriesRead, TotalNumSLocEntries,
5520 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005521 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005522 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005523 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5524 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5525 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005526 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005527 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5528 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005529 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005530 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005531 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5532 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005533 if (!MacrosLoaded.empty())
5534 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5535 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5536 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005537 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005538 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005539 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5540 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005541 if (TotalNumStatements)
5542 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5543 NumStatementsRead, TotalNumStatements,
5544 ((float)NumStatementsRead/TotalNumStatements * 100));
5545 if (TotalNumMacros)
5546 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5547 NumMacrosRead, TotalNumMacros,
5548 ((float)NumMacrosRead/TotalNumMacros * 100));
5549 if (TotalLexicalDeclContexts)
5550 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5551 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5552 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5553 * 100));
5554 if (TotalVisibleDeclContexts)
5555 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5556 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5557 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5558 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005559 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005560 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005561 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5562 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005563 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005564 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005565 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005566 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005567 dump();
5568 std::fprintf(stderr, "\n");
5569}
5570
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005571template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005572static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005573dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005574 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005575 InitialCapacity> &Map) {
5576 if (Map.begin() == Map.end())
5577 return;
5578
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005579 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005580 llvm::errs() << Name << ":\n";
5581 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5582 I != IEnd; ++I) {
5583 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5584 << "\n";
5585 }
5586}
5587
Douglas Gregor23d7df52011-07-21 19:50:14 +00005588void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005589 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005590 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005591 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005592 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005593 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005594 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005595 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005596 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005597 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005598 dumpModuleIDMap("Global preprocessed entity map",
5599 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005600
5601 llvm::errs() << "\n*** PCH/Modules Loaded:";
5602 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5603 MEnd = ModuleMgr.end();
5604 M != MEnd; ++M)
5605 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005606}
5607
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005608/// Return the amount of memory used by memory buffers, breaking down
5609/// by heap-backed versus mmap'ed memory.
5610void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005611 for (ModuleConstIterator I = ModuleMgr.begin(),
5612 E = ModuleMgr.end(); I != E; ++I) {
5613 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005614 size_t bytes = buf->getBufferSize();
5615 switch (buf->getBufferKind()) {
5616 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5617 sizes.malloc_bytes += bytes;
5618 break;
5619 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5620 sizes.mmap_bytes += bytes;
5621 break;
5622 }
5623 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005624 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005625}
5626
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005627void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005628 SemaObj = &S;
Axel Naumann0ec56b72012-10-18 19:05:02 +00005629 S.addExternalSource(this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005630
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005631 // Makes sure any declarations that were deserialized "too early"
5632 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005633 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005634 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5635 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005636 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005637 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005638
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005639 // Load the offsets of the declarations that Sema references.
5640 // They will be lazily deserialized when needed.
5641 if (!SemaDeclRefs.empty()) {
5642 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005643 if (!SemaObj->StdNamespace)
5644 SemaObj->StdNamespace = SemaDeclRefs[0];
5645 if (!SemaObj->StdBadAlloc)
5646 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005647 }
5648
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005649 if (!FPPragmaOptions.empty()) {
5650 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5651 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5652 }
5653
5654 if (!OpenCLExtensions.empty()) {
5655 unsigned I = 0;
5656#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5657#include "clang/Basic/OpenCLExtensions.def"
5658
5659 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5660 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005661}
5662
Douglas Gregor211f6e82011-08-20 04:39:52 +00005663IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005664 // Note that we are loading an identifier.
5665 Deserializing AnIdentifier(this);
5666
Douglas Gregor057df202012-01-18 20:56:22 +00005667 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5668 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005669 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005670 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005671 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005672 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005673}
5674
Douglas Gregor95f42922010-10-14 22:11:03 +00005675namespace clang {
5676 /// \brief An identifier-lookup iterator that enumerates all of the
5677 /// identifiers stored within a set of AST files.
5678 class ASTIdentifierIterator : public IdentifierIterator {
5679 /// \brief The AST reader whose identifiers are being enumerated.
5680 const ASTReader &Reader;
5681
5682 /// \brief The current index into the chain of AST files stored in
5683 /// the AST reader.
5684 unsigned Index;
5685
5686 /// \brief The current position within the identifier lookup table
5687 /// of the current AST file.
5688 ASTIdentifierLookupTable::key_iterator Current;
5689
5690 /// \brief The end position within the identifier lookup table of
5691 /// the current AST file.
5692 ASTIdentifierLookupTable::key_iterator End;
5693
5694 public:
5695 explicit ASTIdentifierIterator(const ASTReader &Reader);
5696
Chris Lattner5f9e2722011-07-23 10:55:15 +00005697 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005698 };
5699}
5700
5701ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005702 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005703 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005704 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005705 Current = IdTable->key_begin();
5706 End = IdTable->key_end();
5707}
5708
Chris Lattner5f9e2722011-07-23 10:55:15 +00005709StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005710 while (Current == End) {
5711 // If we have exhausted all of our AST files, we're done.
5712 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005713 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005714
5715 --Index;
5716 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005717 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5718 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005719 Current = IdTable->key_begin();
5720 End = IdTable->key_end();
5721 }
5722
5723 // We have any identifiers remaining in the current AST file; return
5724 // the next one.
5725 std::pair<const char*, unsigned> Key = *Current;
5726 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005727 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005728}
5729
5730IdentifierIterator *ASTReader::getIdentifiers() const {
5731 return new ASTIdentifierIterator(*this);
5732}
5733
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005734namespace clang { namespace serialization {
5735 class ReadMethodPoolVisitor {
5736 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005737 Selector Sel;
5738 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005739 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5740 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005741
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005742 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005743 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5744 unsigned PriorGeneration)
5745 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005746
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005747 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005748 ReadMethodPoolVisitor *This
5749 = static_cast<ReadMethodPoolVisitor *>(UserData);
5750
5751 if (!M.SelectorLookupTable)
5752 return false;
5753
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005754 // If we've already searched this module file, skip it now.
5755 if (M.Generation <= This->PriorGeneration)
5756 return true;
5757
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005758 ASTSelectorLookupTable *PoolTable
5759 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5760 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5761 if (Pos == PoolTable->end())
5762 return false;
5763
5764 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005765 // FIXME: Not quite happy with the statistics here. We probably should
5766 // disable this tracking when called via LoadSelector.
5767 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005768 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005769 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005770 if (This->Reader.DeserializationListener)
5771 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5772 This->Sel);
5773
5774 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5775 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5776 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005777 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005778
5779 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005780 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5781 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005782 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005783
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005784 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005785 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5786 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005787 }
5788 };
5789} } // end namespace clang::serialization
5790
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005791/// \brief Add the given set of methods to the method list.
5792static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5793 ObjCMethodList &List) {
5794 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5795 S.addMethodToGlobalList(&List, Methods[I]);
5796 }
5797}
5798
5799void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005800 // Get the selector generation and update it to the current generation.
5801 unsigned &Generation = SelectorGeneration[Sel];
5802 unsigned PriorGeneration = Generation;
5803 Generation = CurrentGeneration;
5804
5805 // Search for methods defined with this selector.
5806 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005807 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005808
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005809 if (Visitor.getInstanceMethods().empty() &&
5810 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005811 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005812 return;
5813 }
5814
5815 if (!getSema())
5816 return;
5817
5818 Sema &S = *getSema();
5819 Sema::GlobalMethodPool::iterator Pos
5820 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5821
5822 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5823 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005824}
5825
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005826void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005827 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005828 Namespaces.clear();
5829
5830 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5831 if (NamespaceDecl *Namespace
5832 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5833 Namespaces.push_back(Namespace);
5834 }
5835}
5836
Douglas Gregora8623202011-07-27 20:58:46 +00005837void ASTReader::ReadTentativeDefinitions(
5838 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5839 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5840 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5841 if (Var)
5842 TentativeDefs.push_back(Var);
5843 }
5844 TentativeDefinitions.clear();
5845}
5846
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005847void ASTReader::ReadUnusedFileScopedDecls(
5848 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5849 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5850 DeclaratorDecl *D
5851 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5852 if (D)
5853 Decls.push_back(D);
5854 }
5855 UnusedFileScopedDecls.clear();
5856}
5857
Douglas Gregor0129b562011-07-27 21:57:17 +00005858void ASTReader::ReadDelegatingConstructors(
5859 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5860 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5861 CXXConstructorDecl *D
5862 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5863 if (D)
5864 Decls.push_back(D);
5865 }
5866 DelegatingCtorDecls.clear();
5867}
5868
Douglas Gregord58a0a52011-07-28 00:39:29 +00005869void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5870 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5871 TypedefNameDecl *D
5872 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5873 if (D)
5874 Decls.push_back(D);
5875 }
5876 ExtVectorDecls.clear();
5877}
5878
Douglas Gregora126f172011-07-28 00:53:40 +00005879void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5880 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5881 CXXRecordDecl *D
5882 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5883 if (D)
5884 Decls.push_back(D);
5885 }
5886 DynamicClasses.clear();
5887}
5888
Douglas Gregorec12ce22011-07-28 14:20:37 +00005889void
5890ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5891 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5892 NamedDecl *D
5893 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5894 if (D)
5895 Decls.push_back(D);
5896 }
5897 LocallyScopedExternalDecls.clear();
5898}
5899
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005900void ASTReader::ReadReferencedSelectors(
5901 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5902 if (ReferencedSelectorsData.empty())
5903 return;
5904
5905 // If there are @selector references added them to its pool. This is for
5906 // implementation of -Wselector.
5907 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5908 unsigned I = 0;
5909 while (I < DataSize) {
5910 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5911 SourceLocation SelLoc
5912 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5913 Sels.push_back(std::make_pair(Sel, SelLoc));
5914 }
5915 ReferencedSelectorsData.clear();
5916}
5917
Douglas Gregor31e37b22011-07-28 18:09:57 +00005918void ASTReader::ReadWeakUndeclaredIdentifiers(
5919 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5920 if (WeakUndeclaredIdentifiers.empty())
5921 return;
5922
5923 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5924 IdentifierInfo *WeakId
5925 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5926 IdentifierInfo *AliasId
5927 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5928 SourceLocation Loc
5929 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5930 bool Used = WeakUndeclaredIdentifiers[I++];
5931 WeakInfo WI(AliasId, Loc);
5932 WI.setUsed(Used);
5933 WeakIDs.push_back(std::make_pair(WeakId, WI));
5934 }
5935 WeakUndeclaredIdentifiers.clear();
5936}
5937
Douglas Gregordfe65432011-07-28 19:11:31 +00005938void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5939 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5940 ExternalVTableUse VT;
5941 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5942 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5943 VT.DefinitionRequired = VTableUses[Idx++];
5944 VTables.push_back(VT);
5945 }
5946
5947 VTableUses.clear();
5948}
5949
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005950void ASTReader::ReadPendingInstantiations(
5951 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5952 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5953 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5954 SourceLocation Loc
5955 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005956
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005957 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005958 }
5959 PendingInstantiations.clear();
5960}
5961
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005962void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005963 // It would be complicated to avoid reading the methods anyway. So don't.
5964 ReadMethodPool(Sel);
5965}
5966
Douglas Gregor95eab172011-07-28 20:55:49 +00005967void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005968 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005969 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005970 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005971 if (DeserializationListener)
5972 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005973}
5974
Douglas Gregord89275b2009-07-06 18:54:52 +00005975/// \brief Set the globally-visible declarations associated with the given
5976/// identifier.
5977///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005978/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005979/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005980/// them.
5981///
5982/// \param II an IdentifierInfo that refers to one or more globally-visible
5983/// declarations.
5984///
5985/// \param DeclIDs the set of declaration IDs with the name @p II that are
5986/// visible at global scope.
5987///
5988/// \param Nonrecursive should be true to indicate that the caller knows that
5989/// this call is non-recursive, and therefore the globally-visible declarations
5990/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005991void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005992ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005993 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005994 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005995 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005996 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5997 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5998 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005999 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00006000 return;
6001 }
Mike Stump1eb44332009-09-09 15:08:12 +00006002
Douglas Gregord89275b2009-07-06 18:54:52 +00006003 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
6004 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
6005 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00006006 // Introduce this declaration into the translation-unit scope
6007 // and add it to the declaration chain for this identifier, so
6008 // that (unqualified) name lookup will find it.
6009 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00006010 } else {
6011 // Queue this declaration so that it will be added to the
6012 // translation unit scope and identifier's declaration chain
6013 // once a Sema object is known.
6014 PreloadedDecls.push_back(D);
6015 }
6016 }
6017}
6018
Douglas Gregor95eab172011-07-28 20:55:49 +00006019IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00006020 if (ID == 0)
6021 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00006022
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006023 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006024 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00006025 return 0;
6026 }
Mike Stump1eb44332009-09-09 15:08:12 +00006027
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006028 ID -= 1;
6029 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00006030 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
6031 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006032 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006033 unsigned Index = ID - M->BaseIdentifierID;
6034 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00006035
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006036 // All of the strings in the AST file are preceded by a 16-bit length.
6037 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00006038 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
6039 // unsigned integers. This is important to avoid integer overflow when
6040 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00006041 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00006042 unsigned StrLen = (((unsigned) StrLenPtr[0])
6043 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006044 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006045 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006046 if (DeserializationListener)
6047 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00006048 }
Mike Stump1eb44332009-09-09 15:08:12 +00006049
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006050 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00006051}
6052
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006053IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00006054 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6055}
6056
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006057IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00006058 if (LocalID < NUM_PREDEF_IDENT_IDS)
6059 return LocalID;
6060
6061 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6062 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6063 assert(I != M.IdentifierRemap.end()
6064 && "Invalid index into identifier index remap");
6065
6066 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00006067}
6068
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006069MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00006070 if (ID == 0)
6071 return 0;
6072
6073 if (MacrosLoaded.empty()) {
6074 Error("no macro table in AST file");
6075 return 0;
6076 }
6077
6078 ID -= NUM_PREDEF_MACRO_IDS;
6079 if (!MacrosLoaded[ID]) {
6080 GlobalMacroMapType::iterator I
6081 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6082 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6083 ModuleFile *M = I->second;
6084 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006085 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00006086 }
6087
6088 return MacrosLoaded[ID];
6089}
6090
6091MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6092 if (LocalID < NUM_PREDEF_MACRO_IDS)
6093 return LocalID;
6094
6095 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6096 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6097 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6098
6099 return LocalID + I->second;
6100}
6101
Douglas Gregor26ced122011-12-01 00:59:36 +00006102serialization::SubmoduleID
6103ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6104 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6105 return LocalID;
6106
6107 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6108 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6109 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006110 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00006111
6112 return LocalID + I->second;
6113}
6114
6115Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6116 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6117 assert(GlobalID == 0 && "Unhandled global submodule ID");
6118 return 0;
6119 }
6120
6121 if (GlobalID > SubmodulesLoaded.size()) {
6122 Error("submodule ID out of range in AST file");
6123 return 0;
6124 }
6125
6126 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6127}
6128
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006129Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006130 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6131}
6132
6133Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006134 if (ID == 0)
6135 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00006136
Sebastian Redl725cd962010-08-04 20:40:17 +00006137 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006138 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006139 return Selector();
6140 }
Douglas Gregor83941df2009-04-25 17:48:32 +00006141
Sebastian Redl725cd962010-08-04 20:40:17 +00006142 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00006143 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00006144 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6145 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006146 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006147 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006148 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00006149 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00006150 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00006151 if (DeserializationListener)
6152 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00006153 }
6154
Sebastian Redl725cd962010-08-04 20:40:17 +00006155 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006156}
6157
Douglas Gregor8451ec72011-07-28 14:41:43 +00006158Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00006159 return DecodeSelector(ID);
6160}
6161
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006162uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00006163 // ID 0 (the null selector) is considered an external selector.
6164 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00006165}
6166
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006167serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006168ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006169 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6170 return LocalID;
6171
6172 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6173 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6174 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006175 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006176
6177 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00006178}
6179
Mike Stump1eb44332009-09-09 15:08:12 +00006180DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006181ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00006182 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00006183 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6184 switch (Kind) {
6185 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00006186 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006187
6188 case DeclarationName::ObjCZeroArgSelector:
6189 case DeclarationName::ObjCOneArgSelector:
6190 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006191 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006192
6193 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006194 return Context.DeclarationNames.getCXXConstructorName(
6195 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006196
6197 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006198 return Context.DeclarationNames.getCXXDestructorName(
6199 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006200
6201 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006202 return Context.DeclarationNames.getCXXConversionFunctionName(
6203 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006204
6205 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006206 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006207 (OverloadedOperatorKind)Record[Idx++]);
6208
Sean Hunt3e518bd2009-11-29 07:34:05 +00006209 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006210 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006211 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006212
Douglas Gregor2cf26342009-04-09 22:27:44 +00006213 case DeclarationName::CXXUsingDirective:
6214 return DeclarationName::getUsingDirectiveName();
6215 }
6216
David Blaikie7530c032012-01-17 06:56:22 +00006217 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006218}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006219
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006220void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006221 DeclarationNameLoc &DNLoc,
6222 DeclarationName Name,
6223 const RecordData &Record, unsigned &Idx) {
6224 switch (Name.getNameKind()) {
6225 case DeclarationName::CXXConstructorName:
6226 case DeclarationName::CXXDestructorName:
6227 case DeclarationName::CXXConversionFunctionName:
6228 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6229 break;
6230
6231 case DeclarationName::CXXOperatorName:
6232 DNLoc.CXXOperatorName.BeginOpNameLoc
6233 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6234 DNLoc.CXXOperatorName.EndOpNameLoc
6235 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6236 break;
6237
6238 case DeclarationName::CXXLiteralOperatorName:
6239 DNLoc.CXXLiteralOperatorName.OpNameLoc
6240 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6241 break;
6242
6243 case DeclarationName::Identifier:
6244 case DeclarationName::ObjCZeroArgSelector:
6245 case DeclarationName::ObjCOneArgSelector:
6246 case DeclarationName::ObjCMultiArgSelector:
6247 case DeclarationName::CXXUsingDirective:
6248 break;
6249 }
6250}
6251
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006252void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006253 DeclarationNameInfo &NameInfo,
6254 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006255 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006256 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6257 DeclarationNameLoc DNLoc;
6258 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6259 NameInfo.setInfo(DNLoc);
6260}
6261
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006262void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006263 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006264 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006265 unsigned NumTPLists = Record[Idx++];
6266 Info.NumTemplParamLists = NumTPLists;
6267 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006268 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006269 for (unsigned i=0; i != NumTPLists; ++i)
6270 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6271 }
6272}
6273
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006274TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006275ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006276 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006277 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006278 switch (Kind) {
6279 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006280 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006281
6282 case TemplateName::OverloadedTemplate: {
6283 unsigned size = Record[Idx++];
6284 UnresolvedSet<8> Decls;
6285 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006286 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006287
Douglas Gregor35942772011-09-09 21:34:22 +00006288 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006289 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006290
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006291 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006292 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006293 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006294 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006295 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006296 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006297
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006298 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006299 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006300 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006301 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006302 GetIdentifierInfo(F, Record,
6303 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006304 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006305 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006306 }
John McCall14606042011-06-30 08:33:18 +00006307
6308 case TemplateName::SubstTemplateTemplateParm: {
6309 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006310 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006311 if (!param) return TemplateName();
6312 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006313 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006314 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006315
6316 case TemplateName::SubstTemplateTemplateParmPack: {
6317 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006318 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006319 if (!Param)
6320 return TemplateName();
6321
6322 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6323 if (ArgPack.getKind() != TemplateArgument::Pack)
6324 return TemplateName();
6325
Douglas Gregor35942772011-09-09 21:34:22 +00006326 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006327 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006328 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006329
David Blaikieb219cfc2011-09-23 05:06:16 +00006330 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006331}
6332
6333TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006334ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006335 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006336 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6337 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006338 case TemplateArgument::Null:
6339 return TemplateArgument();
6340 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006341 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006342 case TemplateArgument::Declaration: {
6343 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6344 bool ForReferenceParam = Record[Idx++];
6345 return TemplateArgument(D, ForReferenceParam);
6346 }
6347 case TemplateArgument::NullPtr:
6348 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006349 case TemplateArgument::Integral: {
6350 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006351 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006352 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006353 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006354 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006355 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006356 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006357 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006358 llvm::Optional<unsigned> NumTemplateExpansions;
6359 if (unsigned NumExpansions = Record[Idx++])
6360 NumTemplateExpansions = NumExpansions - 1;
6361 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006362 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006363 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006364 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006365 case TemplateArgument::Pack: {
6366 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006367 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006368 for (unsigned I = 0; I != NumArgs; ++I)
6369 Args[I] = ReadTemplateArgument(F, Record, Idx);
6370 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006371 }
6372 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006373
David Blaikieb219cfc2011-09-23 05:06:16 +00006374 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006375}
6376
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006377TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006378ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006379 const RecordData &Record, unsigned &Idx) {
6380 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6381 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6382 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006383
6384 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006385 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006386 Params.reserve(NumParams);
6387 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006388 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006389
6390 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006391 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006392 Params.data(), Params.size(), RAngleLoc);
6393 return TemplateParams;
6394}
6395
6396void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006397ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006398ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006399 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006400 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006401 unsigned NumTemplateArgs = Record[Idx++];
6402 TemplArgs.reserve(NumTemplateArgs);
6403 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006404 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006405}
6406
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006407/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006408void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006409 const RecordData &Record, unsigned &Idx) {
6410 unsigned NumDecls = Record[Idx++];
6411 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006412 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006413 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6414 Set.addDecl(D, AS);
6415 }
6416}
6417
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006418CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006419ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006420 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006421 bool isVirtual = static_cast<bool>(Record[Idx++]);
6422 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6423 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006424 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006425 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6426 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006427 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006428 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006429 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006430 Result.setInheritConstructors(inheritConstructors);
6431 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006432}
6433
Sean Huntcbb67482011-01-08 20:30:50 +00006434std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006435ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006436 unsigned &Idx) {
6437 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006438 unsigned NumInitializers = Record[Idx++];
6439 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006440 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006441 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006442 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006443 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006444 bool IsBaseVirtual = false;
6445 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006446 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006447
Sean Hunt156b6402011-05-04 01:19:08 +00006448 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6449 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006450 case CTOR_INITIALIZER_BASE:
6451 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006452 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006453 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006454
6455 case CTOR_INITIALIZER_DELEGATING:
6456 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006457 break;
6458
6459 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006460 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006461 break;
6462
6463 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006464 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006465 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006466 }
Sean Hunt156b6402011-05-04 01:19:08 +00006467
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006468 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006469 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006470 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6471 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006472 bool IsWritten = Record[Idx++];
6473 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006474 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006475 if (IsWritten) {
6476 SourceOrderOrNumArrayIndices = Record[Idx++];
6477 } else {
6478 SourceOrderOrNumArrayIndices = Record[Idx++];
6479 Indices.reserve(SourceOrderOrNumArrayIndices);
6480 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006481 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006482 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006483
Sean Huntcbb67482011-01-08 20:30:50 +00006484 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006485 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006486 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006487 LParenLoc, Init, RParenLoc,
6488 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006489 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006490 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6491 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006492 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006493 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006494 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006495 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006496 else
Douglas Gregor35942772011-09-09 21:34:22 +00006497 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006498 MemberOrEllipsisLoc, LParenLoc,
6499 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006500 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006501 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006502 LParenLoc, Init, RParenLoc,
6503 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006504 }
6505
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006506 if (IsWritten)
6507 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006508 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006509 }
6510 }
6511
Sean Huntcbb67482011-01-08 20:30:50 +00006512 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006513}
6514
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006515NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006516ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006517 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006518 unsigned N = Record[Idx++];
6519 NestedNameSpecifier *NNS = 0, *Prev = 0;
6520 for (unsigned I = 0; I != N; ++I) {
6521 NestedNameSpecifier::SpecifierKind Kind
6522 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6523 switch (Kind) {
6524 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006525 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006526 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006527 break;
6528 }
6529
6530 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006531 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006532 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006533 break;
6534 }
6535
Douglas Gregor14aba762011-02-24 02:36:08 +00006536 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006537 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006538 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006539 break;
6540 }
6541
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006542 case NestedNameSpecifier::TypeSpec:
6543 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006544 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006545 if (!T)
6546 return 0;
6547
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006548 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006549 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006550 break;
6551 }
6552
6553 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006554 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006555 // No associated value, and there can't be a prefix.
6556 break;
6557 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006558 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006559 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006560 }
6561 return NNS;
6562}
6563
Douglas Gregordc355712011-02-25 00:36:19 +00006564NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006565ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006566 unsigned &Idx) {
6567 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006568 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006569 for (unsigned I = 0; I != N; ++I) {
6570 NestedNameSpecifier::SpecifierKind Kind
6571 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6572 switch (Kind) {
6573 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006574 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006575 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006576 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006577 break;
6578 }
6579
6580 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006581 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006582 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006583 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006584 break;
6585 }
6586
6587 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006588 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006589 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006590 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006591 break;
6592 }
6593
6594 case NestedNameSpecifier::TypeSpec:
6595 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006596 bool Template = Record[Idx++];
6597 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6598 if (!T)
6599 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006600 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006601
6602 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006603 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006604 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6605 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006606 break;
6607 }
6608
6609 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006610 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006611 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006612 break;
6613 }
6614 }
Douglas Gregordc355712011-02-25 00:36:19 +00006615 }
6616
Douglas Gregor35942772011-09-09 21:34:22 +00006617 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006618}
6619
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006620SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006621ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006622 unsigned &Idx) {
6623 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6624 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006625 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006626}
6627
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006628/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006629llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006630 unsigned BitWidth = Record[Idx++];
6631 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6632 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6633 Idx += NumWords;
6634 return Result;
6635}
6636
6637/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006638llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006639 bool isUnsigned = Record[Idx++];
6640 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6641}
6642
Douglas Gregor17fc2232009-04-14 21:55:33 +00006643/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006644llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006645 return llvm::APFloat(ReadAPInt(Record, Idx));
6646}
6647
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006648// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006649std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006650 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006651 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006652 Idx += Len;
6653 return Result;
6654}
6655
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006656VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6657 unsigned &Idx) {
6658 unsigned Major = Record[Idx++];
6659 unsigned Minor = Record[Idx++];
6660 unsigned Subminor = Record[Idx++];
6661 if (Minor == 0)
6662 return VersionTuple(Major);
6663 if (Subminor == 0)
6664 return VersionTuple(Major, Minor - 1);
6665 return VersionTuple(Major, Minor - 1, Subminor - 1);
6666}
6667
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006668CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006669 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006670 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006671 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006672 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006673}
6674
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006675DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006676 return Diag(SourceLocation(), DiagID);
6677}
6678
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006679DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006680 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006681}
Douglas Gregor025452f2009-04-17 00:04:06 +00006682
Douglas Gregor668c1a42009-04-21 22:25:48 +00006683/// \brief Retrieve the identifier table associated with the
6684/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006685IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006686 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006687}
6688
Douglas Gregor025452f2009-04-17 00:04:06 +00006689/// \brief Record that the given ID maps to the given switch-case
6690/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006691void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006692 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6693 "Already have a SwitchCase with this ID");
6694 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006695}
6696
6697/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006698SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006699 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6700 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006701}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006702
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006703void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006704 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006705}
6706
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006707void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006708 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006709 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6710 serialization::ModuleFile *> >::iterator
6711 I = CommentsCursors.begin(),
6712 E = CommentsCursors.end();
6713 I != E; ++I) {
6714 llvm::BitstreamCursor &Cursor = I->first;
6715 serialization::ModuleFile &F = *I->second;
6716 SavedStreamPosition SavedPosition(Cursor);
6717
6718 RecordData Record;
6719 while (true) {
6720 unsigned Code = Cursor.ReadCode();
6721 if (Code == llvm::bitc::END_BLOCK)
6722 break;
6723
6724 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6725 // No known subblocks, always skip them.
6726 Cursor.ReadSubBlockID();
6727 if (Cursor.SkipBlock()) {
6728 Error("malformed block record in AST file");
6729 return;
6730 }
6731 continue;
6732 }
6733
6734 if (Code == llvm::bitc::DEFINE_ABBREV) {
6735 Cursor.ReadAbbrevRecord();
6736 continue;
6737 }
6738
6739 // Read a record.
6740 Record.clear();
6741 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006742 case COMMENTS_RAW_COMMENT: {
6743 unsigned Idx = 0;
6744 SourceRange SR = ReadSourceRange(F, Record, Idx);
6745 RawComment::CommentKind Kind =
6746 (RawComment::CommentKind) Record[Idx++];
6747 bool IsTrailingComment = Record[Idx++];
6748 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006749 Comments.push_back(new (Context) RawComment(SR, Kind,
6750 IsTrailingComment,
6751 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006752 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006753 }
6754 }
6755 }
6756 }
6757 Context.Comments.addCommentsToFront(Comments);
6758}
6759
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006760void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006761 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6762 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006763 // If any identifiers with corresponding top-level declarations have
6764 // been loaded, load those declarations now.
6765 while (!PendingIdentifierInfos.empty()) {
6766 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6767 PendingIdentifierInfos.front().DeclIDs, true);
6768 PendingIdentifierInfos.pop_front();
6769 }
6770
Douglas Gregora1be2782011-12-17 23:38:30 +00006771 // Load pending declaration chains.
6772 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6773 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006774 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006775 }
6776 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006777
6778 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00006779 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6780 // FIXME: std::move here
6781 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006782 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00006783 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6784 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006785 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00006786 }
6787 }
6788 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006789 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006790
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006791 // If we deserialized any C++ or Objective-C class definitions, any
6792 // Objective-C protocol definitions, or any redeclarable templates, make sure
6793 // that all redeclarations point to the definitions. Note that this can only
6794 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006795 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6796 DEnd = PendingDefinitions.end();
6797 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006798 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6799 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6800 // Make sure that the TagType points at the definition.
6801 const_cast<TagType*>(TagT)->decl = TD;
6802 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006803
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006804 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6805 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6806 REnd = RD->redecls_end();
6807 R != REnd; ++R)
6808 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6809
6810 }
6811
Douglas Gregorfc529f72011-12-19 19:00:47 +00006812 continue;
6813 }
6814
Douglas Gregor1d784b22012-01-01 19:51:50 +00006815 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006816 // Make sure that the ObjCInterfaceType points at the definition.
6817 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6818 ->Decl = ID;
6819
Douglas Gregor1d784b22012-01-01 19:51:50 +00006820 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6821 REnd = ID->redecls_end();
6822 R != REnd; ++R)
6823 R->Data = ID->Data;
6824
6825 continue;
6826 }
6827
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006828 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6829 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6830 REnd = PD->redecls_end();
6831 R != REnd; ++R)
6832 R->Data = PD->Data;
6833
6834 continue;
6835 }
6836
6837 RedeclarableTemplateDecl *RTD
6838 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6839 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6840 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006841 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006842 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006843 }
6844 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006845
6846 // Load the bodies of any functions or methods we've encountered. We do
6847 // this now (delayed) so that we can be sure that the declaration chains
6848 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006849 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6850 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006851 PB != PBEnd; ++PB) {
6852 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6853 // FIXME: Check for =delete/=default?
6854 // FIXME: Complain about ODR violations here?
6855 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6856 FD->setLazyBody(PB->second);
6857 continue;
6858 }
6859
6860 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6861 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6862 MD->setLazyBody(PB->second);
6863 }
6864 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006865}
6866
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006867void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006868 assert(NumCurrentElementsDeserializing &&
6869 "FinishedDeserializing not paired with StartedDeserializing");
6870 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006871 // We decrease NumCurrentElementsDeserializing only after pending actions
6872 // are finished, to avoid recursively re-calling finishPendingActions().
6873 finishPendingActions();
6874 }
6875 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006876
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006877 if (NumCurrentElementsDeserializing == 0 &&
6878 Consumer && !PassingDeclsToConsumer) {
6879 // Guard variable to avoid recursively redoing the process of passing
6880 // decls to consumer.
6881 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6882 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006883
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006884 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006885 // We are not in recursive loading, so it's safe to pass the "interesting"
6886 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006887 Decl *D = InterestingDecls.front();
6888 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006889 PassInterestingDeclToConsumer(D);
6890 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006891 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006892}
Douglas Gregor501c1032010-08-19 00:28:17 +00006893
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006894ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006895 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00006896 bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006897 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6898 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006899 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006900 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregorcaed0602012-10-18 21:31:35 +00006901 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006902 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006903 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006904 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006905 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6906 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6907 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6908 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006909 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6910 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006911 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006912 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006913{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006914 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006915}
6916
Sebastian Redle1dde812010-08-24 00:50:04 +00006917ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006918 for (DeclContextVisibleUpdatesPending::iterator
6919 I = PendingVisibleUpdates.begin(),
6920 E = PendingVisibleUpdates.end();
6921 I != E; ++I) {
6922 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6923 F = I->second.end();
6924 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006925 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006926 }
6927}