blob: d060e795ded858769024a6eaee9e813e39673f89 [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 Gregor445e23e2009-10-05 21:07:28 +000040#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000041#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000045#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000046#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000048#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000049#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000050#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000051#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000053#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000054
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000057using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
59//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000060// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000061//===----------------------------------------------------------------------===//
62
Sebastian Redl571db7f2010-08-18 23:56:56 +000063ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000064
65bool
66PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
David Blaikie4e4d0842012-03-11 07:00:24 +000067 const LangOptions &PPLangOpts = PP.getLangOpts();
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000068
69#define LANGOPT(Name, Bits, Default, Description) \
70 if (PPLangOpts.Name != LangOpts.Name) { \
71 Reader.Diag(diag::err_pch_langopt_mismatch) \
72 << Description << LangOpts.Name << PPLangOpts.Name; \
73 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000074 }
75
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000076#define VALUE_LANGOPT(Name, Bits, Default, Description) \
77 if (PPLangOpts.Name != LangOpts.Name) { \
78 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
79 << Description; \
80 return true; \
81}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000083#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
84 if (PPLangOpts.get##Name() != LangOpts.get##Name()) { \
85 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
86 << Description; \
87 return true; \
88 }
89
90#define BENIGN_LANGOPT(Name, Bits, Default, Description)
91#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
92#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +000093
94 if (PPLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
95 Reader.Diag(diag::err_pch_langopt_value_mismatch)
96 << "target Objective-C runtime";
97 return true;
98 }
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000099
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 return false;
101}
102
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000104 if (Triple == PP.getTargetInfo().getTriple().str())
105 return false;
106
107 Reader.Diag(diag::warn_pch_target_triple)
108 << Triple << PP.getTargetInfo().getTriple().str();
109 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000110}
111
Benjamin Kramer54353f42010-11-25 18:29:30 +0000112namespace {
113 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000115 };
116 struct EmptyBlock {
117 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
118 };
119}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000120
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000122 PCHPredefinesBlocks R) {
123 // First, sum up the lengths.
124 unsigned LL = 0, RL = 0;
125 for (unsigned I = 0, N = L.size(); I != N; ++I) {
126 LL += L[I].size();
127 }
128 for (unsigned I = 0, N = R.size(); I != N; ++I) {
129 RL += R[I].Data.size();
130 }
131 if (LL != RL)
132 return false;
133 if (LL == 0 && RL == 0)
134 return true;
135
136 // Kick out empty parts, they confuse the algorithm below.
137 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
138 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
139
140 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000142 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000143 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000144 for (;;) {
145 // Compare the current pieces.
146 if (LR.size() == RR.size()) {
147 // If they're the same length, it's pretty easy.
148 if (LR != RR)
149 return false;
150 // Both pieces are done, advance.
151 ++LI;
152 ++RI;
153 // If either string is done, they're both done, since they're the same
154 // length.
155 if (LI == LN) {
156 assert(RI == RN && "Strings not the same length after all?");
157 return true;
158 }
159 LR = L[LI];
160 RR = R[RI].Data;
161 } else if (LR.size() < RR.size()) {
162 // Right piece is longer.
163 if (!RR.startswith(LR))
164 return false;
165 ++LI;
166 assert(LI != LN && "Strings not the same length after all?");
167 RR = RR.substr(LR.size());
168 LR = L[LI];
169 } else {
170 // Left piece is longer.
171 if (!LR.startswith(RR))
172 return false;
173 ++RI;
174 assert(RI != RN && "Strings not the same length after all?");
175 LR = LR.substr(RR.size());
176 RR = R[RI].Data;
177 }
178 }
179}
180
Chris Lattner5f9e2722011-07-23 10:55:15 +0000181static std::pair<FileID, StringRef::size_type>
182FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
183 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000184 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
185 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000187 Res.first = Buffers[I].BufferID;
188 break;
189 }
190 }
191 return Res;
192}
193
194bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000195 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000196 std::string &SuggestedPredefines,
197 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000198 // We are in the context of an implicit include, so the predefines buffer will
199 // have a #include entry for the PCH file itself (as normalized by the
200 // preprocessor initialization). Find it and skip over it in the checking
201 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000202 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000203 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000204 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
205 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000206 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 std::pair<StringRef,StringRef> Split =
208 StringRef(PP.getPredefines()).split(PCHInclude.str());
209 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000210 if (Left == PP.getPredefines()) {
211 Error("Missing PCH include entry!");
212 return true;
213 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000214
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000215 // If the concatenation of all the PCH buffers is equal to the adjusted
216 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000218 CommandLine.push_back(Left);
219 CommandLine.push_back(Right);
220 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000221 return false;
222
223 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000225 // The predefines buffers are different. Determine what the differences are,
226 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000227 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000228 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
229 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000230
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000232 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000233
234 // Pick out implicit #includes after the PCH and don't consider them for
235 // validation; we will insert them into SuggestedPredefines so that the
236 // preprocessor includes them.
237 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000239 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
240 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
241 if (AfterPCHLines[i].startswith("#include ")) {
242 IncludesAfterPCH += AfterPCHLines[i];
243 IncludesAfterPCH += '\n';
244 } else {
245 CmdLineLines.push_back(AfterPCHLines[i]);
246 }
247 }
248
249 // Make sure we add the includes last into SuggestedPredefines before we
250 // exit this function.
251 struct AddIncludesRAII {
252 std::string &SuggestedPredefines;
253 std::string &IncludesAfterPCH;
254
255 AddIncludesRAII(std::string &SuggestedPredefines,
256 std::string &IncludesAfterPCH)
257 : SuggestedPredefines(SuggestedPredefines),
258 IncludesAfterPCH(IncludesAfterPCH) { }
259 ~AddIncludesRAII() {
260 SuggestedPredefines += IncludesAfterPCH;
261 }
262 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000263
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000264 // Sort both sets of predefined buffer lines, since we allow some extra
265 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000266 std::sort(CmdLineLines.begin(), CmdLineLines.end());
267 std::sort(PCHLines.begin(), PCHLines.end());
268
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000269 // Determine which predefines that were used to build the PCH file are missing
270 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000272 std::set_difference(PCHLines.begin(), PCHLines.end(),
273 CmdLineLines.begin(), CmdLineLines.end(),
274 std::back_inserter(MissingPredefines));
275
276 bool MissingDefines = false;
277 bool ConflictingDefines = false;
278 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000280 if (Missing.startswith("#include ")) {
281 // An -include was specified when generating the PCH; it is included in
282 // the PCH, just ignore it.
283 continue;
284 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000285 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000286 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
287 return true;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000290 // This is a macro definition. Determine the name of the macro we're
291 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000292 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000293 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000294 = Missing.find_first_of("( \n\r", StartOfMacroName);
295 assert(EndOfMacroName != std::string::npos &&
296 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000299 // Determine whether this macro was given a different definition on the
300 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000301 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000302 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000303 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000304 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
305 MacroDefStart);
306 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000307 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000308 // Different macro; we're done.
309 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000310 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
313 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000315 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000316 (*ConflictPos)[MacroDefLen] != '(')
317 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 // We found a conflicting macro definition.
320 break;
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000323 if (ConflictPos != CmdLineLines.end()) {
324 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
325 << MacroName;
326
327 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000328 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000329 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000331 SourceLocation PCHMissingLoc =
332 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000333 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000334 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000335
336 ConflictingDefines = true;
337 continue;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000340 // If the macro doesn't conflict, then we'll just pick up the macro
341 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342 if (ConflictingDefines)
343 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 if (!MissingDefines) {
346 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
347 MissingDefines = true;
348 }
349
350 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000352 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000353 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000354 SourceLocation PCHMissingLoc =
355 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000356 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000360 if (ConflictingDefines)
361 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 // Determine what predefines were introduced based on command-line
364 // parameters that were not present when building the PCH
365 // file. Extra #defines are okay, so long as the identifiers being
366 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000367 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
369 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000370 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000372 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000373 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000374 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
375 return true;
376 }
377
378 // This is an extra macro definition. Determine the name of the
379 // macro we're defining.
380 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000381 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382 = Extra.find_first_of("( \n\r", StartOfMacroName);
383 assert(EndOfMacroName != std::string::npos &&
384 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000385 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000386
387 // Check whether this name was used somewhere in the PCH file. If
388 // so, defining it as a macro could change behavior, so we reject
389 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000390 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000391 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 return true;
393 }
394
395 // Add this definition to the suggested predefines buffer.
396 SuggestedPredefines += Extra;
397 SuggestedPredefines += '\n';
398 }
399
400 // If we get here, it's because the predefines buffer had compatible
401 // contents. Accept the PCH file.
402 return false;
403}
404
Douglas Gregor12fab312010-03-16 16:35:32 +0000405void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
406 unsigned ID) {
407 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
408 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409}
410
411void PCHValidator::ReadCounter(unsigned Value) {
412 PP.setCounterValue(Value);
413}
414
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000415//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000416// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000417//===----------------------------------------------------------------------===//
418
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000419void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000420ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000421 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000422}
423
Chris Lattner4c6f9522009-04-27 05:14:47 +0000424
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000425
Douglas Gregor98339b92011-08-25 20:47:51 +0000426unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
427 return serialization::ComputeHash(Sel);
428}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000429
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregor98339b92011-08-25 20:47:51 +0000431std::pair<unsigned, unsigned>
432ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
433 using namespace clang::io;
434 unsigned KeyLen = ReadUnalignedLE16(d);
435 unsigned DataLen = ReadUnalignedLE16(d);
436 return std::make_pair(KeyLen, DataLen);
437}
438
439ASTSelectorLookupTrait::internal_key_type
440ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
441 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000442 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000443 unsigned N = ReadUnalignedLE16(d);
444 IdentifierInfo *FirstII
445 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
446 if (N == 0)
447 return SelTable.getNullarySelector(FirstII);
448 else if (N == 1)
449 return SelTable.getUnarySelector(FirstII);
450
451 SmallVector<IdentifierInfo *, 16> Args;
452 Args.push_back(FirstII);
453 for (unsigned I = 1; I != N; ++I)
454 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
455
456 return SelTable.getSelector(N, Args.data());
457}
458
459ASTSelectorLookupTrait::data_type
460ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
461 unsigned DataLen) {
462 using namespace clang::io;
463
464 data_type Result;
465
466 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
467 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
468 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
469
470 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000471 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
472 if (ObjCMethodDecl *Method
473 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
474 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Douglas Gregor98339b92011-08-25 20:47:51 +0000477 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000478 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
479 if (ObjCMethodDecl *Method
480 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
481 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregor98339b92011-08-25 20:47:51 +0000484 return Result;
485}
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregor98339b92011-08-25 20:47:51 +0000487unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
488 return llvm::HashString(StringRef(a.first, a.second));
489}
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregor98339b92011-08-25 20:47:51 +0000491std::pair<unsigned, unsigned>
492ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
493 using namespace clang::io;
494 unsigned DataLen = ReadUnalignedLE16(d);
495 unsigned KeyLen = ReadUnalignedLE16(d);
496 return std::make_pair(KeyLen, DataLen);
497}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498
Douglas Gregor98339b92011-08-25 20:47:51 +0000499std::pair<const char*, unsigned>
500ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
501 assert(n >= 2 && d[n-1] == '\0');
502 return std::make_pair((const char*) d, n-1);
503}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000504
Douglas Gregor98339b92011-08-25 20:47:51 +0000505IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
506 const unsigned char* d,
507 unsigned DataLen) {
508 using namespace clang::io;
509 unsigned RawID = ReadUnalignedLE32(d);
510 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor98339b92011-08-25 20:47:51 +0000512 // Wipe out the "is interesting" bit.
513 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000514
Douglas Gregor98339b92011-08-25 20:47:51 +0000515 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
516 if (!IsInteresting) {
517 // For uninteresting identifiers, just build the IdentifierInfo
518 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000519 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000520 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000521 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000522 KnownII = II;
523 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000524 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000525 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000526 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000527 return II;
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000530 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000531 unsigned Bits = ReadUnalignedLE16(d);
532 bool CPlusPlusOperatorKeyword = Bits & 0x01;
533 Bits >>= 1;
534 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
535 Bits >>= 1;
536 bool Poisoned = Bits & 0x01;
537 Bits >>= 1;
538 bool ExtensionToken = Bits & 0x01;
539 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000540 bool hadMacroDefinition = Bits & 0x01;
541 Bits >>= 1;
Douglas Gregor98339b92011-08-25 20:47:51 +0000542 bool hasMacroDefinition = Bits & 0x01;
543 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000544
Douglas Gregor98339b92011-08-25 20:47:51 +0000545 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000546 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000547
Douglas Gregor98339b92011-08-25 20:47:51 +0000548 // Build the IdentifierInfo itself and link the identifier ID with
549 // the new IdentifierInfo.
550 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000551 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000552 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000553 KnownII = II;
554 }
Douglas Gregor057df202012-01-18 20:56:22 +0000555 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000556 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000557
Douglas Gregor98339b92011-08-25 20:47:51 +0000558 // Set or check the various bits in the IdentifierInfo structure.
559 // Token IDs are read-only.
560 if (HasRevertedTokenIDToIdentifier)
561 II->RevertTokenIDToIdentifier();
562 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
563 assert(II->isExtensionToken() == ExtensionToken &&
564 "Incorrect extension token flag");
565 (void)ExtensionToken;
566 if (Poisoned)
567 II->setIsPoisoned(true);
568 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
569 "Incorrect C++ operator keyword flag");
570 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000571
Douglas Gregor98339b92011-08-25 20:47:51 +0000572 // If this identifier is a macro, deserialize the macro
573 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000574 if (hadMacroDefinition) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000575 // FIXME: Check for conflicts?
Douglas Gregora8235d62012-10-09 23:05:51 +0000576 uint32_t LocalID = ReadUnalignedLE32(d);
Douglas Gregor13292642011-12-02 15:45:10 +0000577 unsigned LocalSubmoduleID = ReadUnalignedLE32(d);
578
579 // Determine whether this macro definition should be visible now, or
580 // whether it is in a hidden submodule.
581 bool Visible = true;
582 if (SubmoduleID GlobalSubmoduleID
583 = Reader.getGlobalSubmoduleID(F, LocalSubmoduleID)) {
584 if (Module *Owner = Reader.getSubmodule(GlobalSubmoduleID)) {
585 if (Owner->NameVisibility == Module::Hidden) {
586 // The owning module is not visible, and this macro definition should
587 // not be, either.
588 Visible = false;
589
590 // Note that this macro definition was hidden because its owning
591 // module is not yet visible.
592 Reader.HiddenNamesMap[Owner].push_back(II);
593 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000594 }
Douglas Gregor13292642011-12-02 15:45:10 +0000595 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000596
Douglas Gregora8235d62012-10-09 23:05:51 +0000597 Reader.setIdentifierIsMacro(II, Reader.getGlobalMacroID(F, LocalID),
598 Visible && hasMacroDefinition);
Douglas Gregor13292642011-12-02 15:45:10 +0000599 DataLen -= 8;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000600 }
601
Douglas Gregoreee242f2011-10-27 09:33:13 +0000602 Reader.SetIdentifierInfo(ID, II);
603
Douglas Gregor98339b92011-08-25 20:47:51 +0000604 // Read all of the declarations visible at global scope with this
605 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000606 if (DataLen > 0) {
607 SmallVector<uint32_t, 4> DeclIDs;
608 for (; DataLen > 0; DataLen -= 4)
609 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
610 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000611 }
612
Douglas Gregor98339b92011-08-25 20:47:51 +0000613 return II;
614}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000615
Douglas Gregor98339b92011-08-25 20:47:51 +0000616unsigned
617ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
618 llvm::FoldingSetNodeID ID;
619 ID.AddInteger(Key.Kind);
620
621 switch (Key.Kind) {
622 case DeclarationName::Identifier:
623 case DeclarationName::CXXLiteralOperatorName:
624 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
625 break;
626 case DeclarationName::ObjCZeroArgSelector:
627 case DeclarationName::ObjCOneArgSelector:
628 case DeclarationName::ObjCMultiArgSelector:
629 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
630 break;
631 case DeclarationName::CXXOperatorName:
632 ID.AddInteger((OverloadedOperatorKind)Key.Data);
633 break;
634 case DeclarationName::CXXConstructorName:
635 case DeclarationName::CXXDestructorName:
636 case DeclarationName::CXXConversionFunctionName:
637 case DeclarationName::CXXUsingDirective:
638 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000639 }
640
Douglas Gregor98339b92011-08-25 20:47:51 +0000641 return ID.ComputeHash();
642}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000643
Douglas Gregor98339b92011-08-25 20:47:51 +0000644ASTDeclContextNameLookupTrait::internal_key_type
645ASTDeclContextNameLookupTrait::GetInternalKey(
646 const external_key_type& Name) const {
647 DeclNameKey Key;
648 Key.Kind = Name.getNameKind();
649 switch (Name.getNameKind()) {
650 case DeclarationName::Identifier:
651 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
652 break;
653 case DeclarationName::ObjCZeroArgSelector:
654 case DeclarationName::ObjCOneArgSelector:
655 case DeclarationName::ObjCMultiArgSelector:
656 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
657 break;
658 case DeclarationName::CXXOperatorName:
659 Key.Data = Name.getCXXOverloadedOperator();
660 break;
661 case DeclarationName::CXXLiteralOperatorName:
662 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
663 break;
664 case DeclarationName::CXXConstructorName:
665 case DeclarationName::CXXDestructorName:
666 case DeclarationName::CXXConversionFunctionName:
667 case DeclarationName::CXXUsingDirective:
668 Key.Data = 0;
669 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000670 }
671
Douglas Gregor98339b92011-08-25 20:47:51 +0000672 return Key;
673}
674
Douglas Gregor98339b92011-08-25 20:47:51 +0000675std::pair<unsigned, unsigned>
676ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
677 using namespace clang::io;
678 unsigned KeyLen = ReadUnalignedLE16(d);
679 unsigned DataLen = ReadUnalignedLE16(d);
680 return std::make_pair(KeyLen, DataLen);
681}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000682
Douglas Gregor98339b92011-08-25 20:47:51 +0000683ASTDeclContextNameLookupTrait::internal_key_type
684ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
685 using namespace clang::io;
686
687 DeclNameKey Key;
688 Key.Kind = (DeclarationName::NameKind)*d++;
689 switch (Key.Kind) {
690 case DeclarationName::Identifier:
691 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
692 break;
693 case DeclarationName::ObjCZeroArgSelector:
694 case DeclarationName::ObjCOneArgSelector:
695 case DeclarationName::ObjCMultiArgSelector:
696 Key.Data =
697 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
698 .getAsOpaquePtr();
699 break;
700 case DeclarationName::CXXOperatorName:
701 Key.Data = *d++; // OverloadedOperatorKind
702 break;
703 case DeclarationName::CXXLiteralOperatorName:
704 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
705 break;
706 case DeclarationName::CXXConstructorName:
707 case DeclarationName::CXXDestructorName:
708 case DeclarationName::CXXConversionFunctionName:
709 case DeclarationName::CXXUsingDirective:
710 Key.Data = 0;
711 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000712 }
713
Douglas Gregor98339b92011-08-25 20:47:51 +0000714 return Key;
715}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000716
Douglas Gregor98339b92011-08-25 20:47:51 +0000717ASTDeclContextNameLookupTrait::data_type
718ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
719 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000720 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000721 using namespace clang::io;
722 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000723 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000724 return std::make_pair(Start, Start + NumDecls);
725}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000726
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000727bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000728 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000729 const std::pair<uint64_t, uint64_t> &Offsets,
730 DeclContextInfo &Info) {
731 SavedStreamPosition SavedPosition(Cursor);
732 // First the lexical decls.
733 if (Offsets.first != 0) {
734 Cursor.JumpToBit(Offsets.first);
735
736 RecordData Record;
737 const char *Blob;
738 unsigned BlobLen;
739 unsigned Code = Cursor.ReadCode();
740 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
741 if (RecCode != DECL_CONTEXT_LEXICAL) {
742 Error("Expected lexical block");
743 return true;
744 }
745
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000746 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
747 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000748 }
749
750 // Now the lookup table.
751 if (Offsets.second != 0) {
752 Cursor.JumpToBit(Offsets.second);
753
754 RecordData Record;
755 const char *Blob;
756 unsigned BlobLen;
757 unsigned Code = Cursor.ReadCode();
758 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
759 if (RecCode != DECL_CONTEXT_VISIBLE) {
760 Error("Expected visible lookup table block");
761 return true;
762 }
763 Info.NameLookupTableData
764 = ASTDeclContextNameLookupTable::Create(
765 (const unsigned char *)Blob + Record[0],
766 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000767 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000768 }
769
770 return false;
771}
772
Chris Lattner5f9e2722011-07-23 10:55:15 +0000773void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000774 Error(diag::err_fe_pch_malformed, Msg);
775}
776
777void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000778 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000779 if (Diags.isDiagnosticInFlight())
780 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
781 else
782 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000783}
784
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000785/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000786bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000787 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000788 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000789 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000790 SuggestedPredefines,
791 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000792 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000793}
794
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000795//===----------------------------------------------------------------------===//
796// Source Manager Deserialization
797//===----------------------------------------------------------------------===//
798
Douglas Gregorbd945002009-04-13 16:31:14 +0000799/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000800/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000801bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000802 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000803 unsigned Idx = 0;
804 LineTableInfo &LineTable = SourceMgr.getLineTable();
805
806 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000807 std::map<int, int> FileIDs;
808 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000809 // Extract the file name
810 unsigned FilenameLen = Record[Idx++];
811 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
812 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000813 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000814 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000815 }
816
817 // Parse the line entries
818 std::vector<LineEntry> Entries;
819 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000820 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000821 assert(FID >= 0 && "Serialized line entries for non-local file.");
822 // Remap FileID from 1-based old view.
823 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000824
825 // Extract the line entries
826 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000827 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000828 Entries.clear();
829 Entries.reserve(NumEntries);
830 for (unsigned I = 0; I != NumEntries; ++I) {
831 unsigned FileOffset = Record[Idx++];
832 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000833 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000834 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000835 = (SrcMgr::CharacteristicKind)Record[Idx++];
836 unsigned IncludeOffset = Record[Idx++];
837 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
838 FileKind, IncludeOffset));
839 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000840 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000841 }
842
843 return false;
844}
845
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000846namespace {
847
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000848class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000849public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850 const ino_t ino;
851 const dev_t dev;
852 const mode_t mode;
853 const time_t mtime;
854 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000856 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000857 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000858};
859
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000860class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000861 public:
862 typedef const char *external_key_type;
863 typedef const char *internal_key_type;
864
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000865 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000866
867 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000868 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000869 }
870
871 static internal_key_type GetInternalKey(const char *path) { return path; }
872
873 static bool EqualKey(internal_key_type a, internal_key_type b) {
874 return strcmp(a, b) == 0;
875 }
876
877 static std::pair<unsigned, unsigned>
878 ReadKeyDataLength(const unsigned char*& d) {
879 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
880 unsigned DataLen = (unsigned) *d++;
881 return std::make_pair(KeyLen + 1, DataLen);
882 }
883
884 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
885 return (const char *)d;
886 }
887
888 static data_type ReadData(const internal_key_type, const unsigned char *d,
889 unsigned /*DataLen*/) {
890 using namespace clang::io;
891
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000892 ino_t ino = (ino_t) ReadUnalignedLE32(d);
893 dev_t dev = (dev_t) ReadUnalignedLE32(d);
894 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000895 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000896 off_t size = (off_t) ReadUnalignedLE64(d);
897 return data_type(ino, dev, mode, mtime, size);
898 }
899};
900
901/// \brief stat() cache for precompiled headers.
902///
903/// This cache is very similar to the stat cache used by pretokenized
904/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000905class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000906 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000907 CacheTy *Cache;
908
909 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000910public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000911 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
912 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000913 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
914 Cache = CacheTy::Create(Buckets, Base);
915 }
916
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000917 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Chris Lattner898a0612010-11-23 21:17:56 +0000919 LookupResult getStat(const char *Path, struct stat &StatBuf,
920 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000921 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000922 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000923
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000924 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000925 if (I == Cache->end()) {
926 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000927 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000931 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Chris Lattner10e286a2010-11-23 19:19:34 +0000933 StatBuf.st_ino = Data.ino;
934 StatBuf.st_dev = Data.dev;
935 StatBuf.st_mtime = Data.mtime;
936 StatBuf.st_mode = Data.mode;
937 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000938 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000939 }
940};
941} // end anonymous namespace
942
943
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000944/// \brief Read a source manager block
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000945ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000946 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000947
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000948 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000949
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000950 // Set the source-location entry cursor to the current position in
951 // the stream. This cursor will be used to read the contents of the
952 // source manager block initially, and then lazily read
953 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000954 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000955
956 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000957 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000958 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000959 return Failure;
960 }
961
962 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000963 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000964 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000965 return Failure;
966 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000967
Douglas Gregor14f79002009-04-10 03:52:48 +0000968 RecordData Record;
969 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000970 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000971 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000972 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000973 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000974 return Failure;
975 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000976 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregor14f79002009-04-10 03:52:48 +0000979 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
980 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000981 SLocEntryCursor.ReadSubBlockID();
982 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000983 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000984 return Failure;
985 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000986 continue;
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregor14f79002009-04-10 03:52:48 +0000989 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000990 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000991 continue;
992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregor14f79002009-04-10 03:52:48 +0000994 // Read a record.
995 const char *BlobStart;
996 unsigned BlobLen;
997 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000998 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000999 default: // Default behavior: ignore.
1000 break;
1001
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001002 case SM_SLOC_FILE_ENTRY:
1003 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001004 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001005 // Once we hit one of the source location entries, we're done.
1006 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001007 }
1008 }
1009}
1010
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001011/// \brief If a header file is not found at the path that we expect it to be
1012/// and the PCH file was moved from its original location, try to resolve the
1013/// file by assuming that header+PCH were moved together and the header is in
1014/// the same place relative to the PCH.
1015static std::string
1016resolveFileRelativeToOriginalDir(const std::string &Filename,
1017 const std::string &OriginalDir,
1018 const std::string &CurrDir) {
1019 assert(OriginalDir != CurrDir &&
1020 "No point trying to resolve the file if the PCH dir didn't change");
1021 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001022 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001023 fs::make_absolute(filePath);
1024 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001025 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001026
1027 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1028 fileDirE = path::end(path::parent_path(filePath));
1029 path::const_iterator origDirI = path::begin(OriginalDir),
1030 origDirE = path::end(OriginalDir);
1031 // Skip the common path components from filePath and OriginalDir.
1032 while (fileDirI != fileDirE && origDirI != origDirE &&
1033 *fileDirI == *origDirI) {
1034 ++fileDirI;
1035 ++origDirI;
1036 }
1037 for (; origDirI != origDirE; ++origDirI)
1038 path::append(currPCHPath, "..");
1039 path::append(currPCHPath, fileDirI, fileDirE);
1040 path::append(currPCHPath, path::filename(Filename));
1041 return currPCHPath.str();
1042}
1043
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001044/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001045ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001046 if (ID == 0)
1047 return Success;
1048
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001049 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001050 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001051 return Failure;
1052 }
1053
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001054 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001055 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001056 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001057 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001058
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001059 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 unsigned Code = SLocEntryCursor.ReadCode();
1061 if (Code == llvm::bitc::END_BLOCK ||
1062 Code == llvm::bitc::ENTER_SUBBLOCK ||
1063 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001064 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001065 return Failure;
1066 }
1067
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001068 RecordData Record;
1069 const char *BlobStart;
1070 unsigned BlobLen;
1071 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1072 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001073 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001074 return Failure;
1075
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001076 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00001077 if (Record.size() < 7) {
1078 Error("source location entry is incorrect");
1079 return Failure;
1080 }
1081
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001082 // We will detect whether a file changed and return 'Failure' for it, but
1083 // we will also try to fail gracefully by setting up the SLocEntry.
1084 ASTReader::ASTReadResult Result = Success;
1085
Douglas Gregora081da52011-11-16 20:05:18 +00001086 bool OverriddenBuffer = Record[6];
1087
1088 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1089 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001090 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora4581a12011-11-17 19:08:51 +00001091 const FileEntry *File =
1092 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1093 (time_t)Record[5])
1094 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001095 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1096 OriginalDir != CurrentDir) {
1097 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1098 OriginalDir,
1099 CurrentDir);
1100 if (!resolved.empty())
1101 File = FileMgr.getFile(resolved);
1102 }
Axel Naumann04331162011-01-27 10:55:51 +00001103 if (File == 0)
1104 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1105 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001106 if (File == 0) {
1107 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001108 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001109 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001110 Error(ErrorStr.c_str());
1111 return Failure;
1112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +00001114 if (!DisableValidation && !OverriddenBuffer &&
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001115 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001116#if !defined(LLVM_ON_WIN32)
1117 // In our regression testing, the Windows file system seems to
1118 // have inconsistent modification times that sometimes
1119 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001120 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001121#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001122 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001123 Error(diag::err_fe_pch_file_modified, Filename);
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001124 Result = Failure;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001125 }
1126
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001127 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001128 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001129 // This is the module's main file.
1130 IncludeLoc = getImportLocation(F);
1131 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001132 SrcMgr::CharacteristicKind
1133 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1134 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001135 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001136 SrcMgr::FileInfo &FileInfo =
1137 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001138 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001140 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001141
Douglas Gregora081da52011-11-16 20:05:18 +00001142 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1143 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001144 if (NumFileDecls) {
1145 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001146 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1147 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001148 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001149
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001150 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001151 = SourceMgr.getOrCreateContentCache(File,
1152 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001153 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1154 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001155 unsigned Code = SLocEntryCursor.ReadCode();
1156 Record.clear();
1157 unsigned RecCode
1158 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1159
1160 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1161 Error("AST record has invalid code");
1162 return Failure;
1163 }
1164
1165 llvm::MemoryBuffer *Buffer
1166 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1167 Filename);
1168 SourceMgr.overrideFileContents(File, Buffer);
1169 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001170
1171 if (Result == Failure)
1172 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001173 break;
1174 }
1175
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001176 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001177 const char *Name = BlobStart;
1178 unsigned Offset = Record[0];
1179 unsigned Code = SLocEntryCursor.ReadCode();
1180 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001181 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001182 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001183
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001184 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001185 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001186 return Failure;
1187 }
1188
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001189 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001190 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1191 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001192 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1193 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Douglas Gregor6236a292011-12-02 21:56:05 +00001195 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001196 PCHPredefinesBlock Block = {
1197 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001198 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001199 };
1200 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001201 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001202
1203 break;
1204 }
1205
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001206 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001207 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001208 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001209 ReadSourceLocation(*F, Record[2]),
1210 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001211 Record[4],
1212 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001213 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001215 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001216 }
1217
1218 return Success;
1219}
1220
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001221/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001222SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001223 if (F->ImportLoc.isValid())
1224 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001225
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001226 // Otherwise we have a PCH. It's considered to be "imported" at the first
1227 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001228 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001229 // Main file is the importer. We assume that it is the first entry in the
1230 // entry table. We can't ask the manager, because at the time of PCH loading
1231 // the main file entry doesn't exist yet.
1232 // The very first entry is the invalid instantiation loc, which takes up
1233 // offsets 0 and 1.
1234 return SourceLocation::getFromRawEncoding(2U);
1235 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001236 //return F->Loaders[0]->FirstLoc;
1237 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001238}
1239
Chris Lattner6367f6d2009-04-27 01:05:14 +00001240/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1241/// specified cursor. Read the abbreviations that are at the top of the block
1242/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001243bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001244 unsigned BlockID) {
1245 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001246 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001247 return Failure;
1248 }
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Chris Lattner6367f6d2009-04-27 01:05:14 +00001250 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001251 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001252 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001253
Chris Lattner6367f6d2009-04-27 01:05:14 +00001254 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001255 if (Code != llvm::bitc::DEFINE_ABBREV) {
1256 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001257 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001258 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001259 Cursor.ReadAbbrevRecord();
1260 }
1261}
1262
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001263void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001264 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Douglas Gregor37e26842009-04-21 23:56:24 +00001266 // Keep track of where we are in the stream, then jump back there
1267 // after reading this macro.
1268 SavedStreamPosition SavedPosition(Stream);
1269
1270 Stream.JumpToBit(Offset);
1271 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001272 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001273 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Douglas Gregor37e26842009-04-21 23:56:24 +00001275 while (true) {
1276 unsigned Code = Stream.ReadCode();
1277 switch (Code) {
1278 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001279 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001280
1281 case llvm::bitc::ENTER_SUBBLOCK:
1282 // No known subblocks, always skip them.
1283 Stream.ReadSubBlockID();
1284 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001285 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001286 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001287 }
1288 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor37e26842009-04-21 23:56:24 +00001290 case llvm::bitc::DEFINE_ABBREV:
1291 Stream.ReadAbbrevRecord();
1292 continue;
1293 default: break;
1294 }
1295
1296 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001297 const char *BlobStart = 0;
1298 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001299 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001300 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001301 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001302 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001303 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001304 case PP_MACRO_OBJECT_LIKE:
1305 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001306 // If we already have a macro, that means that we've hit the end
1307 // of the definition of the macro we were looking for. We're
1308 // done.
1309 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001310 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001311
Douglas Gregor95eab172011-07-28 20:55:49 +00001312 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001313 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001314 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001315 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001316 }
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Douglas Gregora8235d62012-10-09 23:05:51 +00001318 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1319
1320 // If this macro has already been loaded, don't do so again.
1321 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1322 return;
1323
1324 unsigned NextIndex = 2;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001325 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001326 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001327
Douglas Gregora8235d62012-10-09 23:05:51 +00001328 // Record this macro.
1329 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1330
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001331 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1332 if (UndefLoc.isValid())
1333 MI->setUndefLoc(UndefLoc);
1334
1335 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001336 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001338 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001339 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001340
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001341 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001342 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001343 bool isC99VarArgs = Record[NextIndex++];
1344 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001345 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001346 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001347 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001348 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001349
1350 // Install function-like macro info.
1351 MI->setIsFunctionLike();
1352 if (isC99VarArgs) MI->setIsC99Varargs();
1353 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001354 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001355 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001356 }
1357
Douglas Gregora8235d62012-10-09 23:05:51 +00001358 if (DeserializationListener)
1359 DeserializationListener->MacroRead(GlobalID, MI);
1360
1361 // If an update record marked this as undefined, do so now.
1362 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1363 if (Update != MacroUpdates.end()) {
1364 if (MI->getUndefLoc().isInvalid()) {
1365 MI->setUndefLoc(Update->second.UndefLoc);
1366 if (PPMutationListener *Listener = PP.getPPMutationListener())
1367 Listener->UndefinedMacro(MI);
1368 }
1369 MacroUpdates.erase(Update);
1370 }
1371
Douglas Gregor37e26842009-04-21 23:56:24 +00001372 // Finally, install the macro.
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001373 PP.setMacroInfo(II, MI, /*LoadedFromAST=*/true);
Douglas Gregor37e26842009-04-21 23:56:24 +00001374
1375 // Remember that we saw this macro last so that we add the tokens that
1376 // form its body to it.
1377 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001378
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001379 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1380 Record[NextIndex]) {
1381 // We have a macro definition. Register the association
1382 PreprocessedEntityID
1383 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1384 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1385 PPRec.RegisterMacroDefinition(Macro,
1386 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001387 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001388
Douglas Gregor37e26842009-04-21 23:56:24 +00001389 ++NumMacrosRead;
1390 break;
1391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001393 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001394 // If we see a TOKEN before a PP_MACRO_*, then the file is
1395 // erroneous, just pretend we didn't see this.
1396 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregor37e26842009-04-21 23:56:24 +00001398 Token Tok;
1399 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001400 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001401 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001402 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001403 Tok.setIdentifierInfo(II);
1404 Tok.setKind((tok::TokenKind)Record[3]);
1405 Tok.setFlag((Token::TokenFlags)Record[4]);
1406 Macro->AddTokenToBody(Tok);
1407 break;
1408 }
David Blaikie7530c032012-01-17 06:56:22 +00001409 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001410 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001411}
1412
Douglas Gregor86c67d82011-07-28 22:39:26 +00001413PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001414ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001415 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001416 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1417 assert(I != M.PreprocessedEntityRemap.end()
1418 && "Invalid index into preprocessed entity index remap");
1419
1420 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001421}
1422
Douglas Gregor98339b92011-08-25 20:47:51 +00001423unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1424 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001425}
Douglas Gregor98339b92011-08-25 20:47:51 +00001426
1427HeaderFileInfoTrait::internal_key_type
1428HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1429
1430bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1431 if (strcmp(a, b) == 0)
1432 return true;
1433
1434 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1435 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001436
1437 // Determine whether the actual files are equivalent.
1438 bool Result = false;
1439 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001440 return false;
1441
Douglas Gregor99a922b2011-12-09 16:22:07 +00001442 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001443}
1444
1445std::pair<unsigned, unsigned>
1446HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1447 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1448 unsigned DataLen = (unsigned) *d++;
1449 return std::make_pair(KeyLen + 1, DataLen);
1450}
1451
1452HeaderFileInfoTrait::data_type
1453HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1454 unsigned DataLen) {
1455 const unsigned char *End = d + DataLen;
1456 using namespace clang::io;
1457 HeaderFileInfo HFI;
1458 unsigned Flags = *d++;
1459 HFI.isImport = (Flags >> 5) & 0x01;
1460 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1461 HFI.DirInfo = (Flags >> 2) & 0x03;
1462 HFI.Resolved = (Flags >> 1) & 0x01;
1463 HFI.IndexHeaderMapHeader = Flags & 0x01;
1464 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001465 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1466 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001467 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1468 // The framework offset is 1 greater than the actual offset,
1469 // since 0 is used as an indicator for "no framework name".
1470 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1471 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1472 }
1473
1474 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1475 (void)End;
1476
1477 // This HeaderFileInfo was externally loaded.
1478 HFI.External = true;
1479 return HFI;
1480}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001481
Douglas Gregora8235d62012-10-09 23:05:51 +00001482void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, MacroID ID,
1483 bool Visible) {
Douglas Gregor13292642011-12-02 15:45:10 +00001484 if (Visible) {
1485 // Note that this identifier has a macro definition.
1486 II->setHasMacroDefinition(true);
Douglas Gregor3644d972012-10-09 16:01:50 +00001487 } else {
1488 II->setHadMacroDefinition(true);
Douglas Gregor13292642011-12-02 15:45:10 +00001489 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001490
1491 // FIXME: This could end up overwriting a previously recording macro
1492 // definition here, which is not cool at all.
1493 UnreadMacroIDs[II] = ID;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001494}
1495
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001496void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001497 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1498 E = ModuleMgr.rend(); I != E; ++I) {
1499 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001500
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001501 // If there was no preprocessor block, skip this file.
1502 if (!MacroCursor.getBitStreamReader())
1503 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001504
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001505 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001506 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001507
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001508 RecordData Record;
1509 while (true) {
1510 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001511 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001512 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001513
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001514 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1515 // No known subblocks, always skip them.
1516 Cursor.ReadSubBlockID();
1517 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001518 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001519 return;
1520 }
1521 continue;
1522 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001523
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001524 if (Code == llvm::bitc::DEFINE_ABBREV) {
1525 Cursor.ReadAbbrevRecord();
1526 continue;
1527 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001528
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001529 // Read a record.
1530 const char *BlobStart;
1531 unsigned BlobLen;
1532 Record.clear();
1533 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1534 default: // Default behavior: ignore.
1535 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001536
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001537 case PP_MACRO_OBJECT_LIKE:
1538 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001539 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001540 break;
1541
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001542 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001543 // Ignore tokens.
1544 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001545 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001546 }
1547 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001548
1549 // Drain the unread macro-record offsets map.
Douglas Gregora8235d62012-10-09 23:05:51 +00001550 while (!UnreadMacroIDs.empty())
1551 LoadMacroDefinition(UnreadMacroIDs.begin());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001552}
1553
1554void ASTReader::LoadMacroDefinition(
Douglas Gregora8235d62012-10-09 23:05:51 +00001555 llvm::DenseMap<IdentifierInfo *, MacroID>::iterator Pos) {
1556 assert(Pos != UnreadMacroIDs.end() && "Unknown macro definition");
1557 uint64_t GlobalID = Pos->second;
1558 UnreadMacroIDs.erase(Pos);
1559 getMacro(GlobalID);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001560}
1561
1562void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
Douglas Gregora8235d62012-10-09 23:05:51 +00001563 llvm::DenseMap<IdentifierInfo *, MacroID>::iterator Pos
1564 = UnreadMacroIDs.find(II);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001565 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001566}
1567
Douglas Gregoreee242f2011-10-27 09:33:13 +00001568namespace {
1569 /// \brief Visitor class used to look up identifirs in an AST file.
1570 class IdentifierLookupVisitor {
1571 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001572 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001573 IdentifierInfo *Found;
1574 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001575 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1576 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001577
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001578 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001579 IdentifierLookupVisitor *This
1580 = static_cast<IdentifierLookupVisitor *>(UserData);
1581
Douglas Gregor057df202012-01-18 20:56:22 +00001582 // If we've already searched this module file, skip it now.
1583 if (M.Generation <= This->PriorGeneration)
1584 return true;
1585
Douglas Gregoreee242f2011-10-27 09:33:13 +00001586 ASTIdentifierLookupTable *IdTable
1587 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1588 if (!IdTable)
1589 return false;
1590
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001591 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1592 M, This->Found);
1593
Douglas Gregoreee242f2011-10-27 09:33:13 +00001594 std::pair<const char*, unsigned> Key(This->Name.begin(),
1595 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001596 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001597 if (Pos == IdTable->end())
1598 return false;
1599
1600 // Dereferencing the iterator has the effect of building the
1601 // IdentifierInfo node and populating it with the various
1602 // declarations it needs.
1603 This->Found = *Pos;
1604 return true;
1605 }
1606
1607 // \brief Retrieve the identifier info found within the module
1608 // files.
1609 IdentifierInfo *getIdentifierInfo() const { return Found; }
1610 };
1611}
1612
1613void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor057df202012-01-18 20:56:22 +00001614 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001615 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001616 PriorGeneration = IdentifierGeneration[&II];
1617
1618 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1619 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1620 markIdentifierUpToDate(&II);
1621}
1622
1623void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1624 if (!II)
1625 return;
1626
1627 II->setOutOfDate(false);
1628
1629 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001630 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001631 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001632}
1633
Chris Lattner5f9e2722011-07-23 10:55:15 +00001634const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001635 std::string Filename = filenameStrRef;
1636 MaybeAddSystemRootToFilename(Filename);
1637 const FileEntry *File = FileMgr.getFile(Filename);
1638 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1639 OriginalDir != CurrentDir) {
1640 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1641 OriginalDir,
1642 CurrentDir);
1643 if (!resolved.empty())
1644 File = FileMgr.getFile(resolved);
1645 }
1646
1647 return File;
1648}
1649
Douglas Gregore650c8c2009-07-07 00:12:59 +00001650/// \brief If we are loading a relocatable PCH file, and the filename is
1651/// not an absolute path, add the system root to the beginning of the file
1652/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001653void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001654 // If this is not a relocatable PCH file, there's nothing to do.
1655 if (!RelocatablePCH)
1656 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Michael J. Spencer256053b2010-12-17 21:22:22 +00001658 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001659 return;
1660
Douglas Gregor832d6202011-07-22 16:35:34 +00001661 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001662 // If no system root was given, default to '/'
1663 Filename.insert(Filename.begin(), '/');
1664 return;
1665 }
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Douglas Gregor832d6202011-07-22 16:35:34 +00001667 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001668 if (isysroot[Length - 1] != '/')
1669 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor832d6202011-07-22 16:35:34 +00001671 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001672}
1673
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001674ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001675ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001676 llvm::BitstreamCursor &Stream = F.Stream;
1677
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001678 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001679 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001680 return Failure;
1681 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001682
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001683 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001684 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001685 while (!Stream.AtEndOfStream()) {
1686 unsigned Code = Stream.ReadCode();
1687 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001688 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001689 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001690 return Failure;
1691 }
Chris Lattner7356a312009-04-11 21:15:38 +00001692
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001693 DeclContext *DC = Context.getTranslationUnitDecl();
1694 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1695 DC->setMustBuildLookupTable();
1696
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001697 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001698 }
1699
1700 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1701 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001702 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001703 // We lazily load the decls block, but we want to set up the
1704 // DeclsCursor cursor to point into it. Clone our current bitcode
1705 // cursor to it, enter the block and read the abbrevs in that block.
1706 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001707 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001708 if (Stream.SkipBlock() || // Skip with the main cursor.
1709 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001710 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001711 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001712 return Failure;
1713 }
1714 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001716 case DECL_UPDATES_BLOCK_ID:
1717 if (Stream.SkipBlock()) {
1718 Error("malformed block record in AST file");
1719 return Failure;
1720 }
1721 break;
1722
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001723 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001724 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001725 if (!PP.getExternalSource())
1726 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001727
Douglas Gregorecdcb882010-10-20 22:00:55 +00001728 if (Stream.SkipBlock() ||
1729 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001730 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001731 return Failure;
1732 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001733 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001734 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001735
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001736 case PREPROCESSOR_DETAIL_BLOCK_ID:
1737 F.PreprocessorDetailCursor = Stream;
1738 if (Stream.SkipBlock() ||
1739 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1740 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1741 Error("malformed preprocessor detail record in AST file");
1742 return Failure;
1743 }
1744 F.PreprocessorDetailStartOffset
1745 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001746
1747 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001748 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001749 if (!PP.getPreprocessingRecord()->getExternalSource())
1750 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001751 break;
1752
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001753 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001754 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001755 case Success:
1756 break;
1757
1758 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001759 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001760 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001761
1762 case IgnorePCH:
1763 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001764 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001765 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001766
1767 case SUBMODULE_BLOCK_ID:
1768 switch (ReadSubmoduleBlock(F)) {
1769 case Success:
1770 break;
1771
1772 case Failure:
1773 Error("malformed submodule block in AST file");
1774 return Failure;
1775
1776 case IgnorePCH:
1777 return IgnorePCH;
1778 }
1779 break;
1780
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001781 case COMMENTS_BLOCK_ID: {
1782 llvm::BitstreamCursor C = Stream;
1783 if (Stream.SkipBlock() ||
1784 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1785 Error("malformed comments block in AST file");
1786 return Failure;
1787 }
1788 CommentsCursors.push_back(std::make_pair(C, &F));
1789 break;
1790 }
1791
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001792 default:
1793 if (!Stream.SkipBlock())
1794 break;
1795 Error("malformed block record in AST file");
1796 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001797 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001798 continue;
1799 }
1800
1801 if (Code == llvm::bitc::DEFINE_ABBREV) {
1802 Stream.ReadAbbrevRecord();
1803 continue;
1804 }
1805
1806 // Read and process a record.
1807 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001808 const char *BlobStart = 0;
1809 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001810 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001811 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001812 default: // Default behavior: ignore.
1813 break;
1814
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001815 case METADATA: {
1816 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1817 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001818 : diag::warn_pch_version_too_new);
1819 return IgnorePCH;
1820 }
1821
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001822 bool hasErrors = Record[5];
1823 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1824 Diag(diag::err_pch_with_compiler_errors);
1825 return IgnorePCH;
1826 }
1827
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001828 RelocatablePCH = Record[4];
1829 if (Listener) {
1830 std::string TargetTriple(BlobStart, BlobLen);
1831 if (Listener->ReadTargetTriple(TargetTriple))
1832 return IgnorePCH;
1833 }
1834 break;
1835 }
1836
Douglas Gregore95b9192011-08-17 21:07:30 +00001837 case IMPORTS: {
1838 // Load each of the imported PCH files.
1839 unsigned Idx = 0, N = Record.size();
1840 while (Idx < N) {
1841 // Read information about the AST file.
1842 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1843 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001844 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001845 Record.begin() + Idx + Length);
1846 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001847
Douglas Gregore95b9192011-08-17 21:07:30 +00001848 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001849 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001850 case Failure: return Failure;
1851 // If we have to ignore the dependency, we'll have to ignore this too.
1852 case IgnorePCH: return IgnorePCH;
1853 case Success: break;
1854 }
1855 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001856 break;
1857 }
1858
Douglas Gregora119da02011-08-02 16:26:37 +00001859 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001860 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001861 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001862 return Failure;
1863 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001864 F.TypeOffsets = (const uint32_t *)BlobStart;
1865 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001866 unsigned LocalBaseTypeIndex = Record[1];
1867 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001868
Douglas Gregora119da02011-08-02 16:26:37 +00001869 if (F.LocalNumTypes > 0) {
1870 // Introduce the global -> local mapping for types within this module.
1871 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1872
1873 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001874 F.TypeRemap.insertOrReplace(
1875 std::make_pair(LocalBaseTypeIndex,
1876 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001877
1878 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1879 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001880 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001881 }
1882
Douglas Gregor496c7092011-08-03 15:48:04 +00001883 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001884 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001885 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001886 return Failure;
1887 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001888 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001889 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001890 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001891 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001892
Douglas Gregor496c7092011-08-03 15:48:04 +00001893 if (F.LocalNumDecls > 0) {
1894 // Introduce the global -> local mapping for declarations within this
1895 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001896 GlobalDeclMap.insert(
1897 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001898
1899 // Introduce the local -> global mapping for declarations within this
1900 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001901 F.DeclRemap.insertOrReplace(
1902 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001903
Douglas Gregora1be2782011-12-17 23:38:30 +00001904 // Introduce the global -> local mapping for declarations within this
1905 // module.
1906 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1907
Douglas Gregor496c7092011-08-03 15:48:04 +00001908 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1909 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001910 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001911 }
1912
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001913 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001914 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001915 DeclContextInfo &Info = F.DeclContextInfos[TU];
1916 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1917 Info.NumLexicalDecls
1918 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001919 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001920 break;
1921 }
1922
Sebastian Redle1dde812010-08-24 00:50:04 +00001923 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001924 unsigned Idx = 0;
1925 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001926 ASTDeclContextNameLookupTable *Table =
1927 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001928 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001929 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001930 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001931 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1932 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001933 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001934 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001935 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001936 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001937 break;
1938 }
1939
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001940 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001941 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001942 return IgnorePCH;
1943 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001944
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001945 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001946 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001947 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001948 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001949 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001950 (const unsigned char *)F.IdentifierTableData + Record[0],
1951 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001952 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001953
1954 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001955 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001956 break;
1957
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001958 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001959 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001960 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001961 return Failure;
1962 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001963 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1964 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001965 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001966 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001967
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001968 if (F.LocalNumIdentifiers > 0) {
1969 // Introduce the global -> local mapping for identifiers within this
1970 // module.
1971 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1972 &F));
1973
1974 // Introduce the local -> global mapping for identifiers within this
1975 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001976 F.IdentifierRemap.insertOrReplace(
1977 std::make_pair(LocalBaseIdentifierID,
1978 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001979
1980 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1981 + F.LocalNumIdentifiers);
1982 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001983 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001984 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001985
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001986 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001987 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1988 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001989 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001991 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001992 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1993 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001994 break;
1995
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001996 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001997 TotalNumStatements += Record[0];
1998 TotalNumMacros += Record[1];
1999 TotalLexicalDeclContexts += Record[2];
2000 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002001 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002002
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002003 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002004 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2005 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002006 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002007
Sean Huntebcbe1d2011-05-04 23:29:54 +00002008 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002009 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2010 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002011 break;
2012
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002013 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002014 if (Record.size() % 4 != 0) {
2015 Error("invalid weak identifiers record");
2016 return Failure;
2017 }
2018
2019 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2020 // files. This isn't the way to do it :)
2021 WeakUndeclaredIdentifiers.clear();
2022
2023 // Translate the weak, undeclared identifiers into global IDs.
2024 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2025 WeakUndeclaredIdentifiers.push_back(
2026 getGlobalIdentifierID(F, Record[I++]));
2027 WeakUndeclaredIdentifiers.push_back(
2028 getGlobalIdentifierID(F, Record[I++]));
2029 WeakUndeclaredIdentifiers.push_back(
2030 ReadSourceLocation(F, Record, I).getRawEncoding());
2031 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2032 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002033 break;
2034
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002035 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002036 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2037 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002038 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002039
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002040 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002041 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002042 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002043 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002044 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002045
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002046 if (F.LocalNumSelectors > 0) {
2047 // Introduce the global -> local mapping for selectors within this
2048 // module.
2049 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2050
2051 // Introduce the local -> global mapping for selectors within this
2052 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002053 F.SelectorRemap.insertOrReplace(
2054 std::make_pair(LocalBaseSelectorID,
2055 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002056
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002057 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2058 }
2059 break;
2060 }
2061
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002062 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002063 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002064 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002065 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002066 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002067 F.SelectorLookupTableData + Record[0],
2068 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002069 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002070 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002071 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002072
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002073 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002074 if (!Record.empty()) {
2075 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2076 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2077 Record[Idx++]));
2078 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2079 getRawEncoding());
2080 }
2081 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002082 break;
2083
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002084 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002085 if (!Record.empty() && Listener)
2086 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002087 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002088
2089 case FILE_SORTED_DECLS:
2090 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002091 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002092 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002093
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002094 case SOURCE_LOCATION_OFFSETS: {
2095 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002096 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002097 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002098 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002099 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2100 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002101 // Make our entry in the range map. BaseID is negative and growing, so
2102 // we invert it. Because we invert it, though, we need the other end of
2103 // the range.
2104 unsigned RangeStart =
2105 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2106 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2107 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2108
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002109 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2110 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2111 GlobalSLocOffsetMap.insert(
2112 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2113 - SLocSpaceSize,&F));
2114
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002115 // Initialize the remapping table.
2116 // Invalid stays invalid.
2117 F.SLocRemap.insert(std::make_pair(0U, 0));
2118 // This module. Base was 2 when being compiled.
2119 F.SLocRemap.insert(std::make_pair(2U,
2120 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002121
2122 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002123 break;
2124 }
2125
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002126 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002127 // Additional remapping information.
2128 const unsigned char *Data = (const unsigned char*)BlobStart;
2129 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002130
2131 // Continuous range maps we may be updating in our module.
2132 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002133 ContinuousRangeMap<uint32_t, int, 2>::Builder
2134 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002135 ContinuousRangeMap<uint32_t, int, 2>::Builder
2136 MacroRemap(F.MacroRemap);
2137 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002138 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2139 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002140 SubmoduleRemap(F.SubmoduleRemap);
2141 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002142 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002143 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002144 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2145
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002146 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002147 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002148 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002149 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002150 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002151 if (!OM) {
2152 Error("SourceLocation remap refers to unknown module");
2153 return Failure;
2154 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002155
2156 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2157 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002158 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002159 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002160 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002161 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2162 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002163 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002164
2165 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2166 SLocRemap.insert(std::make_pair(SLocOffset,
2167 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002168 IdentifierRemap.insert(
2169 std::make_pair(IdentifierIDOffset,
2170 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002171 MacroRemap.insert(std::make_pair(MacroIDOffset,
2172 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002173 PreprocessedEntityRemap.insert(
2174 std::make_pair(PreprocessedEntityIDOffset,
2175 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002176 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2177 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002178 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2179 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002180 DeclRemap.insert(std::make_pair(DeclIDOffset,
2181 OM->BaseDeclID - DeclIDOffset));
2182
Douglas Gregora119da02011-08-02 16:26:37 +00002183 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002184 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002185
2186 // Global -> local mappings.
2187 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002188 }
2189 break;
2190 }
2191
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002192 case SOURCE_MANAGER_LINE_TABLE:
2193 if (ParseLineTable(F, Record))
2194 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002195 break;
2196
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002197 case FILE_SOURCE_LOCATION_OFFSETS:
2198 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2199 F.LocalNumSLocFileEntries = Record[0];
2200 break;
2201
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002202 case SOURCE_LOCATION_PRELOADS: {
2203 // Need to transform from the local view (1-based IDs) to the global view,
2204 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002205 if (!F.PreloadSLocEntries.empty()) {
2206 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2207 return Failure;
2208 }
2209
2210 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002211 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002212 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002213
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002214 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002215 if (!DisableStatCache) {
2216 ASTStatCache *MyStatCache =
2217 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2218 (const unsigned char *)BlobStart,
2219 NumStatHits, NumStatMisses);
2220 FileMgr.addStatCache(MyStatCache);
2221 F.StatCache = MyStatCache;
2222 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002223 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002224 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002225
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002226 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002227 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2228 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002229 break;
2230
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002231 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002232 if (Record.size() % 3 != 0) {
2233 Error("Invalid VTABLE_USES record");
2234 return Failure;
2235 }
2236
Sebastian Redl40566802010-08-05 18:21:25 +00002237 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002238 // FIXME: Modules will have some trouble with this. This is clearly not
2239 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002240 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002241
2242 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2243 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2244 VTableUses.push_back(
2245 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2246 VTableUses.push_back(Record[Idx++]);
2247 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002248 break;
2249
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002250 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002251 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2252 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002253 break;
2254
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002255 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002256 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002257 Error("Invalid existing PendingInstantiations");
2258 return Failure;
2259 }
2260
2261 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002262 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2263 return Failure;
2264 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002265
Douglas Gregorf2abb522011-07-28 19:26:52 +00002266 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2267 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2268 PendingInstantiations.push_back(
2269 ReadSourceLocation(F, Record, I).getRawEncoding());
2270 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002271 break;
2272
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002273 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002274 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002275 // FIXME: Modules will have some trouble with this.
2276 SemaDeclRefs.clear();
2277 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2278 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002279 break;
2280
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002281 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002282 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002283 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002284 ActualOriginalFileName.assign(BlobStart, BlobLen);
2285 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002286 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002287 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002288
Douglas Gregor31d375f2011-05-06 21:43:30 +00002289 case ORIGINAL_FILE_ID:
2290 OriginalFileID = FileID::get(Record[0]);
2291 break;
2292
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002293 case ORIGINAL_PCH_DIR:
2294 // The primary AST will be the last to get here, so it will be the one
2295 // that's used.
2296 OriginalDir.assign(BlobStart, BlobLen);
2297 break;
2298
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002299 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002300 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002301 StringRef ASTBranch(BlobStart, BlobLen);
2302 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002303 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002304 return IgnorePCH;
2305 }
2306 break;
2307 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002308
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002309 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002310 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2311 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2312 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002313
2314 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002315
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002316 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002317 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002318 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002319 if (!PP.getPreprocessingRecord()->getExternalSource())
2320 PP.getPreprocessingRecord()->SetExternalSource(*this);
2321 StartingID
2322 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002323 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002324 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002325
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002326 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002327 // Introduce the global -> local mapping for preprocessed entities in
2328 // this module.
2329 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2330
2331 // Introduce the local -> global mapping for preprocessed entities in
2332 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002333 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002334 std::make_pair(LocalBasePreprocessedEntityID,
2335 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2336 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002337
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002338 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002339 }
2340
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002341 case DECL_UPDATE_OFFSETS: {
2342 if (Record.size() % 2 != 0) {
2343 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2344 return Failure;
2345 }
2346 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002347 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2348 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002349 break;
2350 }
2351
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002352 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002353 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002354 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002355 return Failure;
2356 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002357 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002358 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002359 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002360 break;
2361 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002362
Douglas Gregorcff9f262012-01-27 01:47:08 +00002363 case OBJC_CATEGORIES_MAP: {
2364 if (F.LocalNumObjCCategoriesInMap != 0) {
2365 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002366 return Failure;
2367 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002368
2369 F.LocalNumObjCCategoriesInMap = Record[0];
2370 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002371 break;
2372 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002373
Douglas Gregorcff9f262012-01-27 01:47:08 +00002374 case OBJC_CATEGORIES:
2375 F.ObjCCategories.swap(Record);
2376 break;
2377
Douglas Gregor7c789c12010-10-29 22:39:52 +00002378 case CXX_BASE_SPECIFIER_OFFSETS: {
2379 if (F.LocalNumCXXBaseSpecifiers != 0) {
2380 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2381 return Failure;
2382 }
2383
2384 F.LocalNumCXXBaseSpecifiers = Record[0];
2385 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002386 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002387 break;
2388 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002389
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002390 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002391 if (Record.size() % 2 != 0) {
2392 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2393 return Failure;
2394 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002395
2396 if (F.PragmaDiagMappings.empty())
2397 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002398 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002399 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2400 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002401 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002402
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002403 case CUDA_SPECIAL_DECL_REFS:
2404 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002405 // FIXME: Modules will have trouble with this.
2406 CUDASpecialDeclRefs.clear();
2407 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2408 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002409 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002410
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002411 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002412 F.HeaderFileInfoTableData = BlobStart;
2413 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002414 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002415 if (Record[0]) {
2416 F.HeaderFileInfoTable
2417 = HeaderFileInfoLookupTable::Create(
2418 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002419 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002420 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002421 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002422 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002423
2424 PP.getHeaderSearchInfo().SetExternalSource(this);
2425 if (!PP.getHeaderSearchInfo().getExternalLookup())
2426 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002427 }
2428 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002429 }
2430
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002431 case FP_PRAGMA_OPTIONS:
2432 // Later tables overwrite earlier ones.
2433 FPPragmaOptions.swap(Record);
2434 break;
2435
2436 case OPENCL_EXTENSIONS:
2437 // Later tables overwrite earlier ones.
2438 OpenCLExtensions.swap(Record);
2439 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002440
2441 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002442 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2443 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002444 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002445
2446 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002447 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2448 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002449 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002450
2451 case IMPORTED_MODULES: {
2452 if (F.Kind != MK_Module) {
2453 // If we aren't loading a module (which has its own exports), make
2454 // all of the imported modules visible.
2455 // FIXME: Deal with macros-only imports.
2456 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2457 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2458 ImportedModules.push_back(GlobalID);
2459 }
2460 }
2461 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002462 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002463
Douglas Gregora1be2782011-12-17 23:38:30 +00002464 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002465 F.RedeclarationChains.swap(Record);
2466 break;
2467 }
2468
2469 case LOCAL_REDECLARATIONS_MAP: {
2470 if (F.LocalNumRedeclarationsInMap != 0) {
2471 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002472 return Failure;
2473 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002474
Douglas Gregor2171bf12012-01-15 16:58:34 +00002475 F.LocalNumRedeclarationsInMap = Record[0];
2476 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002477 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002478 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002479
2480 case MERGED_DECLARATIONS: {
2481 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2482 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2483 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2484 for (unsigned N = Record[Idx++]; N > 0; --N)
2485 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2486 }
2487 break;
2488 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002489
2490 case MACRO_OFFSET: {
2491 if (F.LocalNumMacros != 0) {
2492 Error("duplicate MACRO_OFFSET record in AST file");
2493 return Failure;
2494 }
2495 F.MacroOffsets = (const uint32_t *)BlobStart;
2496 F.LocalNumMacros = Record[0];
2497 unsigned LocalBaseMacroID = Record[1];
2498 F.BaseMacroID = getTotalNumMacros();
2499
2500 if (F.LocalNumMacros > 0) {
2501 // Introduce the global -> local mapping for macros within this module.
2502 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2503
2504 // Introduce the local -> global mapping for macros within this module.
2505 F.MacroRemap.insertOrReplace(
2506 std::make_pair(LocalBaseMacroID,
2507 F.BaseMacroID - LocalBaseMacroID));
2508
2509 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2510 }
2511 break;
2512 }
2513
2514 case MACRO_UPDATES: {
2515 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2516 MacroID ID = getGlobalMacroID(F, Record[I++]);
2517 if (I == N)
2518 break;
2519
2520 MacroUpdates[ID].UndefLoc = ReadSourceLocation(F, Record, I);
2521 }
2522 break;
2523 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002524 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002525 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002526 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002527 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002528}
2529
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002530ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002531 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002532
Douglas Gregorc69a2922011-08-25 20:58:51 +00002533 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2534 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2535 unsigned Code = SLocEntryCursor.ReadCode();
2536 if (Code == llvm::bitc::END_BLOCK ||
2537 Code == llvm::bitc::ENTER_SUBBLOCK ||
2538 Code == llvm::bitc::DEFINE_ABBREV) {
2539 Error("incorrectly-formatted source location entry in AST file");
2540 return Failure;
2541 }
2542
2543 RecordData Record;
2544 const char *BlobStart;
2545 unsigned BlobLen;
2546 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2547 default:
2548 Error("incorrectly-formatted source location entry in AST file");
2549 return Failure;
2550
2551 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002552 // If the buffer was overridden, the file need not exist.
2553 if (Record[6])
2554 break;
2555
Douglas Gregorc69a2922011-08-25 20:58:51 +00002556 StringRef Filename(BlobStart, BlobLen);
2557 const FileEntry *File = getFileEntry(Filename);
2558
2559 if (File == 0) {
2560 std::string ErrorStr = "could not find file '";
2561 ErrorStr += Filename;
2562 ErrorStr += "' referenced by AST file";
2563 Error(ErrorStr.c_str());
2564 return IgnorePCH;
2565 }
2566
Douglas Gregora081da52011-11-16 20:05:18 +00002567 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002568 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002569 return Failure;
2570 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002571
2572 off_t StoredSize = (off_t)Record[4];
2573 time_t StoredTime = (time_t)Record[5];
2574
2575 // Check if there was a request to override the contents of the file
2576 // that was part of the precompiled header. Overridding such a file
2577 // can lead to problems when lexing using the source locations from the
2578 // PCH.
2579 SourceManager &SM = getSourceManager();
2580 if (SM.isFileOverridden(File)) {
2581 Error(diag::err_fe_pch_file_overridden, Filename);
2582 // After emitting the diagnostic, recover by disabling the override so
2583 // that the original file will be used.
2584 SM.disableFileContentsOverride(File);
2585 // The FileEntry is a virtual file entry with the size of the contents
2586 // that would override the original contents. Set it to the original's
2587 // size/time.
2588 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2589 StoredSize, StoredTime);
2590 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002591
Douglas Gregorc69a2922011-08-25 20:58:51 +00002592 // The stat info from the FileEntry came from the cached stat
2593 // info of the PCH, so we cannot trust it.
2594 struct stat StatBuf;
2595 if (::stat(File->getName(), &StatBuf) != 0) {
2596 StatBuf.st_size = File->getSize();
2597 StatBuf.st_mtime = File->getModificationTime();
2598 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002599
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002600 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002601#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002602 // In our regression testing, the Windows file system seems to
2603 // have inconsistent modification times that sometimes
2604 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002605 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002606#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002607 )) {
2608 Error(diag::err_fe_pch_file_modified, Filename);
2609 return IgnorePCH;
2610 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002611
Douglas Gregorc69a2922011-08-25 20:58:51 +00002612 break;
2613 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002614 }
2615 }
2616
2617 return Success;
2618}
2619
Douglas Gregorecc2c092011-12-01 22:20:10 +00002620void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002621 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2622 if (Decl *D = Names[I].dyn_cast<Decl *>())
Douglas Gregorf143ffc2012-01-06 16:22:39 +00002623 D->Hidden = false;
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002624 else {
2625 IdentifierInfo *II = Names[I].get<IdentifierInfo *>();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002626 // FIXME: Check if this works correctly with macro history.
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002627 if (!II->hasMacroDefinition()) {
Douglas Gregorc12906e2012-09-24 19:56:18 +00002628 // Make sure that this macro hasn't been #undef'd in the mean-time.
2629 llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator Known
2630 = PP.Macros.find(II);
2631 if (Known == PP.Macros.end() ||
2632 Known->second->getUndefLoc().isInvalid()) {
2633 II->setHasMacroDefinition(true);
2634 if (DeserializationListener)
2635 DeserializationListener->MacroVisible(II);
2636 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002637 }
2638 }
Douglas Gregor13292642011-12-02 15:45:10 +00002639 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002640}
2641
Douglas Gregor5e356932011-12-01 17:11:21 +00002642void ASTReader::makeModuleVisible(Module *Mod,
2643 Module::NameVisibilityKind NameVisibility) {
2644 llvm::SmallPtrSet<Module *, 4> Visited;
2645 llvm::SmallVector<Module *, 4> Stack;
2646 Stack.push_back(Mod);
2647 while (!Stack.empty()) {
2648 Mod = Stack.back();
2649 Stack.pop_back();
2650
2651 if (NameVisibility <= Mod->NameVisibility) {
2652 // This module already has this level of visibility (or greater), so
2653 // there is nothing more to do.
2654 continue;
2655 }
2656
Douglas Gregor51f564f2011-12-31 04:05:44 +00002657 if (!Mod->isAvailable()) {
2658 // Modules that aren't available cannot be made visible.
2659 continue;
2660 }
2661
Douglas Gregor5e356932011-12-01 17:11:21 +00002662 // Update the module's name visibility.
2663 Mod->NameVisibility = NameVisibility;
2664
Douglas Gregorecc2c092011-12-01 22:20:10 +00002665 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002666 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002667 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2668 if (Hidden != HiddenNamesMap.end()) {
2669 makeNamesVisible(Hidden->second);
2670 HiddenNamesMap.erase(Hidden);
2671 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002672
2673 // Push any non-explicit submodules onto the stack to be marked as
2674 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002675 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2676 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002677 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002678 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2679 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002680 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002681
2682 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002683 bool AnyWildcard = false;
2684 bool UnrestrictedWildcard = false;
2685 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002686 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2687 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002688 if (!Mod->Exports[I].getInt()) {
2689 // Export a named module directly; no wildcards involved.
2690 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002691 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002692
2693 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002694 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002695
2696 // Wildcard export: export all of the imported modules that match
2697 // the given pattern.
2698 AnyWildcard = true;
2699 if (UnrestrictedWildcard)
2700 continue;
2701
2702 if (Module *Restriction = Mod->Exports[I].getPointer())
2703 WildcardRestrictions.push_back(Restriction);
2704 else {
2705 WildcardRestrictions.clear();
2706 UnrestrictedWildcard = true;
2707 }
2708 }
2709
2710 // If there were any wildcards, push any imported modules that were
2711 // re-exported by the wildcard restriction.
2712 if (!AnyWildcard)
2713 continue;
2714
2715 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2716 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002717 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002718 continue;
2719
2720 bool Acceptable = UnrestrictedWildcard;
2721 if (!Acceptable) {
2722 // Check whether this module meets one of the restrictions.
2723 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2724 Module *Restriction = WildcardRestrictions[R];
2725 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2726 Acceptable = true;
2727 break;
2728 }
2729 }
2730 }
2731
2732 if (!Acceptable)
2733 continue;
2734
Douglas Gregor0adaa882011-12-05 17:28:06 +00002735 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002736 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002737 }
2738}
2739
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002740ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002741 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002742 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002743 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002744
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002745 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002746 case Failure: return Failure;
2747 case IgnorePCH: return IgnorePCH;
2748 case Success: break;
2749 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002750
2751 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002752
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002753 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002754 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002755 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2756 // if DisableValidation is true, defines that were set on command-line
2757 // but not in the PCH file will not be added to SuggestedPredefines.
2758 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002759 return IgnorePCH;
2760
Douglas Gregoreee242f2011-10-27 09:33:13 +00002761 // Mark all of the identifiers in the identifier table as being out of date,
2762 // so that various accessors know to check the loaded modules when the
2763 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002764 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2765 IdEnd = PP.getIdentifierTable().end();
2766 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002767 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002768
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002769 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002770 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2771 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2772 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002773 Module *ResolvedMod = getSubmodule(GlobalID);
2774
2775 if (Unresolved.IsImport) {
2776 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002777 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002778 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002779 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002780
2781 if (ResolvedMod || Unresolved.IsWildcard)
2782 Unresolved.Mod->Exports.push_back(
2783 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002784 }
Douglas Gregor55988682011-12-05 16:33:54 +00002785 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002786
Douglas Gregor35942772011-09-09 21:34:22 +00002787 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002788
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002789 if (DeserializationListener)
2790 DeserializationListener->ReaderInitialized(this);
2791
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002792 if (!OriginalFileID.isInvalid()) {
2793 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2794 + OriginalFileID.getOpaqueValue() - 1);
2795
2796 // If this AST file is a precompiled preamble, then set the preamble file ID
2797 // of the source manager to the file source file from which the preamble was
2798 // built.
2799 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002800 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002801 } else if (Type == MK_MainFile) {
2802 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002803 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002804 }
2805
Douglas Gregorcff9f262012-01-27 01:47:08 +00002806 // For any Objective-C class definitions we have already loaded, make sure
2807 // that we load any additional categories.
2808 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2809 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2810 ObjCClassesLoaded[I],
2811 PreviousGeneration);
2812 }
2813
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002814 return Success;
2815}
2816
Chris Lattner5f9e2722011-07-23 10:55:15 +00002817ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002818 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002819 ModuleFile *ImportedBy) {
2820 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002821 bool NewModule;
2822 std::string ErrorStr;
2823 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002824 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002825
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002826 if (!M) {
2827 // We couldn't load the module.
2828 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2829 + ErrorStr;
2830 Error(Msg);
2831 return Failure;
2832 }
2833
2834 if (!NewModule) {
2835 // We've already loaded this module.
2836 return Success;
2837 }
2838
2839 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2840 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002841 if (FileName != "-") {
2842 CurrentDir = llvm::sys::path::parent_path(FileName);
2843 if (CurrentDir.empty()) CurrentDir = ".";
2844 }
2845
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002846 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002847 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002848 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002849 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002850
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002851 // Sniff for the signature.
2852 if (Stream.Read(8) != 'C' ||
2853 Stream.Read(8) != 'P' ||
2854 Stream.Read(8) != 'C' ||
2855 Stream.Read(8) != 'H') {
2856 Diag(diag::err_not_a_pch_file) << FileName;
2857 return Failure;
2858 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002859
Douglas Gregor2cf26342009-04-09 22:27:44 +00002860 while (!Stream.AtEndOfStream()) {
2861 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002862
Douglas Gregore1d918e2009-04-10 23:10:45 +00002863 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002864 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002865 return Failure;
2866 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002867
2868 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002869
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002870 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002871 switch (BlockID) {
2872 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002873 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002874 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002875 return Failure;
2876 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002877 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002878 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002879 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002880 case Success:
2881 break;
2882
2883 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002884 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002885
2886 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002887 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002888 // AST block, skipping subblocks, to see if there are other
2889 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002890
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002891 // FIXME: We can't clear loaded slocentries anymore.
2892 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002893
2894 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002895 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002896 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002897
Douglas Gregore1d918e2009-04-10 23:10:45 +00002898 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002899 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002900 break;
2901 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002902 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002903 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002904 return Failure;
2905 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002906 break;
2907 }
Mike Stump1eb44332009-09-09 15:08:12 +00002908 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002909
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002910 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002911 // bits of all files we've seen.
2912 F.GlobalBitOffset = TotalModulesSizeInBits;
2913 TotalModulesSizeInBits += F.SizeInBits;
2914 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002915
2916 // Make sure that the files this module was built against are still available.
2917 if (!DisableValidation) {
2918 switch(validateFileEntries(*M)) {
2919 case Failure: return Failure;
2920 case IgnorePCH: return IgnorePCH;
2921 case Success: break;
2922 }
2923 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002924
2925 // Preload SLocEntries.
2926 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2927 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002928 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2929 // directly because the entry may have already been loaded in which case
2930 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2931 // SourceManager.
2932 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002933 }
2934
Douglas Gregorc69a2922011-08-25 20:58:51 +00002935
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002936 return Success;
2937}
2938
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002939void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002940 // If there's a listener, notify them that we "read" the translation unit.
2941 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002942 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2943 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002944
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002945 // Make sure we load the declaration update records for the translation unit,
2946 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002947 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2948 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002949
Douglas Gregor5f957282011-08-11 22:18:49 +00002950 // FIXME: Find a better way to deal with collisions between these
2951 // built-in types. Right now, we just ignore the problem.
2952
2953 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002954 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002955 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2956 if (!Context.CFConstantStringTypeDecl)
2957 Context.setCFConstantStringType(GetType(String));
2958 }
2959
2960 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2961 QualType FileType = GetType(File);
2962 if (FileType.isNull()) {
2963 Error("FILE type is NULL");
2964 return;
2965 }
2966
2967 if (!Context.FILEDecl) {
2968 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2969 Context.setFILEDecl(Typedef->getDecl());
2970 else {
2971 const TagType *Tag = FileType->getAs<TagType>();
2972 if (!Tag) {
2973 Error("Invalid FILE type in AST file");
2974 return;
2975 }
2976 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002977 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002978 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002979 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002980
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002981 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002982 QualType Jmp_bufType = GetType(Jmp_buf);
2983 if (Jmp_bufType.isNull()) {
2984 Error("jmp_buf type is NULL");
2985 return;
2986 }
2987
2988 if (!Context.jmp_bufDecl) {
2989 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2990 Context.setjmp_bufDecl(Typedef->getDecl());
2991 else {
2992 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2993 if (!Tag) {
2994 Error("Invalid jmp_buf type in AST file");
2995 return;
2996 }
2997 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002998 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002999 }
Mike Stump782fa302009-07-28 02:25:19 +00003000 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003001
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003002 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003003 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3004 if (Sigjmp_bufType.isNull()) {
3005 Error("sigjmp_buf type is NULL");
3006 return;
3007 }
3008
3009 if (!Context.sigjmp_bufDecl) {
3010 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3011 Context.setsigjmp_bufDecl(Typedef->getDecl());
3012 else {
3013 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3014 assert(Tag && "Invalid sigjmp_buf type in AST file");
3015 Context.setsigjmp_bufDecl(Tag->getDecl());
3016 }
3017 }
3018 }
3019
3020 if (unsigned ObjCIdRedef
3021 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3022 if (Context.ObjCIdRedefinitionType.isNull())
3023 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3024 }
3025
3026 if (unsigned ObjCClassRedef
3027 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3028 if (Context.ObjCClassRedefinitionType.isNull())
3029 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3030 }
3031
3032 if (unsigned ObjCSelRedef
3033 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3034 if (Context.ObjCSelRedefinitionType.isNull())
3035 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3036 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003037
3038 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3039 QualType Ucontext_tType = GetType(Ucontext_t);
3040 if (Ucontext_tType.isNull()) {
3041 Error("ucontext_t type is NULL");
3042 return;
3043 }
3044
3045 if (!Context.ucontext_tDecl) {
3046 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3047 Context.setucontext_tDecl(Typedef->getDecl());
3048 else {
3049 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3050 assert(Tag && "Invalid ucontext_t type in AST file");
3051 Context.setucontext_tDecl(Tag->getDecl());
3052 }
3053 }
3054 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003055 }
3056
Douglas Gregor35942772011-09-09 21:34:22 +00003057 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003058
3059 // If there were any CUDA special declarations, deserialize them.
3060 if (!CUDASpecialDeclRefs.empty()) {
3061 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003062 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003063 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3064 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003065
3066 // Re-export any modules that were imported by a non-module AST file.
3067 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3068 if (Module *Imported = getSubmodule(ImportedModules[I]))
3069 makeModuleVisible(Imported, Module::AllVisible);
3070 }
3071 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003072}
3073
Douglas Gregorecc2c092011-12-01 22:20:10 +00003074void ASTReader::finalizeForWriting() {
3075 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3076 HiddenEnd = HiddenNamesMap.end();
3077 Hidden != HiddenEnd; ++Hidden) {
3078 makeNamesVisible(Hidden->second);
3079 }
3080 HiddenNamesMap.clear();
3081}
3082
Douglas Gregorb64c1932009-05-12 01:31:05 +00003083/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003084/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003085/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003086std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003087 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003088 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003089 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003090 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003091 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003092 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003093 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003094 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003095 return std::string();
3096 }
3097
3098 // Initialize the stream
3099 llvm::BitstreamReader StreamFile;
3100 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003101 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003102 (const unsigned char *)Buffer->getBufferEnd());
3103 Stream.init(StreamFile);
3104
3105 // Sniff for the signature.
3106 if (Stream.Read(8) != 'C' ||
3107 Stream.Read(8) != 'P' ||
3108 Stream.Read(8) != 'C' ||
3109 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003110 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003111 return std::string();
3112 }
3113
3114 RecordData Record;
3115 while (!Stream.AtEndOfStream()) {
3116 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Douglas Gregorb64c1932009-05-12 01:31:05 +00003118 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3119 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003121 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003122 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003123 case AST_BLOCK_ID:
3124 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003125 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003126 return std::string();
3127 }
3128 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003129
Douglas Gregorb64c1932009-05-12 01:31:05 +00003130 default:
3131 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003132 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003133 return std::string();
3134 }
3135 break;
3136 }
3137 continue;
3138 }
3139
3140 if (Code == llvm::bitc::END_BLOCK) {
3141 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003142 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003143 return std::string();
3144 }
3145 continue;
3146 }
3147
3148 if (Code == llvm::bitc::DEFINE_ABBREV) {
3149 Stream.ReadAbbrevRecord();
3150 continue;
3151 }
3152
3153 Record.clear();
3154 const char *BlobStart = 0;
3155 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003156 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003157 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003158 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003159 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003160
3161 return std::string();
3162}
3163
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003164ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003165 // Enter the submodule block.
3166 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3167 Error("malformed submodule block record in AST file");
3168 return Failure;
3169 }
3170
3171 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003172 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003173 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003174 RecordData Record;
3175 while (true) {
3176 unsigned Code = F.Stream.ReadCode();
3177 if (Code == llvm::bitc::END_BLOCK) {
3178 if (F.Stream.ReadBlockEnd()) {
3179 Error("error at end of submodule block in AST file");
3180 return Failure;
3181 }
3182 return Success;
3183 }
3184
3185 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3186 // No known subblocks, always skip them.
3187 F.Stream.ReadSubBlockID();
3188 if (F.Stream.SkipBlock()) {
3189 Error("malformed block record in AST file");
3190 return Failure;
3191 }
3192 continue;
3193 }
3194
3195 if (Code == llvm::bitc::DEFINE_ABBREV) {
3196 F.Stream.ReadAbbrevRecord();
3197 continue;
3198 }
3199
3200 // Read a record.
3201 const char *BlobStart;
3202 unsigned BlobLen;
3203 Record.clear();
3204 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3205 default: // Default behavior: ignore.
3206 break;
3207
3208 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003209 if (First) {
3210 Error("missing submodule metadata record at beginning of block");
3211 return Failure;
3212 }
3213
Douglas Gregore209e502011-12-06 01:10:29 +00003214 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003215 Error("malformed module definition");
3216 return Failure;
3217 }
3218
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003219 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003220 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3221 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3222 bool IsFramework = Record[2];
3223 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003224 bool IsSystem = Record[4];
3225 bool InferSubmodules = Record[5];
3226 bool InferExplicitSubmodules = Record[6];
3227 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003228
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003229 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003230 if (Parent)
3231 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003232
3233 // Retrieve this (sub)module from the module map, creating it if
3234 // necessary.
3235 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3236 IsFramework,
3237 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003238 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3239 if (GlobalIndex >= SubmodulesLoaded.size() ||
3240 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003241 Error("too many submodules");
3242 return Failure;
3243 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003244
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003245 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003246 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003247 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003248 CurrentModule->InferSubmodules = InferSubmodules;
3249 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3250 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003251 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003252 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003253
Douglas Gregore209e502011-12-06 01:10:29 +00003254 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003255 break;
3256 }
3257
Douglas Gregor77d029f2011-12-08 19:11:24 +00003258 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003259 if (First) {
3260 Error("missing submodule metadata record at beginning of block");
3261 return Failure;
3262 }
3263
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003264 if (!CurrentModule)
3265 break;
3266
3267 StringRef FileName(BlobStart, BlobLen);
3268 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003269 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003270 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003271 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003272 Error("mismatched umbrella headers in submodule");
3273 return Failure;
3274 }
3275 }
3276 break;
3277 }
3278
3279 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003280 if (First) {
3281 Error("missing submodule metadata record at beginning of block");
3282 return Failure;
3283 }
3284
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003285 if (!CurrentModule)
3286 break;
3287
3288 // FIXME: Be more lazy about this!
3289 StringRef FileName(BlobStart, BlobLen);
3290 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3291 if (std::find(CurrentModule->Headers.begin(),
3292 CurrentModule->Headers.end(),
3293 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003294 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003295 }
3296 break;
3297 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003298
3299 case SUBMODULE_TOPHEADER: {
3300 if (First) {
3301 Error("missing submodule metadata record at beginning of block");
3302 return Failure;
3303 }
3304
3305 if (!CurrentModule)
3306 break;
3307
3308 // FIXME: Be more lazy about this!
3309 StringRef FileName(BlobStart, BlobLen);
3310 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3311 CurrentModule->TopHeaders.insert(File);
3312 break;
3313 }
3314
Douglas Gregor77d029f2011-12-08 19:11:24 +00003315 case SUBMODULE_UMBRELLA_DIR: {
3316 if (First) {
3317 Error("missing submodule metadata record at beginning of block");
3318 return Failure;
3319 }
3320
3321 if (!CurrentModule)
3322 break;
3323
3324 StringRef DirName(BlobStart, BlobLen);
3325 if (const DirectoryEntry *Umbrella
3326 = PP.getFileManager().getDirectory(DirName)) {
3327 if (!CurrentModule->getUmbrellaDir())
3328 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3329 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3330 Error("mismatched umbrella directories in submodule");
3331 return Failure;
3332 }
3333 }
3334 break;
3335 }
3336
Douglas Gregor26ced122011-12-01 00:59:36 +00003337 case SUBMODULE_METADATA: {
3338 if (!First) {
3339 Error("submodule metadata record not at beginning of block");
3340 return Failure;
3341 }
3342 First = false;
3343
3344 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003345 F.LocalNumSubmodules = Record[0];
3346 unsigned LocalBaseSubmoduleID = Record[1];
3347 if (F.LocalNumSubmodules > 0) {
3348 // Introduce the global -> local mapping for submodules within this
3349 // module.
3350 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3351
3352 // Introduce the local -> global mapping for submodules within this
3353 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003354 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003355 std::make_pair(LocalBaseSubmoduleID,
3356 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3357
3358 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3359 }
3360 break;
3361 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003362
Douglas Gregor55988682011-12-05 16:33:54 +00003363 case SUBMODULE_IMPORTS: {
3364 if (First) {
3365 Error("missing submodule metadata record at beginning of block");
3366 return Failure;
3367 }
3368
3369 if (!CurrentModule)
3370 break;
3371
3372 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3373 UnresolvedModuleImportExport Unresolved;
3374 Unresolved.File = &F;
3375 Unresolved.Mod = CurrentModule;
3376 Unresolved.ID = Record[Idx];
3377 Unresolved.IsImport = true;
3378 Unresolved.IsWildcard = false;
3379 UnresolvedModuleImportExports.push_back(Unresolved);
3380 }
3381 break;
3382 }
3383
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003384 case SUBMODULE_EXPORTS: {
3385 if (First) {
3386 Error("missing submodule metadata record at beginning of block");
3387 return Failure;
3388 }
3389
3390 if (!CurrentModule)
3391 break;
3392
3393 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003394 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003395 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003396 Unresolved.Mod = CurrentModule;
3397 Unresolved.ID = Record[Idx];
3398 Unresolved.IsImport = false;
3399 Unresolved.IsWildcard = Record[Idx + 1];
3400 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003401 }
3402
3403 // Once we've loaded the set of exports, there's no reason to keep
3404 // the parsed, unresolved exports around.
3405 CurrentModule->UnresolvedExports.clear();
3406 break;
3407 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003408 case SUBMODULE_REQUIRES: {
3409 if (First) {
3410 Error("missing submodule metadata record at beginning of block");
3411 return Failure;
3412 }
3413
3414 if (!CurrentModule)
3415 break;
3416
3417 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003418 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003419 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003420 break;
3421 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003422 }
3423 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003424}
3425
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003426/// \brief Parse the record that corresponds to a LangOptions data
3427/// structure.
3428///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003429/// This routine parses the language options from the AST file and then gives
3430/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003431///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003432/// \returns true if the listener deems the file unacceptable, false otherwise.
John McCall260611a2012-06-20 06:18:46 +00003433bool ASTReader::ParseLanguageOptions(const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003434 if (Listener) {
3435 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003436 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003437#define LANGOPT(Name, Bits, Default, Description) \
3438 LangOpts.Name = Record[Idx++];
3439#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3440 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3441#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003442
3443 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3444 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3445 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003446
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003447 unsigned Length = Record[Idx++];
3448 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3449 Record.begin() + Idx + Length);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003450 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003451 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003452
3453 return false;
3454}
3455
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003456std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003457ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003458 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003459 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003460 assert(I != GlobalPreprocessedEntityMap.end() &&
3461 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003462 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003463 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3464 return std::make_pair(M, LocalIndex);
3465}
3466
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003467std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3468ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3469 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3470 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3471 Mod.NumPreprocessedEntities);
3472
3473 return std::make_pair(PreprocessingRecord::iterator(),
3474 PreprocessingRecord::iterator());
3475}
3476
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003477std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3478ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3479 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3480 ModuleDeclIterator(this, &Mod,
3481 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3482}
3483
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003484PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3485 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003486 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3487 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003488 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003489 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003490
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003491 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003492 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003493
3494 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3495 switch (Code) {
3496 case llvm::bitc::END_BLOCK:
3497 return 0;
3498
3499 case llvm::bitc::ENTER_SUBBLOCK:
3500 Error("unexpected subblock record in preprocessor detail block");
3501 return 0;
3502
3503 case llvm::bitc::DEFINE_ABBREV:
3504 Error("unexpected abbrevation record in preprocessor detail block");
3505 return 0;
3506
3507 default:
3508 break;
3509 }
3510
3511 if (!PP.getPreprocessingRecord()) {
3512 Error("no preprocessing record");
3513 return 0;
3514 }
3515
3516 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003517 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3518 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003519 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3520 const char *BlobStart = 0;
3521 unsigned BlobLen = 0;
3522 RecordData Record;
3523 PreprocessorDetailRecordTypes RecType =
3524 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3525 Code, Record, BlobStart, BlobLen);
3526 switch (RecType) {
3527 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003528 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003529 IdentifierInfo *Name = 0;
3530 MacroDefinition *Def = 0;
3531 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003532 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003533 else {
3534 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003535 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003536 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3537 }
3538
3539 MacroExpansion *ME;
3540 if (isBuiltin)
3541 ME = new (PPRec) MacroExpansion(Name, Range);
3542 else
3543 ME = new (PPRec) MacroExpansion(Def, Range);
3544
3545 return ME;
3546 }
3547
3548 case PPD_MACRO_DEFINITION: {
3549 // Decode the identifier info and then check again; if the macro is
3550 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003551 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003552 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003553 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003554
3555 if (DeserializationListener)
3556 DeserializationListener->MacroDefinitionRead(PPID, MD);
3557
3558 return MD;
3559 }
3560
3561 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003562 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003563 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3564 const FileEntry *File = 0;
3565 if (!FullFileName.empty())
3566 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003567
3568 // FIXME: Stable encoding
3569 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003570 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003571 InclusionDirective *ID
3572 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003573 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003574 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003575 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003576 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003577 return ID;
3578 }
3579 }
David Blaikie7530c032012-01-17 06:56:22 +00003580
3581 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003582}
3583
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003584/// \brief \arg SLocMapI points at a chunk of a module that contains no
3585/// preprocessed entities or the entities it contains are not the ones we are
3586/// looking for. Find the next module that contains entities and return the ID
3587/// of the first entry.
3588PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3589 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3590 ++SLocMapI;
3591 for (GlobalSLocOffsetMapType::const_iterator
3592 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003593 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003594 if (M.NumPreprocessedEntities)
3595 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3596 }
3597
3598 return getTotalNumPreprocessedEntities();
3599}
3600
3601namespace {
3602
3603template <unsigned PPEntityOffset::*PPLoc>
3604struct PPEntityComp {
3605 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003606 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003607
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003608 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003609
Benjamin Kramer88df1252011-09-21 06:42:26 +00003610 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3611 SourceLocation LHS = getLoc(L);
3612 SourceLocation RHS = getLoc(R);
3613 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3614 }
3615
3616 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003617 SourceLocation LHS = getLoc(L);
3618 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3619 }
3620
Benjamin Kramer88df1252011-09-21 06:42:26 +00003621 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003622 SourceLocation RHS = getLoc(R);
3623 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3624 }
3625
3626 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3627 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3628 }
3629};
3630
3631}
3632
3633/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3634PreprocessedEntityID
3635ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3636 if (SourceMgr.isLocalSourceLocation(BLoc))
3637 return getTotalNumPreprocessedEntities();
3638
3639 GlobalSLocOffsetMapType::const_iterator
3640 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3641 BLoc.getOffset());
3642 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3643 "Corrupted global sloc offset map");
3644
3645 if (SLocMapI->second->NumPreprocessedEntities == 0)
3646 return findNextPreprocessedEntity(SLocMapI);
3647
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003648 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003649 typedef const PPEntityOffset *pp_iterator;
3650 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3651 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003652
3653 size_t Count = M.NumPreprocessedEntities;
3654 size_t Half;
3655 pp_iterator First = pp_begin;
3656 pp_iterator PPI;
3657
3658 // Do a binary search manually instead of using std::lower_bound because
3659 // The end locations of entities may be unordered (when a macro expansion
3660 // is inside another macro argument), but for this case it is not important
3661 // whether we get the first macro expansion or its containing macro.
3662 while (Count > 0) {
3663 Half = Count/2;
3664 PPI = First;
3665 std::advance(PPI, Half);
3666 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3667 BLoc)){
3668 First = PPI;
3669 ++First;
3670 Count = Count - Half - 1;
3671 } else
3672 Count = Half;
3673 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003674
3675 if (PPI == pp_end)
3676 return findNextPreprocessedEntity(SLocMapI);
3677
3678 return getGlobalPreprocessedEntityID(M,
3679 M.BasePreprocessedEntityID + (PPI - pp_begin));
3680}
3681
3682/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3683PreprocessedEntityID
3684ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3685 if (SourceMgr.isLocalSourceLocation(ELoc))
3686 return getTotalNumPreprocessedEntities();
3687
3688 GlobalSLocOffsetMapType::const_iterator
3689 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3690 ELoc.getOffset());
3691 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3692 "Corrupted global sloc offset map");
3693
3694 if (SLocMapI->second->NumPreprocessedEntities == 0)
3695 return findNextPreprocessedEntity(SLocMapI);
3696
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003697 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003698 typedef const PPEntityOffset *pp_iterator;
3699 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3700 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3701 pp_iterator PPI =
3702 std::upper_bound(pp_begin, pp_end, ELoc,
3703 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3704
3705 if (PPI == pp_end)
3706 return findNextPreprocessedEntity(SLocMapI);
3707
3708 return getGlobalPreprocessedEntityID(M,
3709 M.BasePreprocessedEntityID + (PPI - pp_begin));
3710}
3711
3712/// \brief Returns a pair of [Begin, End) indices of preallocated
3713/// preprocessed entities that \arg Range encompasses.
3714std::pair<unsigned, unsigned>
3715 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3716 if (Range.isInvalid())
3717 return std::make_pair(0,0);
3718 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3719
3720 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3721 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3722 return std::make_pair(BeginID, EndID);
3723}
3724
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003725/// \brief Optionally returns true or false if the preallocated preprocessed
3726/// entity with index \arg Index came from file \arg FID.
3727llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3728 FileID FID) {
3729 if (FID.isInvalid())
3730 return false;
3731
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003732 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3733 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003734 unsigned LocalIndex = PPInfo.second;
3735 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3736
3737 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3738 if (Loc.isInvalid())
3739 return false;
3740
3741 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3742 return true;
3743 else
3744 return false;
3745}
3746
Douglas Gregord10a3812011-08-25 18:14:34 +00003747namespace {
3748 /// \brief Visitor used to search for information about a header file.
3749 class HeaderFileInfoVisitor {
3750 ASTReader &Reader;
3751 const FileEntry *FE;
3752
3753 llvm::Optional<HeaderFileInfo> HFI;
3754
3755 public:
3756 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3757 : Reader(Reader), FE(FE) { }
3758
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003759 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003760 HeaderFileInfoVisitor *This
3761 = static_cast<HeaderFileInfoVisitor *>(UserData);
3762
3763 HeaderFileInfoTrait Trait(This->Reader, M,
3764 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3765 M.HeaderFileFrameworkStrings,
3766 This->FE->getName());
3767
3768 HeaderFileInfoLookupTable *Table
3769 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3770 if (!Table)
3771 return false;
3772
3773 // Look in the on-disk hash table for an entry for this file name.
3774 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3775 &Trait);
3776 if (Pos == Table->end())
3777 return false;
3778
3779 This->HFI = *Pos;
3780 return true;
3781 }
3782
3783 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3784 };
3785}
3786
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003787HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003788 HeaderFileInfoVisitor Visitor(*this, FE);
3789 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3790 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003791 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003792 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3793 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003794 }
3795
3796 return HeaderFileInfo();
3797}
3798
David Blaikied6471f72011-09-25 23:23:43 +00003799void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003800 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003801 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003802 unsigned Idx = 0;
3803 while (Idx < F.PragmaDiagMappings.size()) {
3804 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003805 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3806 Diag.DiagStatePoints.push_back(
3807 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3808 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003809 while (1) {
3810 assert(Idx < F.PragmaDiagMappings.size() &&
3811 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3812 if (Idx >= F.PragmaDiagMappings.size()) {
3813 break; // Something is messed up but at least avoid infinite loop in
3814 // release build.
3815 }
3816 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3817 if (DiagID == (unsigned)-1) {
3818 break; // no more diag/map pairs for this location.
3819 }
3820 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003821 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3822 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003823 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003824 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003825 }
3826}
3827
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003828/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003829ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003830 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003831 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003832 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003833 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003834}
3835
3836/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003837///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003838/// The index is the type ID, shifted and minus the number of predefs. This
3839/// routine actually reads the record corresponding to the type at the given
3840/// location. It is a helper routine for GetType, which deals with reading type
3841/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003842QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003843 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003844 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003845
Douglas Gregor0b748912009-04-14 21:18:50 +00003846 // Keep track of where we are in the stream, then jump back there
3847 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003848 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003849
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003850 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003851
Douglas Gregord89275b2009-07-06 18:54:52 +00003852 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003853 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003854
Douglas Gregor393f2492011-07-22 00:38:23 +00003855 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003856 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003857 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003858 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003859 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3860 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003861 if (Record.size() != 2) {
3862 Error("Incorrect encoding of extended qualifier type");
3863 return QualType();
3864 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003865 QualType Base = readType(*Loc.F, Record, Idx);
3866 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003867 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003868 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003869
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003870 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003871 if (Record.size() != 1) {
3872 Error("Incorrect encoding of complex type");
3873 return QualType();
3874 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003875 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003876 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003877 }
3878
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003879 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003880 if (Record.size() != 1) {
3881 Error("Incorrect encoding of pointer type");
3882 return QualType();
3883 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003884 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003885 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003886 }
3887
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003888 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003889 if (Record.size() != 1) {
3890 Error("Incorrect encoding of block pointer type");
3891 return QualType();
3892 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003893 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003894 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003895 }
3896
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003897 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003898 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003899 Error("Incorrect encoding of lvalue reference type");
3900 return QualType();
3901 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003902 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003903 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003904 }
3905
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003906 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003907 if (Record.size() != 1) {
3908 Error("Incorrect encoding of rvalue reference type");
3909 return QualType();
3910 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003911 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003912 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003913 }
3914
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003915 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003916 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003917 Error("Incorrect encoding of member pointer type");
3918 return QualType();
3919 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003920 QualType PointeeType = readType(*Loc.F, Record, Idx);
3921 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003922 if (PointeeType.isNull() || ClassType.isNull())
3923 return QualType();
3924
Douglas Gregor35942772011-09-09 21:34:22 +00003925 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003926 }
3927
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003928 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003929 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003930 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3931 unsigned IndexTypeQuals = Record[2];
3932 unsigned Idx = 3;
3933 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003934 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003935 ASM, IndexTypeQuals);
3936 }
3937
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003938 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003939 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003940 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3941 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003942 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003943 }
3944
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003945 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003946 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003947 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3948 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003949 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3950 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003951 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003952 ASM, IndexTypeQuals,
3953 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003954 }
3955
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003956 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003957 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003958 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003959 return QualType();
3960 }
3961
Douglas Gregor393f2492011-07-22 00:38:23 +00003962 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003963 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003964 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003965 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003966 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003967 }
3968
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003969 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003970 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003971 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003972 return QualType();
3973 }
3974
Douglas Gregor393f2492011-07-22 00:38:23 +00003975 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003976 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003977 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003978 }
3979
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003980 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003981 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003982 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003983 return QualType();
3984 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003985 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003986 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3987 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003988 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003989 }
3990
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003991 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003992 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003993
3994 FunctionProtoType::ExtProtoInfo EPI;
3995 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003996 /*hasregparm*/ Record[2],
3997 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003998 static_cast<CallingConv>(Record[4]),
3999 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004000
John McCallf85e1932011-06-15 23:02:42 +00004001 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004002 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004003 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004004 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004005 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004006
4007 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004008 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004009 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004010 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004011 ExceptionSpecificationType EST =
4012 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4013 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004014 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004015 if (EST == EST_Dynamic) {
4016 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004017 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004018 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004019 EPI.Exceptions = Exceptions.data();
4020 } else if (EST == EST_ComputedNoexcept) {
4021 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004022 } else if (EST == EST_Uninstantiated) {
4023 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4024 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004025 } else if (EST == EST_Unevaluated) {
4026 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004027 }
Douglas Gregor35942772011-09-09 21:34:22 +00004028 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004029 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004030 }
4031
Douglas Gregor409448c2011-07-21 22:35:25 +00004032 case TYPE_UNRESOLVED_USING: {
4033 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004034 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004035 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4036 }
4037
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004038 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004039 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004040 Error("incorrect encoding of typedef type");
4041 return QualType();
4042 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004043 unsigned Idx = 0;
4044 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004045 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004046 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004047 Canonical = Context.getCanonicalType(Canonical);
4048 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004049 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004051 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004052 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004054 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004055 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004056 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004057 return QualType();
4058 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004059 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004060 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004061 }
Mike Stump1eb44332009-09-09 15:08:12 +00004062
Douglas Gregorf8af9822012-02-12 18:42:33 +00004063 case TYPE_DECLTYPE: {
4064 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4065 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4066 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004067
Sean Huntca63c202011-05-24 22:41:36 +00004068 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004069 QualType BaseType = readType(*Loc.F, Record, Idx);
4070 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004071 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004072 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004073 }
4074
Richard Smith34b41d92011-02-20 03:19:35 +00004075 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004076 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004077
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004078 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004079 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004080 Error("incorrect encoding of record type");
4081 return QualType();
4082 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004083 unsigned Idx = 0;
4084 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004085 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4086 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4087 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004088 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004089 return T;
4090 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004091
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004092 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004093 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004094 Error("incorrect encoding of enum type");
4095 return QualType();
4096 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004097 unsigned Idx = 0;
4098 bool IsDependent = Record[Idx++];
4099 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004100 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004101 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004102 return T;
4103 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004104
John McCall9d156a72011-01-06 01:58:22 +00004105 case TYPE_ATTRIBUTED: {
4106 if (Record.size() != 3) {
4107 Error("incorrect encoding of attributed type");
4108 return QualType();
4109 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004110 QualType modifiedType = readType(*Loc.F, Record, Idx);
4111 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004112 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004113 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004114 }
4115
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004116 case TYPE_PAREN: {
4117 if (Record.size() != 1) {
4118 Error("incorrect encoding of paren type");
4119 return QualType();
4120 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004121 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004122 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004123 }
4124
Douglas Gregor7536dd52010-12-20 02:24:11 +00004125 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004126 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004127 Error("incorrect encoding of pack expansion type");
4128 return QualType();
4129 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004130 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004131 if (Pattern.isNull())
4132 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004133 llvm::Optional<unsigned> NumExpansions;
4134 if (Record[1])
4135 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004136 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004137 }
4138
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004139 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004140 unsigned Idx = 0;
4141 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004142 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004143 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004144 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004145 }
4146
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004147 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004148 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004149 ObjCInterfaceDecl *ItfD
4150 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004151 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004152 }
4153
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004154 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004155 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004156 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004157 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004158 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004159 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004160 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004161 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004162 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004163
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004164 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004165 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004166 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004167 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004168 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004169
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004170 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004171 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004172 QualType Parm = readType(*Loc.F, Record, Idx);
4173 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004174 return
Douglas Gregor35942772011-09-09 21:34:22 +00004175 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004176 Replacement);
4177 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004178
Douglas Gregorc3069d62011-01-14 02:55:32 +00004179 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4180 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004181 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004182 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004183 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004184 cast<TemplateTypeParmType>(Parm),
4185 ArgPack);
4186 }
4187
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004188 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004189 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004190 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004191 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004192 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004193 return
Douglas Gregor35942772011-09-09 21:34:22 +00004194 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004195 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004196
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004197 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004198 unsigned Idx = 0;
4199 unsigned Depth = Record[Idx++];
4200 unsigned Index = Record[Idx++];
4201 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004202 TemplateTypeParmDecl *D
4203 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004204 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004205 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004206
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004207 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004208 unsigned Idx = 0;
4209 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004210 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004211 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004212 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004213 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004214 Canon = Context.getCanonicalType(Canon);
4215 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004216 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004218 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004219 unsigned Idx = 0;
4220 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004221 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004222 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004223 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004224 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004225 Args.reserve(NumArgs);
4226 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004227 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004228 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004229 Args.size(), Args.data());
4230 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004231
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004232 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004233 unsigned Idx = 0;
4234
4235 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004236 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004237 ArrayType::ArraySizeModifier ASM
4238 = (ArrayType::ArraySizeModifier)Record[Idx++];
4239 unsigned IndexTypeQuals = Record[Idx++];
4240
4241 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004242 Expr *NumElts = ReadExpr(*Loc.F);
4243 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004244
Douglas Gregor35942772011-09-09 21:34:22 +00004245 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004246 IndexTypeQuals, Brackets);
4247 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004248
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004249 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004250 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004251 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004252 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004253 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004254 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004255 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004256 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004257 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004258 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004259 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004260 else
Douglas Gregor35942772011-09-09 21:34:22 +00004261 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004262 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004263 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004264 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004265 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004266
4267 case TYPE_ATOMIC: {
4268 if (Record.size() != 1) {
4269 Error("Incorrect encoding of atomic type");
4270 return QualType();
4271 }
4272 QualType ValueType = readType(*Loc.F, Record, Idx);
4273 return Context.getAtomicType(ValueType);
4274 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004275 }
David Blaikie7530c032012-01-17 06:56:22 +00004276 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004277}
4278
Sebastian Redlc3632732010-10-05 15:59:54 +00004279class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004280 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004281 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004282 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004283 unsigned &Idx;
4284
Sebastian Redlc3632732010-10-05 15:59:54 +00004285 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4286 unsigned &I) {
4287 return Reader.ReadSourceLocation(F, R, I);
4288 }
4289
Douglas Gregor409448c2011-07-21 22:35:25 +00004290 template<typename T>
4291 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4292 return Reader.ReadDeclAs<T>(F, Record, Idx);
4293 }
4294
John McCalla1ee0c52009-10-16 21:56:05 +00004295public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004296 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004297 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004298 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004299 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004300
John McCall51bd8032009-10-18 01:05:36 +00004301 // We want compile-time assurance that we've enumerated all of
4302 // these, so unfortunately we have to declare them first, then
4303 // define them out-of-line.
4304#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004305#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004306 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004307#include "clang/AST/TypeLocNodes.def"
4308
John McCall51bd8032009-10-18 01:05:36 +00004309 void VisitFunctionTypeLoc(FunctionTypeLoc);
4310 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004311};
4312
John McCall51bd8032009-10-18 01:05:36 +00004313void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004314 // nothing to do
4315}
John McCall51bd8032009-10-18 01:05:36 +00004316void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004317 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004318 if (TL.needsExtraLocalData()) {
4319 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4320 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4321 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4322 TL.setModeAttr(Record[Idx++]);
4323 }
John McCalla1ee0c52009-10-16 21:56:05 +00004324}
John McCall51bd8032009-10-18 01:05:36 +00004325void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004326 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004327}
John McCall51bd8032009-10-18 01:05:36 +00004328void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004329 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004330}
John McCall51bd8032009-10-18 01:05:36 +00004331void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004332 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004333}
John McCall51bd8032009-10-18 01:05:36 +00004334void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004335 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004336}
John McCall51bd8032009-10-18 01:05:36 +00004337void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004338 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004339}
John McCall51bd8032009-10-18 01:05:36 +00004340void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004341 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004342 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004343}
John McCall51bd8032009-10-18 01:05:36 +00004344void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004345 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4346 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004347 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004348 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004349 else
John McCall51bd8032009-10-18 01:05:36 +00004350 TL.setSizeExpr(0);
4351}
4352void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4353 VisitArrayTypeLoc(TL);
4354}
4355void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4356 VisitArrayTypeLoc(TL);
4357}
4358void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4359 VisitArrayTypeLoc(TL);
4360}
4361void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4362 DependentSizedArrayTypeLoc TL) {
4363 VisitArrayTypeLoc(TL);
4364}
4365void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4366 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004367 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004368}
4369void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004370 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004371}
4372void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004373 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004374}
4375void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004376 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004377 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4378 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004379 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004380 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004381 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004382 }
4383}
4384void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4385 VisitFunctionTypeLoc(TL);
4386}
4387void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4388 VisitFunctionTypeLoc(TL);
4389}
John McCalled976492009-12-04 22:46:56 +00004390void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004391 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004392}
John McCall51bd8032009-10-18 01:05:36 +00004393void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004394 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004395}
4396void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004397 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4398 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4399 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004400}
4401void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004402 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4403 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4404 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4405 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004406}
4407void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004408 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004409}
Sean Huntca63c202011-05-24 22:41:36 +00004410void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4411 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4412 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4413 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4414 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4415}
Richard Smith34b41d92011-02-20 03:19:35 +00004416void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4417 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4418}
John McCall51bd8032009-10-18 01:05:36 +00004419void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004420 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004421}
4422void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004423 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004424}
John McCall9d156a72011-01-06 01:58:22 +00004425void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4426 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4427 if (TL.hasAttrOperand()) {
4428 SourceRange range;
4429 range.setBegin(ReadSourceLocation(Record, Idx));
4430 range.setEnd(ReadSourceLocation(Record, Idx));
4431 TL.setAttrOperandParensRange(range);
4432 }
4433 if (TL.hasAttrExprOperand()) {
4434 if (Record[Idx++])
4435 TL.setAttrExprOperand(Reader.ReadExpr(F));
4436 else
4437 TL.setAttrExprOperand(0);
4438 } else if (TL.hasAttrEnumOperand())
4439 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4440}
John McCall51bd8032009-10-18 01:05:36 +00004441void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004442 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004443}
John McCall49a832b2009-10-18 09:09:24 +00004444void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4445 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004446 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004447}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004448void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4449 SubstTemplateTypeParmPackTypeLoc TL) {
4450 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4451}
John McCall51bd8032009-10-18 01:05:36 +00004452void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4453 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004454 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004455 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4456 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4457 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004458 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4459 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004460 Reader.GetTemplateArgumentLocInfo(F,
4461 TL.getTypePtr()->getArg(i).getKind(),
4462 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004463}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004464void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4465 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4466 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4467}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004468void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004469 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004470 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004471}
John McCall3cb0ebd2010-03-10 03:28:59 +00004472void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004473 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004474}
Douglas Gregor4714c122010-03-31 17:34:00 +00004475void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004476 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004477 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004478 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004479}
John McCall33500952010-06-11 00:33:02 +00004480void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4481 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004482 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004483 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004484 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004485 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004486 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4487 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004488 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4489 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004490 Reader.GetTemplateArgumentLocInfo(F,
4491 TL.getTypePtr()->getArg(I).getKind(),
4492 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004493}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004494void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4495 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4496}
John McCall51bd8032009-10-18 01:05:36 +00004497void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004498 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004499}
4500void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4501 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004502 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4503 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004504 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004505 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004506}
John McCall54e14c42009-10-22 22:37:11 +00004507void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004508 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004509}
Eli Friedmanb001de72011-10-06 23:00:33 +00004510void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4511 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4512 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4513 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4514}
John McCalla1ee0c52009-10-16 21:56:05 +00004515
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004516TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004517 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004518 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004519 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004520 if (InfoTy.isNull())
4521 return 0;
4522
Douglas Gregor35942772011-09-09 21:34:22 +00004523 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004524 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004525 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004526 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004527 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004528}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004529
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004530QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004531 unsigned FastQuals = ID & Qualifiers::FastMask;
4532 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004533
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004534 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004535 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004536 switch ((PredefinedTypeIDs)Index) {
4537 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004538 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4539 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004540
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004541 case PREDEF_TYPE_CHAR_U_ID:
4542 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004543 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004544 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004545 break;
4546
Douglas Gregor35942772011-09-09 21:34:22 +00004547 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4548 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4549 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4550 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4551 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4552 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4553 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4554 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4555 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4556 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4557 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4558 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4559 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004560 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004561 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4562 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4563 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4564 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4565 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004566 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004567 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4568 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4569 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4570 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4571 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4572 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4573 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4574 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4575 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004576
4577 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004578 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004579 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004580
4581 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4582 T = Context.ARCUnbridgedCastTy;
4583 break;
4584
Meador Ingefb40e3f2012-07-01 15:57:25 +00004585 case PREDEF_TYPE_VA_LIST_TAG:
4586 T = Context.getVaListTagType();
4587 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004588
4589 case PREDEF_TYPE_BUILTIN_FN:
4590 T = Context.BuiltinFnTy;
4591 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004592 }
4593
4594 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004595 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004596 }
4597
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004598 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004599 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004600 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004601 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004602 if (TypesLoaded[Index].isNull())
4603 return QualType();
4604
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004605 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004606 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004607 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004608 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004609 }
Mike Stump1eb44332009-09-09 15:08:12 +00004610
John McCall0953e762009-09-24 19:53:00 +00004611 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004612}
4613
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004614QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004615 return GetType(getGlobalTypeID(F, LocalID));
4616}
4617
4618serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004619ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004620 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4621 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4622
4623 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4624 return LocalID;
4625
4626 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4627 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4628 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4629
4630 unsigned GlobalIndex = LocalIndex + I->second;
4631 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4632}
4633
John McCall833ca992009-10-29 08:12:44 +00004634TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004635ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004636 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004637 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004638 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004639 switch (Kind) {
4640 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004641 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004642 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004643 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004644 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004645 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4646 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004647 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004648 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004649 SourceLocation());
4650 }
4651 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004652 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4653 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004654 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004655 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004656 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004657 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004658 }
John McCall833ca992009-10-29 08:12:44 +00004659 case TemplateArgument::Null:
4660 case TemplateArgument::Integral:
4661 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004662 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004663 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004664 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004665 return TemplateArgumentLocInfo();
4666 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004667 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004668}
4669
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004670TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004671ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004672 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004673 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004674
4675 if (Arg.getKind() == TemplateArgument::Expression) {
4676 if (Record[Index++]) // bool InfoHasSameExpr.
4677 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4678 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004679 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004680 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004681}
4682
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004683Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004684 return GetDecl(ID);
4685}
4686
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004687uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004688 unsigned &Idx){
4689 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004690 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004691
Douglas Gregore92b8a12011-08-04 00:01:48 +00004692 unsigned LocalID = Record[Idx++];
4693 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004694}
4695
4696CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004697 RecordLocation Loc = getLocalBitOffset(Offset);
4698 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004699 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004700 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004701 ReadingKindTracker ReadingKind(Read_Decl, *this);
4702 RecordData Record;
4703 unsigned Code = Cursor.ReadCode();
4704 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4705 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4706 Error("Malformed AST file: missing C++ base specifiers");
4707 return 0;
4708 }
4709
4710 unsigned Idx = 0;
4711 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004712 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004713 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4714 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004715 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004716 return Bases;
4717}
4718
Douglas Gregor409448c2011-07-21 22:35:25 +00004719serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004720ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004721 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004722 return LocalID;
4723
4724 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004725 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004726 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4727
4728 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004729}
4730
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004731bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004732 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004733 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4734 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4735 return &M == I->second;
4736}
4737
Douglas Gregorcff9f262012-01-27 01:47:08 +00004738ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4739 if (!D->isFromASTFile())
4740 return 0;
4741 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4742 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4743 return I->second;
4744}
4745
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004746SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4747 if (ID < NUM_PREDEF_DECL_IDS)
4748 return SourceLocation();
4749
4750 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4751
4752 if (Index > DeclsLoaded.size()) {
4753 Error("declaration ID out-of-range for AST file");
4754 return SourceLocation();
4755 }
4756
4757 if (Decl *D = DeclsLoaded[Index])
4758 return D->getLocation();
4759
4760 unsigned RawLocation = 0;
4761 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4762 return ReadSourceLocation(*Rec.F, RawLocation);
4763}
4764
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004765Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004766 if (ID < NUM_PREDEF_DECL_IDS) {
4767 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004768 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004769 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004770
4771 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004772 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004773
4774 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004775 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004776
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004777 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004778 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004779
Douglas Gregor79d67262011-08-12 05:59:41 +00004780 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004781 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004782
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004783 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4784 return Context.getObjCProtocolDecl();
4785
Douglas Gregor772eeae2011-08-12 06:49:56 +00004786 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004787 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004788
4789 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004790 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004791
4792 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004793 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004794
4795 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4796 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004797 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004798 }
4799
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004800 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4801
Richard Smith2fbf3732011-12-20 04:39:57 +00004802 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004803 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004804 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004805 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004806 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004807
Douglas Gregorfd002a72011-12-16 22:37:11 +00004808 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004809 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004810 if (DeserializationListener)
4811 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4812 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004813
4814 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004815}
4816
Douglas Gregora1be2782011-12-17 23:38:30 +00004817DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4818 DeclID GlobalID) {
4819 if (GlobalID < NUM_PREDEF_DECL_IDS)
4820 return GlobalID;
4821
4822 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4823 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4824 ModuleFile *Owner = I->second;
4825
4826 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4827 = M.GlobalToLocalDeclIDs.find(Owner);
4828 if (Pos == M.GlobalToLocalDeclIDs.end())
4829 return 0;
4830
4831 return GlobalID - Owner->BaseDeclID + Pos->second;
4832}
4833
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004834serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004835 const RecordData &Record,
4836 unsigned &Idx) {
4837 if (Idx >= Record.size()) {
4838 Error("Corrupted AST file");
4839 return 0;
4840 }
4841
4842 return getGlobalDeclID(F, Record[Idx++]);
4843}
4844
Chris Lattner887e2b32009-04-27 05:46:25 +00004845/// \brief Resolve the offset of a statement into a statement.
4846///
4847/// This operation will read a new statement from the external
4848/// source each time it is called, and is meant to be used via a
4849/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004850Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004851 // Switch case IDs are per Decl.
4852 ClearSwitchCaseIDs();
4853
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004854 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004855 RecordLocation Loc = getLocalBitOffset(Offset);
4856 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4857 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004858}
4859
Douglas Gregor851c75a2011-08-24 21:27:34 +00004860namespace {
4861 class FindExternalLexicalDeclsVisitor {
4862 ASTReader &Reader;
4863 const DeclContext *DC;
4864 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004865
Douglas Gregor851c75a2011-08-24 21:27:34 +00004866 SmallVectorImpl<Decl*> &Decls;
4867 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4868
4869 public:
4870 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4871 bool (*isKindWeWant)(Decl::Kind),
4872 SmallVectorImpl<Decl*> &Decls)
4873 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4874 {
4875 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4876 PredefsVisited[I] = false;
4877 }
4878
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004879 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004880 if (Preorder)
4881 return false;
4882
4883 FindExternalLexicalDeclsVisitor *This
4884 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4885
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004886 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004887 = M.DeclContextInfos.find(This->DC);
4888 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4889 return false;
4890
4891 // Load all of the declaration IDs
4892 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4893 *IDE = ID + Info->second.NumLexicalDecls;
4894 ID != IDE; ++ID) {
4895 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4896 continue;
4897
4898 // Don't add predefined declarations to the lexical context more
4899 // than once.
4900 if (ID->second < NUM_PREDEF_DECL_IDS) {
4901 if (This->PredefsVisited[ID->second])
4902 continue;
4903
4904 This->PredefsVisited[ID->second] = true;
4905 }
4906
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004907 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4908 if (!This->DC->isDeclInLexicalTraversal(D))
4909 This->Decls.push_back(D);
4910 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004911 }
4912
4913 return false;
4914 }
4915 };
4916}
4917
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004918ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004919 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004920 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004921 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004922 // least. Walk all of the modules in the order they were loaded.
4923 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4924 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004925 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004926 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004927}
4928
Douglas Gregor0d95f772011-08-24 19:03:07 +00004929namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004930
4931class DeclIDComp {
4932 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004933 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004934
4935public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004936 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004937
4938 bool operator()(LocalDeclID L, LocalDeclID R) const {
4939 SourceLocation LHS = getLocation(L);
4940 SourceLocation RHS = getLocation(R);
4941 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4942 }
4943
4944 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4945 SourceLocation RHS = getLocation(R);
4946 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4947 }
4948
4949 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4950 SourceLocation LHS = getLocation(L);
4951 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4952 }
4953
4954 SourceLocation getLocation(LocalDeclID ID) const {
4955 return Reader.getSourceManager().getFileLoc(
4956 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4957 }
4958};
4959
4960}
4961
4962void ASTReader::FindFileRegionDecls(FileID File,
4963 unsigned Offset, unsigned Length,
4964 SmallVectorImpl<Decl *> &Decls) {
4965 SourceManager &SM = getSourceManager();
4966
4967 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4968 if (I == FileDeclIDs.end())
4969 return;
4970
4971 FileDeclsInfo &DInfo = I->second;
4972 if (DInfo.Decls.empty())
4973 return;
4974
4975 SourceLocation
4976 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4977 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4978
4979 DeclIDComp DIDComp(*this, *DInfo.Mod);
4980 ArrayRef<serialization::LocalDeclID>::iterator
4981 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4982 BeginLoc, DIDComp);
4983 if (BeginIt != DInfo.Decls.begin())
4984 --BeginIt;
4985
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004986 // If we are pointing at a top-level decl inside an objc container, we need
4987 // to backtrack until we find it otherwise we will fail to report that the
4988 // region overlaps with an objc container.
4989 while (BeginIt != DInfo.Decls.begin() &&
4990 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4991 ->isTopLevelDeclInObjCContainer())
4992 --BeginIt;
4993
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004994 ArrayRef<serialization::LocalDeclID>::iterator
4995 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4996 EndLoc, DIDComp);
4997 if (EndIt != DInfo.Decls.end())
4998 ++EndIt;
4999
5000 for (ArrayRef<serialization::LocalDeclID>::iterator
5001 DIt = BeginIt; DIt != EndIt; ++DIt)
5002 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5003}
5004
5005namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005006 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005007 /// declaration context.
5008 class DeclContextNameLookupVisitor {
5009 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005010 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005011 DeclarationName Name;
5012 SmallVectorImpl<NamedDecl *> &Decls;
5013
5014 public:
5015 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005016 SmallVectorImpl<const DeclContext *> &Contexts,
5017 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005018 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005019 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005020
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005021 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005022 DeclContextNameLookupVisitor *This
5023 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5024
5025 // Check whether we have any visible declaration information for
5026 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005027 ModuleFile::DeclContextInfosMap::iterator Info;
5028 bool FoundInfo = false;
5029 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5030 Info = M.DeclContextInfos.find(This->Contexts[I]);
5031 if (Info != M.DeclContextInfos.end() &&
5032 Info->second.NameLookupTableData) {
5033 FoundInfo = true;
5034 break;
5035 }
5036 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005037
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005038 if (!FoundInfo)
5039 return false;
5040
Douglas Gregor0d95f772011-08-24 19:03:07 +00005041 // Look for this name within this module.
5042 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005043 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005044 ASTDeclContextNameLookupTable::iterator Pos
5045 = LookupTable->find(This->Name);
5046 if (Pos == LookupTable->end())
5047 return false;
5048
5049 bool FoundAnything = false;
5050 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5051 for (; Data.first != Data.second; ++Data.first) {
5052 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5053 if (!ND)
5054 continue;
5055
5056 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005057 // A name might be null because the decl's redeclarable part is
5058 // currently read before reading its name. The lookup is triggered by
5059 // building that decl (likely indirectly), and so it is later in the
5060 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005061 continue;
5062 }
5063
5064 // Record this declaration.
5065 FoundAnything = true;
5066 This->Decls.push_back(ND);
5067 }
5068
5069 return FoundAnything;
5070 }
5071 };
5072}
5073
John McCall76bd1f32010-06-01 09:23:16 +00005074DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005075ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005076 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005077 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005078 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005079 if (!Name)
5080 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5081 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005082
Chris Lattner5f9e2722011-07-23 10:55:15 +00005083 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005084
5085 // Compute the declaration contexts we need to look into. Multiple such
5086 // declaration contexts occur when two declaration contexts from disjoint
5087 // modules get merged, e.g., when two namespaces with the same name are
5088 // independently defined in separate modules.
5089 SmallVector<const DeclContext *, 2> Contexts;
5090 Contexts.push_back(DC);
5091
5092 if (DC->isNamespace()) {
5093 MergedDeclsMap::iterator Merged
5094 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5095 if (Merged != MergedDecls.end()) {
5096 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5097 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5098 }
5099 }
5100
5101 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005102 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005103 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005104 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005105 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005106}
5107
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005108namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005109 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005110 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005111 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005112 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005113 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005114 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005115
5116 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005117 DeclContextAllNamesVisitor(ASTReader &Reader,
5118 SmallVectorImpl<const DeclContext *> &Contexts,
5119 llvm::DenseMap<DeclarationName,
5120 SmallVector<NamedDecl *, 8> > &Decls)
5121 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005122
5123 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005124 DeclContextAllNamesVisitor *This
5125 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005126
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005127 // Check whether we have any visible declaration information for
5128 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005129 ModuleFile::DeclContextInfosMap::iterator Info;
5130 bool FoundInfo = false;
5131 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5132 Info = M.DeclContextInfos.find(This->Contexts[I]);
5133 if (Info != M.DeclContextInfos.end() &&
5134 Info->second.NameLookupTableData) {
5135 FoundInfo = true;
5136 break;
5137 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005138 }
5139
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005140 if (!FoundInfo)
5141 return false;
5142
5143 ASTDeclContextNameLookupTable *LookupTable =
5144 Info->second.NameLookupTableData;
5145 bool FoundAnything = false;
5146 for (ASTDeclContextNameLookupTable::data_iterator
5147 I = LookupTable->data_begin(), E = LookupTable->data_end();
5148 I != E; ++I) {
5149 ASTDeclContextNameLookupTrait::data_type Data = *I;
5150 for (; Data.first != Data.second; ++Data.first) {
5151 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5152 *Data.first);
5153 if (!ND)
5154 continue;
5155
5156 // Record this declaration.
5157 FoundAnything = true;
5158 This->Decls[ND->getDeclName()].push_back(ND);
5159 }
5160 }
5161
5162 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005163 }
5164 };
5165}
5166
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005167void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005168 if (!DC->hasExternalVisibleStorage())
5169 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005170 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5171
5172 // Compute the declaration contexts we need to look into. Multiple such
5173 // declaration contexts occur when two declaration contexts from disjoint
5174 // modules get merged, e.g., when two namespaces with the same name are
5175 // independently defined in separate modules.
5176 SmallVector<const DeclContext *, 2> Contexts;
5177 Contexts.push_back(DC);
5178
5179 if (DC->isNamespace()) {
5180 MergedDeclsMap::iterator Merged
5181 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5182 if (Merged != MergedDecls.end()) {
5183 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5184 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5185 }
5186 }
5187
5188 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5189 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5190 ++NumVisibleDeclContextsRead;
5191
5192 for (llvm::DenseMap<DeclarationName,
5193 llvm::SmallVector<NamedDecl*, 8> >::iterator
5194 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5195 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5196 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005197 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005198}
5199
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005200/// \brief Under non-PCH compilation the consumer receives the objc methods
5201/// before receiving the implementation, and codegen depends on this.
5202/// We simulate this by deserializing and passing to consumer the methods of the
5203/// implementation before passing the deserialized implementation decl.
5204static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5205 ASTConsumer *Consumer) {
5206 assert(ImplD && Consumer);
5207
5208 for (ObjCImplDecl::method_iterator
5209 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005210 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005211
5212 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5213}
5214
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005215void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005216 assert(Consumer);
5217 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005218 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005219 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005220
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005221 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005222 }
5223}
5224
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005225void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5226 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5227 PassObjCImplDeclToConsumer(ImplD, Consumer);
5228 else
5229 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5230}
5231
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005232void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005233 this->Consumer = Consumer;
5234
Douglas Gregorfdd01722009-04-14 00:24:19 +00005235 if (!Consumer)
5236 return;
5237
5238 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005239 // Force deserialization of this decl, which will cause it to be queued for
5240 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005241 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005242 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005243 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005244
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005245 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005246}
5247
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005248void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005249 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005250
Mike Stump1eb44332009-09-09 15:08:12 +00005251 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005252 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005253 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005254 unsigned NumDeclsLoaded
5255 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5256 (Decl *)0);
5257 unsigned NumIdentifiersLoaded
5258 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5259 IdentifiersLoaded.end(),
5260 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005261 unsigned NumMacrosLoaded
5262 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5263 MacrosLoaded.end(),
5264 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005265 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005266 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5267 SelectorsLoaded.end(),
5268 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005269
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005270 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5271 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005272 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005273 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5274 NumSLocEntriesRead, TotalNumSLocEntries,
5275 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005276 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005277 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005278 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5279 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5280 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005281 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005282 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5283 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005284 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005285 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005286 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5287 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005288 if (!MacrosLoaded.empty())
5289 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5290 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5291 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005292 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005293 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005294 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5295 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005296 if (TotalNumStatements)
5297 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5298 NumStatementsRead, TotalNumStatements,
5299 ((float)NumStatementsRead/TotalNumStatements * 100));
5300 if (TotalNumMacros)
5301 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5302 NumMacrosRead, TotalNumMacros,
5303 ((float)NumMacrosRead/TotalNumMacros * 100));
5304 if (TotalLexicalDeclContexts)
5305 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5306 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5307 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5308 * 100));
5309 if (TotalVisibleDeclContexts)
5310 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5311 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5312 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5313 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005314 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005315 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005316 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5317 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005318 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005319 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005320 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005321 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005322 dump();
5323 std::fprintf(stderr, "\n");
5324}
5325
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005326template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005327static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005328dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005329 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005330 InitialCapacity> &Map) {
5331 if (Map.begin() == Map.end())
5332 return;
5333
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005334 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005335 llvm::errs() << Name << ":\n";
5336 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5337 I != IEnd; ++I) {
5338 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5339 << "\n";
5340 }
5341}
5342
Douglas Gregor23d7df52011-07-21 19:50:14 +00005343void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005344 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005345 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005346 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005347 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005348 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005349 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005350 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005351 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005352 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005353 dumpModuleIDMap("Global preprocessed entity map",
5354 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005355
5356 llvm::errs() << "\n*** PCH/Modules Loaded:";
5357 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5358 MEnd = ModuleMgr.end();
5359 M != MEnd; ++M)
5360 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005361}
5362
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005363/// Return the amount of memory used by memory buffers, breaking down
5364/// by heap-backed versus mmap'ed memory.
5365void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005366 for (ModuleConstIterator I = ModuleMgr.begin(),
5367 E = ModuleMgr.end(); I != E; ++I) {
5368 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005369 size_t bytes = buf->getBufferSize();
5370 switch (buf->getBufferKind()) {
5371 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5372 sizes.malloc_bytes += bytes;
5373 break;
5374 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5375 sizes.mmap_bytes += bytes;
5376 break;
5377 }
5378 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005379 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005380}
5381
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005382void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005383 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005384 S.ExternalSource = this;
5385
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005386 // Makes sure any declarations that were deserialized "too early"
5387 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005388 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005389 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5390 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005391 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005392 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005393
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005394 // Load the offsets of the declarations that Sema references.
5395 // They will be lazily deserialized when needed.
5396 if (!SemaDeclRefs.empty()) {
5397 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005398 if (!SemaObj->StdNamespace)
5399 SemaObj->StdNamespace = SemaDeclRefs[0];
5400 if (!SemaObj->StdBadAlloc)
5401 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005402 }
5403
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005404 if (!FPPragmaOptions.empty()) {
5405 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5406 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5407 }
5408
5409 if (!OpenCLExtensions.empty()) {
5410 unsigned I = 0;
5411#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5412#include "clang/Basic/OpenCLExtensions.def"
5413
5414 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5415 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005416}
5417
Douglas Gregor211f6e82011-08-20 04:39:52 +00005418IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor057df202012-01-18 20:56:22 +00005419 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5420 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005421 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005422 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005423 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005424 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005425}
5426
Douglas Gregor95f42922010-10-14 22:11:03 +00005427namespace clang {
5428 /// \brief An identifier-lookup iterator that enumerates all of the
5429 /// identifiers stored within a set of AST files.
5430 class ASTIdentifierIterator : public IdentifierIterator {
5431 /// \brief The AST reader whose identifiers are being enumerated.
5432 const ASTReader &Reader;
5433
5434 /// \brief The current index into the chain of AST files stored in
5435 /// the AST reader.
5436 unsigned Index;
5437
5438 /// \brief The current position within the identifier lookup table
5439 /// of the current AST file.
5440 ASTIdentifierLookupTable::key_iterator Current;
5441
5442 /// \brief The end position within the identifier lookup table of
5443 /// the current AST file.
5444 ASTIdentifierLookupTable::key_iterator End;
5445
5446 public:
5447 explicit ASTIdentifierIterator(const ASTReader &Reader);
5448
Chris Lattner5f9e2722011-07-23 10:55:15 +00005449 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005450 };
5451}
5452
5453ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005454 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005455 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005456 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005457 Current = IdTable->key_begin();
5458 End = IdTable->key_end();
5459}
5460
Chris Lattner5f9e2722011-07-23 10:55:15 +00005461StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005462 while (Current == End) {
5463 // If we have exhausted all of our AST files, we're done.
5464 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005465 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005466
5467 --Index;
5468 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005469 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5470 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005471 Current = IdTable->key_begin();
5472 End = IdTable->key_end();
5473 }
5474
5475 // We have any identifiers remaining in the current AST file; return
5476 // the next one.
5477 std::pair<const char*, unsigned> Key = *Current;
5478 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005479 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005480}
5481
5482IdentifierIterator *ASTReader::getIdentifiers() const {
5483 return new ASTIdentifierIterator(*this);
5484}
5485
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005486namespace clang { namespace serialization {
5487 class ReadMethodPoolVisitor {
5488 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005489 Selector Sel;
5490 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005491 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5492 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005493
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005494 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005495 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5496 unsigned PriorGeneration)
5497 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005498
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005499 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005500 ReadMethodPoolVisitor *This
5501 = static_cast<ReadMethodPoolVisitor *>(UserData);
5502
5503 if (!M.SelectorLookupTable)
5504 return false;
5505
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005506 // If we've already searched this module file, skip it now.
5507 if (M.Generation <= This->PriorGeneration)
5508 return true;
5509
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005510 ASTSelectorLookupTable *PoolTable
5511 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5512 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5513 if (Pos == PoolTable->end())
5514 return false;
5515
5516 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005517 // FIXME: Not quite happy with the statistics here. We probably should
5518 // disable this tracking when called via LoadSelector.
5519 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005520 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005521 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005522 if (This->Reader.DeserializationListener)
5523 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5524 This->Sel);
5525
5526 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5527 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5528 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005529 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005530
5531 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005532 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5533 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005534 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005535
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005536 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005537 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5538 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005539 }
5540 };
5541} } // end namespace clang::serialization
5542
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005543/// \brief Add the given set of methods to the method list.
5544static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5545 ObjCMethodList &List) {
5546 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5547 S.addMethodToGlobalList(&List, Methods[I]);
5548 }
5549}
5550
5551void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005552 // Get the selector generation and update it to the current generation.
5553 unsigned &Generation = SelectorGeneration[Sel];
5554 unsigned PriorGeneration = Generation;
5555 Generation = CurrentGeneration;
5556
5557 // Search for methods defined with this selector.
5558 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005559 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005560
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005561 if (Visitor.getInstanceMethods().empty() &&
5562 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005563 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005564 return;
5565 }
5566
5567 if (!getSema())
5568 return;
5569
5570 Sema &S = *getSema();
5571 Sema::GlobalMethodPool::iterator Pos
5572 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5573
5574 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5575 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005576}
5577
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005578void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005579 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005580 Namespaces.clear();
5581
5582 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5583 if (NamespaceDecl *Namespace
5584 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5585 Namespaces.push_back(Namespace);
5586 }
5587}
5588
Douglas Gregora8623202011-07-27 20:58:46 +00005589void ASTReader::ReadTentativeDefinitions(
5590 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5591 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5592 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5593 if (Var)
5594 TentativeDefs.push_back(Var);
5595 }
5596 TentativeDefinitions.clear();
5597}
5598
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005599void ASTReader::ReadUnusedFileScopedDecls(
5600 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5601 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5602 DeclaratorDecl *D
5603 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5604 if (D)
5605 Decls.push_back(D);
5606 }
5607 UnusedFileScopedDecls.clear();
5608}
5609
Douglas Gregor0129b562011-07-27 21:57:17 +00005610void ASTReader::ReadDelegatingConstructors(
5611 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5612 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5613 CXXConstructorDecl *D
5614 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5615 if (D)
5616 Decls.push_back(D);
5617 }
5618 DelegatingCtorDecls.clear();
5619}
5620
Douglas Gregord58a0a52011-07-28 00:39:29 +00005621void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5622 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5623 TypedefNameDecl *D
5624 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5625 if (D)
5626 Decls.push_back(D);
5627 }
5628 ExtVectorDecls.clear();
5629}
5630
Douglas Gregora126f172011-07-28 00:53:40 +00005631void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5632 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5633 CXXRecordDecl *D
5634 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5635 if (D)
5636 Decls.push_back(D);
5637 }
5638 DynamicClasses.clear();
5639}
5640
Douglas Gregorec12ce22011-07-28 14:20:37 +00005641void
5642ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5643 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5644 NamedDecl *D
5645 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5646 if (D)
5647 Decls.push_back(D);
5648 }
5649 LocallyScopedExternalDecls.clear();
5650}
5651
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005652void ASTReader::ReadReferencedSelectors(
5653 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5654 if (ReferencedSelectorsData.empty())
5655 return;
5656
5657 // If there are @selector references added them to its pool. This is for
5658 // implementation of -Wselector.
5659 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5660 unsigned I = 0;
5661 while (I < DataSize) {
5662 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5663 SourceLocation SelLoc
5664 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5665 Sels.push_back(std::make_pair(Sel, SelLoc));
5666 }
5667 ReferencedSelectorsData.clear();
5668}
5669
Douglas Gregor31e37b22011-07-28 18:09:57 +00005670void ASTReader::ReadWeakUndeclaredIdentifiers(
5671 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5672 if (WeakUndeclaredIdentifiers.empty())
5673 return;
5674
5675 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5676 IdentifierInfo *WeakId
5677 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5678 IdentifierInfo *AliasId
5679 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5680 SourceLocation Loc
5681 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5682 bool Used = WeakUndeclaredIdentifiers[I++];
5683 WeakInfo WI(AliasId, Loc);
5684 WI.setUsed(Used);
5685 WeakIDs.push_back(std::make_pair(WeakId, WI));
5686 }
5687 WeakUndeclaredIdentifiers.clear();
5688}
5689
Douglas Gregordfe65432011-07-28 19:11:31 +00005690void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5691 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5692 ExternalVTableUse VT;
5693 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5694 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5695 VT.DefinitionRequired = VTableUses[Idx++];
5696 VTables.push_back(VT);
5697 }
5698
5699 VTableUses.clear();
5700}
5701
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005702void ASTReader::ReadPendingInstantiations(
5703 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5704 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5705 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5706 SourceLocation Loc
5707 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005708
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005709 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005710 }
5711 PendingInstantiations.clear();
5712}
5713
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005714void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005715 // It would be complicated to avoid reading the methods anyway. So don't.
5716 ReadMethodPool(Sel);
5717}
5718
Douglas Gregor95eab172011-07-28 20:55:49 +00005719void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005720 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005721 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005722 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005723 if (DeserializationListener)
5724 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005725}
5726
Douglas Gregord89275b2009-07-06 18:54:52 +00005727/// \brief Set the globally-visible declarations associated with the given
5728/// identifier.
5729///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005730/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005731/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005732/// them.
5733///
5734/// \param II an IdentifierInfo that refers to one or more globally-visible
5735/// declarations.
5736///
5737/// \param DeclIDs the set of declaration IDs with the name @p II that are
5738/// visible at global scope.
5739///
5740/// \param Nonrecursive should be true to indicate that the caller knows that
5741/// this call is non-recursive, and therefore the globally-visible declarations
5742/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005743void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005744ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005745 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005746 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005747 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005748 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5749 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5750 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005751 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005752 return;
5753 }
Mike Stump1eb44332009-09-09 15:08:12 +00005754
Douglas Gregord89275b2009-07-06 18:54:52 +00005755 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5756 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5757 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005758 // Introduce this declaration into the translation-unit scope
5759 // and add it to the declaration chain for this identifier, so
5760 // that (unqualified) name lookup will find it.
5761 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005762 } else {
5763 // Queue this declaration so that it will be added to the
5764 // translation unit scope and identifier's declaration chain
5765 // once a Sema object is known.
5766 PreloadedDecls.push_back(D);
5767 }
5768 }
5769}
5770
Douglas Gregor95eab172011-07-28 20:55:49 +00005771IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005772 if (ID == 0)
5773 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005774
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005775 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005776 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005777 return 0;
5778 }
Mike Stump1eb44332009-09-09 15:08:12 +00005779
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005780 ID -= 1;
5781 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005782 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5783 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005784 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005785 unsigned Index = ID - M->BaseIdentifierID;
5786 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005787
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005788 // All of the strings in the AST file are preceded by a 16-bit length.
5789 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005790 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5791 // unsigned integers. This is important to avoid integer overflow when
5792 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005793 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005794 unsigned StrLen = (((unsigned) StrLenPtr[0])
5795 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005796 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005797 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005798 if (DeserializationListener)
5799 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005800 }
Mike Stump1eb44332009-09-09 15:08:12 +00005801
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005802 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005803}
5804
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005805IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005806 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5807}
5808
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005809IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005810 if (LocalID < NUM_PREDEF_IDENT_IDS)
5811 return LocalID;
5812
5813 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5814 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5815 assert(I != M.IdentifierRemap.end()
5816 && "Invalid index into identifier index remap");
5817
5818 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005819}
5820
Douglas Gregora8235d62012-10-09 23:05:51 +00005821MacroInfo *ASTReader::getMacro(MacroID ID) {
5822 if (ID == 0)
5823 return 0;
5824
5825 if (MacrosLoaded.empty()) {
5826 Error("no macro table in AST file");
5827 return 0;
5828 }
5829
5830 ID -= NUM_PREDEF_MACRO_IDS;
5831 if (!MacrosLoaded[ID]) {
5832 GlobalMacroMapType::iterator I
5833 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
5834 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
5835 ModuleFile *M = I->second;
5836 unsigned Index = ID - M->BaseMacroID;
5837 ReadMacroRecord(*M, M->MacroOffsets[Index]);
5838 }
5839
5840 return MacrosLoaded[ID];
5841}
5842
5843MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
5844 if (LocalID < NUM_PREDEF_MACRO_IDS)
5845 return LocalID;
5846
5847 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5848 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
5849 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
5850
5851 return LocalID + I->second;
5852}
5853
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005854bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005855 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005856}
5857
Douglas Gregor26ced122011-12-01 00:59:36 +00005858serialization::SubmoduleID
5859ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5860 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5861 return LocalID;
5862
5863 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5864 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5865 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005866 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00005867
5868 return LocalID + I->second;
5869}
5870
5871Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5872 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5873 assert(GlobalID == 0 && "Unhandled global submodule ID");
5874 return 0;
5875 }
5876
5877 if (GlobalID > SubmodulesLoaded.size()) {
5878 Error("submodule ID out of range in AST file");
5879 return 0;
5880 }
5881
5882 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5883}
5884
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005885Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005886 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5887}
5888
5889Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005890 if (ID == 0)
5891 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005892
Sebastian Redl725cd962010-08-04 20:40:17 +00005893 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005894 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005895 return Selector();
5896 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005897
Sebastian Redl725cd962010-08-04 20:40:17 +00005898 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005899 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005900 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5901 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005902 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005903 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005904 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005905 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005906 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005907 if (DeserializationListener)
5908 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005909 }
5910
Sebastian Redl725cd962010-08-04 20:40:17 +00005911 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005912}
5913
Douglas Gregor8451ec72011-07-28 14:41:43 +00005914Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005915 return DecodeSelector(ID);
5916}
5917
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005918uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005919 // ID 0 (the null selector) is considered an external selector.
5920 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005921}
5922
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005923serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005924ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005925 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5926 return LocalID;
5927
5928 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5929 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5930 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005931 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005932
5933 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005934}
5935
Mike Stump1eb44332009-09-09 15:08:12 +00005936DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005937ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005938 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005939 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5940 switch (Kind) {
5941 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005942 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005943
5944 case DeclarationName::ObjCZeroArgSelector:
5945 case DeclarationName::ObjCOneArgSelector:
5946 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005947 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005948
5949 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005950 return Context.DeclarationNames.getCXXConstructorName(
5951 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005952
5953 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005954 return Context.DeclarationNames.getCXXDestructorName(
5955 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005956
5957 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005958 return Context.DeclarationNames.getCXXConversionFunctionName(
5959 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005960
5961 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005962 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005963 (OverloadedOperatorKind)Record[Idx++]);
5964
Sean Hunt3e518bd2009-11-29 07:34:05 +00005965 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005966 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005967 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005968
Douglas Gregor2cf26342009-04-09 22:27:44 +00005969 case DeclarationName::CXXUsingDirective:
5970 return DeclarationName::getUsingDirectiveName();
5971 }
5972
David Blaikie7530c032012-01-17 06:56:22 +00005973 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005974}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005975
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005976void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005977 DeclarationNameLoc &DNLoc,
5978 DeclarationName Name,
5979 const RecordData &Record, unsigned &Idx) {
5980 switch (Name.getNameKind()) {
5981 case DeclarationName::CXXConstructorName:
5982 case DeclarationName::CXXDestructorName:
5983 case DeclarationName::CXXConversionFunctionName:
5984 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5985 break;
5986
5987 case DeclarationName::CXXOperatorName:
5988 DNLoc.CXXOperatorName.BeginOpNameLoc
5989 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5990 DNLoc.CXXOperatorName.EndOpNameLoc
5991 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5992 break;
5993
5994 case DeclarationName::CXXLiteralOperatorName:
5995 DNLoc.CXXLiteralOperatorName.OpNameLoc
5996 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5997 break;
5998
5999 case DeclarationName::Identifier:
6000 case DeclarationName::ObjCZeroArgSelector:
6001 case DeclarationName::ObjCOneArgSelector:
6002 case DeclarationName::ObjCMultiArgSelector:
6003 case DeclarationName::CXXUsingDirective:
6004 break;
6005 }
6006}
6007
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006008void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006009 DeclarationNameInfo &NameInfo,
6010 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006011 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006012 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6013 DeclarationNameLoc DNLoc;
6014 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6015 NameInfo.setInfo(DNLoc);
6016}
6017
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006018void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006019 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006020 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006021 unsigned NumTPLists = Record[Idx++];
6022 Info.NumTemplParamLists = NumTPLists;
6023 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006024 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006025 for (unsigned i=0; i != NumTPLists; ++i)
6026 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6027 }
6028}
6029
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006030TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006031ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006032 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006033 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006034 switch (Kind) {
6035 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006036 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006037
6038 case TemplateName::OverloadedTemplate: {
6039 unsigned size = Record[Idx++];
6040 UnresolvedSet<8> Decls;
6041 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006042 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006043
Douglas Gregor35942772011-09-09 21:34:22 +00006044 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006045 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006046
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006047 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006048 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006049 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006050 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006051 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006052 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006053
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006054 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006055 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006056 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006057 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006058 GetIdentifierInfo(F, Record,
6059 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006060 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006061 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006062 }
John McCall14606042011-06-30 08:33:18 +00006063
6064 case TemplateName::SubstTemplateTemplateParm: {
6065 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006066 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006067 if (!param) return TemplateName();
6068 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006069 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006070 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006071
6072 case TemplateName::SubstTemplateTemplateParmPack: {
6073 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006074 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006075 if (!Param)
6076 return TemplateName();
6077
6078 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6079 if (ArgPack.getKind() != TemplateArgument::Pack)
6080 return TemplateName();
6081
Douglas Gregor35942772011-09-09 21:34:22 +00006082 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006083 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006084 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006085
David Blaikieb219cfc2011-09-23 05:06:16 +00006086 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006087}
6088
6089TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006090ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006091 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006092 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6093 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006094 case TemplateArgument::Null:
6095 return TemplateArgument();
6096 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006097 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006098 case TemplateArgument::Declaration: {
6099 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6100 bool ForReferenceParam = Record[Idx++];
6101 return TemplateArgument(D, ForReferenceParam);
6102 }
6103 case TemplateArgument::NullPtr:
6104 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006105 case TemplateArgument::Integral: {
6106 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006107 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006108 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006109 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006110 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006111 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006112 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006113 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006114 llvm::Optional<unsigned> NumTemplateExpansions;
6115 if (unsigned NumExpansions = Record[Idx++])
6116 NumTemplateExpansions = NumExpansions - 1;
6117 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006118 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006119 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006120 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006121 case TemplateArgument::Pack: {
6122 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006123 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006124 for (unsigned I = 0; I != NumArgs; ++I)
6125 Args[I] = ReadTemplateArgument(F, Record, Idx);
6126 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006127 }
6128 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006129
David Blaikieb219cfc2011-09-23 05:06:16 +00006130 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006131}
6132
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006133TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006134ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006135 const RecordData &Record, unsigned &Idx) {
6136 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6137 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6138 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006139
6140 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006141 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006142 Params.reserve(NumParams);
6143 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006144 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006145
6146 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006147 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006148 Params.data(), Params.size(), RAngleLoc);
6149 return TemplateParams;
6150}
6151
6152void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006153ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006154ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006155 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006156 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006157 unsigned NumTemplateArgs = Record[Idx++];
6158 TemplArgs.reserve(NumTemplateArgs);
6159 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006160 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006161}
6162
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006163/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006164void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006165 const RecordData &Record, unsigned &Idx) {
6166 unsigned NumDecls = Record[Idx++];
6167 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006168 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006169 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6170 Set.addDecl(D, AS);
6171 }
6172}
6173
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006174CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006175ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006176 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006177 bool isVirtual = static_cast<bool>(Record[Idx++]);
6178 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6179 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006180 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006181 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6182 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006183 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006184 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006185 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006186 Result.setInheritConstructors(inheritConstructors);
6187 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006188}
6189
Sean Huntcbb67482011-01-08 20:30:50 +00006190std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006191ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006192 unsigned &Idx) {
6193 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006194 unsigned NumInitializers = Record[Idx++];
6195 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006196 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006197 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006198 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006199 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006200 bool IsBaseVirtual = false;
6201 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006202 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006203
Sean Hunt156b6402011-05-04 01:19:08 +00006204 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6205 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006206 case CTOR_INITIALIZER_BASE:
6207 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006208 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006209 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006210
6211 case CTOR_INITIALIZER_DELEGATING:
6212 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006213 break;
6214
6215 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006216 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006217 break;
6218
6219 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006220 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006221 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006222 }
Sean Hunt156b6402011-05-04 01:19:08 +00006223
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006224 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006225 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006226 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6227 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006228 bool IsWritten = Record[Idx++];
6229 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006230 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006231 if (IsWritten) {
6232 SourceOrderOrNumArrayIndices = Record[Idx++];
6233 } else {
6234 SourceOrderOrNumArrayIndices = Record[Idx++];
6235 Indices.reserve(SourceOrderOrNumArrayIndices);
6236 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006237 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006238 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006239
Sean Huntcbb67482011-01-08 20:30:50 +00006240 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006241 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006242 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006243 LParenLoc, Init, RParenLoc,
6244 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006245 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006246 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6247 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006248 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006249 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006250 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006251 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006252 else
Douglas Gregor35942772011-09-09 21:34:22 +00006253 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006254 MemberOrEllipsisLoc, LParenLoc,
6255 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006256 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006257 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006258 LParenLoc, Init, RParenLoc,
6259 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006260 }
6261
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006262 if (IsWritten)
6263 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006264 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006265 }
6266 }
6267
Sean Huntcbb67482011-01-08 20:30:50 +00006268 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006269}
6270
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006271NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006272ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006273 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006274 unsigned N = Record[Idx++];
6275 NestedNameSpecifier *NNS = 0, *Prev = 0;
6276 for (unsigned I = 0; I != N; ++I) {
6277 NestedNameSpecifier::SpecifierKind Kind
6278 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6279 switch (Kind) {
6280 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006281 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006282 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006283 break;
6284 }
6285
6286 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006287 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006288 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006289 break;
6290 }
6291
Douglas Gregor14aba762011-02-24 02:36:08 +00006292 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006293 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006294 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006295 break;
6296 }
6297
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006298 case NestedNameSpecifier::TypeSpec:
6299 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006300 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006301 if (!T)
6302 return 0;
6303
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006304 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006305 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006306 break;
6307 }
6308
6309 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006310 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006311 // No associated value, and there can't be a prefix.
6312 break;
6313 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006314 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006315 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006316 }
6317 return NNS;
6318}
6319
Douglas Gregordc355712011-02-25 00:36:19 +00006320NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006321ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006322 unsigned &Idx) {
6323 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006324 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006325 for (unsigned I = 0; I != N; ++I) {
6326 NestedNameSpecifier::SpecifierKind Kind
6327 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6328 switch (Kind) {
6329 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006330 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006331 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006332 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006333 break;
6334 }
6335
6336 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006337 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006338 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006339 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006340 break;
6341 }
6342
6343 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006344 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006345 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006346 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006347 break;
6348 }
6349
6350 case NestedNameSpecifier::TypeSpec:
6351 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006352 bool Template = Record[Idx++];
6353 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6354 if (!T)
6355 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006356 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006357
6358 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006359 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006360 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6361 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006362 break;
6363 }
6364
6365 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006366 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006367 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006368 break;
6369 }
6370 }
Douglas Gregordc355712011-02-25 00:36:19 +00006371 }
6372
Douglas Gregor35942772011-09-09 21:34:22 +00006373 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006374}
6375
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006376SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006377ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006378 unsigned &Idx) {
6379 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6380 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006381 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006382}
6383
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006384/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006385llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006386 unsigned BitWidth = Record[Idx++];
6387 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6388 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6389 Idx += NumWords;
6390 return Result;
6391}
6392
6393/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006394llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006395 bool isUnsigned = Record[Idx++];
6396 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6397}
6398
Douglas Gregor17fc2232009-04-14 21:55:33 +00006399/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006400llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006401 return llvm::APFloat(ReadAPInt(Record, Idx));
6402}
6403
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006404// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006405std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006406 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006407 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006408 Idx += Len;
6409 return Result;
6410}
6411
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006412VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6413 unsigned &Idx) {
6414 unsigned Major = Record[Idx++];
6415 unsigned Minor = Record[Idx++];
6416 unsigned Subminor = Record[Idx++];
6417 if (Minor == 0)
6418 return VersionTuple(Major);
6419 if (Subminor == 0)
6420 return VersionTuple(Major, Minor - 1);
6421 return VersionTuple(Major, Minor - 1, Subminor - 1);
6422}
6423
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006424CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006425 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006426 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006427 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006428 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006429}
6430
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006431DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006432 return Diag(SourceLocation(), DiagID);
6433}
6434
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006435DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006436 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006437}
Douglas Gregor025452f2009-04-17 00:04:06 +00006438
Douglas Gregor668c1a42009-04-21 22:25:48 +00006439/// \brief Retrieve the identifier table associated with the
6440/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006441IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006442 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006443}
6444
Douglas Gregor025452f2009-04-17 00:04:06 +00006445/// \brief Record that the given ID maps to the given switch-case
6446/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006447void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006448 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6449 "Already have a SwitchCase with this ID");
6450 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006451}
6452
6453/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006454SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006455 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6456 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006457}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006458
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006459void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006460 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006461}
6462
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006463void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006464 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006465 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6466 serialization::ModuleFile *> >::iterator
6467 I = CommentsCursors.begin(),
6468 E = CommentsCursors.end();
6469 I != E; ++I) {
6470 llvm::BitstreamCursor &Cursor = I->first;
6471 serialization::ModuleFile &F = *I->second;
6472 SavedStreamPosition SavedPosition(Cursor);
6473
6474 RecordData Record;
6475 while (true) {
6476 unsigned Code = Cursor.ReadCode();
6477 if (Code == llvm::bitc::END_BLOCK)
6478 break;
6479
6480 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6481 // No known subblocks, always skip them.
6482 Cursor.ReadSubBlockID();
6483 if (Cursor.SkipBlock()) {
6484 Error("malformed block record in AST file");
6485 return;
6486 }
6487 continue;
6488 }
6489
6490 if (Code == llvm::bitc::DEFINE_ABBREV) {
6491 Cursor.ReadAbbrevRecord();
6492 continue;
6493 }
6494
6495 // Read a record.
6496 Record.clear();
6497 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006498 case COMMENTS_RAW_COMMENT: {
6499 unsigned Idx = 0;
6500 SourceRange SR = ReadSourceRange(F, Record, Idx);
6501 RawComment::CommentKind Kind =
6502 (RawComment::CommentKind) Record[Idx++];
6503 bool IsTrailingComment = Record[Idx++];
6504 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006505 Comments.push_back(new (Context) RawComment(SR, Kind,
6506 IsTrailingComment,
6507 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006508 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006509 }
6510 }
6511 }
6512 }
6513 Context.Comments.addCommentsToFront(Comments);
6514}
6515
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006516void ASTReader::finishPendingActions() {
Douglas Gregorcff9f262012-01-27 01:47:08 +00006517 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006518 // If any identifiers with corresponding top-level declarations have
6519 // been loaded, load those declarations now.
6520 while (!PendingIdentifierInfos.empty()) {
6521 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6522 PendingIdentifierInfos.front().DeclIDs, true);
6523 PendingIdentifierInfos.pop_front();
6524 }
6525
Douglas Gregora1be2782011-12-17 23:38:30 +00006526 // Load pending declaration chains.
6527 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6528 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006529 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006530 }
6531 PendingDeclChains.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006532 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006533
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006534 // If we deserialized any C++ or Objective-C class definitions, any
6535 // Objective-C protocol definitions, or any redeclarable templates, make sure
6536 // that all redeclarations point to the definitions. Note that this can only
6537 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006538 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6539 DEnd = PendingDefinitions.end();
6540 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006541 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6542 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6543 // Make sure that the TagType points at the definition.
6544 const_cast<TagType*>(TagT)->decl = TD;
6545 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006546
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006547 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6548 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6549 REnd = RD->redecls_end();
6550 R != REnd; ++R)
6551 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6552
6553 }
6554
Douglas Gregorfc529f72011-12-19 19:00:47 +00006555 continue;
6556 }
6557
Douglas Gregor1d784b22012-01-01 19:51:50 +00006558 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006559 // Make sure that the ObjCInterfaceType points at the definition.
6560 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6561 ->Decl = ID;
6562
Douglas Gregor1d784b22012-01-01 19:51:50 +00006563 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6564 REnd = ID->redecls_end();
6565 R != REnd; ++R)
6566 R->Data = ID->Data;
6567
6568 continue;
6569 }
6570
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006571 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6572 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6573 REnd = PD->redecls_end();
6574 R != REnd; ++R)
6575 R->Data = PD->Data;
6576
6577 continue;
6578 }
6579
6580 RedeclarableTemplateDecl *RTD
6581 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6582 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6583 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006584 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006585 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006586 }
6587 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006588
6589 // Load the bodies of any functions or methods we've encountered. We do
6590 // this now (delayed) so that we can be sure that the declaration chains
6591 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006592 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6593 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006594 PB != PBEnd; ++PB) {
6595 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6596 // FIXME: Check for =delete/=default?
6597 // FIXME: Complain about ODR violations here?
6598 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6599 FD->setLazyBody(PB->second);
6600 continue;
6601 }
6602
6603 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6604 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6605 MD->setLazyBody(PB->second);
6606 }
6607 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006608}
6609
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006610void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006611 assert(NumCurrentElementsDeserializing &&
6612 "FinishedDeserializing not paired with StartedDeserializing");
6613 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006614 // We decrease NumCurrentElementsDeserializing only after pending actions
6615 // are finished, to avoid recursively re-calling finishPendingActions().
6616 finishPendingActions();
6617 }
6618 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006619
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006620 if (NumCurrentElementsDeserializing == 0 &&
6621 Consumer && !PassingDeclsToConsumer) {
6622 // Guard variable to avoid recursively redoing the process of passing
6623 // decls to consumer.
6624 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6625 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006626
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006627 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006628 // We are not in recursive loading, so it's safe to pass the "interesting"
6629 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006630 Decl *D = InterestingDecls.front();
6631 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006632 PassInterestingDeclToConsumer(D);
6633 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006634 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006635}
Douglas Gregor501c1032010-08-19 00:28:17 +00006636
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006637ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006638 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006639 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006640 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6641 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006642 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006643 Consumer(0), ModuleMgr(PP.getFileManager()),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006644 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006645 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006646 DisableStatCache(DisableStatCache),
6647 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006648 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6649 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006650 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006651 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6652 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6653 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6654 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006655 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6656 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006657 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006658 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006659{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006660 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006661}
6662
Sebastian Redle1dde812010-08-24 00:50:04 +00006663ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006664 for (DeclContextVisibleUpdatesPending::iterator
6665 I = PendingVisibleUpdates.begin(),
6666 E = PendingVisibleUpdates.end();
6667 I != E; ++I) {
6668 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6669 F = I->second.end();
6670 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006671 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006672 }
6673}