blob: a541e241417ad1fc92fb64bc2f1f948061b2b22d [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"
Steve Naroff83d63c72009-04-24 20:03:17 +000033#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000038#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000039#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000040#include "clang/Basic/TargetOptions.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000041#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000042#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000045#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000046#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000047#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000048#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000049#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000050#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000051#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000052#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000053#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000054#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000055
Douglas Gregor2cf26342009-04-09 22:27:44 +000056using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000057using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000058using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000059
60//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000061// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000062//===----------------------------------------------------------------------===//
63
Sebastian Redl571db7f2010-08-18 23:56:56 +000064ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000065
66bool
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +000067PCHValidator::ReadLanguageOptions(const ModuleFile &M,
Douglas Gregor38295be2012-10-22 23:51:00 +000068 const LangOptions &LangOpts,
69 bool Complain) {
David Blaikie4e4d0842012-03-11 07:00:24 +000070 const LangOptions &PPLangOpts = PP.getLangOpts();
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000071
Douglas Gregor38295be2012-10-22 23:51:00 +000072#define LANGOPT(Name, Bits, Default, Description) \
73 if (PPLangOpts.Name != LangOpts.Name) { \
74 if (Complain) \
75 Reader.Diag(diag::err_pch_langopt_mismatch) \
76 << Description << LangOpts.Name << PPLangOpts.Name; \
77 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000078 }
79
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000080#define VALUE_LANGOPT(Name, Bits, Default, Description) \
81 if (PPLangOpts.Name != LangOpts.Name) { \
Douglas Gregor38295be2012-10-22 23:51:00 +000082 if (Complain) \
83 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
84 << Description; \
85 return true; \
86 }
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000087
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000088#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
89 if (PPLangOpts.get##Name() != LangOpts.get##Name()) { \
Douglas Gregor38295be2012-10-22 23:51:00 +000090 if (Complain) \
91 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
92 << Description; \
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000093 return true; \
94 }
95
96#define BENIGN_LANGOPT(Name, Bits, Default, Description)
97#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
98#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +000099
100 if (PPLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000101 if (Complain)
102 Reader.Diag(diag::err_pch_langopt_value_mismatch)
103 << "target Objective-C runtime";
John McCall260611a2012-06-20 06:18:46 +0000104 return true;
105 }
Douglas Gregor7d5e81b2011-09-13 18:26:39 +0000106
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000107 return false;
108}
109
Douglas Gregor57016dd2012-10-16 23:40:58 +0000110bool PCHValidator::ReadTargetOptions(const ModuleFile &M,
Douglas Gregor38295be2012-10-22 23:51:00 +0000111 const TargetOptions &TargetOpts,
112 bool Complain) {
Douglas Gregor57016dd2012-10-16 23:40:58 +0000113 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000114
Douglas Gregor38295be2012-10-22 23:51:00 +0000115#define CHECK_TARGET_OPT(Field, Name) \
116 if (TargetOpts.Field != ExistingTargetOpts.Field) { \
117 if (Complain) \
118 Reader.Diag(diag::err_pch_targetopt_mismatch) \
119 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \
120 return true; \
Douglas Gregor57016dd2012-10-16 23:40:58 +0000121 }
122
123 CHECK_TARGET_OPT(Triple, "target");
124 CHECK_TARGET_OPT(CPU, "target CPU");
125 CHECK_TARGET_OPT(ABI, "target ABI");
126 CHECK_TARGET_OPT(CXXABI, "target C++ ABI");
127 CHECK_TARGET_OPT(LinkerVersion, "target linker version");
128#undef CHECK_TARGET_OPT
129
130 // Compare feature sets.
131 SmallVector<StringRef, 4> ExistingFeatures(
132 ExistingTargetOpts.FeaturesAsWritten.begin(),
133 ExistingTargetOpts.FeaturesAsWritten.end());
134 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
135 TargetOpts.FeaturesAsWritten.end());
136 std::sort(ExistingFeatures.begin(), ExistingFeatures.end());
137 std::sort(ReadFeatures.begin(), ReadFeatures.end());
138
139 unsigned ExistingIdx = 0, ExistingN = ExistingFeatures.size();
140 unsigned ReadIdx = 0, ReadN = ReadFeatures.size();
141 while (ExistingIdx < ExistingN && ReadIdx < ReadN) {
142 if (ExistingFeatures[ExistingIdx] == ReadFeatures[ReadIdx]) {
143 ++ExistingIdx;
144 ++ReadIdx;
145 continue;
146 }
147
148 if (ReadFeatures[ReadIdx] < ExistingFeatures[ExistingIdx]) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000149 if (Complain)
150 Reader.Diag(diag::err_pch_targetopt_feature_mismatch)
151 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000152 return true;
153 }
154
Douglas Gregor38295be2012-10-22 23:51:00 +0000155 if (Complain)
156 Reader.Diag(diag::err_pch_targetopt_feature_mismatch)
157 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000158 return true;
159 }
160
161 if (ExistingIdx < ExistingN) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000162 if (Complain)
163 Reader.Diag(diag::err_pch_targetopt_feature_mismatch)
164 << true << ExistingFeatures[ExistingIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000165 return true;
166 }
167
168 if (ReadIdx < ReadN) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000169 if (Complain)
170 Reader.Diag(diag::err_pch_targetopt_feature_mismatch)
171 << false << ReadFeatures[ReadIdx];
Douglas Gregor57016dd2012-10-16 23:40:58 +0000172 return true;
173 }
174
175 return false;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000176}
177
Benjamin Kramer54353f42010-11-25 18:29:30 +0000178namespace {
179 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000180 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000181 };
182 struct EmptyBlock {
183 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
184 };
185}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000186
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000188 PCHPredefinesBlocks R) {
189 // First, sum up the lengths.
190 unsigned LL = 0, RL = 0;
191 for (unsigned I = 0, N = L.size(); I != N; ++I) {
192 LL += L[I].size();
193 }
194 for (unsigned I = 0, N = R.size(); I != N; ++I) {
195 RL += R[I].Data.size();
196 }
197 if (LL != RL)
198 return false;
199 if (LL == 0 && RL == 0)
200 return true;
201
202 // Kick out empty parts, they confuse the algorithm below.
203 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
204 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
205
206 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000208 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000209 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000210 for (;;) {
211 // Compare the current pieces.
212 if (LR.size() == RR.size()) {
213 // If they're the same length, it's pretty easy.
214 if (LR != RR)
215 return false;
216 // Both pieces are done, advance.
217 ++LI;
218 ++RI;
219 // If either string is done, they're both done, since they're the same
220 // length.
221 if (LI == LN) {
222 assert(RI == RN && "Strings not the same length after all?");
223 return true;
224 }
225 LR = L[LI];
226 RR = R[RI].Data;
227 } else if (LR.size() < RR.size()) {
228 // Right piece is longer.
229 if (!RR.startswith(LR))
230 return false;
231 ++LI;
232 assert(LI != LN && "Strings not the same length after all?");
233 RR = RR.substr(LR.size());
234 LR = L[LI];
235 } else {
236 // Left piece is longer.
237 if (!LR.startswith(RR))
238 return false;
239 ++RI;
240 assert(RI != RN && "Strings not the same length after all?");
241 LR = LR.substr(RR.size());
242 RR = R[RI].Data;
243 }
244 }
245}
246
Chris Lattner5f9e2722011-07-23 10:55:15 +0000247static std::pair<FileID, StringRef::size_type>
248FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
249 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000250 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
251 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000252 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000253 Res.first = Buffers[I].BufferID;
254 break;
255 }
256 }
257 return Res;
258}
259
260bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000261 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000262 std::string &SuggestedPredefines,
Douglas Gregor38295be2012-10-22 23:51:00 +0000263 FileManager &FileMgr,
264 bool Complain) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000265 // We are in the context of an implicit include, so the predefines buffer will
266 // have a #include entry for the PCH file itself (as normalized by the
267 // preprocessor initialization). Find it and skip over it in the checking
268 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000269 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000270 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000271 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
272 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000273 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000274 std::pair<StringRef,StringRef> Split =
275 StringRef(PP.getPredefines()).split(PCHInclude.str());
276 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000277 if (Left == PP.getPredefines()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000278 if (Complain)
279 Error("Missing PCH include entry!");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000280 return true;
281 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000282
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000283 // If the concatenation of all the PCH buffers is equal to the adjusted
284 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000285 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000286 CommandLine.push_back(Left);
287 CommandLine.push_back(Right);
288 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000289 return false;
290
291 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000293 // The predefines buffers are different. Determine what the differences are,
294 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000295 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000296 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
297 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000298
Chris Lattner5f9e2722011-07-23 10:55:15 +0000299 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000300 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000301
302 // Pick out implicit #includes after the PCH and don't consider them for
303 // validation; we will insert them into SuggestedPredefines so that the
304 // preprocessor includes them.
305 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000306 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000307 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
308 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
309 if (AfterPCHLines[i].startswith("#include ")) {
310 IncludesAfterPCH += AfterPCHLines[i];
311 IncludesAfterPCH += '\n';
312 } else {
313 CmdLineLines.push_back(AfterPCHLines[i]);
314 }
315 }
316
317 // Make sure we add the includes last into SuggestedPredefines before we
318 // exit this function.
319 struct AddIncludesRAII {
320 std::string &SuggestedPredefines;
321 std::string &IncludesAfterPCH;
322
323 AddIncludesRAII(std::string &SuggestedPredefines,
324 std::string &IncludesAfterPCH)
325 : SuggestedPredefines(SuggestedPredefines),
326 IncludesAfterPCH(IncludesAfterPCH) { }
327 ~AddIncludesRAII() {
328 SuggestedPredefines += IncludesAfterPCH;
329 }
330 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000331
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000332 // Sort both sets of predefined buffer lines, since we allow some extra
333 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000334 std::sort(CmdLineLines.begin(), CmdLineLines.end());
335 std::sort(PCHLines.begin(), PCHLines.end());
336
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000337 // Determine which predefines that were used to build the PCH file are missing
338 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000339 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000340 std::set_difference(PCHLines.begin(), PCHLines.end(),
341 CmdLineLines.begin(), CmdLineLines.end(),
342 std::back_inserter(MissingPredefines));
343
344 bool MissingDefines = false;
345 bool ConflictingDefines = false;
346 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000347 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000348 if (Missing.startswith("#include ")) {
349 // An -include was specified when generating the PCH; it is included in
350 // the PCH, just ignore it.
351 continue;
352 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000353 if (!Missing.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000354 if (Complain)
355 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000356 return true;
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000359 // This is a macro definition. Determine the name of the macro we're
360 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000362 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 = Missing.find_first_of("( \n\r", StartOfMacroName);
364 assert(EndOfMacroName != std::string::npos &&
365 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000366 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000367
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000368 // Determine whether this macro was given a different definition on the
369 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000370 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000372 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000373 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
374 MacroDefStart);
375 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000376 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000377 // Different macro; we're done.
378 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000379 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000380 }
Mike Stump1eb44332009-09-09 15:08:12 +0000381
382 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000383 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000384 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000385 (*ConflictPos)[MacroDefLen] != '(')
386 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000388 // We found a conflicting macro definition.
389 break;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 if (ConflictPos != CmdLineLines.end()) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000393 if (!Complain)
394 return true;
395
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000396 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
397 << MacroName;
398
399 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000400 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000401 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000402 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000403 SourceLocation PCHMissingLoc =
404 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000405 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000406 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000407
408 ConflictingDefines = true;
409 continue;
410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000412 // If the macro doesn't conflict, then we'll just pick up the macro
413 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000414 if (ConflictingDefines)
415 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000417 if (!MissingDefines) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000418 if (!Complain)
419 return true;
420
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000421 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
422 MissingDefines = true;
423 }
424
Douglas Gregor38295be2012-10-22 23:51:00 +0000425 if (!Complain)
426 return true;
427
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000428 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000429 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000430 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000431 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000432 SourceLocation PCHMissingLoc =
433 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000434 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000435 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000438 if (ConflictingDefines)
439 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000441 // Determine what predefines were introduced based on command-line
442 // parameters that were not present when building the PCH
443 // file. Extra #defines are okay, so long as the identifiers being
444 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000445 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000446 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
447 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000448 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000449 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000450 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000451 if (!Extra.startswith("#define ")) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000452 if (Complain)
453 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000454 return true;
455 }
456
457 // This is an extra macro definition. Determine the name of the
458 // macro we're defining.
459 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000460 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000461 = Extra.find_first_of("( \n\r", StartOfMacroName);
462 assert(EndOfMacroName != std::string::npos &&
463 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000464 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000465
466 // Check whether this name was used somewhere in the PCH file. If
467 // so, defining it as a macro could change behavior, so we reject
468 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000469 if (IdentifierInfo *II = Reader.get(MacroName)) {
Douglas Gregor38295be2012-10-22 23:51:00 +0000470 if (Complain)
471 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000472 return true;
473 }
474
475 // Add this definition to the suggested predefines buffer.
476 SuggestedPredefines += Extra;
477 SuggestedPredefines += '\n';
478 }
479
480 // If we get here, it's because the predefines buffer had compatible
481 // contents. Accept the PCH file.
482 return false;
483}
484
Douglas Gregor12fab312010-03-16 16:35:32 +0000485void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
486 unsigned ID) {
487 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
488 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000489}
490
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000491void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000492 PP.setCounterValue(Value);
493}
494
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000495//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000496// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000497//===----------------------------------------------------------------------===//
498
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000499void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000500ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000501 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000502}
503
Chris Lattner4c6f9522009-04-27 05:14:47 +0000504
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000505
Douglas Gregor98339b92011-08-25 20:47:51 +0000506unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
507 return serialization::ComputeHash(Sel);
508}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000509
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor98339b92011-08-25 20:47:51 +0000511std::pair<unsigned, unsigned>
512ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
513 using namespace clang::io;
514 unsigned KeyLen = ReadUnalignedLE16(d);
515 unsigned DataLen = ReadUnalignedLE16(d);
516 return std::make_pair(KeyLen, DataLen);
517}
518
519ASTSelectorLookupTrait::internal_key_type
520ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
521 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000522 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000523 unsigned N = ReadUnalignedLE16(d);
524 IdentifierInfo *FirstII
525 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
526 if (N == 0)
527 return SelTable.getNullarySelector(FirstII);
528 else if (N == 1)
529 return SelTable.getUnarySelector(FirstII);
530
531 SmallVector<IdentifierInfo *, 16> Args;
532 Args.push_back(FirstII);
533 for (unsigned I = 1; I != N; ++I)
534 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
535
536 return SelTable.getSelector(N, Args.data());
537}
538
539ASTSelectorLookupTrait::data_type
540ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
541 unsigned DataLen) {
542 using namespace clang::io;
543
544 data_type Result;
545
546 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
547 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
548 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
549
550 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000551 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
552 if (ObjCMethodDecl *Method
553 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
554 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor98339b92011-08-25 20:47:51 +0000557 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000558 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
559 if (ObjCMethodDecl *Method
560 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
561 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000562 }
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Douglas Gregor98339b92011-08-25 20:47:51 +0000564 return Result;
565}
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Douglas Gregor98339b92011-08-25 20:47:51 +0000567unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
568 return llvm::HashString(StringRef(a.first, a.second));
569}
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregor98339b92011-08-25 20:47:51 +0000571std::pair<unsigned, unsigned>
572ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
573 using namespace clang::io;
574 unsigned DataLen = ReadUnalignedLE16(d);
575 unsigned KeyLen = ReadUnalignedLE16(d);
576 return std::make_pair(KeyLen, DataLen);
577}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000578
Douglas Gregor98339b92011-08-25 20:47:51 +0000579std::pair<const char*, unsigned>
580ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
581 assert(n >= 2 && d[n-1] == '\0');
582 return std::make_pair((const char*) d, n-1);
583}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000584
Douglas Gregor98339b92011-08-25 20:47:51 +0000585IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
586 const unsigned char* d,
587 unsigned DataLen) {
588 using namespace clang::io;
589 unsigned RawID = ReadUnalignedLE32(d);
590 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Douglas Gregor98339b92011-08-25 20:47:51 +0000592 // Wipe out the "is interesting" bit.
593 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000594
Douglas Gregor98339b92011-08-25 20:47:51 +0000595 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
596 if (!IsInteresting) {
597 // For uninteresting identifiers, just build the IdentifierInfo
598 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000599 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000600 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000601 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000602 KnownII = II;
603 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000604 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000605 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000606 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000607 return II;
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000610 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000611 unsigned Bits = ReadUnalignedLE16(d);
612 bool CPlusPlusOperatorKeyword = Bits & 0x01;
613 Bits >>= 1;
614 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
615 Bits >>= 1;
616 bool Poisoned = Bits & 0x01;
617 Bits >>= 1;
618 bool ExtensionToken = Bits & 0x01;
619 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000620 bool hadMacroDefinition = Bits & 0x01;
621 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000622
Douglas Gregor98339b92011-08-25 20:47:51 +0000623 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000624 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000625
Douglas Gregor98339b92011-08-25 20:47:51 +0000626 // Build the IdentifierInfo itself and link the identifier ID with
627 // the new IdentifierInfo.
628 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000629 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000630 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000631 KnownII = II;
632 }
Douglas Gregor057df202012-01-18 20:56:22 +0000633 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000634 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000635
Douglas Gregor98339b92011-08-25 20:47:51 +0000636 // Set or check the various bits in the IdentifierInfo structure.
637 // Token IDs are read-only.
638 if (HasRevertedTokenIDToIdentifier)
639 II->RevertTokenIDToIdentifier();
640 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
641 assert(II->isExtensionToken() == ExtensionToken &&
642 "Incorrect extension token flag");
643 (void)ExtensionToken;
644 if (Poisoned)
645 II->setIsPoisoned(true);
646 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
647 "Incorrect C++ operator keyword flag");
648 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000649
Douglas Gregor98339b92011-08-25 20:47:51 +0000650 // If this identifier is a macro, deserialize the macro
651 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000652 if (hadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000653 SmallVector<MacroID, 4> MacroIDs;
654 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
655 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
656 DataLen -= 4;
Douglas Gregor13292642011-12-02 15:45:10 +0000657 }
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000658 DataLen -= 4;
659 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000660 }
661
Douglas Gregoreee242f2011-10-27 09:33:13 +0000662 Reader.SetIdentifierInfo(ID, II);
663
Douglas Gregor98339b92011-08-25 20:47:51 +0000664 // Read all of the declarations visible at global scope with this
665 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000666 if (DataLen > 0) {
667 SmallVector<uint32_t, 4> DeclIDs;
668 for (; DataLen > 0; DataLen -= 4)
669 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
670 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000671 }
672
Douglas Gregor98339b92011-08-25 20:47:51 +0000673 return II;
674}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000675
Douglas Gregor98339b92011-08-25 20:47:51 +0000676unsigned
677ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
678 llvm::FoldingSetNodeID ID;
679 ID.AddInteger(Key.Kind);
680
681 switch (Key.Kind) {
682 case DeclarationName::Identifier:
683 case DeclarationName::CXXLiteralOperatorName:
684 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
685 break;
686 case DeclarationName::ObjCZeroArgSelector:
687 case DeclarationName::ObjCOneArgSelector:
688 case DeclarationName::ObjCMultiArgSelector:
689 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
690 break;
691 case DeclarationName::CXXOperatorName:
692 ID.AddInteger((OverloadedOperatorKind)Key.Data);
693 break;
694 case DeclarationName::CXXConstructorName:
695 case DeclarationName::CXXDestructorName:
696 case DeclarationName::CXXConversionFunctionName:
697 case DeclarationName::CXXUsingDirective:
698 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000699 }
700
Douglas Gregor98339b92011-08-25 20:47:51 +0000701 return ID.ComputeHash();
702}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000703
Douglas Gregor98339b92011-08-25 20:47:51 +0000704ASTDeclContextNameLookupTrait::internal_key_type
705ASTDeclContextNameLookupTrait::GetInternalKey(
706 const external_key_type& Name) const {
707 DeclNameKey Key;
708 Key.Kind = Name.getNameKind();
709 switch (Name.getNameKind()) {
710 case DeclarationName::Identifier:
711 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
712 break;
713 case DeclarationName::ObjCZeroArgSelector:
714 case DeclarationName::ObjCOneArgSelector:
715 case DeclarationName::ObjCMultiArgSelector:
716 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
717 break;
718 case DeclarationName::CXXOperatorName:
719 Key.Data = Name.getCXXOverloadedOperator();
720 break;
721 case DeclarationName::CXXLiteralOperatorName:
722 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
723 break;
724 case DeclarationName::CXXConstructorName:
725 case DeclarationName::CXXDestructorName:
726 case DeclarationName::CXXConversionFunctionName:
727 case DeclarationName::CXXUsingDirective:
728 Key.Data = 0;
729 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000730 }
731
Douglas Gregor98339b92011-08-25 20:47:51 +0000732 return Key;
733}
734
Douglas Gregor98339b92011-08-25 20:47:51 +0000735std::pair<unsigned, unsigned>
736ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
737 using namespace clang::io;
738 unsigned KeyLen = ReadUnalignedLE16(d);
739 unsigned DataLen = ReadUnalignedLE16(d);
740 return std::make_pair(KeyLen, DataLen);
741}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000742
Douglas Gregor98339b92011-08-25 20:47:51 +0000743ASTDeclContextNameLookupTrait::internal_key_type
744ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
745 using namespace clang::io;
746
747 DeclNameKey Key;
748 Key.Kind = (DeclarationName::NameKind)*d++;
749 switch (Key.Kind) {
750 case DeclarationName::Identifier:
751 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
752 break;
753 case DeclarationName::ObjCZeroArgSelector:
754 case DeclarationName::ObjCOneArgSelector:
755 case DeclarationName::ObjCMultiArgSelector:
756 Key.Data =
757 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
758 .getAsOpaquePtr();
759 break;
760 case DeclarationName::CXXOperatorName:
761 Key.Data = *d++; // OverloadedOperatorKind
762 break;
763 case DeclarationName::CXXLiteralOperatorName:
764 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
765 break;
766 case DeclarationName::CXXConstructorName:
767 case DeclarationName::CXXDestructorName:
768 case DeclarationName::CXXConversionFunctionName:
769 case DeclarationName::CXXUsingDirective:
770 Key.Data = 0;
771 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000772 }
773
Douglas Gregor98339b92011-08-25 20:47:51 +0000774 return Key;
775}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000776
Douglas Gregor98339b92011-08-25 20:47:51 +0000777ASTDeclContextNameLookupTrait::data_type
778ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
779 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000780 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000781 using namespace clang::io;
782 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000783 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000784 return std::make_pair(Start, Start + NumDecls);
785}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000786
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000787bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000788 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000789 const std::pair<uint64_t, uint64_t> &Offsets,
790 DeclContextInfo &Info) {
791 SavedStreamPosition SavedPosition(Cursor);
792 // First the lexical decls.
793 if (Offsets.first != 0) {
794 Cursor.JumpToBit(Offsets.first);
795
796 RecordData Record;
797 const char *Blob;
798 unsigned BlobLen;
799 unsigned Code = Cursor.ReadCode();
800 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
801 if (RecCode != DECL_CONTEXT_LEXICAL) {
802 Error("Expected lexical block");
803 return true;
804 }
805
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000806 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
807 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000808 }
809
810 // Now the lookup table.
811 if (Offsets.second != 0) {
812 Cursor.JumpToBit(Offsets.second);
813
814 RecordData Record;
815 const char *Blob;
816 unsigned BlobLen;
817 unsigned Code = Cursor.ReadCode();
818 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
819 if (RecCode != DECL_CONTEXT_VISIBLE) {
820 Error("Expected visible lookup table block");
821 return true;
822 }
823 Info.NameLookupTableData
824 = ASTDeclContextNameLookupTable::Create(
825 (const unsigned char *)Blob + Record[0],
826 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000827 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000828 }
829
830 return false;
831}
832
Chris Lattner5f9e2722011-07-23 10:55:15 +0000833void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000834 Error(diag::err_fe_pch_malformed, Msg);
835}
836
837void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000838 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000839 if (Diags.isDiagnosticInFlight())
840 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
841 else
842 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000843}
844
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000845/// \brief Tell the AST listener about the predefines buffers in the chain.
Douglas Gregor38295be2012-10-22 23:51:00 +0000846bool ASTReader::CheckPredefinesBuffers(bool Complain) {
Douglas Gregor11407b82012-10-18 21:18:25 +0000847 if (Listener) {
848 // We only care about the primary module.
849 ModuleFile &M = ModuleMgr.getPrimaryModule();
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000850 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Douglas Gregor11407b82012-10-18 21:18:25 +0000851 M.ActualOriginalSourceFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000852 SuggestedPredefines,
Douglas Gregor38295be2012-10-22 23:51:00 +0000853 FileMgr,
854 Complain);
Douglas Gregor11407b82012-10-18 21:18:25 +0000855 }
Douglas Gregore721f952009-04-28 18:58:38 +0000856 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000857}
858
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000859//===----------------------------------------------------------------------===//
860// Source Manager Deserialization
861//===----------------------------------------------------------------------===//
862
Douglas Gregorbd945002009-04-13 16:31:14 +0000863/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000864/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000865bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000866 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000867 unsigned Idx = 0;
868 LineTableInfo &LineTable = SourceMgr.getLineTable();
869
870 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000871 std::map<int, int> FileIDs;
872 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000873 // Extract the file name
874 unsigned FilenameLen = Record[Idx++];
875 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
876 Idx += FilenameLen;
Douglas Gregorcaed0602012-10-18 21:31:35 +0000877 MaybeAddSystemRootToFilename(F, Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000878 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000879 }
880
881 // Parse the line entries
882 std::vector<LineEntry> Entries;
883 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000884 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000885 assert(FID >= 0 && "Serialized line entries for non-local file.");
886 // Remap FileID from 1-based old view.
887 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000888
889 // Extract the line entries
890 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000891 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000892 Entries.clear();
893 Entries.reserve(NumEntries);
894 for (unsigned I = 0; I != NumEntries; ++I) {
895 unsigned FileOffset = Record[Idx++];
896 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000897 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000898 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000899 = (SrcMgr::CharacteristicKind)Record[Idx++];
900 unsigned IncludeOffset = Record[Idx++];
901 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
902 FileKind, IncludeOffset));
903 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000904 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000905 }
906
907 return false;
908}
909
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000910namespace {
911
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000912class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000913public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000914 const ino_t ino;
915 const dev_t dev;
916 const mode_t mode;
917 const time_t mtime;
918 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000920 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000921 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922};
923
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000924class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000925 public:
926 typedef const char *external_key_type;
927 typedef const char *internal_key_type;
928
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000929 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930
931 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000932 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000933 }
934
935 static internal_key_type GetInternalKey(const char *path) { return path; }
936
937 static bool EqualKey(internal_key_type a, internal_key_type b) {
938 return strcmp(a, b) == 0;
939 }
940
941 static std::pair<unsigned, unsigned>
942 ReadKeyDataLength(const unsigned char*& d) {
943 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
944 unsigned DataLen = (unsigned) *d++;
945 return std::make_pair(KeyLen + 1, DataLen);
946 }
947
948 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
949 return (const char *)d;
950 }
951
952 static data_type ReadData(const internal_key_type, const unsigned char *d,
953 unsigned /*DataLen*/) {
954 using namespace clang::io;
955
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000956 ino_t ino = (ino_t) ReadUnalignedLE32(d);
957 dev_t dev = (dev_t) ReadUnalignedLE32(d);
958 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000959 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000960 off_t size = (off_t) ReadUnalignedLE64(d);
961 return data_type(ino, dev, mode, mtime, size);
962 }
963};
964
965/// \brief stat() cache for precompiled headers.
966///
967/// This cache is very similar to the stat cache used by pretokenized
968/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000969class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000970 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000971 CacheTy *Cache;
972
973 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000974public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000975 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
976 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000977 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
978 Cache = CacheTy::Create(Buckets, Base);
979 }
980
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000981 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Chris Lattner898a0612010-11-23 21:17:56 +0000983 LookupResult getStat(const char *Path, struct stat &StatBuf,
984 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000985 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000986 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000987
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000988 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000989 if (I == Cache->end()) {
990 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000991 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000994 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000995 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Chris Lattner10e286a2010-11-23 19:19:34 +0000997 StatBuf.st_ino = Data.ino;
998 StatBuf.st_dev = Data.dev;
999 StatBuf.st_mtime = Data.mtime;
1000 StatBuf.st_mode = Data.mode;
1001 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +00001002 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001003 }
1004};
1005} // end anonymous namespace
1006
1007
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001008/// \brief Read a source manager block
Douglas Gregor4825fd72012-10-22 22:50:17 +00001009bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001010 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001011
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001012 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001013
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001014 // Set the source-location entry cursor to the current position in
1015 // the stream. This cursor will be used to read the contents of the
1016 // source manager block initially, and then lazily read
1017 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001018 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001019
1020 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001021 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001022 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001023 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001024 }
1025
1026 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001027 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001028 Error("malformed source manager block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001029 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001030 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001031
Douglas Gregor14f79002009-04-10 03:52:48 +00001032 RecordData Record;
1033 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001034 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +00001035 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001036 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001037 Error("error at end of Source Manager block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001038 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001039 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00001040 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Douglas Gregor14f79002009-04-10 03:52:48 +00001043 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1044 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001045 SLocEntryCursor.ReadSubBlockID();
1046 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001047 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001048 return true;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001049 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001050 continue;
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Douglas Gregor14f79002009-04-10 03:52:48 +00001053 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001054 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +00001055 continue;
1056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Douglas Gregor14f79002009-04-10 03:52:48 +00001058 // Read a record.
1059 const char *BlobStart;
1060 unsigned BlobLen;
1061 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001062 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001063 default: // Default behavior: ignore.
1064 break;
1065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001066 case SM_SLOC_FILE_ENTRY:
1067 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001068 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001069 // Once we hit one of the source location entries, we're done.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001070 return false;
Douglas Gregor14f79002009-04-10 03:52:48 +00001071 }
1072 }
1073}
1074
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001075/// \brief If a header file is not found at the path that we expect it to be
1076/// and the PCH file was moved from its original location, try to resolve the
1077/// file by assuming that header+PCH were moved together and the header is in
1078/// the same place relative to the PCH.
1079static std::string
1080resolveFileRelativeToOriginalDir(const std::string &Filename,
1081 const std::string &OriginalDir,
1082 const std::string &CurrDir) {
1083 assert(OriginalDir != CurrDir &&
1084 "No point trying to resolve the file if the PCH dir didn't change");
1085 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001086 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001087 fs::make_absolute(filePath);
1088 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001089 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001090
1091 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1092 fileDirE = path::end(path::parent_path(filePath));
1093 path::const_iterator origDirI = path::begin(OriginalDir),
1094 origDirE = path::end(OriginalDir);
1095 // Skip the common path components from filePath and OriginalDir.
1096 while (fileDirI != fileDirE && origDirI != origDirE &&
1097 *fileDirI == *origDirI) {
1098 ++fileDirI;
1099 ++origDirI;
1100 }
1101 for (; origDirI != origDirE; ++origDirI)
1102 path::append(currPCHPath, "..");
1103 path::append(currPCHPath, fileDirI, fileDirE);
1104 path::append(currPCHPath, path::filename(Filename));
1105 return currPCHPath.str();
1106}
1107
Douglas Gregor8b53d142012-10-22 22:53:10 +00001108bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001109 if (ID == 0)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001110 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001112 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001113 Error("source location entry ID out-of-range for AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001114 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001115 }
1116
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001117 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001118 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001119 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001120 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001121
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001122 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001123 unsigned Code = SLocEntryCursor.ReadCode();
1124 if (Code == llvm::bitc::END_BLOCK ||
1125 Code == llvm::bitc::ENTER_SUBBLOCK ||
1126 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001127 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001128 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001129 }
1130
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131 RecordData Record;
1132 const char *BlobStart;
1133 unsigned BlobLen;
1134 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1135 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001136 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001137 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001138
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001139 case SM_SLOC_FILE_ENTRY: {
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001140 // We will detect whether a file changed and return 'Failure' for it, but
1141 // we will also try to fail gracefully by setting up the SLocEntry.
Douglas Gregora930dc92012-10-22 18:42:04 +00001142 unsigned InputID = Record[4];
1143 InputFile IF = getInputFile(*F, InputID);
1144 const FileEntry *File = IF.getPointer();
1145 bool OverriddenBuffer = IF.getInt();
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001146
Douglas Gregora930dc92012-10-22 18:42:04 +00001147 if (!IF.getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001148 return true;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001149
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001150 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001151 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001152 // This is the module's main file.
1153 IncludeLoc = getImportLocation(F);
1154 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001155 SrcMgr::CharacteristicKind
1156 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1157 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001158 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001159 SrcMgr::FileInfo &FileInfo =
1160 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora930dc92012-10-22 18:42:04 +00001161 FileInfo.NumCreatedFIDs = Record[5];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001162 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001163 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001164
Douglas Gregora930dc92012-10-22 18:42:04 +00001165 const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1166 unsigned NumFileDecls = Record[7];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001167 if (NumFileDecls) {
1168 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001169 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1170 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001171 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001172
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001173 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001174 = SourceMgr.getOrCreateContentCache(File,
1175 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001176 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1177 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001178 unsigned Code = SLocEntryCursor.ReadCode();
1179 Record.clear();
1180 unsigned RecCode
1181 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1182
1183 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1184 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001185 return true;
Douglas Gregora081da52011-11-16 20:05:18 +00001186 }
1187
1188 llvm::MemoryBuffer *Buffer
1189 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
Douglas Gregora930dc92012-10-22 18:42:04 +00001190 File->getName());
Douglas Gregora081da52011-11-16 20:05:18 +00001191 SourceMgr.overrideFileContents(File, Buffer);
1192 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001193
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001194 break;
1195 }
1196
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001197 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001198 const char *Name = BlobStart;
1199 unsigned Offset = Record[0];
1200 unsigned Code = SLocEntryCursor.ReadCode();
1201 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001202 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001203 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001204
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001205 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001206 Error("AST record has invalid code");
Douglas Gregor4825fd72012-10-22 22:50:17 +00001207 return true;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001208 }
1209
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001211 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1212 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001213 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1214 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Douglas Gregor6236a292011-12-02 21:56:05 +00001216 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001217 PCHPredefinesBlock Block = {
1218 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001219 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001220 };
1221 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001222 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001223
1224 break;
1225 }
1226
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001227 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001228 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001229 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001230 ReadSourceLocation(*F, Record[2]),
1231 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001232 Record[4],
1233 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001234 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001235 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001236 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001237 }
1238
Douglas Gregor4825fd72012-10-22 22:50:17 +00001239 return false;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001240}
1241
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001242/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001243SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001244 if (F->ImportLoc.isValid())
1245 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001246
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001247 // Otherwise we have a PCH. It's considered to be "imported" at the first
1248 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001249 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001250 // Main file is the importer. We assume that it is the first entry in the
1251 // entry table. We can't ask the manager, because at the time of PCH loading
1252 // the main file entry doesn't exist yet.
1253 // The very first entry is the invalid instantiation loc, which takes up
1254 // offsets 0 and 1.
1255 return SourceLocation::getFromRawEncoding(2U);
1256 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001257 //return F->Loaders[0]->FirstLoc;
1258 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001259}
1260
Chris Lattner6367f6d2009-04-27 01:05:14 +00001261/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1262/// specified cursor. Read the abbreviations that are at the top of the block
1263/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001264bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001265 unsigned BlockID) {
1266 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001267 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001268 return Failure;
1269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Chris Lattner6367f6d2009-04-27 01:05:14 +00001271 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001272 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001273 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001274
Chris Lattner6367f6d2009-04-27 01:05:14 +00001275 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001276 if (Code != llvm::bitc::DEFINE_ABBREV) {
1277 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001278 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001279 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001280 Cursor.ReadAbbrevRecord();
1281 }
1282}
1283
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00001284void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1285 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001286 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Douglas Gregor37e26842009-04-21 23:56:24 +00001288 // Keep track of where we are in the stream, then jump back there
1289 // after reading this macro.
1290 SavedStreamPosition SavedPosition(Stream);
1291
1292 Stream.JumpToBit(Offset);
1293 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001294 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001295 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Douglas Gregore8219a62012-10-11 21:07:39 +00001297 // RAII object to add the loaded macro information once we're done
1298 // adding tokens.
1299 struct AddLoadedMacroInfoRAII {
1300 Preprocessor &PP;
1301 MacroInfo *Hint;
1302 MacroInfo *MI;
1303 IdentifierInfo *II;
1304
1305 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1306 : PP(PP), Hint(Hint), MI(), II() { }
1307 ~AddLoadedMacroInfoRAII( ) {
1308 if (MI) {
1309 // Finally, install the macro.
1310 PP.addLoadedMacroInfo(II, MI, Hint);
1311 }
1312 }
1313 } AddLoadedMacroInfo(PP, Hint);
1314
Douglas Gregor37e26842009-04-21 23:56:24 +00001315 while (true) {
1316 unsigned Code = Stream.ReadCode();
1317 switch (Code) {
1318 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001319 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001320
1321 case llvm::bitc::ENTER_SUBBLOCK:
1322 // No known subblocks, always skip them.
1323 Stream.ReadSubBlockID();
1324 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001325 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001326 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001327 }
1328 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Douglas Gregor37e26842009-04-21 23:56:24 +00001330 case llvm::bitc::DEFINE_ABBREV:
1331 Stream.ReadAbbrevRecord();
1332 continue;
1333 default: break;
1334 }
1335
1336 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001337 const char *BlobStart = 0;
1338 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001339 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001340 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001341 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001342 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001343 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001344 case PP_MACRO_OBJECT_LIKE:
1345 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001346 // If we already have a macro, that means that we've hit the end
1347 // of the definition of the macro we were looking for. We're
1348 // done.
1349 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001350 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001351
Douglas Gregor95eab172011-07-28 20:55:49 +00001352 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001353 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001354 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001355 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001356 }
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Douglas Gregora8235d62012-10-09 23:05:51 +00001358 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1359
1360 // If this macro has already been loaded, don't do so again.
1361 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1362 return;
1363
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001364 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1365 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001366 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001367 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001368
Douglas Gregora8235d62012-10-09 23:05:51 +00001369 // Record this macro.
1370 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1371
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001372 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1373 if (UndefLoc.isValid())
1374 MI->setUndefLoc(UndefLoc);
1375
1376 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001377 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001379 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001380 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001381
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001382 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001383 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001384 bool isC99VarArgs = Record[NextIndex++];
1385 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001386 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001387 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001388 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001389 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001390
1391 // Install function-like macro info.
1392 MI->setIsFunctionLike();
1393 if (isC99VarArgs) MI->setIsC99Varargs();
1394 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001395 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001396 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001397 }
1398
Douglas Gregora8235d62012-10-09 23:05:51 +00001399 if (DeserializationListener)
1400 DeserializationListener->MacroRead(GlobalID, MI);
1401
1402 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001403 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001404 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1405 if (Update != MacroUpdates.end()) {
1406 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001407 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1408 bool Hidden = false;
1409 if (unsigned SubmoduleID = Update->second[I].first) {
1410 if (Module *Owner = getSubmodule(SubmoduleID)) {
1411 if (Owner->NameVisibility == Module::Hidden) {
1412 // Note that this #undef is hidden.
1413 Hidden = true;
1414
1415 // Record this hiding for later.
1416 HiddenNamesMap[Owner].push_back(
1417 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1418 }
1419 }
1420 }
1421
1422 if (!Hidden) {
1423 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1424 if (PPMutationListener *Listener = PP.getPPMutationListener())
1425 Listener->UndefinedMacro(MI);
1426 break;
1427 }
1428 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001429 }
1430 MacroUpdates.erase(Update);
1431 }
1432
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001433 // Determine whether this macro definition is visible.
1434 bool Hidden = !MI->isPublic();
1435 if (!Hidden && GlobalSubmoduleID) {
1436 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1437 if (Owner->NameVisibility == Module::Hidden) {
1438 // The owning module is not visible, and this macro definition
1439 // should not be, either.
1440 Hidden = true;
1441
1442 // Note that this macro definition was hidden because its owning
1443 // module is not yet visible.
1444 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1445 }
1446 }
1447 }
1448 MI->setHidden(Hidden);
1449
Douglas Gregore8219a62012-10-11 21:07:39 +00001450 // Make sure we install the macro once we're done.
1451 AddLoadedMacroInfo.MI = MI;
1452 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001453
1454 // Remember that we saw this macro last so that we add the tokens that
1455 // form its body to it.
1456 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001457
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001458 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1459 Record[NextIndex]) {
1460 // We have a macro definition. Register the association
1461 PreprocessedEntityID
1462 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1463 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1464 PPRec.RegisterMacroDefinition(Macro,
1465 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001466 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001467
Douglas Gregor37e26842009-04-21 23:56:24 +00001468 ++NumMacrosRead;
1469 break;
1470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001472 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001473 // If we see a TOKEN before a PP_MACRO_*, then the file is
1474 // erroneous, just pretend we didn't see this.
1475 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Douglas Gregor37e26842009-04-21 23:56:24 +00001477 Token Tok;
1478 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001479 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001480 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001481 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001482 Tok.setIdentifierInfo(II);
1483 Tok.setKind((tok::TokenKind)Record[3]);
1484 Tok.setFlag((Token::TokenFlags)Record[4]);
1485 Macro->AddTokenToBody(Tok);
1486 break;
1487 }
David Blaikie7530c032012-01-17 06:56:22 +00001488 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001489 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001490}
1491
Douglas Gregor86c67d82011-07-28 22:39:26 +00001492PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001493ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001494 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001495 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1496 assert(I != M.PreprocessedEntityRemap.end()
1497 && "Invalid index into preprocessed entity index remap");
1498
1499 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001500}
1501
Douglas Gregor98339b92011-08-25 20:47:51 +00001502unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1503 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001504}
Douglas Gregor98339b92011-08-25 20:47:51 +00001505
1506HeaderFileInfoTrait::internal_key_type
1507HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1508
1509bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1510 if (strcmp(a, b) == 0)
1511 return true;
1512
1513 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1514 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001515
1516 // Determine whether the actual files are equivalent.
1517 bool Result = false;
1518 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001519 return false;
1520
Douglas Gregor99a922b2011-12-09 16:22:07 +00001521 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001522}
1523
1524std::pair<unsigned, unsigned>
1525HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1526 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1527 unsigned DataLen = (unsigned) *d++;
1528 return std::make_pair(KeyLen + 1, DataLen);
1529}
1530
1531HeaderFileInfoTrait::data_type
1532HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1533 unsigned DataLen) {
1534 const unsigned char *End = d + DataLen;
1535 using namespace clang::io;
1536 HeaderFileInfo HFI;
1537 unsigned Flags = *d++;
1538 HFI.isImport = (Flags >> 5) & 0x01;
1539 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1540 HFI.DirInfo = (Flags >> 2) & 0x03;
1541 HFI.Resolved = (Flags >> 1) & 0x01;
1542 HFI.IndexHeaderMapHeader = Flags & 0x01;
1543 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001544 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1545 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001546 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1547 // The framework offset is 1 greater than the actual offset,
1548 // since 0 is used as an indicator for "no framework name".
1549 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1550 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1551 }
1552
1553 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1554 (void)End;
1555
1556 // This HeaderFileInfo was externally loaded.
1557 HFI.External = true;
1558 return HFI;
1559}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001560
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001561void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1562 II->setHadMacroDefinition(true);
1563 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1564 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001565}
1566
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001567void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001568 // Note that we are loading defined macros.
1569 Deserializing Macros(this);
1570
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001571 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1572 E = ModuleMgr.rend(); I != E; ++I) {
1573 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001574
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001575 // If there was no preprocessor block, skip this file.
1576 if (!MacroCursor.getBitStreamReader())
1577 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001578
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001579 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001580 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001581
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001582 RecordData Record;
1583 while (true) {
1584 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001585 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001586 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001587
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001588 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1589 // No known subblocks, always skip them.
1590 Cursor.ReadSubBlockID();
1591 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001592 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001593 return;
1594 }
1595 continue;
1596 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001597
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001598 if (Code == llvm::bitc::DEFINE_ABBREV) {
1599 Cursor.ReadAbbrevRecord();
1600 continue;
1601 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001602
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001603 // Read a record.
1604 const char *BlobStart;
1605 unsigned BlobLen;
1606 Record.clear();
1607 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1608 default: // Default behavior: ignore.
1609 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001610
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001611 case PP_MACRO_OBJECT_LIKE:
1612 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001613 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001614 break;
1615
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001616 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001617 // Ignore tokens.
1618 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001619 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001620 }
1621 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001622}
1623
Douglas Gregoreee242f2011-10-27 09:33:13 +00001624namespace {
1625 /// \brief Visitor class used to look up identifirs in an AST file.
1626 class IdentifierLookupVisitor {
1627 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001628 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001629 IdentifierInfo *Found;
1630 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001631 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1632 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001633
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001634 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001635 IdentifierLookupVisitor *This
1636 = static_cast<IdentifierLookupVisitor *>(UserData);
1637
Douglas Gregor057df202012-01-18 20:56:22 +00001638 // If we've already searched this module file, skip it now.
1639 if (M.Generation <= This->PriorGeneration)
1640 return true;
1641
Douglas Gregoreee242f2011-10-27 09:33:13 +00001642 ASTIdentifierLookupTable *IdTable
1643 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1644 if (!IdTable)
1645 return false;
1646
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001647 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1648 M, This->Found);
1649
Douglas Gregoreee242f2011-10-27 09:33:13 +00001650 std::pair<const char*, unsigned> Key(This->Name.begin(),
1651 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001652 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001653 if (Pos == IdTable->end())
1654 return false;
1655
1656 // Dereferencing the iterator has the effect of building the
1657 // IdentifierInfo node and populating it with the various
1658 // declarations it needs.
1659 This->Found = *Pos;
1660 return true;
1661 }
1662
1663 // \brief Retrieve the identifier info found within the module
1664 // files.
1665 IdentifierInfo *getIdentifierInfo() const { return Found; }
1666 };
1667}
1668
1669void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001670 // Note that we are loading an identifier.
1671 Deserializing AnIdentifier(this);
1672
Douglas Gregor057df202012-01-18 20:56:22 +00001673 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001674 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001675 PriorGeneration = IdentifierGeneration[&II];
1676
1677 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1678 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1679 markIdentifierUpToDate(&II);
1680}
1681
1682void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1683 if (!II)
1684 return;
1685
1686 II->setOutOfDate(false);
1687
1688 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001689 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001690 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001691}
1692
Douglas Gregora930dc92012-10-22 18:42:04 +00001693llvm::PointerIntPair<const FileEntry *, 1, bool>
Douglas Gregor38295be2012-10-22 23:51:00 +00001694ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
Douglas Gregora930dc92012-10-22 18:42:04 +00001695 // If this ID is bogus, just return an empty input file.
1696 if (ID == 0 || ID > F.InputFilesLoaded.size())
1697 return InputFile();
1698
1699 // If we've already loaded this input file, return it.
1700 if (F.InputFilesLoaded[ID-1].getPointer())
1701 return F.InputFilesLoaded[ID-1];
1702
1703 // Go find this input file.
1704 llvm::BitstreamCursor &Cursor = F.InputFilesCursor;
1705 SavedStreamPosition SavedPosition(Cursor);
1706 Cursor.JumpToBit(F.InputFileOffsets[ID-1]);
1707
1708 unsigned Code = Cursor.ReadCode();
1709 RecordData Record;
1710 const char *BlobStart = 0;
1711 unsigned BlobLen = 0;
1712 switch ((InputFileRecordTypes)Cursor.ReadRecord(Code, Record,
1713 &BlobStart, &BlobLen)) {
1714 case INPUT_FILE: {
1715 unsigned StoredID = Record[0];
1716 assert(ID == StoredID && "Bogus stored ID or offset");
NAKAMURA Takumi6b8194e2012-10-22 21:50:39 +00001717 (void)StoredID;
Douglas Gregora930dc92012-10-22 18:42:04 +00001718 off_t StoredSize = (off_t)Record[1];
1719 time_t StoredTime = (time_t)Record[2];
1720 bool Overridden = (bool)Record[3];
1721
1722 // Get the file entry for this input file.
1723 StringRef OrigFilename(BlobStart, BlobLen);
1724 std::string Filename = OrigFilename;
1725 MaybeAddSystemRootToFilename(F, Filename);
1726 const FileEntry *File
1727 = Overridden? FileMgr.getVirtualFile(Filename, StoredSize, StoredTime)
1728 : FileMgr.getFile(Filename, /*OpenFile=*/false);
1729
1730 // If we didn't find the file, resolve it relative to the
1731 // original directory from which this AST file was created.
1732 if (File == 0 && !F.OriginalDir.empty() && !CurrentDir.empty() &&
1733 F.OriginalDir != CurrentDir) {
1734 std::string Resolved = resolveFileRelativeToOriginalDir(Filename,
1735 F.OriginalDir,
1736 CurrentDir);
1737 if (!Resolved.empty())
1738 File = FileMgr.getFile(Resolved);
1739 }
1740
1741 // For an overridden file, create a virtual file with the stored
1742 // size/timestamp.
1743 if (Overridden && File == 0) {
1744 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
1745 }
1746
1747 if (File == 0) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001748 if (Complain) {
1749 std::string ErrorStr = "could not find file '";
1750 ErrorStr += Filename;
1751 ErrorStr += "' referenced by AST file";
1752 Error(ErrorStr.c_str());
1753 }
Douglas Gregora930dc92012-10-22 18:42:04 +00001754 return InputFile();
1755 }
1756
1757 // Note that we've loaded this input file.
1758 F.InputFilesLoaded[ID-1] = InputFile(File, Overridden);
1759
1760 // Check if there was a request to override the contents of the file
1761 // that was part of the precompiled header. Overridding such a file
1762 // can lead to problems when lexing using the source locations from the
1763 // PCH.
1764 SourceManager &SM = getSourceManager();
1765 if (!Overridden && SM.isFileOverridden(File)) {
1766 Error(diag::err_fe_pch_file_overridden, Filename);
1767 // After emitting the diagnostic, recover by disabling the override so
1768 // that the original file will be used.
1769 SM.disableFileContentsOverride(File);
1770 // The FileEntry is a virtual file entry with the size of the contents
1771 // that would override the original contents. Set it to the original's
1772 // size/time.
1773 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
1774 StoredSize, StoredTime);
1775 }
1776
1777 // For an overridden file, there is nothing to validate.
1778 if (Overridden)
1779 return InputFile(File, Overridden);
1780
1781 // The stat info from the FileEntry came from the cached stat
1782 // info of the PCH, so we cannot trust it.
1783 struct stat StatBuf;
1784 if (::stat(File->getName(), &StatBuf) != 0) {
1785 StatBuf.st_size = File->getSize();
1786 StatBuf.st_mtime = File->getModificationTime();
1787 }
1788
1789 if ((StoredSize != StatBuf.st_size
1790#if !defined(LLVM_ON_WIN32)
1791 // In our regression testing, the Windows file system seems to
1792 // have inconsistent modification times that sometimes
1793 // erroneously trigger this error-handling path.
1794 || StoredTime != StatBuf.st_mtime
1795#endif
1796 )) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001797 if (Complain)
1798 Error(diag::err_fe_pch_file_modified, Filename);
1799
Douglas Gregora930dc92012-10-22 18:42:04 +00001800 return InputFile();
1801 }
1802
1803 return InputFile(File, Overridden);
1804 }
1805 }
1806
1807 return InputFile();
1808}
1809
Chris Lattner5f9e2722011-07-23 10:55:15 +00001810const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Douglas Gregor69e16082012-10-18 21:47:16 +00001811 ModuleFile &M = ModuleMgr.getPrimaryModule();
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001812 std::string Filename = filenameStrRef;
Douglas Gregor69e16082012-10-18 21:47:16 +00001813 MaybeAddSystemRootToFilename(M, Filename);
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001814 const FileEntry *File = FileMgr.getFile(Filename);
Douglas Gregor69e16082012-10-18 21:47:16 +00001815 if (File == 0 && !M.OriginalDir.empty() && !CurrentDir.empty() &&
1816 M.OriginalDir != CurrentDir) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001817 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
Douglas Gregor69e16082012-10-18 21:47:16 +00001818 M.OriginalDir,
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001819 CurrentDir);
1820 if (!resolved.empty())
1821 File = FileMgr.getFile(resolved);
1822 }
1823
1824 return File;
1825}
1826
Douglas Gregore650c8c2009-07-07 00:12:59 +00001827/// \brief If we are loading a relocatable PCH file, and the filename is
1828/// not an absolute path, add the system root to the beginning of the file
1829/// name.
Douglas Gregora930dc92012-10-22 18:42:04 +00001830StringRef ASTReader::MaybeAddSystemRootToFilename(ModuleFile &M,
1831 std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001832 // If this is not a relocatable PCH file, there's nothing to do.
Douglas Gregorcaed0602012-10-18 21:31:35 +00001833 if (!M.RelocatablePCH)
Douglas Gregora930dc92012-10-22 18:42:04 +00001834 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Michael J. Spencer256053b2010-12-17 21:22:22 +00001836 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregora930dc92012-10-22 18:42:04 +00001837 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001838
Douglas Gregor832d6202011-07-22 16:35:34 +00001839 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001840 // If no system root was given, default to '/'
1841 Filename.insert(Filename.begin(), '/');
Douglas Gregora930dc92012-10-22 18:42:04 +00001842 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001843 }
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Douglas Gregor832d6202011-07-22 16:35:34 +00001845 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001846 if (isysroot[Length - 1] != '/')
1847 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Douglas Gregor832d6202011-07-22 16:35:34 +00001849 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregora930dc92012-10-22 18:42:04 +00001850 return Filename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001851}
1852
Douglas Gregor38295be2012-10-22 23:51:00 +00001853ASTReader::ASTReadResult
1854ASTReader::ReadControlBlock(ModuleFile &F,
1855 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
1856 unsigned ClientLoadCapabilities) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001857 llvm::BitstreamCursor &Stream = F.Stream;
1858
1859 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
1860 Error("malformed block record in AST file");
1861 return Failure;
1862 }
1863
1864 // Read all of the records and blocks in the control block.
1865 RecordData Record;
1866 while (!Stream.AtEndOfStream()) {
1867 unsigned Code = Stream.ReadCode();
1868 if (Code == llvm::bitc::END_BLOCK) {
1869 if (Stream.ReadBlockEnd()) {
1870 Error("error at end of control block in AST file");
1871 return Failure;
1872 }
1873
Douglas Gregora930dc92012-10-22 18:42:04 +00001874 // Validate all of the input files.
1875 if (!DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001876 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
Douglas Gregora930dc92012-10-22 18:42:04 +00001877 for (unsigned I = 0, N = Record[0]; I < N; ++I)
Douglas Gregor38295be2012-10-22 23:51:00 +00001878 if (!getInputFile(F, I+1, Complain).getPointer())
Douglas Gregor4825fd72012-10-22 22:50:17 +00001879 return OutOfDate;
Douglas Gregora930dc92012-10-22 18:42:04 +00001880 }
1881
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001882 return Success;
1883 }
1884
1885 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001886 switch (Stream.ReadSubBlockID()) {
1887 case INPUT_FILES_BLOCK_ID:
Douglas Gregora930dc92012-10-22 18:42:04 +00001888 F.InputFilesCursor = Stream;
1889 if (Stream.SkipBlock() || // Skip with the main cursor
1890 // Read the abbreviations
1891 ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
1892 Error("malformed block record in AST file");
Douglas Gregor745e6f12012-10-19 00:38:02 +00001893 return Failure;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001894 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001895 continue;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001896
1897 default:
1898 if (!Stream.SkipBlock())
1899 continue;
1900 break;
1901 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001902
1903 Error("malformed block record in AST file");
1904 return Failure;
1905 }
1906
1907 if (Code == llvm::bitc::DEFINE_ABBREV) {
1908 Stream.ReadAbbrevRecord();
1909 continue;
1910 }
1911
1912 // Read and process a record.
1913 Record.clear();
1914 const char *BlobStart = 0;
1915 unsigned BlobLen = 0;
1916 switch ((ControlRecordTypes)Stream.ReadRecord(Code, Record,
1917 &BlobStart, &BlobLen)) {
1918 case METADATA: {
1919 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001920 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1921 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
1922 : diag::warn_pch_version_too_new);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001923 return VersionMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001924 }
1925
1926 bool hasErrors = Record[5];
1927 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1928 Diag(diag::err_pch_with_compiler_errors);
Douglas Gregor4825fd72012-10-22 22:50:17 +00001929 return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001930 }
1931
Douglas Gregorcaed0602012-10-18 21:31:35 +00001932 F.RelocatablePCH = Record[4];
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001933
1934 const std::string &CurBranch = getClangFullRepositoryVersion();
1935 StringRef ASTBranch(BlobStart, BlobLen);
1936 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Douglas Gregor38295be2012-10-22 23:51:00 +00001937 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
1938 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor4825fd72012-10-22 22:50:17 +00001939 return VersionMismatch;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001940 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001941 break;
1942 }
1943
1944 case IMPORTS: {
1945 // Load each of the imported PCH files.
1946 unsigned Idx = 0, N = Record.size();
1947 while (Idx < N) {
1948 // Read information about the AST file.
1949 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1950 unsigned Length = Record[Idx++];
1951 SmallString<128> ImportedFile(Record.begin() + Idx,
1952 Record.begin() + Idx + Length);
1953 Idx += Length;
1954
1955 // Load the AST file.
Douglas Gregor38295be2012-10-22 23:51:00 +00001956 switch(ReadASTCore(ImportedFile, ImportedKind, &F, Loaded,
1957 ClientLoadCapabilities)) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001958 case Failure: return Failure;
1959 // If we have to ignore the dependency, we'll have to ignore this too.
Douglas Gregor4825fd72012-10-22 22:50:17 +00001960 case OutOfDate: return OutOfDate;
1961 case VersionMismatch: return VersionMismatch;
1962 case ConfigurationMismatch: return ConfigurationMismatch;
1963 case HadErrors: return HadErrors;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001964 case Success: break;
1965 }
1966 }
1967 break;
1968 }
1969
Douglas Gregor38295be2012-10-22 23:51:00 +00001970 case LANGUAGE_OPTIONS: {
1971 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
1972 if (Listener && &F == *ModuleMgr.begin() &&
1973 ParseLanguageOptions(F, Record, Complain) && !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001974 return ConfigurationMismatch;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001975 break;
Douglas Gregor38295be2012-10-22 23:51:00 +00001976 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001977
Douglas Gregoree097c12012-10-18 17:58:09 +00001978 case TARGET_OPTIONS: {
1979 if (Listener && &F == *ModuleMgr.begin()) {
1980 unsigned Idx = 0;
1981 TargetOptions TargetOpts;
1982 TargetOpts.Triple = ReadString(Record, Idx);
1983 TargetOpts.CPU = ReadString(Record, Idx);
1984 TargetOpts.ABI = ReadString(Record, Idx);
1985 TargetOpts.CXXABI = ReadString(Record, Idx);
1986 TargetOpts.LinkerVersion = ReadString(Record, Idx);
1987 for (unsigned N = Record[Idx++]; N; --N) {
1988 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
1989 }
1990 for (unsigned N = Record[Idx++]; N; --N) {
1991 TargetOpts.Features.push_back(ReadString(Record, Idx));
1992 }
1993
Douglas Gregor38295be2012-10-22 23:51:00 +00001994 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
1995 if (Listener->ReadTargetOptions(F, TargetOpts, Complain) &&
1996 !DisableValidation)
Douglas Gregor4825fd72012-10-22 22:50:17 +00001997 return ConfigurationMismatch;
Douglas Gregoree097c12012-10-18 17:58:09 +00001998 }
1999 break;
2000 }
2001
Douglas Gregor39c497b2012-10-18 18:36:53 +00002002 case ORIGINAL_FILE:
Douglas Gregor69e16082012-10-18 21:47:16 +00002003 F.OriginalSourceFileID = FileID::get(Record[0]);
2004 F.ActualOriginalSourceFileName.assign(BlobStart, BlobLen);
2005 F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2006 MaybeAddSystemRootToFilename(F, F.OriginalSourceFileName);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002007 break;
2008
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002009 case ORIGINAL_PCH_DIR:
Douglas Gregor69e16082012-10-18 21:47:16 +00002010 F.OriginalDir.assign(BlobStart, BlobLen);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002011 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002012
Douglas Gregora930dc92012-10-22 18:42:04 +00002013 case INPUT_FILE_OFFSETS:
2014 F.InputFileOffsets = (const uint32_t *)BlobStart;
2015 F.InputFilesLoaded.resize(Record[0]);
Douglas Gregor745e6f12012-10-19 00:38:02 +00002016 break;
2017 }
Douglas Gregor745e6f12012-10-19 00:38:02 +00002018 }
2019
2020 Error("premature end of bitstream in AST file");
2021 return Failure;
2022}
2023
Douglas Gregor4825fd72012-10-22 22:50:17 +00002024bool ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00002025 llvm::BitstreamCursor &Stream = F.Stream;
2026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002027 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002028 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002029 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002030 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002031
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002032 // Read all of the records and blocks for the AST file.
Douglas Gregor8038d512009-04-10 17:25:41 +00002033 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002034 while (!Stream.AtEndOfStream()) {
2035 unsigned Code = Stream.ReadCode();
2036 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002037 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002038 Error("error at end of module block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002039 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002040 }
Chris Lattner7356a312009-04-11 21:15:38 +00002041
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00002042 DeclContext *DC = Context.getTranslationUnitDecl();
2043 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
2044 DC->setMustBuildLookupTable();
2045
Douglas Gregor4825fd72012-10-22 22:50:17 +00002046 return false;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002047 }
2048
2049 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
2050 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002051 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00002052 // We lazily load the decls block, but we want to set up the
2053 // DeclsCursor cursor to point into it. Clone our current bitcode
2054 // cursor to it, enter the block and read the abbrevs in that block.
2055 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00002056 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002057 if (Stream.SkipBlock() || // Skip with the main cursor.
2058 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002059 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002060 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002061 return true;
Chris Lattner6367f6d2009-04-27 01:05:14 +00002062 }
2063 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002064
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002065 case DECL_UPDATES_BLOCK_ID:
2066 if (Stream.SkipBlock()) {
2067 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002068 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002069 }
2070 break;
2071
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002072 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00002073 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002074 if (!PP.getExternalSource())
2075 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00002076
Douglas Gregorecdcb882010-10-20 22:00:55 +00002077 if (Stream.SkipBlock() ||
2078 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002079 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002080 return true;
Chris Lattner7356a312009-04-11 21:15:38 +00002081 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00002082 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00002083 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002084
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002085 case PREPROCESSOR_DETAIL_BLOCK_ID:
2086 F.PreprocessorDetailCursor = Stream;
2087 if (Stream.SkipBlock() ||
2088 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
2089 PREPROCESSOR_DETAIL_BLOCK_ID)) {
2090 Error("malformed preprocessor detail record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002091 return true;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002092 }
2093 F.PreprocessorDetailStartOffset
2094 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002095
2096 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002097 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002098 if (!PP.getPreprocessingRecord()->getExternalSource())
2099 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002100 break;
2101
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002102 case SOURCE_MANAGER_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002103 if (ReadSourceManagerBlock(F))
2104 return true;
Douglas Gregor14f79002009-04-10 03:52:48 +00002105 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002106
2107 case SUBMODULE_BLOCK_ID:
Douglas Gregor4825fd72012-10-22 22:50:17 +00002108 if (ReadSubmoduleBlock(F))
2109 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002110 break;
2111
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002112 case COMMENTS_BLOCK_ID: {
2113 llvm::BitstreamCursor C = Stream;
2114 if (Stream.SkipBlock() ||
2115 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
2116 Error("malformed comments block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002117 return true;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002118 }
2119 CommentsCursors.push_back(std::make_pair(C, &F));
2120 break;
2121 }
2122
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002123 default:
2124 if (!Stream.SkipBlock())
2125 break;
2126 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002127 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002128 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002129 continue;
2130 }
2131
2132 if (Code == llvm::bitc::DEFINE_ABBREV) {
2133 Stream.ReadAbbrevRecord();
2134 continue;
2135 }
2136
2137 // Read and process a record.
2138 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00002139 const char *BlobStart = 0;
2140 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002141 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00002142 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00002143 default: // Default behavior: ignore.
2144 break;
2145
Douglas Gregora119da02011-08-02 16:26:37 +00002146 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002147 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002148 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002149 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002150 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00002151 F.TypeOffsets = (const uint32_t *)BlobStart;
2152 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00002153 unsigned LocalBaseTypeIndex = Record[1];
2154 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00002155
Douglas Gregora119da02011-08-02 16:26:37 +00002156 if (F.LocalNumTypes > 0) {
2157 // Introduce the global -> local mapping for types within this module.
2158 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
2159
2160 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002161 F.TypeRemap.insertOrReplace(
2162 std::make_pair(LocalBaseTypeIndex,
2163 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00002164
2165 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
2166 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002167 break;
Douglas Gregora119da02011-08-02 16:26:37 +00002168 }
2169
Douglas Gregor496c7092011-08-03 15:48:04 +00002170 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00002171 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002172 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002173 return true;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002174 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00002175 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00002176 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00002177 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002178 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00002179
Douglas Gregor496c7092011-08-03 15:48:04 +00002180 if (F.LocalNumDecls > 0) {
2181 // Introduce the global -> local mapping for declarations within this
2182 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002183 GlobalDeclMap.insert(
2184 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00002185
2186 // Introduce the local -> global mapping for declarations within this
2187 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002188 F.DeclRemap.insertOrReplace(
2189 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00002190
Douglas Gregora1be2782011-12-17 23:38:30 +00002191 // Introduce the global -> local mapping for declarations within this
2192 // module.
2193 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
2194
Douglas Gregor496c7092011-08-03 15:48:04 +00002195 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
2196 }
Douglas Gregor8038d512009-04-10 17:25:41 +00002197 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00002198 }
2199
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002200 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00002201 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002202 DeclContextInfo &Info = F.DeclContextInfos[TU];
2203 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
2204 Info.NumLexicalDecls
2205 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00002206 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00002207 break;
2208 }
2209
Sebastian Redle1dde812010-08-24 00:50:04 +00002210 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00002211 unsigned Idx = 0;
2212 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00002213 ASTDeclContextNameLookupTable *Table =
2214 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00002215 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00002216 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00002217 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00002218 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
2219 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00002220 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002221 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00002222 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00002223 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00002224 break;
2225 }
2226
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002227 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002228 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002229 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002230 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002231 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00002232 (const unsigned char *)F.IdentifierTableData + Record[0],
2233 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00002234 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002235
2236 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002237 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002238 break;
2239
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002240 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00002241 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002242 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002243 return true;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002244 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00002245 F.IdentifierOffsets = (const uint32_t *)BlobStart;
2246 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002247 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002248 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00002249
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002250 if (F.LocalNumIdentifiers > 0) {
2251 // Introduce the global -> local mapping for identifiers within this
2252 // module.
2253 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
2254 &F));
2255
2256 // Introduce the local -> global mapping for identifiers within this
2257 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002258 F.IdentifierRemap.insertOrReplace(
2259 std::make_pair(LocalBaseIdentifierID,
2260 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002261
2262 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2263 + F.LocalNumIdentifiers);
2264 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002265 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002266 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002267
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002268 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002269 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2270 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002271 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002272
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002273 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002274 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2275 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002276 break;
2277
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002278 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002279 TotalNumStatements += Record[0];
2280 TotalNumMacros += Record[1];
2281 TotalLexicalDeclContexts += Record[2];
2282 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002283 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002284
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002285 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002286 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2287 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002288 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002289
Sean Huntebcbe1d2011-05-04 23:29:54 +00002290 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002291 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2292 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002293 break;
2294
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002295 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002296 if (Record.size() % 4 != 0) {
2297 Error("invalid weak identifiers record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002298 return true;
Douglas Gregor31e37b22011-07-28 18:09:57 +00002299 }
2300
2301 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2302 // files. This isn't the way to do it :)
2303 WeakUndeclaredIdentifiers.clear();
2304
2305 // Translate the weak, undeclared identifiers into global IDs.
2306 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2307 WeakUndeclaredIdentifiers.push_back(
2308 getGlobalIdentifierID(F, Record[I++]));
2309 WeakUndeclaredIdentifiers.push_back(
2310 getGlobalIdentifierID(F, Record[I++]));
2311 WeakUndeclaredIdentifiers.push_back(
2312 ReadSourceLocation(F, Record, I).getRawEncoding());
2313 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2314 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002315 break;
2316
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002317 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002318 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2319 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002320 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002321
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002322 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002323 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002324 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002325 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002326 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002327
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002328 if (F.LocalNumSelectors > 0) {
2329 // Introduce the global -> local mapping for selectors within this
2330 // module.
2331 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2332
2333 // Introduce the local -> global mapping for selectors within this
2334 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002335 F.SelectorRemap.insertOrReplace(
2336 std::make_pair(LocalBaseSelectorID,
2337 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002338
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002339 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2340 }
2341 break;
2342 }
2343
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002344 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002345 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002346 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002347 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002348 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002349 F.SelectorLookupTableData + Record[0],
2350 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002351 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002352 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002353 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002354
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002355 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002356 if (!Record.empty()) {
2357 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2358 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2359 Record[Idx++]));
2360 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2361 getRawEncoding());
2362 }
2363 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002364 break;
2365
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002366 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002367 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002368 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002369 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002370
2371 case FILE_SORTED_DECLS:
2372 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002373 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002374 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002375
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002376 case SOURCE_LOCATION_OFFSETS: {
2377 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002378 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002379 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002380 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002381 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2382 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002383 // Make our entry in the range map. BaseID is negative and growing, so
2384 // we invert it. Because we invert it, though, we need the other end of
2385 // the range.
2386 unsigned RangeStart =
2387 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2388 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2389 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2390
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002391 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2392 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2393 GlobalSLocOffsetMap.insert(
2394 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2395 - SLocSpaceSize,&F));
2396
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002397 // Initialize the remapping table.
2398 // Invalid stays invalid.
2399 F.SLocRemap.insert(std::make_pair(0U, 0));
2400 // This module. Base was 2 when being compiled.
2401 F.SLocRemap.insert(std::make_pair(2U,
2402 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002403
2404 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002405 break;
2406 }
2407
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002408 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002409 // Additional remapping information.
2410 const unsigned char *Data = (const unsigned char*)BlobStart;
2411 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002412
2413 // Continuous range maps we may be updating in our module.
2414 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002415 ContinuousRangeMap<uint32_t, int, 2>::Builder
2416 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002417 ContinuousRangeMap<uint32_t, int, 2>::Builder
2418 MacroRemap(F.MacroRemap);
2419 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002420 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2421 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002422 SubmoduleRemap(F.SubmoduleRemap);
2423 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002424 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002425 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002426 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2427
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002428 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002429 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002430 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002431 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002432 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002433 if (!OM) {
2434 Error("SourceLocation remap refers to unknown module");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002435 return true;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002436 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002437
2438 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2439 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002440 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002441 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002442 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002443 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2444 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002445 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002446
2447 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2448 SLocRemap.insert(std::make_pair(SLocOffset,
2449 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002450 IdentifierRemap.insert(
2451 std::make_pair(IdentifierIDOffset,
2452 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002453 MacroRemap.insert(std::make_pair(MacroIDOffset,
2454 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002455 PreprocessedEntityRemap.insert(
2456 std::make_pair(PreprocessedEntityIDOffset,
2457 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002458 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2459 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002460 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2461 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002462 DeclRemap.insert(std::make_pair(DeclIDOffset,
2463 OM->BaseDeclID - DeclIDOffset));
2464
Douglas Gregora119da02011-08-02 16:26:37 +00002465 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002466 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002467
2468 // Global -> local mappings.
2469 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002470 }
2471 break;
2472 }
2473
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002474 case SOURCE_MANAGER_LINE_TABLE:
2475 if (ParseLineTable(F, Record))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002476 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002477 break;
2478
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002479 case SOURCE_LOCATION_PRELOADS: {
2480 // Need to transform from the local view (1-based IDs) to the global view,
2481 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002482 if (!F.PreloadSLocEntries.empty()) {
2483 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002484 return true;
Douglas Gregorf249bf32011-08-25 21:09:44 +00002485 }
2486
2487 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002488 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002489 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002490
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002491 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002492 if (!DisableStatCache) {
2493 ASTStatCache *MyStatCache =
2494 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2495 (const unsigned char *)BlobStart,
2496 NumStatHits, NumStatMisses);
2497 FileMgr.addStatCache(MyStatCache);
2498 F.StatCache = MyStatCache;
2499 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002500 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002501 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002502
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002503 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002504 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2505 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002506 break;
2507
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002508 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002509 if (Record.size() % 3 != 0) {
2510 Error("Invalid VTABLE_USES record");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002511 return true;
Douglas Gregordfe65432011-07-28 19:11:31 +00002512 }
2513
Sebastian Redl40566802010-08-05 18:21:25 +00002514 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002515 // FIXME: Modules will have some trouble with this. This is clearly not
2516 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002517 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002518
2519 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2520 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2521 VTableUses.push_back(
2522 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2523 VTableUses.push_back(Record[Idx++]);
2524 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002525 break;
2526
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002527 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002528 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2529 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002530 break;
2531
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002532 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002533 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002534 Error("Invalid existing PendingInstantiations");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002535 return true;
Axel Naumann39d26c32012-10-02 09:09:43 +00002536 }
2537
2538 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002539 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002540 return true;
Douglas Gregorf2abb522011-07-28 19:26:52 +00002541 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002542
Douglas Gregorf2abb522011-07-28 19:26:52 +00002543 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2544 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2545 PendingInstantiations.push_back(
2546 ReadSourceLocation(F, Record, I).getRawEncoding());
2547 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002548 break;
2549
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002550 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002551 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002552 // FIXME: Modules will have some trouble with this.
2553 SemaDeclRefs.clear();
2554 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2555 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002556 break;
2557
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002558 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002559 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2560 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2561 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002562
2563 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002564
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002565 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002566 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002567 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002568 if (!PP.getPreprocessingRecord()->getExternalSource())
2569 PP.getPreprocessingRecord()->SetExternalSource(*this);
2570 StartingID
2571 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002572 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002573 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002574
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002575 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002576 // Introduce the global -> local mapping for preprocessed entities in
2577 // this module.
2578 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2579
2580 // Introduce the local -> global mapping for preprocessed entities in
2581 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002582 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002583 std::make_pair(LocalBasePreprocessedEntityID,
2584 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2585 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002586
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002587 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002588 }
2589
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002590 case DECL_UPDATE_OFFSETS: {
2591 if (Record.size() % 2 != 0) {
2592 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002593 return true;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002594 }
2595 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002596 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2597 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002598 break;
2599 }
2600
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002601 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002602 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002603 Error("invalid DECL_REPLACEMENTS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002604 return true;
Sebastian Redl0b17c612010-08-13 00:28:03 +00002605 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002606 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002607 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002608 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002609 break;
2610 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002611
Douglas Gregorcff9f262012-01-27 01:47:08 +00002612 case OBJC_CATEGORIES_MAP: {
2613 if (F.LocalNumObjCCategoriesInMap != 0) {
2614 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002615 return true;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002616 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002617
2618 F.LocalNumObjCCategoriesInMap = Record[0];
2619 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002620 break;
2621 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002622
Douglas Gregorcff9f262012-01-27 01:47:08 +00002623 case OBJC_CATEGORIES:
2624 F.ObjCCategories.swap(Record);
2625 break;
2626
Douglas Gregor7c789c12010-10-29 22:39:52 +00002627 case CXX_BASE_SPECIFIER_OFFSETS: {
2628 if (F.LocalNumCXXBaseSpecifiers != 0) {
2629 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002630 return true;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002631 }
2632
2633 F.LocalNumCXXBaseSpecifiers = Record[0];
2634 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002635 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002636 break;
2637 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002638
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002639 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002640 if (Record.size() % 2 != 0) {
2641 Error("invalid DIAG_USER_MAPPINGS block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002642 return true;
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002643 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002644
2645 if (F.PragmaDiagMappings.empty())
2646 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002647 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002648 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2649 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002650 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002651
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002652 case CUDA_SPECIAL_DECL_REFS:
2653 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002654 // FIXME: Modules will have trouble with this.
2655 CUDASpecialDeclRefs.clear();
2656 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2657 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002658 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002659
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002660 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002661 F.HeaderFileInfoTableData = BlobStart;
2662 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002663 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002664 if (Record[0]) {
2665 F.HeaderFileInfoTable
2666 = HeaderFileInfoLookupTable::Create(
2667 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002668 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002669 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002670 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002671 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002672
2673 PP.getHeaderSearchInfo().SetExternalSource(this);
2674 if (!PP.getHeaderSearchInfo().getExternalLookup())
2675 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002676 }
2677 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002678 }
2679
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002680 case FP_PRAGMA_OPTIONS:
2681 // Later tables overwrite earlier ones.
2682 FPPragmaOptions.swap(Record);
2683 break;
2684
2685 case OPENCL_EXTENSIONS:
2686 // Later tables overwrite earlier ones.
2687 OpenCLExtensions.swap(Record);
2688 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002689
2690 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002691 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2692 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002693 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002694
2695 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002696 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2697 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002698 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002699
2700 case IMPORTED_MODULES: {
2701 if (F.Kind != MK_Module) {
2702 // If we aren't loading a module (which has its own exports), make
2703 // all of the imported modules visible.
2704 // FIXME: Deal with macros-only imports.
2705 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2706 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2707 ImportedModules.push_back(GlobalID);
2708 }
2709 }
2710 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002711 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002712
Douglas Gregora1be2782011-12-17 23:38:30 +00002713 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002714 F.RedeclarationChains.swap(Record);
2715 break;
2716 }
2717
2718 case LOCAL_REDECLARATIONS_MAP: {
2719 if (F.LocalNumRedeclarationsInMap != 0) {
2720 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002721 return true;
Douglas Gregora1be2782011-12-17 23:38:30 +00002722 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002723
Douglas Gregor2171bf12012-01-15 16:58:34 +00002724 F.LocalNumRedeclarationsInMap = Record[0];
2725 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002726 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002727 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002728
2729 case MERGED_DECLARATIONS: {
2730 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2731 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2732 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2733 for (unsigned N = Record[Idx++]; N > 0; --N)
2734 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2735 }
2736 break;
2737 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002738
2739 case MACRO_OFFSET: {
2740 if (F.LocalNumMacros != 0) {
2741 Error("duplicate MACRO_OFFSET record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002742 return true;
Douglas Gregora8235d62012-10-09 23:05:51 +00002743 }
2744 F.MacroOffsets = (const uint32_t *)BlobStart;
2745 F.LocalNumMacros = Record[0];
2746 unsigned LocalBaseMacroID = Record[1];
2747 F.BaseMacroID = getTotalNumMacros();
2748
2749 if (F.LocalNumMacros > 0) {
2750 // Introduce the global -> local mapping for macros within this module.
2751 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2752
2753 // Introduce the local -> global mapping for macros within this module.
2754 F.MacroRemap.insertOrReplace(
2755 std::make_pair(LocalBaseMacroID,
2756 F.BaseMacroID - LocalBaseMacroID));
2757
2758 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2759 }
2760 break;
2761 }
2762
2763 case MACRO_UPDATES: {
2764 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2765 MacroID ID = getGlobalMacroID(F, Record[I++]);
2766 if (I == N)
2767 break;
2768
Douglas Gregor54c8a402012-10-12 00:16:50 +00002769 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2770 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2771 MacroUpdate Update;
2772 Update.UndefLoc = UndefLoc;
2773 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002774 }
2775 break;
2776 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002777 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002778 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002779 Error("premature end of bitstream in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00002780 return true;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002781}
2782
Douglas Gregorecc2c092011-12-01 22:20:10 +00002783void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002784 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002785 switch (Names[I].getKind()) {
2786 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002787 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002788 break;
2789
2790 case HiddenName::MacroVisibility: {
2791 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2792 Macro.second->setHidden(!Macro.second->isPublic());
2793 if (Macro.second->isDefined()) {
2794 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2795 }
2796 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002797 }
2798
Douglas Gregor54c8a402012-10-12 00:16:50 +00002799 case HiddenName::MacroUndef: {
2800 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2801 if (Macro.second->isDefined()) {
2802 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2803 if (PPMutationListener *Listener = PP.getPPMutationListener())
2804 Listener->UndefinedMacro(Macro.second);
2805 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2806 }
2807 break;
2808 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002809 }
Douglas Gregor13292642011-12-02 15:45:10 +00002810 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002811}
2812
Douglas Gregor5e356932011-12-01 17:11:21 +00002813void ASTReader::makeModuleVisible(Module *Mod,
2814 Module::NameVisibilityKind NameVisibility) {
2815 llvm::SmallPtrSet<Module *, 4> Visited;
2816 llvm::SmallVector<Module *, 4> Stack;
2817 Stack.push_back(Mod);
2818 while (!Stack.empty()) {
2819 Mod = Stack.back();
2820 Stack.pop_back();
2821
2822 if (NameVisibility <= Mod->NameVisibility) {
2823 // This module already has this level of visibility (or greater), so
2824 // there is nothing more to do.
2825 continue;
2826 }
2827
Douglas Gregor51f564f2011-12-31 04:05:44 +00002828 if (!Mod->isAvailable()) {
2829 // Modules that aren't available cannot be made visible.
2830 continue;
2831 }
2832
Douglas Gregor5e356932011-12-01 17:11:21 +00002833 // Update the module's name visibility.
2834 Mod->NameVisibility = NameVisibility;
2835
Douglas Gregorecc2c092011-12-01 22:20:10 +00002836 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002837 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002838 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2839 if (Hidden != HiddenNamesMap.end()) {
2840 makeNamesVisible(Hidden->second);
2841 HiddenNamesMap.erase(Hidden);
2842 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002843
2844 // Push any non-explicit submodules onto the stack to be marked as
2845 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002846 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2847 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002848 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002849 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2850 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002851 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002852
2853 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002854 bool AnyWildcard = false;
2855 bool UnrestrictedWildcard = false;
2856 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002857 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2858 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002859 if (!Mod->Exports[I].getInt()) {
2860 // Export a named module directly; no wildcards involved.
2861 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002862 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002863
2864 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002865 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002866
2867 // Wildcard export: export all of the imported modules that match
2868 // the given pattern.
2869 AnyWildcard = true;
2870 if (UnrestrictedWildcard)
2871 continue;
2872
2873 if (Module *Restriction = Mod->Exports[I].getPointer())
2874 WildcardRestrictions.push_back(Restriction);
2875 else {
2876 WildcardRestrictions.clear();
2877 UnrestrictedWildcard = true;
2878 }
2879 }
2880
2881 // If there were any wildcards, push any imported modules that were
2882 // re-exported by the wildcard restriction.
2883 if (!AnyWildcard)
2884 continue;
2885
2886 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2887 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002888 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002889 continue;
2890
2891 bool Acceptable = UnrestrictedWildcard;
2892 if (!Acceptable) {
2893 // Check whether this module meets one of the restrictions.
2894 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2895 Module *Restriction = WildcardRestrictions[R];
2896 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2897 Acceptable = true;
2898 break;
2899 }
2900 }
2901 }
2902
2903 if (!Acceptable)
2904 continue;
2905
Douglas Gregor0adaa882011-12-05 17:28:06 +00002906 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002907 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002908 }
2909}
2910
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002911ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor38295be2012-10-22 23:51:00 +00002912 ModuleKind Type,
2913 unsigned ClientLoadCapabilities) {
Douglas Gregor057df202012-01-18 20:56:22 +00002914 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002915 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002916
2917 // Load the core of the AST files.
2918 llvm::SmallVector<ModuleFile *, 4> Loaded;
Douglas Gregor38295be2012-10-22 23:51:00 +00002919 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0, Loaded,
2920 ClientLoadCapabilities)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002921 case Failure: return Failure;
Douglas Gregor4825fd72012-10-22 22:50:17 +00002922 case OutOfDate: return OutOfDate;
2923 case VersionMismatch: return VersionMismatch;
2924 case ConfigurationMismatch: return ConfigurationMismatch;
2925 case HadErrors: return HadErrors;
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002926 case Success: break;
2927 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002928
2929 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002930
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002931 // Load the AST blocks of all of the modules that we loaded.
2932 for (llvm::SmallVectorImpl<ModuleFile *>::iterator M = Loaded.begin(),
2933 MEnd = Loaded.end();
2934 M != MEnd; ++M) {
2935 ModuleFile &F = **M;
2936
2937 // Read the AST block.
Douglas Gregor4825fd72012-10-22 22:50:17 +00002938 if (ReadASTBlock(F))
2939 return Failure;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002940
2941 // Once read, set the ModuleFile bit base offset and update the size in
2942 // bits of all files we've seen.
2943 F.GlobalBitOffset = TotalModulesSizeInBits;
2944 TotalModulesSizeInBits += F.SizeInBits;
2945 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
2946
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002947 // Preload SLocEntries.
2948 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
2949 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Douglas Gregor8b53d142012-10-22 22:53:10 +00002950 // Load it through the SourceManager and don't call ReadSLocEntry()
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002951 // directly because the entry may have already been loaded in which case
Douglas Gregor8b53d142012-10-22 22:53:10 +00002952 // calling ReadSLocEntry() directly would trigger an assertion in
Douglas Gregor1d9d9892012-10-18 05:31:06 +00002953 // SourceManager.
2954 SourceMgr.getLoadedSLocEntryByID(Index);
2955 }
2956 }
2957
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002958 // Check the predefines buffers.
Douglas Gregor38295be2012-10-22 23:51:00 +00002959 bool ConfigComplain = (ClientLoadCapabilities & ARR_ConfigurationMismatch)==0;
Douglas Gregor6236a292011-12-02 21:56:05 +00002960 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002961 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2962 // if DisableValidation is true, defines that were set on command-line
2963 // but not in the PCH file will not be added to SuggestedPredefines.
Douglas Gregor38295be2012-10-22 23:51:00 +00002964 CheckPredefinesBuffers(ConfigComplain))
Douglas Gregor4825fd72012-10-22 22:50:17 +00002965 return ConfigurationMismatch;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002966
Douglas Gregoreee242f2011-10-27 09:33:13 +00002967 // Mark all of the identifiers in the identifier table as being out of date,
2968 // so that various accessors know to check the loaded modules when the
2969 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002970 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2971 IdEnd = PP.getIdentifierTable().end();
2972 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002973 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002974
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002975 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002976 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2977 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2978 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002979 Module *ResolvedMod = getSubmodule(GlobalID);
2980
2981 if (Unresolved.IsImport) {
2982 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002983 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002984 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002985 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002986
2987 if (ResolvedMod || Unresolved.IsWildcard)
2988 Unresolved.Mod->Exports.push_back(
2989 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002990 }
Douglas Gregor55988682011-12-05 16:33:54 +00002991 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002992
Douglas Gregor35942772011-09-09 21:34:22 +00002993 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002994
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002995 if (DeserializationListener)
2996 DeserializationListener->ReaderInitialized(this);
2997
Douglas Gregor11407b82012-10-18 21:18:25 +00002998 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
2999 if (!PrimaryModule.OriginalSourceFileID.isInvalid()) {
3000 PrimaryModule.OriginalSourceFileID
3001 = FileID::get(PrimaryModule.SLocEntryBaseID
3002 + PrimaryModule.OriginalSourceFileID.getOpaqueValue() - 1);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003003
Douglas Gregor11407b82012-10-18 21:18:25 +00003004 // If this AST file is a precompiled preamble, then set the
3005 // preamble file ID of the source manager to the file source file
3006 // from which the preamble was built.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003007 if (Type == MK_Preamble) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003008 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00003009 } else if (Type == MK_MainFile) {
Douglas Gregor11407b82012-10-18 21:18:25 +00003010 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003011 }
Douglas Gregor414cb642010-11-30 05:23:00 +00003012 }
3013
Douglas Gregorcff9f262012-01-27 01:47:08 +00003014 // For any Objective-C class definitions we have already loaded, make sure
3015 // that we load any additional categories.
3016 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
3017 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
3018 ObjCClassesLoaded[I],
3019 PreviousGeneration);
3020 }
3021
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003022 return Success;
3023}
3024
Douglas Gregor38295be2012-10-22 23:51:00 +00003025ASTReader::ASTReadResult
3026ASTReader::ReadASTCore(StringRef FileName,
3027 ModuleKind Type,
3028 ModuleFile *ImportedBy,
3029 llvm::SmallVectorImpl<ModuleFile *> &Loaded,
3030 unsigned ClientLoadCapabilities) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003031 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003032 bool NewModule;
3033 std::string ErrorStr;
3034 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00003035 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003036
Douglas Gregorfac4ece2011-08-19 02:29:29 +00003037 if (!M) {
3038 // We couldn't load the module.
3039 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
3040 + ErrorStr;
3041 Error(Msg);
3042 return Failure;
3043 }
3044
3045 if (!NewModule) {
3046 // We've already loaded this module.
3047 return Success;
3048 }
3049
3050 // FIXME: This seems rather a hack. Should CurrentDir be part of the
3051 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003052 if (FileName != "-") {
3053 CurrentDir = llvm::sys::path::parent_path(FileName);
3054 if (CurrentDir.empty()) CurrentDir = ".";
3055 }
3056
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003057 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00003058 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003059 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00003060 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003061
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00003062 // Sniff for the signature.
3063 if (Stream.Read(8) != 'C' ||
3064 Stream.Read(8) != 'P' ||
3065 Stream.Read(8) != 'C' ||
3066 Stream.Read(8) != 'H') {
3067 Diag(diag::err_not_a_pch_file) << FileName;
3068 return Failure;
3069 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003070
Douglas Gregor2cf26342009-04-09 22:27:44 +00003071 while (!Stream.AtEndOfStream()) {
3072 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003073
Douglas Gregore1d918e2009-04-10 23:10:45 +00003074 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003075 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003076 return Failure;
3077 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003078
3079 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00003080
Douglas Gregor7ae467f2012-10-18 18:27:37 +00003081 // We only know the control subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003082 switch (BlockID) {
3083 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003084 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003085 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003086 return Failure;
3087 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003088 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003089 case CONTROL_BLOCK_ID:
Douglas Gregor38295be2012-10-22 23:51:00 +00003090 switch (ReadControlBlock(F, Loaded, ClientLoadCapabilities)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003091 case Success:
3092 break;
3093
Douglas Gregor4825fd72012-10-22 22:50:17 +00003094 case Failure: return Failure;
3095 case OutOfDate: return OutOfDate;
3096 case VersionMismatch: return VersionMismatch;
3097 case ConfigurationMismatch: return ConfigurationMismatch;
3098 case HadErrors: return HadErrors;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003099 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003100 break;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003101 case AST_BLOCK_ID:
3102 // Record that we've loaded this module.
3103 Loaded.push_back(M);
3104 return Success;
3105
Douglas Gregor2cf26342009-04-09 22:27:44 +00003106 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00003107 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003108 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00003109 return Failure;
3110 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00003111 break;
3112 }
Mike Stump1eb44332009-09-09 15:08:12 +00003113 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00003114
Sebastian Redlcdf3b832010-07-16 20:41:52 +00003115 return Success;
3116}
3117
Douglas Gregor712f2fc2011-09-09 22:02:16 +00003118void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003119 // If there's a listener, notify them that we "read" the translation unit.
3120 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00003121 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
3122 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00003123
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003124 // Make sure we load the declaration update records for the translation unit,
3125 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00003126 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
3127 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003128
Douglas Gregor5f957282011-08-11 22:18:49 +00003129 // FIXME: Find a better way to deal with collisions between these
3130 // built-in types. Right now, we just ignore the problem.
3131
3132 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003133 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003134 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
3135 if (!Context.CFConstantStringTypeDecl)
3136 Context.setCFConstantStringType(GetType(String));
3137 }
3138
3139 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
3140 QualType FileType = GetType(File);
3141 if (FileType.isNull()) {
3142 Error("FILE type is NULL");
3143 return;
3144 }
3145
3146 if (!Context.FILEDecl) {
3147 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3148 Context.setFILEDecl(Typedef->getDecl());
3149 else {
3150 const TagType *Tag = FileType->getAs<TagType>();
3151 if (!Tag) {
3152 Error("Invalid FILE type in AST file");
3153 return;
3154 }
3155 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003156 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003157 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003158 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003159
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003160 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003161 QualType Jmp_bufType = GetType(Jmp_buf);
3162 if (Jmp_bufType.isNull()) {
3163 Error("jmp_buf type is NULL");
3164 return;
3165 }
3166
3167 if (!Context.jmp_bufDecl) {
3168 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3169 Context.setjmp_bufDecl(Typedef->getDecl());
3170 else {
3171 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3172 if (!Tag) {
3173 Error("Invalid jmp_buf type in AST file");
3174 return;
3175 }
3176 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003177 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003178 }
Mike Stump782fa302009-07-28 02:25:19 +00003179 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003180
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003181 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003182 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3183 if (Sigjmp_bufType.isNull()) {
3184 Error("sigjmp_buf type is NULL");
3185 return;
3186 }
3187
3188 if (!Context.sigjmp_bufDecl) {
3189 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3190 Context.setsigjmp_bufDecl(Typedef->getDecl());
3191 else {
3192 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3193 assert(Tag && "Invalid sigjmp_buf type in AST file");
3194 Context.setsigjmp_bufDecl(Tag->getDecl());
3195 }
3196 }
3197 }
3198
3199 if (unsigned ObjCIdRedef
3200 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3201 if (Context.ObjCIdRedefinitionType.isNull())
3202 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3203 }
3204
3205 if (unsigned ObjCClassRedef
3206 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3207 if (Context.ObjCClassRedefinitionType.isNull())
3208 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3209 }
3210
3211 if (unsigned ObjCSelRedef
3212 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3213 if (Context.ObjCSelRedefinitionType.isNull())
3214 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3215 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003216
3217 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3218 QualType Ucontext_tType = GetType(Ucontext_t);
3219 if (Ucontext_tType.isNull()) {
3220 Error("ucontext_t type is NULL");
3221 return;
3222 }
3223
3224 if (!Context.ucontext_tDecl) {
3225 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3226 Context.setucontext_tDecl(Typedef->getDecl());
3227 else {
3228 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3229 assert(Tag && "Invalid ucontext_t type in AST file");
3230 Context.setucontext_tDecl(Tag->getDecl());
3231 }
3232 }
3233 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003234 }
3235
Douglas Gregor35942772011-09-09 21:34:22 +00003236 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003237
3238 // If there were any CUDA special declarations, deserialize them.
3239 if (!CUDASpecialDeclRefs.empty()) {
3240 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003241 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003242 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3243 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003244
3245 // Re-export any modules that were imported by a non-module AST file.
3246 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3247 if (Module *Imported = getSubmodule(ImportedModules[I]))
3248 makeModuleVisible(Imported, Module::AllVisible);
3249 }
3250 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003251}
3252
Douglas Gregorecc2c092011-12-01 22:20:10 +00003253void ASTReader::finalizeForWriting() {
3254 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3255 HiddenEnd = HiddenNamesMap.end();
3256 Hidden != HiddenEnd; ++Hidden) {
3257 makeNamesVisible(Hidden->second);
3258 }
3259 HiddenNamesMap.clear();
3260}
3261
Douglas Gregorb64c1932009-05-12 01:31:05 +00003262/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003263/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003264/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003265std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003266 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003267 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003268 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003269 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003270 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003271 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003272 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003273 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003274 return std::string();
3275 }
3276
3277 // Initialize the stream
3278 llvm::BitstreamReader StreamFile;
3279 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003280 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003281 (const unsigned char *)Buffer->getBufferEnd());
3282 Stream.init(StreamFile);
3283
3284 // Sniff for the signature.
3285 if (Stream.Read(8) != 'C' ||
3286 Stream.Read(8) != 'P' ||
3287 Stream.Read(8) != 'C' ||
3288 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003289 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003290 return std::string();
3291 }
3292
3293 RecordData Record;
3294 while (!Stream.AtEndOfStream()) {
3295 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003296
Douglas Gregorb64c1932009-05-12 01:31:05 +00003297 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3298 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003299
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003300 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003301 switch (BlockID) {
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003302 case CONTROL_BLOCK_ID:
3303 if (Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003304 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003305 return std::string();
3306 }
3307 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003308
Douglas Gregorb64c1932009-05-12 01:31:05 +00003309 default:
3310 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003311 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003312 return std::string();
3313 }
3314 break;
3315 }
3316 continue;
3317 }
3318
3319 if (Code == llvm::bitc::END_BLOCK) {
3320 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003321 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003322 return std::string();
3323 }
3324 continue;
3325 }
3326
3327 if (Code == llvm::bitc::DEFINE_ABBREV) {
3328 Stream.ReadAbbrevRecord();
3329 continue;
3330 }
3331
3332 Record.clear();
3333 const char *BlobStart = 0;
3334 unsigned BlobLen = 0;
Douglas Gregor39c497b2012-10-18 18:36:53 +00003335 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen) == ORIGINAL_FILE)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003336 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003337 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003338
3339 return std::string();
3340}
3341
Douglas Gregor4825fd72012-10-22 22:50:17 +00003342bool ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003343 // Enter the submodule block.
3344 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3345 Error("malformed submodule block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003346 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003347 }
3348
3349 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003350 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003351 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003352 RecordData Record;
3353 while (true) {
3354 unsigned Code = F.Stream.ReadCode();
3355 if (Code == llvm::bitc::END_BLOCK) {
3356 if (F.Stream.ReadBlockEnd()) {
3357 Error("error at end of submodule block in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003358 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003359 }
Douglas Gregor4825fd72012-10-22 22:50:17 +00003360 return false;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003361 }
3362
3363 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3364 // No known subblocks, always skip them.
3365 F.Stream.ReadSubBlockID();
3366 if (F.Stream.SkipBlock()) {
3367 Error("malformed block record in AST file");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003368 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003369 }
3370 continue;
3371 }
3372
3373 if (Code == llvm::bitc::DEFINE_ABBREV) {
3374 F.Stream.ReadAbbrevRecord();
3375 continue;
3376 }
3377
3378 // Read a record.
3379 const char *BlobStart;
3380 unsigned BlobLen;
3381 Record.clear();
3382 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3383 default: // Default behavior: ignore.
3384 break;
3385
3386 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003387 if (First) {
3388 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003389 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003390 }
3391
Douglas Gregore209e502011-12-06 01:10:29 +00003392 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003393 Error("malformed module definition");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003394 return true;
Douglas Gregor1e123682011-12-05 22:27:44 +00003395 }
3396
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003397 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003398 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3399 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3400 bool IsFramework = Record[2];
3401 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003402 bool IsSystem = Record[4];
3403 bool InferSubmodules = Record[5];
3404 bool InferExplicitSubmodules = Record[6];
3405 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003406
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003407 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003408 if (Parent)
3409 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003410
3411 // Retrieve this (sub)module from the module map, creating it if
3412 // necessary.
3413 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3414 IsFramework,
3415 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003416 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3417 if (GlobalIndex >= SubmodulesLoaded.size() ||
3418 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003419 Error("too many submodules");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003420 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003421 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003422
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003423 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003424 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003425 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003426 CurrentModule->InferSubmodules = InferSubmodules;
3427 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3428 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003429 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003430 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003431
Douglas Gregore209e502011-12-06 01:10:29 +00003432 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003433 break;
3434 }
3435
Douglas Gregor77d029f2011-12-08 19:11:24 +00003436 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003437 if (First) {
3438 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003439 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003440 }
3441
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003442 if (!CurrentModule)
3443 break;
3444
3445 StringRef FileName(BlobStart, BlobLen);
3446 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003447 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003448 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003449 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003450 Error("mismatched umbrella headers in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003451 return true;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003452 }
3453 }
3454 break;
3455 }
3456
3457 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003458 if (First) {
3459 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003460 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003461 }
3462
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003463 if (!CurrentModule)
3464 break;
3465
3466 // FIXME: Be more lazy about this!
3467 StringRef FileName(BlobStart, BlobLen);
3468 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3469 if (std::find(CurrentModule->Headers.begin(),
3470 CurrentModule->Headers.end(),
3471 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003472 ModMap.addHeader(CurrentModule, File, false);
3473 }
3474 break;
3475 }
3476
3477 case SUBMODULE_EXCLUDED_HEADER: {
3478 if (First) {
3479 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003480 return true;
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003481 }
3482
3483 if (!CurrentModule)
3484 break;
3485
3486 // FIXME: Be more lazy about this!
3487 StringRef FileName(BlobStart, BlobLen);
3488 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3489 if (std::find(CurrentModule->Headers.begin(),
3490 CurrentModule->Headers.end(),
3491 File) == CurrentModule->Headers.end())
3492 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003493 }
3494 break;
3495 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003496
3497 case SUBMODULE_TOPHEADER: {
3498 if (First) {
3499 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003500 return true;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003501 }
3502
3503 if (!CurrentModule)
3504 break;
3505
3506 // FIXME: Be more lazy about this!
3507 StringRef FileName(BlobStart, BlobLen);
3508 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3509 CurrentModule->TopHeaders.insert(File);
3510 break;
3511 }
3512
Douglas Gregor77d029f2011-12-08 19:11:24 +00003513 case SUBMODULE_UMBRELLA_DIR: {
3514 if (First) {
3515 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003516 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003517 }
3518
3519 if (!CurrentModule)
3520 break;
3521
3522 StringRef DirName(BlobStart, BlobLen);
3523 if (const DirectoryEntry *Umbrella
3524 = PP.getFileManager().getDirectory(DirName)) {
3525 if (!CurrentModule->getUmbrellaDir())
3526 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3527 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3528 Error("mismatched umbrella directories in submodule");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003529 return true;
Douglas Gregor77d029f2011-12-08 19:11:24 +00003530 }
3531 }
3532 break;
3533 }
3534
Douglas Gregor26ced122011-12-01 00:59:36 +00003535 case SUBMODULE_METADATA: {
3536 if (!First) {
3537 Error("submodule metadata record not at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003538 return true;
Douglas Gregor26ced122011-12-01 00:59:36 +00003539 }
3540 First = false;
3541
3542 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003543 F.LocalNumSubmodules = Record[0];
3544 unsigned LocalBaseSubmoduleID = Record[1];
3545 if (F.LocalNumSubmodules > 0) {
3546 // Introduce the global -> local mapping for submodules within this
3547 // module.
3548 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3549
3550 // Introduce the local -> global mapping for submodules within this
3551 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003552 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003553 std::make_pair(LocalBaseSubmoduleID,
3554 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3555
3556 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3557 }
3558 break;
3559 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003560
Douglas Gregor55988682011-12-05 16:33:54 +00003561 case SUBMODULE_IMPORTS: {
3562 if (First) {
3563 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003564 return true;
Douglas Gregor55988682011-12-05 16:33:54 +00003565 }
3566
3567 if (!CurrentModule)
3568 break;
3569
3570 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3571 UnresolvedModuleImportExport Unresolved;
3572 Unresolved.File = &F;
3573 Unresolved.Mod = CurrentModule;
3574 Unresolved.ID = Record[Idx];
3575 Unresolved.IsImport = true;
3576 Unresolved.IsWildcard = false;
3577 UnresolvedModuleImportExports.push_back(Unresolved);
3578 }
3579 break;
3580 }
3581
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003582 case SUBMODULE_EXPORTS: {
3583 if (First) {
3584 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003585 return true;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003586 }
3587
3588 if (!CurrentModule)
3589 break;
3590
3591 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003592 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003593 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003594 Unresolved.Mod = CurrentModule;
3595 Unresolved.ID = Record[Idx];
3596 Unresolved.IsImport = false;
3597 Unresolved.IsWildcard = Record[Idx + 1];
3598 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003599 }
3600
3601 // Once we've loaded the set of exports, there's no reason to keep
3602 // the parsed, unresolved exports around.
3603 CurrentModule->UnresolvedExports.clear();
3604 break;
3605 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003606 case SUBMODULE_REQUIRES: {
3607 if (First) {
3608 Error("missing submodule metadata record at beginning of block");
Douglas Gregor4825fd72012-10-22 22:50:17 +00003609 return true;
Douglas Gregor51f564f2011-12-31 04:05:44 +00003610 }
3611
3612 if (!CurrentModule)
3613 break;
3614
3615 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003616 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003617 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003618 break;
3619 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003620 }
3621 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003622}
3623
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003624/// \brief Parse the record that corresponds to a LangOptions data
3625/// structure.
3626///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003627/// This routine parses the language options from the AST file and then gives
3628/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003629///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003630/// \returns true if the listener deems the file unacceptable, false otherwise.
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003631bool ASTReader::ParseLanguageOptions(const ModuleFile &M,
Douglas Gregor38295be2012-10-22 23:51:00 +00003632 const RecordData &Record,
3633 bool Complain) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003634 if (Listener) {
3635 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003636 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003637#define LANGOPT(Name, Bits, Default, Description) \
3638 LangOpts.Name = Record[Idx++];
3639#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3640 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3641#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003642
3643 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3644 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3645 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003646
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003647 unsigned Length = Record[Idx++];
3648 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3649 Record.begin() + Idx + Length);
Douglas Gregor38295be2012-10-22 23:51:00 +00003650 return Listener->ReadLanguageOptions(M, LangOpts, Complain);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003651 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003652
3653 return false;
3654}
3655
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003656std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003657ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003658 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003659 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003660 assert(I != GlobalPreprocessedEntityMap.end() &&
3661 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003662 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003663 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3664 return std::make_pair(M, LocalIndex);
3665}
3666
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003667std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3668ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3669 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3670 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3671 Mod.NumPreprocessedEntities);
3672
3673 return std::make_pair(PreprocessingRecord::iterator(),
3674 PreprocessingRecord::iterator());
3675}
3676
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003677std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3678ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3679 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3680 ModuleDeclIterator(this, &Mod,
3681 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3682}
3683
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003684PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3685 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003686 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3687 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003688 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003689 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003690
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003691 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003692 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003693
3694 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3695 switch (Code) {
3696 case llvm::bitc::END_BLOCK:
3697 return 0;
3698
3699 case llvm::bitc::ENTER_SUBBLOCK:
3700 Error("unexpected subblock record in preprocessor detail block");
3701 return 0;
3702
3703 case llvm::bitc::DEFINE_ABBREV:
3704 Error("unexpected abbrevation record in preprocessor detail block");
3705 return 0;
3706
3707 default:
3708 break;
3709 }
3710
3711 if (!PP.getPreprocessingRecord()) {
3712 Error("no preprocessing record");
3713 return 0;
3714 }
3715
3716 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003717 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3718 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003719 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3720 const char *BlobStart = 0;
3721 unsigned BlobLen = 0;
3722 RecordData Record;
3723 PreprocessorDetailRecordTypes RecType =
3724 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3725 Code, Record, BlobStart, BlobLen);
3726 switch (RecType) {
3727 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003728 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003729 IdentifierInfo *Name = 0;
3730 MacroDefinition *Def = 0;
3731 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003732 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003733 else {
3734 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003735 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003736 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3737 }
3738
3739 MacroExpansion *ME;
3740 if (isBuiltin)
3741 ME = new (PPRec) MacroExpansion(Name, Range);
3742 else
3743 ME = new (PPRec) MacroExpansion(Def, Range);
3744
3745 return ME;
3746 }
3747
3748 case PPD_MACRO_DEFINITION: {
3749 // Decode the identifier info and then check again; if the macro is
3750 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003751 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003752 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003753 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003754
3755 if (DeserializationListener)
3756 DeserializationListener->MacroDefinitionRead(PPID, MD);
3757
3758 return MD;
3759 }
3760
3761 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003762 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003763 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3764 const FileEntry *File = 0;
3765 if (!FullFileName.empty())
3766 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003767
3768 // FIXME: Stable encoding
3769 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003770 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003771 InclusionDirective *ID
3772 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003773 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003774 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003775 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003776 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003777 return ID;
3778 }
3779 }
David Blaikie7530c032012-01-17 06:56:22 +00003780
3781 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003782}
3783
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003784/// \brief \arg SLocMapI points at a chunk of a module that contains no
3785/// preprocessed entities or the entities it contains are not the ones we are
3786/// looking for. Find the next module that contains entities and return the ID
3787/// of the first entry.
3788PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3789 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3790 ++SLocMapI;
3791 for (GlobalSLocOffsetMapType::const_iterator
3792 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003793 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003794 if (M.NumPreprocessedEntities)
3795 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3796 }
3797
3798 return getTotalNumPreprocessedEntities();
3799}
3800
3801namespace {
3802
3803template <unsigned PPEntityOffset::*PPLoc>
3804struct PPEntityComp {
3805 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003806 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003807
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003808 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003809
Benjamin Kramer88df1252011-09-21 06:42:26 +00003810 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3811 SourceLocation LHS = getLoc(L);
3812 SourceLocation RHS = getLoc(R);
3813 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3814 }
3815
3816 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003817 SourceLocation LHS = getLoc(L);
3818 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3819 }
3820
Benjamin Kramer88df1252011-09-21 06:42:26 +00003821 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003822 SourceLocation RHS = getLoc(R);
3823 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3824 }
3825
3826 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3827 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3828 }
3829};
3830
3831}
3832
3833/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3834PreprocessedEntityID
3835ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3836 if (SourceMgr.isLocalSourceLocation(BLoc))
3837 return getTotalNumPreprocessedEntities();
3838
3839 GlobalSLocOffsetMapType::const_iterator
3840 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3841 BLoc.getOffset());
3842 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3843 "Corrupted global sloc offset map");
3844
3845 if (SLocMapI->second->NumPreprocessedEntities == 0)
3846 return findNextPreprocessedEntity(SLocMapI);
3847
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003848 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003849 typedef const PPEntityOffset *pp_iterator;
3850 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3851 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003852
3853 size_t Count = M.NumPreprocessedEntities;
3854 size_t Half;
3855 pp_iterator First = pp_begin;
3856 pp_iterator PPI;
3857
3858 // Do a binary search manually instead of using std::lower_bound because
3859 // The end locations of entities may be unordered (when a macro expansion
3860 // is inside another macro argument), but for this case it is not important
3861 // whether we get the first macro expansion or its containing macro.
3862 while (Count > 0) {
3863 Half = Count/2;
3864 PPI = First;
3865 std::advance(PPI, Half);
3866 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3867 BLoc)){
3868 First = PPI;
3869 ++First;
3870 Count = Count - Half - 1;
3871 } else
3872 Count = Half;
3873 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003874
3875 if (PPI == pp_end)
3876 return findNextPreprocessedEntity(SLocMapI);
3877
3878 return getGlobalPreprocessedEntityID(M,
3879 M.BasePreprocessedEntityID + (PPI - pp_begin));
3880}
3881
3882/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3883PreprocessedEntityID
3884ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3885 if (SourceMgr.isLocalSourceLocation(ELoc))
3886 return getTotalNumPreprocessedEntities();
3887
3888 GlobalSLocOffsetMapType::const_iterator
3889 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3890 ELoc.getOffset());
3891 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3892 "Corrupted global sloc offset map");
3893
3894 if (SLocMapI->second->NumPreprocessedEntities == 0)
3895 return findNextPreprocessedEntity(SLocMapI);
3896
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003897 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003898 typedef const PPEntityOffset *pp_iterator;
3899 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3900 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3901 pp_iterator PPI =
3902 std::upper_bound(pp_begin, pp_end, ELoc,
3903 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3904
3905 if (PPI == pp_end)
3906 return findNextPreprocessedEntity(SLocMapI);
3907
3908 return getGlobalPreprocessedEntityID(M,
3909 M.BasePreprocessedEntityID + (PPI - pp_begin));
3910}
3911
3912/// \brief Returns a pair of [Begin, End) indices of preallocated
3913/// preprocessed entities that \arg Range encompasses.
3914std::pair<unsigned, unsigned>
3915 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3916 if (Range.isInvalid())
3917 return std::make_pair(0,0);
3918 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3919
3920 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3921 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3922 return std::make_pair(BeginID, EndID);
3923}
3924
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003925/// \brief Optionally returns true or false if the preallocated preprocessed
3926/// entity with index \arg Index came from file \arg FID.
3927llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3928 FileID FID) {
3929 if (FID.isInvalid())
3930 return false;
3931
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003932 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3933 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003934 unsigned LocalIndex = PPInfo.second;
3935 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3936
3937 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3938 if (Loc.isInvalid())
3939 return false;
3940
3941 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3942 return true;
3943 else
3944 return false;
3945}
3946
Douglas Gregord10a3812011-08-25 18:14:34 +00003947namespace {
3948 /// \brief Visitor used to search for information about a header file.
3949 class HeaderFileInfoVisitor {
3950 ASTReader &Reader;
3951 const FileEntry *FE;
3952
3953 llvm::Optional<HeaderFileInfo> HFI;
3954
3955 public:
3956 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3957 : Reader(Reader), FE(FE) { }
3958
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003959 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003960 HeaderFileInfoVisitor *This
3961 = static_cast<HeaderFileInfoVisitor *>(UserData);
3962
3963 HeaderFileInfoTrait Trait(This->Reader, M,
3964 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3965 M.HeaderFileFrameworkStrings,
3966 This->FE->getName());
3967
3968 HeaderFileInfoLookupTable *Table
3969 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3970 if (!Table)
3971 return false;
3972
3973 // Look in the on-disk hash table for an entry for this file name.
3974 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3975 &Trait);
3976 if (Pos == Table->end())
3977 return false;
3978
3979 This->HFI = *Pos;
3980 return true;
3981 }
3982
3983 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3984 };
3985}
3986
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003987HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003988 HeaderFileInfoVisitor Visitor(*this, FE);
3989 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3990 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003991 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003992 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3993 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003994 }
3995
3996 return HeaderFileInfo();
3997}
3998
David Blaikied6471f72011-09-25 23:23:43 +00003999void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00004000 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004001 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004002 unsigned Idx = 0;
4003 while (Idx < F.PragmaDiagMappings.size()) {
4004 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004005 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
4006 Diag.DiagStatePoints.push_back(
4007 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
4008 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004009 while (1) {
4010 assert(Idx < F.PragmaDiagMappings.size() &&
4011 "Invalid data, didn't find '-1' marking end of diag/map pairs");
4012 if (Idx >= F.PragmaDiagMappings.size()) {
4013 break; // Something is messed up but at least avoid infinite loop in
4014 // release build.
4015 }
4016 unsigned DiagID = F.PragmaDiagMappings[Idx++];
4017 if (DiagID == (unsigned)-1) {
4018 break; // no more diag/map pairs for this location.
4019 }
4020 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00004021 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
4022 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004023 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00004024 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00004025 }
4026}
4027
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004028/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004029ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00004030 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00004031 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004032 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00004033 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004034}
4035
4036/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00004037///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004038/// The index is the type ID, shifted and minus the number of predefs. This
4039/// routine actually reads the record corresponding to the type at the given
4040/// location. It is a helper routine for GetType, which deals with reading type
4041/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00004042QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004043 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004044 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00004045
Douglas Gregor0b748912009-04-14 21:18:50 +00004046 // Keep track of where we are in the stream, then jump back there
4047 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004048 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00004049
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004050 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00004051
Douglas Gregord89275b2009-07-06 18:54:52 +00004052 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00004053 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00004054
Douglas Gregor393f2492011-07-22 00:38:23 +00004055 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00004056 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004057 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004058 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004059 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
4060 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004061 if (Record.size() != 2) {
4062 Error("Incorrect encoding of extended qualifier type");
4063 return QualType();
4064 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004065 QualType Base = readType(*Loc.F, Record, Idx);
4066 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00004067 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00004068 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004069
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004070 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004071 if (Record.size() != 1) {
4072 Error("Incorrect encoding of complex type");
4073 return QualType();
4074 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004075 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004076 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004077 }
4078
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004079 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004080 if (Record.size() != 1) {
4081 Error("Incorrect encoding of pointer type");
4082 return QualType();
4083 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004084 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004085 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004086 }
4087
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004088 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004089 if (Record.size() != 1) {
4090 Error("Incorrect encoding of block pointer type");
4091 return QualType();
4092 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004093 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004094 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004095 }
4096
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004097 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00004098 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004099 Error("Incorrect encoding of lvalue reference type");
4100 return QualType();
4101 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004102 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004103 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004104 }
4105
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004106 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004107 if (Record.size() != 1) {
4108 Error("Incorrect encoding of rvalue reference type");
4109 return QualType();
4110 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004111 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004112 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004113 }
4114
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004115 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00004116 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004117 Error("Incorrect encoding of member pointer type");
4118 return QualType();
4119 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004120 QualType PointeeType = readType(*Loc.F, Record, Idx);
4121 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00004122 if (PointeeType.isNull() || ClassType.isNull())
4123 return QualType();
4124
Douglas Gregor35942772011-09-09 21:34:22 +00004125 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004126 }
4127
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004128 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004129 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004130 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4131 unsigned IndexTypeQuals = Record[2];
4132 unsigned Idx = 3;
4133 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004134 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004135 ASM, IndexTypeQuals);
4136 }
4137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004138 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004139 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004140 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4141 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004142 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004143 }
4144
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004145 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004146 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00004147 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4148 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004149 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4150 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004151 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004152 ASM, IndexTypeQuals,
4153 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004154 }
4155
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004156 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004157 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004158 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004159 return QualType();
4160 }
4161
Douglas Gregor393f2492011-07-22 00:38:23 +00004162 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004163 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004164 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004165 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004166 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004167 }
4168
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004169 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004170 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004171 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004172 return QualType();
4173 }
4174
Douglas Gregor393f2492011-07-22 00:38:23 +00004175 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004176 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004177 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004178 }
4179
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004180 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004181 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004182 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004183 return QualType();
4184 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004185 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004186 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4187 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004188 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004189 }
4190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004191 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004192 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004193
4194 FunctionProtoType::ExtProtoInfo EPI;
4195 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004196 /*hasregparm*/ Record[2],
4197 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004198 static_cast<CallingConv>(Record[4]),
4199 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004200
John McCallf85e1932011-06-15 23:02:42 +00004201 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004202 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004203 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004204 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004205 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004206
4207 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004208 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004209 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004210 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004211 ExceptionSpecificationType EST =
4212 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4213 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004214 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004215 if (EST == EST_Dynamic) {
4216 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004217 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004218 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004219 EPI.Exceptions = Exceptions.data();
4220 } else if (EST == EST_ComputedNoexcept) {
4221 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004222 } else if (EST == EST_Uninstantiated) {
4223 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4224 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004225 } else if (EST == EST_Unevaluated) {
4226 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004227 }
Douglas Gregor35942772011-09-09 21:34:22 +00004228 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004229 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004230 }
4231
Douglas Gregor409448c2011-07-21 22:35:25 +00004232 case TYPE_UNRESOLVED_USING: {
4233 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004234 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004235 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4236 }
4237
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004238 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004239 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004240 Error("incorrect encoding of typedef type");
4241 return QualType();
4242 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004243 unsigned Idx = 0;
4244 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004245 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004246 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004247 Canonical = Context.getCanonicalType(Canonical);
4248 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004249 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004250
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004251 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004252 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004253
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004254 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004255 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004256 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004257 return QualType();
4258 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004259 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004260 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004261 }
Mike Stump1eb44332009-09-09 15:08:12 +00004262
Douglas Gregorf8af9822012-02-12 18:42:33 +00004263 case TYPE_DECLTYPE: {
4264 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4265 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4266 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004267
Sean Huntca63c202011-05-24 22:41:36 +00004268 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004269 QualType BaseType = readType(*Loc.F, Record, Idx);
4270 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004271 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004272 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004273 }
4274
Richard Smith34b41d92011-02-20 03:19:35 +00004275 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004276 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004277
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004278 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004279 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004280 Error("incorrect encoding of record type");
4281 return QualType();
4282 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004283 unsigned Idx = 0;
4284 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004285 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4286 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4287 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004288 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004289 return T;
4290 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004291
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004292 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004293 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004294 Error("incorrect encoding of enum type");
4295 return QualType();
4296 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004297 unsigned Idx = 0;
4298 bool IsDependent = Record[Idx++];
4299 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004300 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004301 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004302 return T;
4303 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004304
John McCall9d156a72011-01-06 01:58:22 +00004305 case TYPE_ATTRIBUTED: {
4306 if (Record.size() != 3) {
4307 Error("incorrect encoding of attributed type");
4308 return QualType();
4309 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004310 QualType modifiedType = readType(*Loc.F, Record, Idx);
4311 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004312 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004313 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004314 }
4315
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004316 case TYPE_PAREN: {
4317 if (Record.size() != 1) {
4318 Error("incorrect encoding of paren type");
4319 return QualType();
4320 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004321 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004322 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004323 }
4324
Douglas Gregor7536dd52010-12-20 02:24:11 +00004325 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004326 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004327 Error("incorrect encoding of pack expansion type");
4328 return QualType();
4329 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004330 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004331 if (Pattern.isNull())
4332 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004333 llvm::Optional<unsigned> NumExpansions;
4334 if (Record[1])
4335 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004336 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004337 }
4338
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004339 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004340 unsigned Idx = 0;
4341 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004342 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004343 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004344 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004345 }
4346
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004347 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004348 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004349 ObjCInterfaceDecl *ItfD
4350 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004351 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004352 }
4353
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004354 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004355 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004356 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004357 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004358 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004359 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004360 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004361 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004362 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004363
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004364 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004365 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004366 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004367 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004368 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004369
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004370 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004371 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004372 QualType Parm = readType(*Loc.F, Record, Idx);
4373 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004374 return
Douglas Gregor35942772011-09-09 21:34:22 +00004375 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004376 Replacement);
4377 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004378
Douglas Gregorc3069d62011-01-14 02:55:32 +00004379 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4380 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004381 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004382 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004383 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004384 cast<TemplateTypeParmType>(Parm),
4385 ArgPack);
4386 }
4387
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004388 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004389 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004390 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004391 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004392 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004393 return
Douglas Gregor35942772011-09-09 21:34:22 +00004394 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004395 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004396
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004397 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004398 unsigned Idx = 0;
4399 unsigned Depth = Record[Idx++];
4400 unsigned Index = Record[Idx++];
4401 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004402 TemplateTypeParmDecl *D
4403 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004404 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004405 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004406
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004407 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004408 unsigned Idx = 0;
4409 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004410 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004411 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004412 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004413 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004414 Canon = Context.getCanonicalType(Canon);
4415 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004416 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004417
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004418 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004419 unsigned Idx = 0;
4420 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004421 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004422 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004423 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004424 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004425 Args.reserve(NumArgs);
4426 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004427 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004428 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004429 Args.size(), Args.data());
4430 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004431
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004432 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004433 unsigned Idx = 0;
4434
4435 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004436 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004437 ArrayType::ArraySizeModifier ASM
4438 = (ArrayType::ArraySizeModifier)Record[Idx++];
4439 unsigned IndexTypeQuals = Record[Idx++];
4440
4441 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004442 Expr *NumElts = ReadExpr(*Loc.F);
4443 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004444
Douglas Gregor35942772011-09-09 21:34:22 +00004445 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004446 IndexTypeQuals, Brackets);
4447 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004448
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004449 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004450 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004451 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004452 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004453 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004454 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004455 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004456 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004457 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004458 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004459 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004460 else
Douglas Gregor35942772011-09-09 21:34:22 +00004461 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004462 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004463 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004464 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004465 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004466
4467 case TYPE_ATOMIC: {
4468 if (Record.size() != 1) {
4469 Error("Incorrect encoding of atomic type");
4470 return QualType();
4471 }
4472 QualType ValueType = readType(*Loc.F, Record, Idx);
4473 return Context.getAtomicType(ValueType);
4474 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004475 }
David Blaikie7530c032012-01-17 06:56:22 +00004476 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004477}
4478
Sebastian Redlc3632732010-10-05 15:59:54 +00004479class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004480 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004481 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004482 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004483 unsigned &Idx;
4484
Sebastian Redlc3632732010-10-05 15:59:54 +00004485 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4486 unsigned &I) {
4487 return Reader.ReadSourceLocation(F, R, I);
4488 }
4489
Douglas Gregor409448c2011-07-21 22:35:25 +00004490 template<typename T>
4491 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4492 return Reader.ReadDeclAs<T>(F, Record, Idx);
4493 }
4494
John McCalla1ee0c52009-10-16 21:56:05 +00004495public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004496 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004497 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004498 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004499 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004500
John McCall51bd8032009-10-18 01:05:36 +00004501 // We want compile-time assurance that we've enumerated all of
4502 // these, so unfortunately we have to declare them first, then
4503 // define them out-of-line.
4504#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004505#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004506 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004507#include "clang/AST/TypeLocNodes.def"
4508
John McCall51bd8032009-10-18 01:05:36 +00004509 void VisitFunctionTypeLoc(FunctionTypeLoc);
4510 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004511};
4512
John McCall51bd8032009-10-18 01:05:36 +00004513void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004514 // nothing to do
4515}
John McCall51bd8032009-10-18 01:05:36 +00004516void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004517 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004518 if (TL.needsExtraLocalData()) {
4519 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4520 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4521 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4522 TL.setModeAttr(Record[Idx++]);
4523 }
John McCalla1ee0c52009-10-16 21:56:05 +00004524}
John McCall51bd8032009-10-18 01:05:36 +00004525void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004526 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004527}
John McCall51bd8032009-10-18 01:05:36 +00004528void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004529 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004530}
John McCall51bd8032009-10-18 01:05:36 +00004531void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004532 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004533}
John McCall51bd8032009-10-18 01:05:36 +00004534void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004535 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004536}
John McCall51bd8032009-10-18 01:05:36 +00004537void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004538 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004539}
John McCall51bd8032009-10-18 01:05:36 +00004540void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004541 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004542 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004543}
John McCall51bd8032009-10-18 01:05:36 +00004544void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004545 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4546 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004547 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004548 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004549 else
John McCall51bd8032009-10-18 01:05:36 +00004550 TL.setSizeExpr(0);
4551}
4552void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4553 VisitArrayTypeLoc(TL);
4554}
4555void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4556 VisitArrayTypeLoc(TL);
4557}
4558void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4559 VisitArrayTypeLoc(TL);
4560}
4561void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4562 DependentSizedArrayTypeLoc TL) {
4563 VisitArrayTypeLoc(TL);
4564}
4565void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4566 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004567 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004568}
4569void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004570 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004571}
4572void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004573 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004574}
4575void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004576 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004577 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4578 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004579 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004580 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004581 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004582 }
4583}
4584void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4585 VisitFunctionTypeLoc(TL);
4586}
4587void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4588 VisitFunctionTypeLoc(TL);
4589}
John McCalled976492009-12-04 22:46:56 +00004590void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004591 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004592}
John McCall51bd8032009-10-18 01:05:36 +00004593void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004594 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004595}
4596void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004597 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4598 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4599 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004600}
4601void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004602 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4603 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4604 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4605 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004606}
4607void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004608 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004609}
Sean Huntca63c202011-05-24 22:41:36 +00004610void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4611 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4612 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4613 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4614 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4615}
Richard Smith34b41d92011-02-20 03:19:35 +00004616void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4617 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4618}
John McCall51bd8032009-10-18 01:05:36 +00004619void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004620 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004621}
4622void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004623 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004624}
John McCall9d156a72011-01-06 01:58:22 +00004625void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4626 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4627 if (TL.hasAttrOperand()) {
4628 SourceRange range;
4629 range.setBegin(ReadSourceLocation(Record, Idx));
4630 range.setEnd(ReadSourceLocation(Record, Idx));
4631 TL.setAttrOperandParensRange(range);
4632 }
4633 if (TL.hasAttrExprOperand()) {
4634 if (Record[Idx++])
4635 TL.setAttrExprOperand(Reader.ReadExpr(F));
4636 else
4637 TL.setAttrExprOperand(0);
4638 } else if (TL.hasAttrEnumOperand())
4639 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4640}
John McCall51bd8032009-10-18 01:05:36 +00004641void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004642 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004643}
John McCall49a832b2009-10-18 09:09:24 +00004644void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4645 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004646 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004647}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004648void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4649 SubstTemplateTypeParmPackTypeLoc TL) {
4650 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4651}
John McCall51bd8032009-10-18 01:05:36 +00004652void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4653 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004654 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004655 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4656 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4657 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004658 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4659 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004660 Reader.GetTemplateArgumentLocInfo(F,
4661 TL.getTypePtr()->getArg(i).getKind(),
4662 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004663}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004664void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4665 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4666 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4667}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004668void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004669 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004670 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004671}
John McCall3cb0ebd2010-03-10 03:28:59 +00004672void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004673 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004674}
Douglas Gregor4714c122010-03-31 17:34:00 +00004675void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004676 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004677 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004678 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004679}
John McCall33500952010-06-11 00:33:02 +00004680void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4681 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004682 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004683 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004684 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004685 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004686 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4687 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004688 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4689 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004690 Reader.GetTemplateArgumentLocInfo(F,
4691 TL.getTypePtr()->getArg(I).getKind(),
4692 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004693}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004694void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4695 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4696}
John McCall51bd8032009-10-18 01:05:36 +00004697void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004698 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004699}
4700void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4701 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004702 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4703 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004704 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004705 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004706}
John McCall54e14c42009-10-22 22:37:11 +00004707void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004708 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004709}
Eli Friedmanb001de72011-10-06 23:00:33 +00004710void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4711 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4712 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4713 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4714}
John McCalla1ee0c52009-10-16 21:56:05 +00004715
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004716TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004717 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004718 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004719 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004720 if (InfoTy.isNull())
4721 return 0;
4722
Douglas Gregor35942772011-09-09 21:34:22 +00004723 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004724 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004725 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004726 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004727 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004728}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004729
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004730QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004731 unsigned FastQuals = ID & Qualifiers::FastMask;
4732 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004733
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004734 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004735 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004736 switch ((PredefinedTypeIDs)Index) {
4737 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004738 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4739 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004740
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004741 case PREDEF_TYPE_CHAR_U_ID:
4742 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004743 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004744 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004745 break;
4746
Douglas Gregor35942772011-09-09 21:34:22 +00004747 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4748 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4749 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4750 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4751 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4752 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4753 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4754 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4755 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4756 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4757 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4758 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4759 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004760 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004761 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4762 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4763 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4764 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4765 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004766 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004767 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4768 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4769 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4770 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4771 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4772 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4773 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4774 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4775 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004776
4777 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004778 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004779 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004780
4781 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4782 T = Context.ARCUnbridgedCastTy;
4783 break;
4784
Meador Ingefb40e3f2012-07-01 15:57:25 +00004785 case PREDEF_TYPE_VA_LIST_TAG:
4786 T = Context.getVaListTagType();
4787 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004788
4789 case PREDEF_TYPE_BUILTIN_FN:
4790 T = Context.BuiltinFnTy;
4791 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004792 }
4793
4794 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004795 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004796 }
4797
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004798 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004799 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004800 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004801 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004802 if (TypesLoaded[Index].isNull())
4803 return QualType();
4804
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004805 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004806 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004807 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004808 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004809 }
Mike Stump1eb44332009-09-09 15:08:12 +00004810
John McCall0953e762009-09-24 19:53:00 +00004811 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004812}
4813
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004814QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004815 return GetType(getGlobalTypeID(F, LocalID));
4816}
4817
4818serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004819ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004820 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4821 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4822
4823 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4824 return LocalID;
4825
4826 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4827 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4828 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4829
4830 unsigned GlobalIndex = LocalIndex + I->second;
4831 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4832}
4833
John McCall833ca992009-10-29 08:12:44 +00004834TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004835ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004836 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004837 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004838 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004839 switch (Kind) {
4840 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004841 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004842 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004843 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004844 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004845 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4846 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004847 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004848 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004849 SourceLocation());
4850 }
4851 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004852 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4853 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004854 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004855 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004856 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004857 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004858 }
John McCall833ca992009-10-29 08:12:44 +00004859 case TemplateArgument::Null:
4860 case TemplateArgument::Integral:
4861 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004862 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004863 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004864 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004865 return TemplateArgumentLocInfo();
4866 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004867 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004868}
4869
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004870TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004871ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004872 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004873 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004874
4875 if (Arg.getKind() == TemplateArgument::Expression) {
4876 if (Record[Index++]) // bool InfoHasSameExpr.
4877 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4878 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004879 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004880 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004881}
4882
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004883Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004884 return GetDecl(ID);
4885}
4886
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004887uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004888 unsigned &Idx){
4889 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004890 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004891
Douglas Gregore92b8a12011-08-04 00:01:48 +00004892 unsigned LocalID = Record[Idx++];
4893 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004894}
4895
4896CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004897 RecordLocation Loc = getLocalBitOffset(Offset);
4898 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004899 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004900 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004901 ReadingKindTracker ReadingKind(Read_Decl, *this);
4902 RecordData Record;
4903 unsigned Code = Cursor.ReadCode();
4904 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4905 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4906 Error("Malformed AST file: missing C++ base specifiers");
4907 return 0;
4908 }
4909
4910 unsigned Idx = 0;
4911 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004912 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004913 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4914 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004915 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004916 return Bases;
4917}
4918
Douglas Gregor409448c2011-07-21 22:35:25 +00004919serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004920ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004921 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004922 return LocalID;
4923
4924 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004925 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004926 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4927
4928 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004929}
4930
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004931bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004932 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004933 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4934 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4935 return &M == I->second;
4936}
4937
Douglas Gregorcff9f262012-01-27 01:47:08 +00004938ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4939 if (!D->isFromASTFile())
4940 return 0;
4941 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4942 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4943 return I->second;
4944}
4945
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004946SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4947 if (ID < NUM_PREDEF_DECL_IDS)
4948 return SourceLocation();
4949
4950 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4951
4952 if (Index > DeclsLoaded.size()) {
4953 Error("declaration ID out-of-range for AST file");
4954 return SourceLocation();
4955 }
4956
4957 if (Decl *D = DeclsLoaded[Index])
4958 return D->getLocation();
4959
4960 unsigned RawLocation = 0;
4961 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4962 return ReadSourceLocation(*Rec.F, RawLocation);
4963}
4964
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004965Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004966 if (ID < NUM_PREDEF_DECL_IDS) {
4967 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004968 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004969 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004970
4971 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004972 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004973
4974 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004975 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004976
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004977 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004978 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004979
Douglas Gregor79d67262011-08-12 05:59:41 +00004980 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004981 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004982
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004983 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4984 return Context.getObjCProtocolDecl();
4985
Douglas Gregor772eeae2011-08-12 06:49:56 +00004986 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004987 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004988
4989 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004990 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004991
4992 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004993 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004994
4995 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4996 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004997 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004998 }
4999
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005000 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
5001
Richard Smith2fbf3732011-12-20 04:39:57 +00005002 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005003 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005004 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005005 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005006 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00005007
Douglas Gregorfd002a72011-12-16 22:37:11 +00005008 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00005009 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00005010 if (DeserializationListener)
5011 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
5012 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005013
5014 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005015}
5016
Douglas Gregora1be2782011-12-17 23:38:30 +00005017DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
5018 DeclID GlobalID) {
5019 if (GlobalID < NUM_PREDEF_DECL_IDS)
5020 return GlobalID;
5021
5022 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
5023 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
5024 ModuleFile *Owner = I->second;
5025
5026 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
5027 = M.GlobalToLocalDeclIDs.find(Owner);
5028 if (Pos == M.GlobalToLocalDeclIDs.end())
5029 return 0;
5030
5031 return GlobalID - Owner->BaseDeclID + Pos->second;
5032}
5033
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005034serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00005035 const RecordData &Record,
5036 unsigned &Idx) {
5037 if (Idx >= Record.size()) {
5038 Error("Corrupted AST file");
5039 return 0;
5040 }
5041
5042 return getGlobalDeclID(F, Record[Idx++]);
5043}
5044
Chris Lattner887e2b32009-04-27 05:46:25 +00005045/// \brief Resolve the offset of a statement into a statement.
5046///
5047/// This operation will read a new statement from the external
5048/// source each time it is called, and is meant to be used via a
5049/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005050Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00005051 // Switch case IDs are per Decl.
5052 ClearSwitchCaseIDs();
5053
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00005054 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005055 RecordLocation Loc = getLocalBitOffset(Offset);
5056 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
5057 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00005058}
5059
Douglas Gregor851c75a2011-08-24 21:27:34 +00005060namespace {
5061 class FindExternalLexicalDeclsVisitor {
5062 ASTReader &Reader;
5063 const DeclContext *DC;
5064 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005065
Douglas Gregor851c75a2011-08-24 21:27:34 +00005066 SmallVectorImpl<Decl*> &Decls;
5067 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
5068
5069 public:
5070 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
5071 bool (*isKindWeWant)(Decl::Kind),
5072 SmallVectorImpl<Decl*> &Decls)
5073 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
5074 {
5075 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
5076 PredefsVisited[I] = false;
5077 }
5078
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005079 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00005080 if (Preorder)
5081 return false;
5082
5083 FindExternalLexicalDeclsVisitor *This
5084 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
5085
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005086 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00005087 = M.DeclContextInfos.find(This->DC);
5088 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
5089 return false;
5090
5091 // Load all of the declaration IDs
5092 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
5093 *IDE = ID + Info->second.NumLexicalDecls;
5094 ID != IDE; ++ID) {
5095 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
5096 continue;
5097
5098 // Don't add predefined declarations to the lexical context more
5099 // than once.
5100 if (ID->second < NUM_PREDEF_DECL_IDS) {
5101 if (This->PredefsVisited[ID->second])
5102 continue;
5103
5104 This->PredefsVisited[ID->second] = true;
5105 }
5106
Douglas Gregor2ea054f2011-08-26 22:04:51 +00005107 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
5108 if (!This->DC->isDeclInLexicalTraversal(D))
5109 This->Decls.push_back(D);
5110 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00005111 }
5112
5113 return false;
5114 }
5115 };
5116}
5117
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005118ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00005119 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00005120 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005121 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00005122 // least. Walk all of the modules in the order they were loaded.
5123 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
5124 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005125 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00005126 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005127}
5128
Douglas Gregor0d95f772011-08-24 19:03:07 +00005129namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005130
5131class DeclIDComp {
5132 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005133 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005134
5135public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005136 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005137
5138 bool operator()(LocalDeclID L, LocalDeclID R) const {
5139 SourceLocation LHS = getLocation(L);
5140 SourceLocation RHS = getLocation(R);
5141 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5142 }
5143
5144 bool operator()(SourceLocation LHS, LocalDeclID R) const {
5145 SourceLocation RHS = getLocation(R);
5146 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5147 }
5148
5149 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5150 SourceLocation LHS = getLocation(L);
5151 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5152 }
5153
5154 SourceLocation getLocation(LocalDeclID ID) const {
5155 return Reader.getSourceManager().getFileLoc(
5156 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5157 }
5158};
5159
5160}
5161
5162void ASTReader::FindFileRegionDecls(FileID File,
5163 unsigned Offset, unsigned Length,
5164 SmallVectorImpl<Decl *> &Decls) {
5165 SourceManager &SM = getSourceManager();
5166
5167 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5168 if (I == FileDeclIDs.end())
5169 return;
5170
5171 FileDeclsInfo &DInfo = I->second;
5172 if (DInfo.Decls.empty())
5173 return;
5174
5175 SourceLocation
5176 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5177 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5178
5179 DeclIDComp DIDComp(*this, *DInfo.Mod);
5180 ArrayRef<serialization::LocalDeclID>::iterator
5181 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5182 BeginLoc, DIDComp);
5183 if (BeginIt != DInfo.Decls.begin())
5184 --BeginIt;
5185
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005186 // If we are pointing at a top-level decl inside an objc container, we need
5187 // to backtrack until we find it otherwise we will fail to report that the
5188 // region overlaps with an objc container.
5189 while (BeginIt != DInfo.Decls.begin() &&
5190 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5191 ->isTopLevelDeclInObjCContainer())
5192 --BeginIt;
5193
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005194 ArrayRef<serialization::LocalDeclID>::iterator
5195 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5196 EndLoc, DIDComp);
5197 if (EndIt != DInfo.Decls.end())
5198 ++EndIt;
5199
5200 for (ArrayRef<serialization::LocalDeclID>::iterator
5201 DIt = BeginIt; DIt != EndIt; ++DIt)
5202 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5203}
5204
5205namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005206 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005207 /// declaration context.
5208 class DeclContextNameLookupVisitor {
5209 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005210 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005211 DeclarationName Name;
5212 SmallVectorImpl<NamedDecl *> &Decls;
5213
5214 public:
5215 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005216 SmallVectorImpl<const DeclContext *> &Contexts,
5217 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005218 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005219 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005220
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005221 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005222 DeclContextNameLookupVisitor *This
5223 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5224
5225 // Check whether we have any visible declaration information for
5226 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005227 ModuleFile::DeclContextInfosMap::iterator Info;
5228 bool FoundInfo = false;
5229 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5230 Info = M.DeclContextInfos.find(This->Contexts[I]);
5231 if (Info != M.DeclContextInfos.end() &&
5232 Info->second.NameLookupTableData) {
5233 FoundInfo = true;
5234 break;
5235 }
5236 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005237
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005238 if (!FoundInfo)
5239 return false;
5240
Douglas Gregor0d95f772011-08-24 19:03:07 +00005241 // Look for this name within this module.
5242 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005243 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005244 ASTDeclContextNameLookupTable::iterator Pos
5245 = LookupTable->find(This->Name);
5246 if (Pos == LookupTable->end())
5247 return false;
5248
5249 bool FoundAnything = false;
5250 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5251 for (; Data.first != Data.second; ++Data.first) {
5252 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5253 if (!ND)
5254 continue;
5255
5256 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005257 // A name might be null because the decl's redeclarable part is
5258 // currently read before reading its name. The lookup is triggered by
5259 // building that decl (likely indirectly), and so it is later in the
5260 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005261 continue;
5262 }
5263
5264 // Record this declaration.
5265 FoundAnything = true;
5266 This->Decls.push_back(ND);
5267 }
5268
5269 return FoundAnything;
5270 }
5271 };
5272}
5273
John McCall76bd1f32010-06-01 09:23:16 +00005274DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005275ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005276 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005277 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005278 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005279 if (!Name)
5280 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5281 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005282
Chris Lattner5f9e2722011-07-23 10:55:15 +00005283 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005284
5285 // Compute the declaration contexts we need to look into. Multiple such
5286 // declaration contexts occur when two declaration contexts from disjoint
5287 // modules get merged, e.g., when two namespaces with the same name are
5288 // independently defined in separate modules.
5289 SmallVector<const DeclContext *, 2> Contexts;
5290 Contexts.push_back(DC);
5291
5292 if (DC->isNamespace()) {
5293 MergedDeclsMap::iterator Merged
5294 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5295 if (Merged != MergedDecls.end()) {
5296 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5297 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5298 }
5299 }
5300
5301 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005302 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005303 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005304 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005305 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005306}
5307
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005308namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005309 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005310 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005311 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005312 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005313 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005314 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005315
5316 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005317 DeclContextAllNamesVisitor(ASTReader &Reader,
5318 SmallVectorImpl<const DeclContext *> &Contexts,
5319 llvm::DenseMap<DeclarationName,
5320 SmallVector<NamedDecl *, 8> > &Decls)
5321 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005322
5323 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005324 DeclContextAllNamesVisitor *This
5325 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005326
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005327 // Check whether we have any visible declaration information for
5328 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005329 ModuleFile::DeclContextInfosMap::iterator Info;
5330 bool FoundInfo = false;
5331 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5332 Info = M.DeclContextInfos.find(This->Contexts[I]);
5333 if (Info != M.DeclContextInfos.end() &&
5334 Info->second.NameLookupTableData) {
5335 FoundInfo = true;
5336 break;
5337 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005338 }
5339
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005340 if (!FoundInfo)
5341 return false;
5342
5343 ASTDeclContextNameLookupTable *LookupTable =
5344 Info->second.NameLookupTableData;
5345 bool FoundAnything = false;
5346 for (ASTDeclContextNameLookupTable::data_iterator
5347 I = LookupTable->data_begin(), E = LookupTable->data_end();
5348 I != E; ++I) {
5349 ASTDeclContextNameLookupTrait::data_type Data = *I;
5350 for (; Data.first != Data.second; ++Data.first) {
5351 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5352 *Data.first);
5353 if (!ND)
5354 continue;
5355
5356 // Record this declaration.
5357 FoundAnything = true;
5358 This->Decls[ND->getDeclName()].push_back(ND);
5359 }
5360 }
5361
5362 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005363 }
5364 };
5365}
5366
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005367void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005368 if (!DC->hasExternalVisibleStorage())
5369 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005370 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5371
5372 // Compute the declaration contexts we need to look into. Multiple such
5373 // declaration contexts occur when two declaration contexts from disjoint
5374 // modules get merged, e.g., when two namespaces with the same name are
5375 // independently defined in separate modules.
5376 SmallVector<const DeclContext *, 2> Contexts;
5377 Contexts.push_back(DC);
5378
5379 if (DC->isNamespace()) {
5380 MergedDeclsMap::iterator Merged
5381 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5382 if (Merged != MergedDecls.end()) {
5383 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5384 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5385 }
5386 }
5387
5388 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5389 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5390 ++NumVisibleDeclContextsRead;
5391
5392 for (llvm::DenseMap<DeclarationName,
5393 llvm::SmallVector<NamedDecl*, 8> >::iterator
5394 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5395 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5396 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005397 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005398}
5399
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005400/// \brief Under non-PCH compilation the consumer receives the objc methods
5401/// before receiving the implementation, and codegen depends on this.
5402/// We simulate this by deserializing and passing to consumer the methods of the
5403/// implementation before passing the deserialized implementation decl.
5404static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5405 ASTConsumer *Consumer) {
5406 assert(ImplD && Consumer);
5407
5408 for (ObjCImplDecl::method_iterator
5409 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005410 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005411
5412 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5413}
5414
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005415void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005416 assert(Consumer);
5417 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005418 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005419 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005420
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005421 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005422 }
5423}
5424
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005425void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5426 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5427 PassObjCImplDeclToConsumer(ImplD, Consumer);
5428 else
5429 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5430}
5431
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005432void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005433 this->Consumer = Consumer;
5434
Douglas Gregorfdd01722009-04-14 00:24:19 +00005435 if (!Consumer)
5436 return;
5437
5438 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005439 // Force deserialization of this decl, which will cause it to be queued for
5440 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005441 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005442 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005443 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005444
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005445 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005446}
5447
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005448void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005449 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005450
Mike Stump1eb44332009-09-09 15:08:12 +00005451 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005452 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005453 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005454 unsigned NumDeclsLoaded
5455 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5456 (Decl *)0);
5457 unsigned NumIdentifiersLoaded
5458 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5459 IdentifiersLoaded.end(),
5460 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005461 unsigned NumMacrosLoaded
5462 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5463 MacrosLoaded.end(),
5464 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005465 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005466 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5467 SelectorsLoaded.end(),
5468 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005469
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005470 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5471 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005472 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005473 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5474 NumSLocEntriesRead, TotalNumSLocEntries,
5475 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005476 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005477 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005478 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5479 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5480 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005481 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005482 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5483 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005484 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005485 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005486 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5487 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005488 if (!MacrosLoaded.empty())
5489 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5490 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5491 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005492 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005493 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005494 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5495 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005496 if (TotalNumStatements)
5497 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5498 NumStatementsRead, TotalNumStatements,
5499 ((float)NumStatementsRead/TotalNumStatements * 100));
5500 if (TotalNumMacros)
5501 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5502 NumMacrosRead, TotalNumMacros,
5503 ((float)NumMacrosRead/TotalNumMacros * 100));
5504 if (TotalLexicalDeclContexts)
5505 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5506 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5507 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5508 * 100));
5509 if (TotalVisibleDeclContexts)
5510 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5511 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5512 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5513 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005514 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005515 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005516 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5517 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005518 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005519 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005520 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005521 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005522 dump();
5523 std::fprintf(stderr, "\n");
5524}
5525
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005526template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005527static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005528dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005529 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005530 InitialCapacity> &Map) {
5531 if (Map.begin() == Map.end())
5532 return;
5533
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005534 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005535 llvm::errs() << Name << ":\n";
5536 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5537 I != IEnd; ++I) {
5538 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5539 << "\n";
5540 }
5541}
5542
Douglas Gregor23d7df52011-07-21 19:50:14 +00005543void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005544 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005545 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005546 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005547 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005548 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005549 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005550 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005551 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005552 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005553 dumpModuleIDMap("Global preprocessed entity map",
5554 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005555
5556 llvm::errs() << "\n*** PCH/Modules Loaded:";
5557 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5558 MEnd = ModuleMgr.end();
5559 M != MEnd; ++M)
5560 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005561}
5562
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005563/// Return the amount of memory used by memory buffers, breaking down
5564/// by heap-backed versus mmap'ed memory.
5565void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005566 for (ModuleConstIterator I = ModuleMgr.begin(),
5567 E = ModuleMgr.end(); I != E; ++I) {
5568 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005569 size_t bytes = buf->getBufferSize();
5570 switch (buf->getBufferKind()) {
5571 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5572 sizes.malloc_bytes += bytes;
5573 break;
5574 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5575 sizes.mmap_bytes += bytes;
5576 break;
5577 }
5578 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005579 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005580}
5581
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005582void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005583 SemaObj = &S;
Axel Naumann0ec56b72012-10-18 19:05:02 +00005584 S.addExternalSource(this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005585
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005586 // Makes sure any declarations that were deserialized "too early"
5587 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005588 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005589 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5590 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005591 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005592 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005593
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005594 // Load the offsets of the declarations that Sema references.
5595 // They will be lazily deserialized when needed.
5596 if (!SemaDeclRefs.empty()) {
5597 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005598 if (!SemaObj->StdNamespace)
5599 SemaObj->StdNamespace = SemaDeclRefs[0];
5600 if (!SemaObj->StdBadAlloc)
5601 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005602 }
5603
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005604 if (!FPPragmaOptions.empty()) {
5605 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5606 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5607 }
5608
5609 if (!OpenCLExtensions.empty()) {
5610 unsigned I = 0;
5611#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5612#include "clang/Basic/OpenCLExtensions.def"
5613
5614 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5615 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005616}
5617
Douglas Gregor211f6e82011-08-20 04:39:52 +00005618IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005619 // Note that we are loading an identifier.
5620 Deserializing AnIdentifier(this);
5621
Douglas Gregor057df202012-01-18 20:56:22 +00005622 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5623 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005624 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005625 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005626 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005627 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005628}
5629
Douglas Gregor95f42922010-10-14 22:11:03 +00005630namespace clang {
5631 /// \brief An identifier-lookup iterator that enumerates all of the
5632 /// identifiers stored within a set of AST files.
5633 class ASTIdentifierIterator : public IdentifierIterator {
5634 /// \brief The AST reader whose identifiers are being enumerated.
5635 const ASTReader &Reader;
5636
5637 /// \brief The current index into the chain of AST files stored in
5638 /// the AST reader.
5639 unsigned Index;
5640
5641 /// \brief The current position within the identifier lookup table
5642 /// of the current AST file.
5643 ASTIdentifierLookupTable::key_iterator Current;
5644
5645 /// \brief The end position within the identifier lookup table of
5646 /// the current AST file.
5647 ASTIdentifierLookupTable::key_iterator End;
5648
5649 public:
5650 explicit ASTIdentifierIterator(const ASTReader &Reader);
5651
Chris Lattner5f9e2722011-07-23 10:55:15 +00005652 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005653 };
5654}
5655
5656ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005657 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005658 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005659 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005660 Current = IdTable->key_begin();
5661 End = IdTable->key_end();
5662}
5663
Chris Lattner5f9e2722011-07-23 10:55:15 +00005664StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005665 while (Current == End) {
5666 // If we have exhausted all of our AST files, we're done.
5667 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005668 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005669
5670 --Index;
5671 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005672 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5673 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005674 Current = IdTable->key_begin();
5675 End = IdTable->key_end();
5676 }
5677
5678 // We have any identifiers remaining in the current AST file; return
5679 // the next one.
5680 std::pair<const char*, unsigned> Key = *Current;
5681 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005682 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005683}
5684
5685IdentifierIterator *ASTReader::getIdentifiers() const {
5686 return new ASTIdentifierIterator(*this);
5687}
5688
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005689namespace clang { namespace serialization {
5690 class ReadMethodPoolVisitor {
5691 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005692 Selector Sel;
5693 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005694 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5695 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005696
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005697 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005698 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5699 unsigned PriorGeneration)
5700 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005701
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005702 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005703 ReadMethodPoolVisitor *This
5704 = static_cast<ReadMethodPoolVisitor *>(UserData);
5705
5706 if (!M.SelectorLookupTable)
5707 return false;
5708
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005709 // If we've already searched this module file, skip it now.
5710 if (M.Generation <= This->PriorGeneration)
5711 return true;
5712
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005713 ASTSelectorLookupTable *PoolTable
5714 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5715 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5716 if (Pos == PoolTable->end())
5717 return false;
5718
5719 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005720 // FIXME: Not quite happy with the statistics here. We probably should
5721 // disable this tracking when called via LoadSelector.
5722 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005723 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005724 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005725 if (This->Reader.DeserializationListener)
5726 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5727 This->Sel);
5728
5729 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5730 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5731 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005732 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005733
5734 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005735 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5736 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005737 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005738
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005739 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005740 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5741 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005742 }
5743 };
5744} } // end namespace clang::serialization
5745
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005746/// \brief Add the given set of methods to the method list.
5747static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5748 ObjCMethodList &List) {
5749 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5750 S.addMethodToGlobalList(&List, Methods[I]);
5751 }
5752}
5753
5754void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005755 // Get the selector generation and update it to the current generation.
5756 unsigned &Generation = SelectorGeneration[Sel];
5757 unsigned PriorGeneration = Generation;
5758 Generation = CurrentGeneration;
5759
5760 // Search for methods defined with this selector.
5761 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005762 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005763
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005764 if (Visitor.getInstanceMethods().empty() &&
5765 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005766 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005767 return;
5768 }
5769
5770 if (!getSema())
5771 return;
5772
5773 Sema &S = *getSema();
5774 Sema::GlobalMethodPool::iterator Pos
5775 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5776
5777 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5778 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005779}
5780
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005781void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005782 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005783 Namespaces.clear();
5784
5785 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5786 if (NamespaceDecl *Namespace
5787 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5788 Namespaces.push_back(Namespace);
5789 }
5790}
5791
Douglas Gregora8623202011-07-27 20:58:46 +00005792void ASTReader::ReadTentativeDefinitions(
5793 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5794 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5795 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5796 if (Var)
5797 TentativeDefs.push_back(Var);
5798 }
5799 TentativeDefinitions.clear();
5800}
5801
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005802void ASTReader::ReadUnusedFileScopedDecls(
5803 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5804 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5805 DeclaratorDecl *D
5806 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5807 if (D)
5808 Decls.push_back(D);
5809 }
5810 UnusedFileScopedDecls.clear();
5811}
5812
Douglas Gregor0129b562011-07-27 21:57:17 +00005813void ASTReader::ReadDelegatingConstructors(
5814 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5815 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5816 CXXConstructorDecl *D
5817 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5818 if (D)
5819 Decls.push_back(D);
5820 }
5821 DelegatingCtorDecls.clear();
5822}
5823
Douglas Gregord58a0a52011-07-28 00:39:29 +00005824void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5825 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5826 TypedefNameDecl *D
5827 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5828 if (D)
5829 Decls.push_back(D);
5830 }
5831 ExtVectorDecls.clear();
5832}
5833
Douglas Gregora126f172011-07-28 00:53:40 +00005834void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5835 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5836 CXXRecordDecl *D
5837 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5838 if (D)
5839 Decls.push_back(D);
5840 }
5841 DynamicClasses.clear();
5842}
5843
Douglas Gregorec12ce22011-07-28 14:20:37 +00005844void
5845ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5846 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5847 NamedDecl *D
5848 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5849 if (D)
5850 Decls.push_back(D);
5851 }
5852 LocallyScopedExternalDecls.clear();
5853}
5854
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005855void ASTReader::ReadReferencedSelectors(
5856 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5857 if (ReferencedSelectorsData.empty())
5858 return;
5859
5860 // If there are @selector references added them to its pool. This is for
5861 // implementation of -Wselector.
5862 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5863 unsigned I = 0;
5864 while (I < DataSize) {
5865 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5866 SourceLocation SelLoc
5867 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5868 Sels.push_back(std::make_pair(Sel, SelLoc));
5869 }
5870 ReferencedSelectorsData.clear();
5871}
5872
Douglas Gregor31e37b22011-07-28 18:09:57 +00005873void ASTReader::ReadWeakUndeclaredIdentifiers(
5874 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5875 if (WeakUndeclaredIdentifiers.empty())
5876 return;
5877
5878 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5879 IdentifierInfo *WeakId
5880 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5881 IdentifierInfo *AliasId
5882 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5883 SourceLocation Loc
5884 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5885 bool Used = WeakUndeclaredIdentifiers[I++];
5886 WeakInfo WI(AliasId, Loc);
5887 WI.setUsed(Used);
5888 WeakIDs.push_back(std::make_pair(WeakId, WI));
5889 }
5890 WeakUndeclaredIdentifiers.clear();
5891}
5892
Douglas Gregordfe65432011-07-28 19:11:31 +00005893void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5894 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5895 ExternalVTableUse VT;
5896 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5897 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5898 VT.DefinitionRequired = VTableUses[Idx++];
5899 VTables.push_back(VT);
5900 }
5901
5902 VTableUses.clear();
5903}
5904
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005905void ASTReader::ReadPendingInstantiations(
5906 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5907 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5908 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5909 SourceLocation Loc
5910 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005911
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005912 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005913 }
5914 PendingInstantiations.clear();
5915}
5916
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005917void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005918 // It would be complicated to avoid reading the methods anyway. So don't.
5919 ReadMethodPool(Sel);
5920}
5921
Douglas Gregor95eab172011-07-28 20:55:49 +00005922void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005923 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005924 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005925 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005926 if (DeserializationListener)
5927 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005928}
5929
Douglas Gregord89275b2009-07-06 18:54:52 +00005930/// \brief Set the globally-visible declarations associated with the given
5931/// identifier.
5932///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005933/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005934/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005935/// them.
5936///
5937/// \param II an IdentifierInfo that refers to one or more globally-visible
5938/// declarations.
5939///
5940/// \param DeclIDs the set of declaration IDs with the name @p II that are
5941/// visible at global scope.
5942///
5943/// \param Nonrecursive should be true to indicate that the caller knows that
5944/// this call is non-recursive, and therefore the globally-visible declarations
5945/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005946void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005947ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005948 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005949 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005950 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005951 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5952 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5953 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005954 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005955 return;
5956 }
Mike Stump1eb44332009-09-09 15:08:12 +00005957
Douglas Gregord89275b2009-07-06 18:54:52 +00005958 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5959 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5960 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005961 // Introduce this declaration into the translation-unit scope
5962 // and add it to the declaration chain for this identifier, so
5963 // that (unqualified) name lookup will find it.
5964 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005965 } else {
5966 // Queue this declaration so that it will be added to the
5967 // translation unit scope and identifier's declaration chain
5968 // once a Sema object is known.
5969 PreloadedDecls.push_back(D);
5970 }
5971 }
5972}
5973
Douglas Gregor95eab172011-07-28 20:55:49 +00005974IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005975 if (ID == 0)
5976 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005977
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005978 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005979 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005980 return 0;
5981 }
Mike Stump1eb44332009-09-09 15:08:12 +00005982
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005983 ID -= 1;
5984 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005985 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5986 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005987 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005988 unsigned Index = ID - M->BaseIdentifierID;
5989 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005990
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005991 // All of the strings in the AST file are preceded by a 16-bit length.
5992 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005993 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5994 // unsigned integers. This is important to avoid integer overflow when
5995 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005996 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005997 unsigned StrLen = (((unsigned) StrLenPtr[0])
5998 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005999 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006000 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00006001 if (DeserializationListener)
6002 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00006003 }
Mike Stump1eb44332009-09-09 15:08:12 +00006004
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00006005 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00006006}
6007
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006008IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00006009 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
6010}
6011
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006012IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00006013 if (LocalID < NUM_PREDEF_IDENT_IDS)
6014 return LocalID;
6015
6016 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6017 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
6018 assert(I != M.IdentifierRemap.end()
6019 && "Invalid index into identifier index remap");
6020
6021 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00006022}
6023
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006024MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00006025 if (ID == 0)
6026 return 0;
6027
6028 if (MacrosLoaded.empty()) {
6029 Error("no macro table in AST file");
6030 return 0;
6031 }
6032
6033 ID -= NUM_PREDEF_MACRO_IDS;
6034 if (!MacrosLoaded[ID]) {
6035 GlobalMacroMapType::iterator I
6036 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
6037 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
6038 ModuleFile *M = I->second;
6039 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006040 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00006041 }
6042
6043 return MacrosLoaded[ID];
6044}
6045
6046MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
6047 if (LocalID < NUM_PREDEF_MACRO_IDS)
6048 return LocalID;
6049
6050 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6051 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
6052 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
6053
6054 return LocalID + I->second;
6055}
6056
Douglas Gregor26ced122011-12-01 00:59:36 +00006057serialization::SubmoduleID
6058ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
6059 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
6060 return LocalID;
6061
6062 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6063 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
6064 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006065 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00006066
6067 return LocalID + I->second;
6068}
6069
6070Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
6071 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
6072 assert(GlobalID == 0 && "Unhandled global submodule ID");
6073 return 0;
6074 }
6075
6076 if (GlobalID > SubmodulesLoaded.size()) {
6077 Error("submodule ID out of range in AST file");
6078 return 0;
6079 }
6080
6081 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
6082}
6083
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006084Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006085 return DecodeSelector(getGlobalSelectorID(M, LocalID));
6086}
6087
6088Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006089 if (ID == 0)
6090 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00006091
Sebastian Redl725cd962010-08-04 20:40:17 +00006092 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00006093 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006094 return Selector();
6095 }
Douglas Gregor83941df2009-04-25 17:48:32 +00006096
Sebastian Redl725cd962010-08-04 20:40:17 +00006097 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00006098 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00006099 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
6100 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006101 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00006102 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006103 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00006104 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00006105 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00006106 if (DeserializationListener)
6107 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00006108 }
6109
Sebastian Redl725cd962010-08-04 20:40:17 +00006110 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00006111}
6112
Douglas Gregor8451ec72011-07-28 14:41:43 +00006113Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00006114 return DecodeSelector(ID);
6115}
6116
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006117uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00006118 // ID 0 (the null selector) is considered an external selector.
6119 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00006120}
6121
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006122serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006123ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006124 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
6125 return LocalID;
6126
6127 ContinuousRangeMap<uint32_t, int, 2>::iterator I
6128 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
6129 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00006130 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00006131
6132 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00006133}
6134
Mike Stump1eb44332009-09-09 15:08:12 +00006135DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006136ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00006137 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00006138 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
6139 switch (Kind) {
6140 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00006141 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006142
6143 case DeclarationName::ObjCZeroArgSelector:
6144 case DeclarationName::ObjCOneArgSelector:
6145 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006146 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006147
6148 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006149 return Context.DeclarationNames.getCXXConstructorName(
6150 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006151
6152 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006153 return Context.DeclarationNames.getCXXDestructorName(
6154 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006155
6156 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006157 return Context.DeclarationNames.getCXXConversionFunctionName(
6158 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006159
6160 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006161 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006162 (OverloadedOperatorKind)Record[Idx++]);
6163
Sean Hunt3e518bd2009-11-29 07:34:05 +00006164 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006165 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006166 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006167
Douglas Gregor2cf26342009-04-09 22:27:44 +00006168 case DeclarationName::CXXUsingDirective:
6169 return DeclarationName::getUsingDirectiveName();
6170 }
6171
David Blaikie7530c032012-01-17 06:56:22 +00006172 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006173}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006174
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006175void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006176 DeclarationNameLoc &DNLoc,
6177 DeclarationName Name,
6178 const RecordData &Record, unsigned &Idx) {
6179 switch (Name.getNameKind()) {
6180 case DeclarationName::CXXConstructorName:
6181 case DeclarationName::CXXDestructorName:
6182 case DeclarationName::CXXConversionFunctionName:
6183 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6184 break;
6185
6186 case DeclarationName::CXXOperatorName:
6187 DNLoc.CXXOperatorName.BeginOpNameLoc
6188 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6189 DNLoc.CXXOperatorName.EndOpNameLoc
6190 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6191 break;
6192
6193 case DeclarationName::CXXLiteralOperatorName:
6194 DNLoc.CXXLiteralOperatorName.OpNameLoc
6195 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6196 break;
6197
6198 case DeclarationName::Identifier:
6199 case DeclarationName::ObjCZeroArgSelector:
6200 case DeclarationName::ObjCOneArgSelector:
6201 case DeclarationName::ObjCMultiArgSelector:
6202 case DeclarationName::CXXUsingDirective:
6203 break;
6204 }
6205}
6206
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006207void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006208 DeclarationNameInfo &NameInfo,
6209 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006210 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006211 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6212 DeclarationNameLoc DNLoc;
6213 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6214 NameInfo.setInfo(DNLoc);
6215}
6216
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006217void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006218 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006219 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006220 unsigned NumTPLists = Record[Idx++];
6221 Info.NumTemplParamLists = NumTPLists;
6222 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006223 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006224 for (unsigned i=0; i != NumTPLists; ++i)
6225 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6226 }
6227}
6228
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006229TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006230ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006231 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006232 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006233 switch (Kind) {
6234 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006235 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006236
6237 case TemplateName::OverloadedTemplate: {
6238 unsigned size = Record[Idx++];
6239 UnresolvedSet<8> Decls;
6240 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006241 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006242
Douglas Gregor35942772011-09-09 21:34:22 +00006243 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006244 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006245
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006246 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006247 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006248 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006249 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006250 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006251 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006252
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006253 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006254 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006255 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006256 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006257 GetIdentifierInfo(F, Record,
6258 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006259 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006260 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006261 }
John McCall14606042011-06-30 08:33:18 +00006262
6263 case TemplateName::SubstTemplateTemplateParm: {
6264 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006265 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006266 if (!param) return TemplateName();
6267 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006268 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006269 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006270
6271 case TemplateName::SubstTemplateTemplateParmPack: {
6272 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006273 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006274 if (!Param)
6275 return TemplateName();
6276
6277 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6278 if (ArgPack.getKind() != TemplateArgument::Pack)
6279 return TemplateName();
6280
Douglas Gregor35942772011-09-09 21:34:22 +00006281 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006282 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006283 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006284
David Blaikieb219cfc2011-09-23 05:06:16 +00006285 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006286}
6287
6288TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006289ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006290 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006291 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6292 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006293 case TemplateArgument::Null:
6294 return TemplateArgument();
6295 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006296 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006297 case TemplateArgument::Declaration: {
6298 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6299 bool ForReferenceParam = Record[Idx++];
6300 return TemplateArgument(D, ForReferenceParam);
6301 }
6302 case TemplateArgument::NullPtr:
6303 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006304 case TemplateArgument::Integral: {
6305 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006306 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006307 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006308 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006309 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006310 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006311 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006312 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006313 llvm::Optional<unsigned> NumTemplateExpansions;
6314 if (unsigned NumExpansions = Record[Idx++])
6315 NumTemplateExpansions = NumExpansions - 1;
6316 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006317 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006318 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006319 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006320 case TemplateArgument::Pack: {
6321 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006322 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006323 for (unsigned I = 0; I != NumArgs; ++I)
6324 Args[I] = ReadTemplateArgument(F, Record, Idx);
6325 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006326 }
6327 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006328
David Blaikieb219cfc2011-09-23 05:06:16 +00006329 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006330}
6331
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006332TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006333ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006334 const RecordData &Record, unsigned &Idx) {
6335 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6336 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6337 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006338
6339 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006340 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006341 Params.reserve(NumParams);
6342 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006343 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006344
6345 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006346 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006347 Params.data(), Params.size(), RAngleLoc);
6348 return TemplateParams;
6349}
6350
6351void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006352ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006353ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006354 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006355 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006356 unsigned NumTemplateArgs = Record[Idx++];
6357 TemplArgs.reserve(NumTemplateArgs);
6358 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006359 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006360}
6361
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006362/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006363void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006364 const RecordData &Record, unsigned &Idx) {
6365 unsigned NumDecls = Record[Idx++];
6366 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006367 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006368 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6369 Set.addDecl(D, AS);
6370 }
6371}
6372
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006373CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006374ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006375 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006376 bool isVirtual = static_cast<bool>(Record[Idx++]);
6377 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6378 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006379 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006380 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6381 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006382 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006383 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006384 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006385 Result.setInheritConstructors(inheritConstructors);
6386 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006387}
6388
Sean Huntcbb67482011-01-08 20:30:50 +00006389std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006390ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006391 unsigned &Idx) {
6392 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006393 unsigned NumInitializers = Record[Idx++];
6394 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006395 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006396 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006397 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006398 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006399 bool IsBaseVirtual = false;
6400 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006401 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006402
Sean Hunt156b6402011-05-04 01:19:08 +00006403 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6404 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006405 case CTOR_INITIALIZER_BASE:
6406 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006407 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006408 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006409
6410 case CTOR_INITIALIZER_DELEGATING:
6411 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006412 break;
6413
6414 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006415 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006416 break;
6417
6418 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006419 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006420 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006421 }
Sean Hunt156b6402011-05-04 01:19:08 +00006422
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006423 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006424 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006425 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6426 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006427 bool IsWritten = Record[Idx++];
6428 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006429 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006430 if (IsWritten) {
6431 SourceOrderOrNumArrayIndices = Record[Idx++];
6432 } else {
6433 SourceOrderOrNumArrayIndices = Record[Idx++];
6434 Indices.reserve(SourceOrderOrNumArrayIndices);
6435 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006436 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006437 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006438
Sean Huntcbb67482011-01-08 20:30:50 +00006439 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006440 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006441 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006442 LParenLoc, Init, RParenLoc,
6443 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006444 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006445 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6446 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006447 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006448 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006449 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006450 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006451 else
Douglas Gregor35942772011-09-09 21:34:22 +00006452 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006453 MemberOrEllipsisLoc, LParenLoc,
6454 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006455 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006456 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006457 LParenLoc, Init, RParenLoc,
6458 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006459 }
6460
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006461 if (IsWritten)
6462 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006463 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006464 }
6465 }
6466
Sean Huntcbb67482011-01-08 20:30:50 +00006467 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006468}
6469
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006470NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006471ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006472 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006473 unsigned N = Record[Idx++];
6474 NestedNameSpecifier *NNS = 0, *Prev = 0;
6475 for (unsigned I = 0; I != N; ++I) {
6476 NestedNameSpecifier::SpecifierKind Kind
6477 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6478 switch (Kind) {
6479 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006480 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006481 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006482 break;
6483 }
6484
6485 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006486 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006487 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006488 break;
6489 }
6490
Douglas Gregor14aba762011-02-24 02:36:08 +00006491 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006492 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006493 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006494 break;
6495 }
6496
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006497 case NestedNameSpecifier::TypeSpec:
6498 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006499 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006500 if (!T)
6501 return 0;
6502
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006503 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006504 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006505 break;
6506 }
6507
6508 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006509 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006510 // No associated value, and there can't be a prefix.
6511 break;
6512 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006513 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006514 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006515 }
6516 return NNS;
6517}
6518
Douglas Gregordc355712011-02-25 00:36:19 +00006519NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006520ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006521 unsigned &Idx) {
6522 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006523 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006524 for (unsigned I = 0; I != N; ++I) {
6525 NestedNameSpecifier::SpecifierKind Kind
6526 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6527 switch (Kind) {
6528 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006529 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006530 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006531 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006532 break;
6533 }
6534
6535 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006536 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006537 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006538 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006539 break;
6540 }
6541
6542 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006543 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006544 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006545 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006546 break;
6547 }
6548
6549 case NestedNameSpecifier::TypeSpec:
6550 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006551 bool Template = Record[Idx++];
6552 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6553 if (!T)
6554 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006555 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006556
6557 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006558 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006559 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6560 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006561 break;
6562 }
6563
6564 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006565 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006566 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006567 break;
6568 }
6569 }
Douglas Gregordc355712011-02-25 00:36:19 +00006570 }
6571
Douglas Gregor35942772011-09-09 21:34:22 +00006572 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006573}
6574
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006575SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006576ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006577 unsigned &Idx) {
6578 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6579 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006580 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006581}
6582
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006583/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006584llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006585 unsigned BitWidth = Record[Idx++];
6586 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6587 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6588 Idx += NumWords;
6589 return Result;
6590}
6591
6592/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006593llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006594 bool isUnsigned = Record[Idx++];
6595 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6596}
6597
Douglas Gregor17fc2232009-04-14 21:55:33 +00006598/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006599llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006600 return llvm::APFloat(ReadAPInt(Record, Idx));
6601}
6602
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006603// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006604std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006605 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006606 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006607 Idx += Len;
6608 return Result;
6609}
6610
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006611VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6612 unsigned &Idx) {
6613 unsigned Major = Record[Idx++];
6614 unsigned Minor = Record[Idx++];
6615 unsigned Subminor = Record[Idx++];
6616 if (Minor == 0)
6617 return VersionTuple(Major);
6618 if (Subminor == 0)
6619 return VersionTuple(Major, Minor - 1);
6620 return VersionTuple(Major, Minor - 1, Subminor - 1);
6621}
6622
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006623CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006624 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006625 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006626 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006627 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006628}
6629
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006630DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006631 return Diag(SourceLocation(), DiagID);
6632}
6633
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006634DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006635 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006636}
Douglas Gregor025452f2009-04-17 00:04:06 +00006637
Douglas Gregor668c1a42009-04-21 22:25:48 +00006638/// \brief Retrieve the identifier table associated with the
6639/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006640IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006641 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006642}
6643
Douglas Gregor025452f2009-04-17 00:04:06 +00006644/// \brief Record that the given ID maps to the given switch-case
6645/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006646void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006647 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6648 "Already have a SwitchCase with this ID");
6649 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006650}
6651
6652/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006653SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006654 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6655 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006656}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006657
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006658void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006659 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006660}
6661
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006662void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006663 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006664 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6665 serialization::ModuleFile *> >::iterator
6666 I = CommentsCursors.begin(),
6667 E = CommentsCursors.end();
6668 I != E; ++I) {
6669 llvm::BitstreamCursor &Cursor = I->first;
6670 serialization::ModuleFile &F = *I->second;
6671 SavedStreamPosition SavedPosition(Cursor);
6672
6673 RecordData Record;
6674 while (true) {
6675 unsigned Code = Cursor.ReadCode();
6676 if (Code == llvm::bitc::END_BLOCK)
6677 break;
6678
6679 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6680 // No known subblocks, always skip them.
6681 Cursor.ReadSubBlockID();
6682 if (Cursor.SkipBlock()) {
6683 Error("malformed block record in AST file");
6684 return;
6685 }
6686 continue;
6687 }
6688
6689 if (Code == llvm::bitc::DEFINE_ABBREV) {
6690 Cursor.ReadAbbrevRecord();
6691 continue;
6692 }
6693
6694 // Read a record.
6695 Record.clear();
6696 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006697 case COMMENTS_RAW_COMMENT: {
6698 unsigned Idx = 0;
6699 SourceRange SR = ReadSourceRange(F, Record, Idx);
6700 RawComment::CommentKind Kind =
6701 (RawComment::CommentKind) Record[Idx++];
6702 bool IsTrailingComment = Record[Idx++];
6703 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006704 Comments.push_back(new (Context) RawComment(SR, Kind,
6705 IsTrailingComment,
6706 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006707 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006708 }
6709 }
6710 }
6711 }
6712 Context.Comments.addCommentsToFront(Comments);
6713}
6714
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006715void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006716 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6717 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006718 // If any identifiers with corresponding top-level declarations have
6719 // been loaded, load those declarations now.
6720 while (!PendingIdentifierInfos.empty()) {
6721 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6722 PendingIdentifierInfos.front().DeclIDs, true);
6723 PendingIdentifierInfos.pop_front();
6724 }
6725
Douglas Gregora1be2782011-12-17 23:38:30 +00006726 // Load pending declaration chains.
6727 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6728 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006729 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006730 }
6731 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006732
6733 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00006734 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6735 // FIXME: std::move here
6736 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006737 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00006738 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6739 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006740 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00006741 }
6742 }
6743 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006744 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006745
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006746 // If we deserialized any C++ or Objective-C class definitions, any
6747 // Objective-C protocol definitions, or any redeclarable templates, make sure
6748 // that all redeclarations point to the definitions. Note that this can only
6749 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006750 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6751 DEnd = PendingDefinitions.end();
6752 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006753 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6754 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6755 // Make sure that the TagType points at the definition.
6756 const_cast<TagType*>(TagT)->decl = TD;
6757 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006758
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006759 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6760 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6761 REnd = RD->redecls_end();
6762 R != REnd; ++R)
6763 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6764
6765 }
6766
Douglas Gregorfc529f72011-12-19 19:00:47 +00006767 continue;
6768 }
6769
Douglas Gregor1d784b22012-01-01 19:51:50 +00006770 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006771 // Make sure that the ObjCInterfaceType points at the definition.
6772 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6773 ->Decl = ID;
6774
Douglas Gregor1d784b22012-01-01 19:51:50 +00006775 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6776 REnd = ID->redecls_end();
6777 R != REnd; ++R)
6778 R->Data = ID->Data;
6779
6780 continue;
6781 }
6782
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006783 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6784 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6785 REnd = PD->redecls_end();
6786 R != REnd; ++R)
6787 R->Data = PD->Data;
6788
6789 continue;
6790 }
6791
6792 RedeclarableTemplateDecl *RTD
6793 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6794 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6795 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006796 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006797 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006798 }
6799 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006800
6801 // Load the bodies of any functions or methods we've encountered. We do
6802 // this now (delayed) so that we can be sure that the declaration chains
6803 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006804 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6805 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006806 PB != PBEnd; ++PB) {
6807 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6808 // FIXME: Check for =delete/=default?
6809 // FIXME: Complain about ODR violations here?
6810 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6811 FD->setLazyBody(PB->second);
6812 continue;
6813 }
6814
6815 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6816 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6817 MD->setLazyBody(PB->second);
6818 }
6819 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006820}
6821
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006822void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006823 assert(NumCurrentElementsDeserializing &&
6824 "FinishedDeserializing not paired with StartedDeserializing");
6825 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006826 // We decrease NumCurrentElementsDeserializing only after pending actions
6827 // are finished, to avoid recursively re-calling finishPendingActions().
6828 finishPendingActions();
6829 }
6830 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006831
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006832 if (NumCurrentElementsDeserializing == 0 &&
6833 Consumer && !PassingDeclsToConsumer) {
6834 // Guard variable to avoid recursively redoing the process of passing
6835 // decls to consumer.
6836 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6837 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006838
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006839 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006840 // We are not in recursive loading, so it's safe to pass the "interesting"
6841 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006842 Decl *D = InterestingDecls.front();
6843 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006844 PassInterestingDeclToConsumer(D);
6845 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006846 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006847}
Douglas Gregor501c1032010-08-19 00:28:17 +00006848
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006849ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006850 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006851 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006852 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6853 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006854 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006855 Consumer(0), ModuleMgr(PP.getFileManager()),
Douglas Gregorcaed0602012-10-18 21:31:35 +00006856 isysroot(isysroot), DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006857 DisableStatCache(DisableStatCache),
6858 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006859 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6860 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006861 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006862 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6863 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6864 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6865 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006866 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6867 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006868 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006869 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006870{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006871 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006872}
6873
Sebastian Redle1dde812010-08-24 00:50:04 +00006874ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006875 for (DeclContextVisibleUpdatesPending::iterator
6876 I = PendingVisibleUpdates.begin(),
6877 E = PendingVisibleUpdates.end();
6878 I != E; ++I) {
6879 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6880 F = I->second.end();
6881 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006882 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006883 }
6884}