blob: 6e3eee953361b05da0c278f43a36a3930c29948f [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
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +000066PCHValidator::ReadLanguageOptions(const ModuleFile &M,
67 const LangOptions &LangOpts) {
David Blaikie4e4d0842012-03-11 07:00:24 +000068 const LangOptions &PPLangOpts = PP.getLangOpts();
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000069
70#define LANGOPT(Name, Bits, Default, Description) \
71 if (PPLangOpts.Name != LangOpts.Name) { \
72 Reader.Diag(diag::err_pch_langopt_mismatch) \
73 << Description << LangOpts.Name << PPLangOpts.Name; \
74 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000075 }
76
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000077#define VALUE_LANGOPT(Name, Bits, Default, Description) \
78 if (PPLangOpts.Name != LangOpts.Name) { \
79 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
80 << Description; \
81 return true; \
82}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000084#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
85 if (PPLangOpts.get##Name() != LangOpts.get##Name()) { \
86 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
87 << Description; \
88 return true; \
89 }
90
91#define BENIGN_LANGOPT(Name, Bits, Default, Description)
92#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
93#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +000094
95 if (PPLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
96 Reader.Diag(diag::err_pch_langopt_value_mismatch)
97 << "target Objective-C runtime";
98 return true;
99 }
Douglas Gregor7d5e81b2011-09-13 18:26:39 +0000100
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000101 return false;
102}
103
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000104bool PCHValidator::ReadTargetTriple(const ModuleFile &M, StringRef Triple) {
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000105 if (Triple == PP.getTargetInfo().getTriple().str())
106 return false;
107
108 Reader.Diag(diag::warn_pch_target_triple)
109 << Triple << PP.getTargetInfo().getTriple().str();
110 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000111}
112
Benjamin Kramer54353f42010-11-25 18:29:30 +0000113namespace {
114 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000116 };
117 struct EmptyBlock {
118 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
119 };
120}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000121
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000123 PCHPredefinesBlocks R) {
124 // First, sum up the lengths.
125 unsigned LL = 0, RL = 0;
126 for (unsigned I = 0, N = L.size(); I != N; ++I) {
127 LL += L[I].size();
128 }
129 for (unsigned I = 0, N = R.size(); I != N; ++I) {
130 RL += R[I].Data.size();
131 }
132 if (LL != RL)
133 return false;
134 if (LL == 0 && RL == 0)
135 return true;
136
137 // Kick out empty parts, they confuse the algorithm below.
138 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
139 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
140
141 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000143 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000144 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000145 for (;;) {
146 // Compare the current pieces.
147 if (LR.size() == RR.size()) {
148 // If they're the same length, it's pretty easy.
149 if (LR != RR)
150 return false;
151 // Both pieces are done, advance.
152 ++LI;
153 ++RI;
154 // If either string is done, they're both done, since they're the same
155 // length.
156 if (LI == LN) {
157 assert(RI == RN && "Strings not the same length after all?");
158 return true;
159 }
160 LR = L[LI];
161 RR = R[RI].Data;
162 } else if (LR.size() < RR.size()) {
163 // Right piece is longer.
164 if (!RR.startswith(LR))
165 return false;
166 ++LI;
167 assert(LI != LN && "Strings not the same length after all?");
168 RR = RR.substr(LR.size());
169 LR = L[LI];
170 } else {
171 // Left piece is longer.
172 if (!LR.startswith(RR))
173 return false;
174 ++RI;
175 assert(RI != RN && "Strings not the same length after all?");
176 LR = LR.substr(RR.size());
177 RR = R[RI].Data;
178 }
179 }
180}
181
Chris Lattner5f9e2722011-07-23 10:55:15 +0000182static std::pair<FileID, StringRef::size_type>
183FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
184 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000185 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
186 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000188 Res.first = Buffers[I].BufferID;
189 break;
190 }
191 }
192 return Res;
193}
194
195bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000196 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000197 std::string &SuggestedPredefines,
198 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000199 // We are in the context of an implicit include, so the predefines buffer will
200 // have a #include entry for the PCH file itself (as normalized by the
201 // preprocessor initialization). Find it and skip over it in the checking
202 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000203 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000204 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000205 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
206 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000207 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 std::pair<StringRef,StringRef> Split =
209 StringRef(PP.getPredefines()).split(PCHInclude.str());
210 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000211 if (Left == PP.getPredefines()) {
212 Error("Missing PCH include entry!");
213 return true;
214 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000215
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000216 // If the concatenation of all the PCH buffers is equal to the adjusted
217 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000218 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000219 CommandLine.push_back(Left);
220 CommandLine.push_back(Right);
221 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000222 return false;
223
224 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000226 // The predefines buffers are different. Determine what the differences are,
227 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000228 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000229 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
230 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000231
Chris Lattner5f9e2722011-07-23 10:55:15 +0000232 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000233 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000234
235 // Pick out implicit #includes after the PCH and don't consider them for
236 // validation; we will insert them into SuggestedPredefines so that the
237 // preprocessor includes them.
238 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000240 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
241 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
242 if (AfterPCHLines[i].startswith("#include ")) {
243 IncludesAfterPCH += AfterPCHLines[i];
244 IncludesAfterPCH += '\n';
245 } else {
246 CmdLineLines.push_back(AfterPCHLines[i]);
247 }
248 }
249
250 // Make sure we add the includes last into SuggestedPredefines before we
251 // exit this function.
252 struct AddIncludesRAII {
253 std::string &SuggestedPredefines;
254 std::string &IncludesAfterPCH;
255
256 AddIncludesRAII(std::string &SuggestedPredefines,
257 std::string &IncludesAfterPCH)
258 : SuggestedPredefines(SuggestedPredefines),
259 IncludesAfterPCH(IncludesAfterPCH) { }
260 ~AddIncludesRAII() {
261 SuggestedPredefines += IncludesAfterPCH;
262 }
263 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000264
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000265 // Sort both sets of predefined buffer lines, since we allow some extra
266 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000267 std::sort(CmdLineLines.begin(), CmdLineLines.end());
268 std::sort(PCHLines.begin(), PCHLines.end());
269
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000270 // Determine which predefines that were used to build the PCH file are missing
271 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000272 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000273 std::set_difference(PCHLines.begin(), PCHLines.end(),
274 CmdLineLines.begin(), CmdLineLines.end(),
275 std::back_inserter(MissingPredefines));
276
277 bool MissingDefines = false;
278 bool ConflictingDefines = false;
279 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000281 if (Missing.startswith("#include ")) {
282 // An -include was specified when generating the PCH; it is included in
283 // the PCH, just ignore it.
284 continue;
285 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000286 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000287 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
288 return true;
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000291 // This is a macro definition. Determine the name of the macro we're
292 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000293 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000294 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000295 = Missing.find_first_of("( \n\r", StartOfMacroName);
296 assert(EndOfMacroName != std::string::npos &&
297 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000298 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000299
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000300 // Determine whether this macro was given a different definition on the
301 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000302 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000303 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000304 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000305 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
306 MacroDefStart);
307 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000308 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000309 // Different macro; we're done.
310 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000311 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
314 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000315 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000316 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000317 (*ConflictPos)[MacroDefLen] != '(')
318 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000320 // We found a conflicting macro definition.
321 break;
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000324 if (ConflictPos != CmdLineLines.end()) {
325 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
326 << MacroName;
327
328 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000329 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000330 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000331 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000332 SourceLocation PCHMissingLoc =
333 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000334 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000335 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000336
337 ConflictingDefines = true;
338 continue;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000341 // If the macro doesn't conflict, then we'll just pick up the macro
342 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000343 if (ConflictingDefines)
344 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000346 if (!MissingDefines) {
347 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
348 MissingDefines = true;
349 }
350
351 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000353 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000354 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000355 SourceLocation PCHMissingLoc =
356 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000357 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 if (ConflictingDefines)
362 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 // Determine what predefines were introduced based on command-line
365 // parameters that were not present when building the PCH
366 // file. Extra #defines are okay, so long as the identifiers being
367 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000368 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000369 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
370 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000371 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000372 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000373 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000374 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000375 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
376 return true;
377 }
378
379 // This is an extra macro definition. Determine the name of the
380 // macro we're defining.
381 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000382 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000383 = Extra.find_first_of("( \n\r", StartOfMacroName);
384 assert(EndOfMacroName != std::string::npos &&
385 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000386 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000387
388 // Check whether this name was used somewhere in the PCH file. If
389 // so, defining it as a macro could change behavior, so we reject
390 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000391 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000392 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000393 return true;
394 }
395
396 // Add this definition to the suggested predefines buffer.
397 SuggestedPredefines += Extra;
398 SuggestedPredefines += '\n';
399 }
400
401 // If we get here, it's because the predefines buffer had compatible
402 // contents. Accept the PCH file.
403 return false;
404}
405
Douglas Gregor12fab312010-03-16 16:35:32 +0000406void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
407 unsigned ID) {
408 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
409 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410}
411
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000412void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000413 PP.setCounterValue(Value);
414}
415
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000417// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000418//===----------------------------------------------------------------------===//
419
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000420void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000421ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000422 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000423}
424
Chris Lattner4c6f9522009-04-27 05:14:47 +0000425
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000426
Douglas Gregor98339b92011-08-25 20:47:51 +0000427unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
428 return serialization::ComputeHash(Sel);
429}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000430
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregor98339b92011-08-25 20:47:51 +0000432std::pair<unsigned, unsigned>
433ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
434 using namespace clang::io;
435 unsigned KeyLen = ReadUnalignedLE16(d);
436 unsigned DataLen = ReadUnalignedLE16(d);
437 return std::make_pair(KeyLen, DataLen);
438}
439
440ASTSelectorLookupTrait::internal_key_type
441ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
442 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000443 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000444 unsigned N = ReadUnalignedLE16(d);
445 IdentifierInfo *FirstII
446 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
447 if (N == 0)
448 return SelTable.getNullarySelector(FirstII);
449 else if (N == 1)
450 return SelTable.getUnarySelector(FirstII);
451
452 SmallVector<IdentifierInfo *, 16> Args;
453 Args.push_back(FirstII);
454 for (unsigned I = 1; I != N; ++I)
455 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
456
457 return SelTable.getSelector(N, Args.data());
458}
459
460ASTSelectorLookupTrait::data_type
461ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
462 unsigned DataLen) {
463 using namespace clang::io;
464
465 data_type Result;
466
467 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
468 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
469 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
470
471 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000472 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
473 if (ObjCMethodDecl *Method
474 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
475 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Douglas Gregor98339b92011-08-25 20:47:51 +0000478 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000479 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
480 if (ObjCMethodDecl *Method
481 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
482 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregor98339b92011-08-25 20:47:51 +0000485 return Result;
486}
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor98339b92011-08-25 20:47:51 +0000488unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
489 return llvm::HashString(StringRef(a.first, a.second));
490}
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregor98339b92011-08-25 20:47:51 +0000492std::pair<unsigned, unsigned>
493ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
494 using namespace clang::io;
495 unsigned DataLen = ReadUnalignedLE16(d);
496 unsigned KeyLen = ReadUnalignedLE16(d);
497 return std::make_pair(KeyLen, DataLen);
498}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499
Douglas Gregor98339b92011-08-25 20:47:51 +0000500std::pair<const char*, unsigned>
501ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
502 assert(n >= 2 && d[n-1] == '\0');
503 return std::make_pair((const char*) d, n-1);
504}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000505
Douglas Gregor98339b92011-08-25 20:47:51 +0000506IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
507 const unsigned char* d,
508 unsigned DataLen) {
509 using namespace clang::io;
510 unsigned RawID = ReadUnalignedLE32(d);
511 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Douglas Gregor98339b92011-08-25 20:47:51 +0000513 // Wipe out the "is interesting" bit.
514 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515
Douglas Gregor98339b92011-08-25 20:47:51 +0000516 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
517 if (!IsInteresting) {
518 // For uninteresting identifiers, just build the IdentifierInfo
519 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000520 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000521 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000522 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000523 KnownII = II;
524 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000525 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000526 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000527 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000528 return II;
529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000531 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000532 unsigned Bits = ReadUnalignedLE16(d);
533 bool CPlusPlusOperatorKeyword = Bits & 0x01;
534 Bits >>= 1;
535 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
536 Bits >>= 1;
537 bool Poisoned = Bits & 0x01;
538 Bits >>= 1;
539 bool ExtensionToken = Bits & 0x01;
540 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000541 bool hadMacroDefinition = Bits & 0x01;
542 Bits >>= 1;
Douglas Gregor98339b92011-08-25 20:47:51 +0000543 bool hasMacroDefinition = Bits & 0x01;
544 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000545
Douglas Gregor98339b92011-08-25 20:47:51 +0000546 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000547 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000548
Douglas Gregor98339b92011-08-25 20:47:51 +0000549 // Build the IdentifierInfo itself and link the identifier ID with
550 // the new IdentifierInfo.
551 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000552 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000553 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000554 KnownII = II;
555 }
Douglas Gregor057df202012-01-18 20:56:22 +0000556 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000557 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000558
Douglas Gregor98339b92011-08-25 20:47:51 +0000559 // Set or check the various bits in the IdentifierInfo structure.
560 // Token IDs are read-only.
561 if (HasRevertedTokenIDToIdentifier)
562 II->RevertTokenIDToIdentifier();
563 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
564 assert(II->isExtensionToken() == ExtensionToken &&
565 "Incorrect extension token flag");
566 (void)ExtensionToken;
567 if (Poisoned)
568 II->setIsPoisoned(true);
569 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
570 "Incorrect C++ operator keyword flag");
571 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000572
Douglas Gregor98339b92011-08-25 20:47:51 +0000573 // If this identifier is a macro, deserialize the macro
574 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000575 if (hadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000576 SmallVector<MacroID, 4> MacroIDs;
577 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
578 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
579 DataLen -= 4;
Douglas Gregor13292642011-12-02 15:45:10 +0000580 }
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000581 DataLen -= 4;
582 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000583 }
584
Douglas Gregoreee242f2011-10-27 09:33:13 +0000585 Reader.SetIdentifierInfo(ID, II);
586
Douglas Gregor98339b92011-08-25 20:47:51 +0000587 // Read all of the declarations visible at global scope with this
588 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000589 if (DataLen > 0) {
590 SmallVector<uint32_t, 4> DeclIDs;
591 for (; DataLen > 0; DataLen -= 4)
592 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
593 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000594 }
595
Douglas Gregor98339b92011-08-25 20:47:51 +0000596 return II;
597}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000598
Douglas Gregor98339b92011-08-25 20:47:51 +0000599unsigned
600ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
601 llvm::FoldingSetNodeID ID;
602 ID.AddInteger(Key.Kind);
603
604 switch (Key.Kind) {
605 case DeclarationName::Identifier:
606 case DeclarationName::CXXLiteralOperatorName:
607 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
608 break;
609 case DeclarationName::ObjCZeroArgSelector:
610 case DeclarationName::ObjCOneArgSelector:
611 case DeclarationName::ObjCMultiArgSelector:
612 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
613 break;
614 case DeclarationName::CXXOperatorName:
615 ID.AddInteger((OverloadedOperatorKind)Key.Data);
616 break;
617 case DeclarationName::CXXConstructorName:
618 case DeclarationName::CXXDestructorName:
619 case DeclarationName::CXXConversionFunctionName:
620 case DeclarationName::CXXUsingDirective:
621 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000622 }
623
Douglas Gregor98339b92011-08-25 20:47:51 +0000624 return ID.ComputeHash();
625}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000626
Douglas Gregor98339b92011-08-25 20:47:51 +0000627ASTDeclContextNameLookupTrait::internal_key_type
628ASTDeclContextNameLookupTrait::GetInternalKey(
629 const external_key_type& Name) const {
630 DeclNameKey Key;
631 Key.Kind = Name.getNameKind();
632 switch (Name.getNameKind()) {
633 case DeclarationName::Identifier:
634 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
635 break;
636 case DeclarationName::ObjCZeroArgSelector:
637 case DeclarationName::ObjCOneArgSelector:
638 case DeclarationName::ObjCMultiArgSelector:
639 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
640 break;
641 case DeclarationName::CXXOperatorName:
642 Key.Data = Name.getCXXOverloadedOperator();
643 break;
644 case DeclarationName::CXXLiteralOperatorName:
645 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
646 break;
647 case DeclarationName::CXXConstructorName:
648 case DeclarationName::CXXDestructorName:
649 case DeclarationName::CXXConversionFunctionName:
650 case DeclarationName::CXXUsingDirective:
651 Key.Data = 0;
652 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000653 }
654
Douglas Gregor98339b92011-08-25 20:47:51 +0000655 return Key;
656}
657
Douglas Gregor98339b92011-08-25 20:47:51 +0000658std::pair<unsigned, unsigned>
659ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
660 using namespace clang::io;
661 unsigned KeyLen = ReadUnalignedLE16(d);
662 unsigned DataLen = ReadUnalignedLE16(d);
663 return std::make_pair(KeyLen, DataLen);
664}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000665
Douglas Gregor98339b92011-08-25 20:47:51 +0000666ASTDeclContextNameLookupTrait::internal_key_type
667ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
668 using namespace clang::io;
669
670 DeclNameKey Key;
671 Key.Kind = (DeclarationName::NameKind)*d++;
672 switch (Key.Kind) {
673 case DeclarationName::Identifier:
674 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
675 break;
676 case DeclarationName::ObjCZeroArgSelector:
677 case DeclarationName::ObjCOneArgSelector:
678 case DeclarationName::ObjCMultiArgSelector:
679 Key.Data =
680 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
681 .getAsOpaquePtr();
682 break;
683 case DeclarationName::CXXOperatorName:
684 Key.Data = *d++; // OverloadedOperatorKind
685 break;
686 case DeclarationName::CXXLiteralOperatorName:
687 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
688 break;
689 case DeclarationName::CXXConstructorName:
690 case DeclarationName::CXXDestructorName:
691 case DeclarationName::CXXConversionFunctionName:
692 case DeclarationName::CXXUsingDirective:
693 Key.Data = 0;
694 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000695 }
696
Douglas Gregor98339b92011-08-25 20:47:51 +0000697 return Key;
698}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000699
Douglas Gregor98339b92011-08-25 20:47:51 +0000700ASTDeclContextNameLookupTrait::data_type
701ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
702 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000703 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000704 using namespace clang::io;
705 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000706 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000707 return std::make_pair(Start, Start + NumDecls);
708}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000709
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000710bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000711 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000712 const std::pair<uint64_t, uint64_t> &Offsets,
713 DeclContextInfo &Info) {
714 SavedStreamPosition SavedPosition(Cursor);
715 // First the lexical decls.
716 if (Offsets.first != 0) {
717 Cursor.JumpToBit(Offsets.first);
718
719 RecordData Record;
720 const char *Blob;
721 unsigned BlobLen;
722 unsigned Code = Cursor.ReadCode();
723 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
724 if (RecCode != DECL_CONTEXT_LEXICAL) {
725 Error("Expected lexical block");
726 return true;
727 }
728
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000729 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
730 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000731 }
732
733 // Now the lookup table.
734 if (Offsets.second != 0) {
735 Cursor.JumpToBit(Offsets.second);
736
737 RecordData Record;
738 const char *Blob;
739 unsigned BlobLen;
740 unsigned Code = Cursor.ReadCode();
741 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
742 if (RecCode != DECL_CONTEXT_VISIBLE) {
743 Error("Expected visible lookup table block");
744 return true;
745 }
746 Info.NameLookupTableData
747 = ASTDeclContextNameLookupTable::Create(
748 (const unsigned char *)Blob + Record[0],
749 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000750 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000751 }
752
753 return false;
754}
755
Chris Lattner5f9e2722011-07-23 10:55:15 +0000756void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000757 Error(diag::err_fe_pch_malformed, Msg);
758}
759
760void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000761 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000762 if (Diags.isDiagnosticInFlight())
763 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
764 else
765 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000766}
767
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000768/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000769bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000770 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000771 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000772 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000773 SuggestedPredefines,
774 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000775 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000776}
777
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000778//===----------------------------------------------------------------------===//
779// Source Manager Deserialization
780//===----------------------------------------------------------------------===//
781
Douglas Gregorbd945002009-04-13 16:31:14 +0000782/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000783/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000784bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000785 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000786 unsigned Idx = 0;
787 LineTableInfo &LineTable = SourceMgr.getLineTable();
788
789 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000790 std::map<int, int> FileIDs;
791 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000792 // Extract the file name
793 unsigned FilenameLen = Record[Idx++];
794 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
795 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000796 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000797 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000798 }
799
800 // Parse the line entries
801 std::vector<LineEntry> Entries;
802 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000803 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000804 assert(FID >= 0 && "Serialized line entries for non-local file.");
805 // Remap FileID from 1-based old view.
806 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000807
808 // Extract the line entries
809 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000810 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000811 Entries.clear();
812 Entries.reserve(NumEntries);
813 for (unsigned I = 0; I != NumEntries; ++I) {
814 unsigned FileOffset = Record[Idx++];
815 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000816 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000817 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000818 = (SrcMgr::CharacteristicKind)Record[Idx++];
819 unsigned IncludeOffset = Record[Idx++];
820 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
821 FileKind, IncludeOffset));
822 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000823 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000824 }
825
826 return false;
827}
828
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000829namespace {
830
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000831class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000832public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000833 const ino_t ino;
834 const dev_t dev;
835 const mode_t mode;
836 const time_t mtime;
837 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000839 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000840 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000841};
842
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000843class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844 public:
845 typedef const char *external_key_type;
846 typedef const char *internal_key_type;
847
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000848 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000849
850 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000851 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000852 }
853
854 static internal_key_type GetInternalKey(const char *path) { return path; }
855
856 static bool EqualKey(internal_key_type a, internal_key_type b) {
857 return strcmp(a, b) == 0;
858 }
859
860 static std::pair<unsigned, unsigned>
861 ReadKeyDataLength(const unsigned char*& d) {
862 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
863 unsigned DataLen = (unsigned) *d++;
864 return std::make_pair(KeyLen + 1, DataLen);
865 }
866
867 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
868 return (const char *)d;
869 }
870
871 static data_type ReadData(const internal_key_type, const unsigned char *d,
872 unsigned /*DataLen*/) {
873 using namespace clang::io;
874
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000875 ino_t ino = (ino_t) ReadUnalignedLE32(d);
876 dev_t dev = (dev_t) ReadUnalignedLE32(d);
877 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000878 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000879 off_t size = (off_t) ReadUnalignedLE64(d);
880 return data_type(ino, dev, mode, mtime, size);
881 }
882};
883
884/// \brief stat() cache for precompiled headers.
885///
886/// This cache is very similar to the stat cache used by pretokenized
887/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000888class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000889 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000890 CacheTy *Cache;
891
892 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000893public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000894 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
895 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000896 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
897 Cache = CacheTy::Create(Buckets, Base);
898 }
899
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000900 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Chris Lattner898a0612010-11-23 21:17:56 +0000902 LookupResult getStat(const char *Path, struct stat &StatBuf,
903 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000904 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000905 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000906
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000907 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000908 if (I == Cache->end()) {
909 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000910 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000913 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000914 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Chris Lattner10e286a2010-11-23 19:19:34 +0000916 StatBuf.st_ino = Data.ino;
917 StatBuf.st_dev = Data.dev;
918 StatBuf.st_mtime = Data.mtime;
919 StatBuf.st_mode = Data.mode;
920 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000921 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922 }
923};
924} // end anonymous namespace
925
926
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000927/// \brief Read a source manager block
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000928ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000929 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000930
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000931 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000932
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000933 // Set the source-location entry cursor to the current position in
934 // the stream. This cursor will be used to read the contents of the
935 // source manager block initially, and then lazily read
936 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000937 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000938
939 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000940 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000941 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000942 return Failure;
943 }
944
945 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000946 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000947 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000948 return Failure;
949 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000950
Douglas Gregor14f79002009-04-10 03:52:48 +0000951 RecordData Record;
952 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000953 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000954 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000955 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000956 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000957 return Failure;
958 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000959 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000960 }
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Douglas Gregor14f79002009-04-10 03:52:48 +0000962 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
963 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000964 SLocEntryCursor.ReadSubBlockID();
965 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000966 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000967 return Failure;
968 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000969 continue;
970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregor14f79002009-04-10 03:52:48 +0000972 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000973 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000974 continue;
975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor14f79002009-04-10 03:52:48 +0000977 // Read a record.
978 const char *BlobStart;
979 unsigned BlobLen;
980 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000981 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000982 default: // Default behavior: ignore.
983 break;
984
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000985 case SM_SLOC_FILE_ENTRY:
986 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000987 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000988 // Once we hit one of the source location entries, we're done.
989 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000990 }
991 }
992}
993
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000994/// \brief If a header file is not found at the path that we expect it to be
995/// and the PCH file was moved from its original location, try to resolve the
996/// file by assuming that header+PCH were moved together and the header is in
997/// the same place relative to the PCH.
998static std::string
999resolveFileRelativeToOriginalDir(const std::string &Filename,
1000 const std::string &OriginalDir,
1001 const std::string &CurrDir) {
1002 assert(OriginalDir != CurrDir &&
1003 "No point trying to resolve the file if the PCH dir didn't change");
1004 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001005 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001006 fs::make_absolute(filePath);
1007 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001008 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001009
1010 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1011 fileDirE = path::end(path::parent_path(filePath));
1012 path::const_iterator origDirI = path::begin(OriginalDir),
1013 origDirE = path::end(OriginalDir);
1014 // Skip the common path components from filePath and OriginalDir.
1015 while (fileDirI != fileDirE && origDirI != origDirE &&
1016 *fileDirI == *origDirI) {
1017 ++fileDirI;
1018 ++origDirI;
1019 }
1020 for (; origDirI != origDirE; ++origDirI)
1021 path::append(currPCHPath, "..");
1022 path::append(currPCHPath, fileDirI, fileDirE);
1023 path::append(currPCHPath, path::filename(Filename));
1024 return currPCHPath.str();
1025}
1026
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001027/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001028ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001029 if (ID == 0)
1030 return Success;
1031
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001032 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001033 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001034 return Failure;
1035 }
1036
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001037 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001038 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001039 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001040 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001041
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001042 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001043 unsigned Code = SLocEntryCursor.ReadCode();
1044 if (Code == llvm::bitc::END_BLOCK ||
1045 Code == llvm::bitc::ENTER_SUBBLOCK ||
1046 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001047 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001048 return Failure;
1049 }
1050
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001051 RecordData Record;
1052 const char *BlobStart;
1053 unsigned BlobLen;
1054 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1055 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001056 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001057 return Failure;
1058
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001059 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00001060 if (Record.size() < 7) {
1061 Error("source location entry is incorrect");
1062 return Failure;
1063 }
1064
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001065 // We will detect whether a file changed and return 'Failure' for it, but
1066 // we will also try to fail gracefully by setting up the SLocEntry.
1067 ASTReader::ASTReadResult Result = Success;
1068
Douglas Gregora081da52011-11-16 20:05:18 +00001069 bool OverriddenBuffer = Record[6];
1070
1071 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1072 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001073 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora4581a12011-11-17 19:08:51 +00001074 const FileEntry *File =
1075 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1076 (time_t)Record[5])
1077 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001078 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1079 OriginalDir != CurrentDir) {
1080 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1081 OriginalDir,
1082 CurrentDir);
1083 if (!resolved.empty())
1084 File = FileMgr.getFile(resolved);
1085 }
Axel Naumann04331162011-01-27 10:55:51 +00001086 if (File == 0)
1087 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1088 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001089 if (File == 0) {
1090 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001091 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001092 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001093 Error(ErrorStr.c_str());
1094 return Failure;
1095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +00001097 if (!DisableValidation && !OverriddenBuffer &&
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001098 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001099#if !defined(LLVM_ON_WIN32)
1100 // In our regression testing, the Windows file system seems to
1101 // have inconsistent modification times that sometimes
1102 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001103 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001104#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001105 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001106 Error(diag::err_fe_pch_file_modified, Filename);
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001107 Result = Failure;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001108 }
1109
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001110 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001111 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001112 // This is the module's main file.
1113 IncludeLoc = getImportLocation(F);
1114 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001115 SrcMgr::CharacteristicKind
1116 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1117 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001118 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001119 SrcMgr::FileInfo &FileInfo =
1120 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001121 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001122 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001123 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001124
Douglas Gregora081da52011-11-16 20:05:18 +00001125 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1126 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001127 if (NumFileDecls) {
1128 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001129 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1130 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001131 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001132
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001133 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001134 = SourceMgr.getOrCreateContentCache(File,
1135 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001136 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1137 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001138 unsigned Code = SLocEntryCursor.ReadCode();
1139 Record.clear();
1140 unsigned RecCode
1141 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1142
1143 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1144 Error("AST record has invalid code");
1145 return Failure;
1146 }
1147
1148 llvm::MemoryBuffer *Buffer
1149 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1150 Filename);
1151 SourceMgr.overrideFileContents(File, Buffer);
1152 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001153
1154 if (Result == Failure)
1155 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001156 break;
1157 }
1158
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001159 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001160 const char *Name = BlobStart;
1161 unsigned Offset = Record[0];
1162 unsigned Code = SLocEntryCursor.ReadCode();
1163 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001164 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001165 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001166
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001167 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001168 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001169 return Failure;
1170 }
1171
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001172 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001173 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1174 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001175 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1176 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregor6236a292011-12-02 21:56:05 +00001178 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001179 PCHPredefinesBlock Block = {
1180 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001181 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001182 };
1183 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001184 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185
1186 break;
1187 }
1188
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001189 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001190 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001191 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001192 ReadSourceLocation(*F, Record[2]),
1193 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001194 Record[4],
1195 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001196 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001197 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001198 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001199 }
1200
1201 return Success;
1202}
1203
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001204/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001205SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001206 if (F->ImportLoc.isValid())
1207 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001208
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001209 // Otherwise we have a PCH. It's considered to be "imported" at the first
1210 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001211 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001212 // Main file is the importer. We assume that it is the first entry in the
1213 // entry table. We can't ask the manager, because at the time of PCH loading
1214 // the main file entry doesn't exist yet.
1215 // The very first entry is the invalid instantiation loc, which takes up
1216 // offsets 0 and 1.
1217 return SourceLocation::getFromRawEncoding(2U);
1218 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001219 //return F->Loaders[0]->FirstLoc;
1220 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001221}
1222
Chris Lattner6367f6d2009-04-27 01:05:14 +00001223/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1224/// specified cursor. Read the abbreviations that are at the top of the block
1225/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001226bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001227 unsigned BlockID) {
1228 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001229 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001230 return Failure;
1231 }
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Chris Lattner6367f6d2009-04-27 01:05:14 +00001233 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001234 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001235 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001236
Chris Lattner6367f6d2009-04-27 01:05:14 +00001237 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001238 if (Code != llvm::bitc::DEFINE_ABBREV) {
1239 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001240 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001241 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001242 Cursor.ReadAbbrevRecord();
1243 }
1244}
1245
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001246void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001247 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Douglas Gregor37e26842009-04-21 23:56:24 +00001249 // Keep track of where we are in the stream, then jump back there
1250 // after reading this macro.
1251 SavedStreamPosition SavedPosition(Stream);
1252
1253 Stream.JumpToBit(Offset);
1254 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001255 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001256 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Douglas Gregor37e26842009-04-21 23:56:24 +00001258 while (true) {
1259 unsigned Code = Stream.ReadCode();
1260 switch (Code) {
1261 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001262 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001263
1264 case llvm::bitc::ENTER_SUBBLOCK:
1265 // No known subblocks, always skip them.
1266 Stream.ReadSubBlockID();
1267 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001268 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001269 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001270 }
1271 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregor37e26842009-04-21 23:56:24 +00001273 case llvm::bitc::DEFINE_ABBREV:
1274 Stream.ReadAbbrevRecord();
1275 continue;
1276 default: break;
1277 }
1278
1279 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001280 const char *BlobStart = 0;
1281 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001282 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001283 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001284 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001285 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001286 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001287 case PP_MACRO_OBJECT_LIKE:
1288 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001289 // If we already have a macro, that means that we've hit the end
1290 // of the definition of the macro we were looking for. We're
1291 // done.
1292 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001293 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001294
Douglas Gregor95eab172011-07-28 20:55:49 +00001295 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001296 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001297 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001298 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Douglas Gregora8235d62012-10-09 23:05:51 +00001301 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1302
1303 // If this macro has already been loaded, don't do so again.
1304 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1305 return;
1306
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001307 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1308 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001309 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001310 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001311
Douglas Gregora8235d62012-10-09 23:05:51 +00001312 // Record this macro.
1313 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1314
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001315 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1316 if (UndefLoc.isValid())
1317 MI->setUndefLoc(UndefLoc);
1318
1319 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001320 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001322 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001323 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001324
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001325 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001326 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001327 bool isC99VarArgs = Record[NextIndex++];
1328 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001330 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001331 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001332 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001333
1334 // Install function-like macro info.
1335 MI->setIsFunctionLike();
1336 if (isC99VarArgs) MI->setIsC99Varargs();
1337 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001338 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001339 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001340 }
1341
Douglas Gregora8235d62012-10-09 23:05:51 +00001342 if (DeserializationListener)
1343 DeserializationListener->MacroRead(GlobalID, MI);
1344
1345 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001346 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001347 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1348 if (Update != MacroUpdates.end()) {
1349 if (MI->getUndefLoc().isInvalid()) {
1350 MI->setUndefLoc(Update->second.UndefLoc);
1351 if (PPMutationListener *Listener = PP.getPPMutationListener())
1352 Listener->UndefinedMacro(MI);
1353 }
1354 MacroUpdates.erase(Update);
1355 }
1356
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001357 // Determine whether this macro definition is visible.
1358 bool Hidden = !MI->isPublic();
1359 if (!Hidden && GlobalSubmoduleID) {
1360 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1361 if (Owner->NameVisibility == Module::Hidden) {
1362 // The owning module is not visible, and this macro definition
1363 // should not be, either.
1364 Hidden = true;
1365
1366 // Note that this macro definition was hidden because its owning
1367 // module is not yet visible.
1368 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1369 }
1370 }
1371 }
1372 MI->setHidden(Hidden);
1373
Douglas Gregor37e26842009-04-21 23:56:24 +00001374 // Finally, install the macro.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001375 PP.addLoadedMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001376
1377 // Remember that we saw this macro last so that we add the tokens that
1378 // form its body to it.
1379 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001380
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001381 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1382 Record[NextIndex]) {
1383 // We have a macro definition. Register the association
1384 PreprocessedEntityID
1385 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1386 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1387 PPRec.RegisterMacroDefinition(Macro,
1388 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001389 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001390
Douglas Gregor37e26842009-04-21 23:56:24 +00001391 ++NumMacrosRead;
1392 break;
1393 }
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001395 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001396 // If we see a TOKEN before a PP_MACRO_*, then the file is
1397 // erroneous, just pretend we didn't see this.
1398 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Douglas Gregor37e26842009-04-21 23:56:24 +00001400 Token Tok;
1401 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001402 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001403 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001404 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001405 Tok.setIdentifierInfo(II);
1406 Tok.setKind((tok::TokenKind)Record[3]);
1407 Tok.setFlag((Token::TokenFlags)Record[4]);
1408 Macro->AddTokenToBody(Tok);
1409 break;
1410 }
David Blaikie7530c032012-01-17 06:56:22 +00001411 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001412 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001413}
1414
Douglas Gregor86c67d82011-07-28 22:39:26 +00001415PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001416ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001417 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001418 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1419 assert(I != M.PreprocessedEntityRemap.end()
1420 && "Invalid index into preprocessed entity index remap");
1421
1422 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001423}
1424
Douglas Gregor98339b92011-08-25 20:47:51 +00001425unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1426 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001427}
Douglas Gregor98339b92011-08-25 20:47:51 +00001428
1429HeaderFileInfoTrait::internal_key_type
1430HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1431
1432bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1433 if (strcmp(a, b) == 0)
1434 return true;
1435
1436 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1437 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001438
1439 // Determine whether the actual files are equivalent.
1440 bool Result = false;
1441 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001442 return false;
1443
Douglas Gregor99a922b2011-12-09 16:22:07 +00001444 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001445}
1446
1447std::pair<unsigned, unsigned>
1448HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1449 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1450 unsigned DataLen = (unsigned) *d++;
1451 return std::make_pair(KeyLen + 1, DataLen);
1452}
1453
1454HeaderFileInfoTrait::data_type
1455HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1456 unsigned DataLen) {
1457 const unsigned char *End = d + DataLen;
1458 using namespace clang::io;
1459 HeaderFileInfo HFI;
1460 unsigned Flags = *d++;
1461 HFI.isImport = (Flags >> 5) & 0x01;
1462 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1463 HFI.DirInfo = (Flags >> 2) & 0x03;
1464 HFI.Resolved = (Flags >> 1) & 0x01;
1465 HFI.IndexHeaderMapHeader = Flags & 0x01;
1466 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001467 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1468 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001469 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1470 // The framework offset is 1 greater than the actual offset,
1471 // since 0 is used as an indicator for "no framework name".
1472 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1473 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1474 }
1475
1476 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1477 (void)End;
1478
1479 // This HeaderFileInfo was externally loaded.
1480 HFI.External = true;
1481 return HFI;
1482}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001483
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001484void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1485 II->setHadMacroDefinition(true);
1486 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1487 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001488}
1489
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001490void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001491 // Note that we are loading defined macros.
1492 Deserializing Macros(this);
1493
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001494 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1495 E = ModuleMgr.rend(); I != E; ++I) {
1496 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001497
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001498 // If there was no preprocessor block, skip this file.
1499 if (!MacroCursor.getBitStreamReader())
1500 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001501
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001502 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001503 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001504
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001505 RecordData Record;
1506 while (true) {
1507 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001508 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001509 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001510
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001511 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1512 // No known subblocks, always skip them.
1513 Cursor.ReadSubBlockID();
1514 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001515 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001516 return;
1517 }
1518 continue;
1519 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001520
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001521 if (Code == llvm::bitc::DEFINE_ABBREV) {
1522 Cursor.ReadAbbrevRecord();
1523 continue;
1524 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001525
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001526 // Read a record.
1527 const char *BlobStart;
1528 unsigned BlobLen;
1529 Record.clear();
1530 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1531 default: // Default behavior: ignore.
1532 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001533
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001534 case PP_MACRO_OBJECT_LIKE:
1535 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001536 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001537 break;
1538
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001539 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001540 // Ignore tokens.
1541 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001542 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001543 }
1544 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001545}
1546
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001547void ASTReader::LoadMacroDefinition(PendingMacroIDsMap::iterator Pos) {
1548 assert(Pos != PendingMacroIDs.end() && "Unknown macro definition");
1549 SmallVector<MacroID, 2> GlobalIDs = Pos->second;
1550 PendingMacroIDs.erase(Pos);
1551 for (unsigned I = 0, N = GlobalIDs.size(); I != N; ++I)
1552 getMacro(GlobalIDs[I]);
Douglas Gregor88a35862010-01-04 19:18:44 +00001553}
1554
Douglas Gregoreee242f2011-10-27 09:33:13 +00001555namespace {
1556 /// \brief Visitor class used to look up identifirs in an AST file.
1557 class IdentifierLookupVisitor {
1558 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001559 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001560 IdentifierInfo *Found;
1561 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001562 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1563 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001564
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001565 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001566 IdentifierLookupVisitor *This
1567 = static_cast<IdentifierLookupVisitor *>(UserData);
1568
Douglas Gregor057df202012-01-18 20:56:22 +00001569 // If we've already searched this module file, skip it now.
1570 if (M.Generation <= This->PriorGeneration)
1571 return true;
1572
Douglas Gregoreee242f2011-10-27 09:33:13 +00001573 ASTIdentifierLookupTable *IdTable
1574 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1575 if (!IdTable)
1576 return false;
1577
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001578 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1579 M, This->Found);
1580
Douglas Gregoreee242f2011-10-27 09:33:13 +00001581 std::pair<const char*, unsigned> Key(This->Name.begin(),
1582 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001583 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001584 if (Pos == IdTable->end())
1585 return false;
1586
1587 // Dereferencing the iterator has the effect of building the
1588 // IdentifierInfo node and populating it with the various
1589 // declarations it needs.
1590 This->Found = *Pos;
1591 return true;
1592 }
1593
1594 // \brief Retrieve the identifier info found within the module
1595 // files.
1596 IdentifierInfo *getIdentifierInfo() const { return Found; }
1597 };
1598}
1599
1600void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001601 // Note that we are loading an identifier.
1602 Deserializing AnIdentifier(this);
1603
Douglas Gregor057df202012-01-18 20:56:22 +00001604 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001605 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001606 PriorGeneration = IdentifierGeneration[&II];
1607
1608 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1609 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1610 markIdentifierUpToDate(&II);
1611}
1612
1613void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1614 if (!II)
1615 return;
1616
1617 II->setOutOfDate(false);
1618
1619 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001620 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001621 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001622}
1623
Chris Lattner5f9e2722011-07-23 10:55:15 +00001624const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001625 std::string Filename = filenameStrRef;
1626 MaybeAddSystemRootToFilename(Filename);
1627 const FileEntry *File = FileMgr.getFile(Filename);
1628 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1629 OriginalDir != CurrentDir) {
1630 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1631 OriginalDir,
1632 CurrentDir);
1633 if (!resolved.empty())
1634 File = FileMgr.getFile(resolved);
1635 }
1636
1637 return File;
1638}
1639
Douglas Gregore650c8c2009-07-07 00:12:59 +00001640/// \brief If we are loading a relocatable PCH file, and the filename is
1641/// not an absolute path, add the system root to the beginning of the file
1642/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001643void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001644 // If this is not a relocatable PCH file, there's nothing to do.
1645 if (!RelocatablePCH)
1646 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Michael J. Spencer256053b2010-12-17 21:22:22 +00001648 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001649 return;
1650
Douglas Gregor832d6202011-07-22 16:35:34 +00001651 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001652 // If no system root was given, default to '/'
1653 Filename.insert(Filename.begin(), '/');
1654 return;
1655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Douglas Gregor832d6202011-07-22 16:35:34 +00001657 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001658 if (isysroot[Length - 1] != '/')
1659 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Douglas Gregor832d6202011-07-22 16:35:34 +00001661 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001662}
1663
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001664ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001665ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001666 llvm::BitstreamCursor &Stream = F.Stream;
1667
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001668 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001669 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001670 return Failure;
1671 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001672
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001673 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001674 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001675 while (!Stream.AtEndOfStream()) {
1676 unsigned Code = Stream.ReadCode();
1677 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001678 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001679 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001680 return Failure;
1681 }
Chris Lattner7356a312009-04-11 21:15:38 +00001682
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001683 DeclContext *DC = Context.getTranslationUnitDecl();
1684 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1685 DC->setMustBuildLookupTable();
1686
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001687 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001688 }
1689
1690 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1691 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001692 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001693 // We lazily load the decls block, but we want to set up the
1694 // DeclsCursor cursor to point into it. Clone our current bitcode
1695 // cursor to it, enter the block and read the abbrevs in that block.
1696 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001697 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001698 if (Stream.SkipBlock() || // Skip with the main cursor.
1699 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001700 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001701 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001702 return Failure;
1703 }
1704 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001706 case DECL_UPDATES_BLOCK_ID:
1707 if (Stream.SkipBlock()) {
1708 Error("malformed block record in AST file");
1709 return Failure;
1710 }
1711 break;
1712
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001713 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001714 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001715 if (!PP.getExternalSource())
1716 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001717
Douglas Gregorecdcb882010-10-20 22:00:55 +00001718 if (Stream.SkipBlock() ||
1719 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001720 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001721 return Failure;
1722 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001723 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001724 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001725
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001726 case PREPROCESSOR_DETAIL_BLOCK_ID:
1727 F.PreprocessorDetailCursor = Stream;
1728 if (Stream.SkipBlock() ||
1729 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1730 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1731 Error("malformed preprocessor detail record in AST file");
1732 return Failure;
1733 }
1734 F.PreprocessorDetailStartOffset
1735 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001736
1737 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001738 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001739 if (!PP.getPreprocessingRecord()->getExternalSource())
1740 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001741 break;
1742
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001743 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001744 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001745 case Success:
1746 break;
1747
1748 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001749 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001750 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001751
1752 case IgnorePCH:
1753 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001754 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001755 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001756
1757 case SUBMODULE_BLOCK_ID:
1758 switch (ReadSubmoduleBlock(F)) {
1759 case Success:
1760 break;
1761
1762 case Failure:
1763 Error("malformed submodule block in AST file");
1764 return Failure;
1765
1766 case IgnorePCH:
1767 return IgnorePCH;
1768 }
1769 break;
1770
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001771 case COMMENTS_BLOCK_ID: {
1772 llvm::BitstreamCursor C = Stream;
1773 if (Stream.SkipBlock() ||
1774 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1775 Error("malformed comments block in AST file");
1776 return Failure;
1777 }
1778 CommentsCursors.push_back(std::make_pair(C, &F));
1779 break;
1780 }
1781
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001782 default:
1783 if (!Stream.SkipBlock())
1784 break;
1785 Error("malformed block record in AST file");
1786 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001787 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001788 continue;
1789 }
1790
1791 if (Code == llvm::bitc::DEFINE_ABBREV) {
1792 Stream.ReadAbbrevRecord();
1793 continue;
1794 }
1795
1796 // Read and process a record.
1797 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001798 const char *BlobStart = 0;
1799 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001800 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001801 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001802 default: // Default behavior: ignore.
1803 break;
1804
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001805 case METADATA: {
1806 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1807 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001808 : diag::warn_pch_version_too_new);
1809 return IgnorePCH;
1810 }
1811
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001812 bool hasErrors = Record[5];
1813 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1814 Diag(diag::err_pch_with_compiler_errors);
1815 return IgnorePCH;
1816 }
1817
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001818 RelocatablePCH = Record[4];
1819 if (Listener) {
1820 std::string TargetTriple(BlobStart, BlobLen);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001821 if (Listener->ReadTargetTriple(F, TargetTriple))
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001822 return IgnorePCH;
1823 }
1824 break;
1825 }
1826
Douglas Gregore95b9192011-08-17 21:07:30 +00001827 case IMPORTS: {
1828 // Load each of the imported PCH files.
1829 unsigned Idx = 0, N = Record.size();
1830 while (Idx < N) {
1831 // Read information about the AST file.
1832 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1833 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001834 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001835 Record.begin() + Idx + Length);
1836 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001837
Douglas Gregore95b9192011-08-17 21:07:30 +00001838 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001839 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001840 case Failure: return Failure;
1841 // If we have to ignore the dependency, we'll have to ignore this too.
1842 case IgnorePCH: return IgnorePCH;
1843 case Success: break;
1844 }
1845 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001846 break;
1847 }
1848
Douglas Gregora119da02011-08-02 16:26:37 +00001849 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001850 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001851 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001852 return Failure;
1853 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001854 F.TypeOffsets = (const uint32_t *)BlobStart;
1855 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001856 unsigned LocalBaseTypeIndex = Record[1];
1857 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001858
Douglas Gregora119da02011-08-02 16:26:37 +00001859 if (F.LocalNumTypes > 0) {
1860 // Introduce the global -> local mapping for types within this module.
1861 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1862
1863 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001864 F.TypeRemap.insertOrReplace(
1865 std::make_pair(LocalBaseTypeIndex,
1866 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001867
1868 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1869 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001870 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001871 }
1872
Douglas Gregor496c7092011-08-03 15:48:04 +00001873 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001874 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001875 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001876 return Failure;
1877 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001878 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001879 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001880 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001881 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001882
Douglas Gregor496c7092011-08-03 15:48:04 +00001883 if (F.LocalNumDecls > 0) {
1884 // Introduce the global -> local mapping for declarations within this
1885 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001886 GlobalDeclMap.insert(
1887 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001888
1889 // Introduce the local -> global mapping for declarations within this
1890 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001891 F.DeclRemap.insertOrReplace(
1892 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001893
Douglas Gregora1be2782011-12-17 23:38:30 +00001894 // Introduce the global -> local mapping for declarations within this
1895 // module.
1896 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1897
Douglas Gregor496c7092011-08-03 15:48:04 +00001898 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1899 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001900 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001901 }
1902
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001903 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001904 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001905 DeclContextInfo &Info = F.DeclContextInfos[TU];
1906 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1907 Info.NumLexicalDecls
1908 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001909 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001910 break;
1911 }
1912
Sebastian Redle1dde812010-08-24 00:50:04 +00001913 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001914 unsigned Idx = 0;
1915 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001916 ASTDeclContextNameLookupTable *Table =
1917 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001918 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001919 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001920 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001921 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1922 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001923 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001924 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001925 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001926 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001927 break;
1928 }
1929
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001930 case LANGUAGE_OPTIONS:
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001931 if (ParseLanguageOptions(F, Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001932 return IgnorePCH;
1933 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001934
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001935 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001936 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001937 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001938 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001939 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001940 (const unsigned char *)F.IdentifierTableData + Record[0],
1941 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001942 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001943
1944 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001945 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001946 break;
1947
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001948 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001949 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001950 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001951 return Failure;
1952 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001953 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1954 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001955 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001956 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001957
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001958 if (F.LocalNumIdentifiers > 0) {
1959 // Introduce the global -> local mapping for identifiers within this
1960 // module.
1961 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1962 &F));
1963
1964 // Introduce the local -> global mapping for identifiers within this
1965 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001966 F.IdentifierRemap.insertOrReplace(
1967 std::make_pair(LocalBaseIdentifierID,
1968 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001969
1970 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1971 + F.LocalNumIdentifiers);
1972 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001973 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001974 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001977 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1978 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001979 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001980
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001981 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001982 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1983 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001984 break;
1985
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001986 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001987 TotalNumStatements += Record[0];
1988 TotalNumMacros += Record[1];
1989 TotalLexicalDeclContexts += Record[2];
1990 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001991 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001992
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001993 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001994 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1995 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001996 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001997
Sean Huntebcbe1d2011-05-04 23:29:54 +00001998 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001999 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2000 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002001 break;
2002
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002003 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002004 if (Record.size() % 4 != 0) {
2005 Error("invalid weak identifiers record");
2006 return Failure;
2007 }
2008
2009 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2010 // files. This isn't the way to do it :)
2011 WeakUndeclaredIdentifiers.clear();
2012
2013 // Translate the weak, undeclared identifiers into global IDs.
2014 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2015 WeakUndeclaredIdentifiers.push_back(
2016 getGlobalIdentifierID(F, Record[I++]));
2017 WeakUndeclaredIdentifiers.push_back(
2018 getGlobalIdentifierID(F, Record[I++]));
2019 WeakUndeclaredIdentifiers.push_back(
2020 ReadSourceLocation(F, Record, I).getRawEncoding());
2021 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2022 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002023 break;
2024
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002025 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002026 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2027 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002028 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002029
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002030 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002031 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002032 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002033 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002034 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002035
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002036 if (F.LocalNumSelectors > 0) {
2037 // Introduce the global -> local mapping for selectors within this
2038 // module.
2039 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2040
2041 // Introduce the local -> global mapping for selectors within this
2042 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002043 F.SelectorRemap.insertOrReplace(
2044 std::make_pair(LocalBaseSelectorID,
2045 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002046
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002047 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2048 }
2049 break;
2050 }
2051
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002052 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002053 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002054 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002055 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002056 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002057 F.SelectorLookupTableData + Record[0],
2058 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002059 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002060 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002061 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002062
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002063 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002064 if (!Record.empty()) {
2065 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2066 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2067 Record[Idx++]));
2068 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2069 getRawEncoding());
2070 }
2071 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002072 break;
2073
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002074 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002075 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002076 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002077 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002078
2079 case FILE_SORTED_DECLS:
2080 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002081 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002082 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002083
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002084 case SOURCE_LOCATION_OFFSETS: {
2085 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002086 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002087 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002088 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002089 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2090 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002091 // Make our entry in the range map. BaseID is negative and growing, so
2092 // we invert it. Because we invert it, though, we need the other end of
2093 // the range.
2094 unsigned RangeStart =
2095 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2096 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2097 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2098
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002099 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2100 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2101 GlobalSLocOffsetMap.insert(
2102 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2103 - SLocSpaceSize,&F));
2104
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002105 // Initialize the remapping table.
2106 // Invalid stays invalid.
2107 F.SLocRemap.insert(std::make_pair(0U, 0));
2108 // This module. Base was 2 when being compiled.
2109 F.SLocRemap.insert(std::make_pair(2U,
2110 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002111
2112 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002113 break;
2114 }
2115
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002116 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002117 // Additional remapping information.
2118 const unsigned char *Data = (const unsigned char*)BlobStart;
2119 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002120
2121 // Continuous range maps we may be updating in our module.
2122 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002123 ContinuousRangeMap<uint32_t, int, 2>::Builder
2124 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002125 ContinuousRangeMap<uint32_t, int, 2>::Builder
2126 MacroRemap(F.MacroRemap);
2127 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002128 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2129 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002130 SubmoduleRemap(F.SubmoduleRemap);
2131 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002132 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002133 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002134 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2135
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002136 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002137 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002138 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002139 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002140 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002141 if (!OM) {
2142 Error("SourceLocation remap refers to unknown module");
2143 return Failure;
2144 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002145
2146 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2147 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002148 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002149 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002150 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002151 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2152 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002153 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002154
2155 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2156 SLocRemap.insert(std::make_pair(SLocOffset,
2157 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002158 IdentifierRemap.insert(
2159 std::make_pair(IdentifierIDOffset,
2160 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002161 MacroRemap.insert(std::make_pair(MacroIDOffset,
2162 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002163 PreprocessedEntityRemap.insert(
2164 std::make_pair(PreprocessedEntityIDOffset,
2165 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002166 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2167 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002168 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2169 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002170 DeclRemap.insert(std::make_pair(DeclIDOffset,
2171 OM->BaseDeclID - DeclIDOffset));
2172
Douglas Gregora119da02011-08-02 16:26:37 +00002173 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002174 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002175
2176 // Global -> local mappings.
2177 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002178 }
2179 break;
2180 }
2181
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002182 case SOURCE_MANAGER_LINE_TABLE:
2183 if (ParseLineTable(F, Record))
2184 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002185 break;
2186
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002187 case FILE_SOURCE_LOCATION_OFFSETS:
2188 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2189 F.LocalNumSLocFileEntries = Record[0];
2190 break;
2191
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002192 case SOURCE_LOCATION_PRELOADS: {
2193 // Need to transform from the local view (1-based IDs) to the global view,
2194 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002195 if (!F.PreloadSLocEntries.empty()) {
2196 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2197 return Failure;
2198 }
2199
2200 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002201 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002202 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002203
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002204 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002205 if (!DisableStatCache) {
2206 ASTStatCache *MyStatCache =
2207 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2208 (const unsigned char *)BlobStart,
2209 NumStatHits, NumStatMisses);
2210 FileMgr.addStatCache(MyStatCache);
2211 F.StatCache = MyStatCache;
2212 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002213 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002214 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002217 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2218 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002219 break;
2220
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002221 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002222 if (Record.size() % 3 != 0) {
2223 Error("Invalid VTABLE_USES record");
2224 return Failure;
2225 }
2226
Sebastian Redl40566802010-08-05 18:21:25 +00002227 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002228 // FIXME: Modules will have some trouble with this. This is clearly not
2229 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002230 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002231
2232 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2233 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2234 VTableUses.push_back(
2235 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2236 VTableUses.push_back(Record[Idx++]);
2237 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002238 break;
2239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002240 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002241 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2242 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002243 break;
2244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002245 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002246 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002247 Error("Invalid existing PendingInstantiations");
2248 return Failure;
2249 }
2250
2251 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002252 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2253 return Failure;
2254 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002255
Douglas Gregorf2abb522011-07-28 19:26:52 +00002256 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2257 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2258 PendingInstantiations.push_back(
2259 ReadSourceLocation(F, Record, I).getRawEncoding());
2260 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002261 break;
2262
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002263 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002264 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002265 // FIXME: Modules will have some trouble with this.
2266 SemaDeclRefs.clear();
2267 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2268 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002269 break;
2270
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002271 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002272 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002273 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002274 ActualOriginalFileName.assign(BlobStart, BlobLen);
2275 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002276 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002277 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Douglas Gregor31d375f2011-05-06 21:43:30 +00002279 case ORIGINAL_FILE_ID:
2280 OriginalFileID = FileID::get(Record[0]);
2281 break;
2282
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002283 case ORIGINAL_PCH_DIR:
2284 // The primary AST will be the last to get here, so it will be the one
2285 // that's used.
2286 OriginalDir.assign(BlobStart, BlobLen);
2287 break;
2288
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002289 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002290 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002291 StringRef ASTBranch(BlobStart, BlobLen);
2292 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002293 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002294 return IgnorePCH;
2295 }
2296 break;
2297 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002298
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002299 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002300 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2301 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2302 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002303
2304 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002305
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002306 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002307 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002308 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002309 if (!PP.getPreprocessingRecord()->getExternalSource())
2310 PP.getPreprocessingRecord()->SetExternalSource(*this);
2311 StartingID
2312 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002313 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002314 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002315
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002316 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002317 // Introduce the global -> local mapping for preprocessed entities in
2318 // this module.
2319 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2320
2321 // Introduce the local -> global mapping for preprocessed entities in
2322 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002323 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002324 std::make_pair(LocalBasePreprocessedEntityID,
2325 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2326 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002327
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002328 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002329 }
2330
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002331 case DECL_UPDATE_OFFSETS: {
2332 if (Record.size() % 2 != 0) {
2333 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2334 return Failure;
2335 }
2336 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002337 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2338 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002339 break;
2340 }
2341
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002342 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002343 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002344 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002345 return Failure;
2346 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002347 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002348 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002349 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002350 break;
2351 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002352
Douglas Gregorcff9f262012-01-27 01:47:08 +00002353 case OBJC_CATEGORIES_MAP: {
2354 if (F.LocalNumObjCCategoriesInMap != 0) {
2355 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002356 return Failure;
2357 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002358
2359 F.LocalNumObjCCategoriesInMap = Record[0];
2360 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002361 break;
2362 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002363
Douglas Gregorcff9f262012-01-27 01:47:08 +00002364 case OBJC_CATEGORIES:
2365 F.ObjCCategories.swap(Record);
2366 break;
2367
Douglas Gregor7c789c12010-10-29 22:39:52 +00002368 case CXX_BASE_SPECIFIER_OFFSETS: {
2369 if (F.LocalNumCXXBaseSpecifiers != 0) {
2370 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2371 return Failure;
2372 }
2373
2374 F.LocalNumCXXBaseSpecifiers = Record[0];
2375 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002376 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002377 break;
2378 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002379
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002380 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002381 if (Record.size() % 2 != 0) {
2382 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2383 return Failure;
2384 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002385
2386 if (F.PragmaDiagMappings.empty())
2387 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002388 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002389 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2390 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002391 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002392
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002393 case CUDA_SPECIAL_DECL_REFS:
2394 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002395 // FIXME: Modules will have trouble with this.
2396 CUDASpecialDeclRefs.clear();
2397 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2398 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002399 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002400
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002401 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002402 F.HeaderFileInfoTableData = BlobStart;
2403 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002404 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002405 if (Record[0]) {
2406 F.HeaderFileInfoTable
2407 = HeaderFileInfoLookupTable::Create(
2408 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002409 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002410 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002411 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002412 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002413
2414 PP.getHeaderSearchInfo().SetExternalSource(this);
2415 if (!PP.getHeaderSearchInfo().getExternalLookup())
2416 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002417 }
2418 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002419 }
2420
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002421 case FP_PRAGMA_OPTIONS:
2422 // Later tables overwrite earlier ones.
2423 FPPragmaOptions.swap(Record);
2424 break;
2425
2426 case OPENCL_EXTENSIONS:
2427 // Later tables overwrite earlier ones.
2428 OpenCLExtensions.swap(Record);
2429 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002430
2431 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002432 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2433 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002434 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002435
2436 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002437 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2438 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002439 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002440
2441 case IMPORTED_MODULES: {
2442 if (F.Kind != MK_Module) {
2443 // If we aren't loading a module (which has its own exports), make
2444 // all of the imported modules visible.
2445 // FIXME: Deal with macros-only imports.
2446 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2447 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2448 ImportedModules.push_back(GlobalID);
2449 }
2450 }
2451 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002452 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002453
Douglas Gregora1be2782011-12-17 23:38:30 +00002454 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002455 F.RedeclarationChains.swap(Record);
2456 break;
2457 }
2458
2459 case LOCAL_REDECLARATIONS_MAP: {
2460 if (F.LocalNumRedeclarationsInMap != 0) {
2461 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002462 return Failure;
2463 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002464
Douglas Gregor2171bf12012-01-15 16:58:34 +00002465 F.LocalNumRedeclarationsInMap = Record[0];
2466 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002467 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002468 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002469
2470 case MERGED_DECLARATIONS: {
2471 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2472 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2473 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2474 for (unsigned N = Record[Idx++]; N > 0; --N)
2475 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2476 }
2477 break;
2478 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002479
2480 case MACRO_OFFSET: {
2481 if (F.LocalNumMacros != 0) {
2482 Error("duplicate MACRO_OFFSET record in AST file");
2483 return Failure;
2484 }
2485 F.MacroOffsets = (const uint32_t *)BlobStart;
2486 F.LocalNumMacros = Record[0];
2487 unsigned LocalBaseMacroID = Record[1];
2488 F.BaseMacroID = getTotalNumMacros();
2489
2490 if (F.LocalNumMacros > 0) {
2491 // Introduce the global -> local mapping for macros within this module.
2492 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2493
2494 // Introduce the local -> global mapping for macros within this module.
2495 F.MacroRemap.insertOrReplace(
2496 std::make_pair(LocalBaseMacroID,
2497 F.BaseMacroID - LocalBaseMacroID));
2498
2499 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2500 }
2501 break;
2502 }
2503
2504 case MACRO_UPDATES: {
2505 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2506 MacroID ID = getGlobalMacroID(F, Record[I++]);
2507 if (I == N)
2508 break;
2509
2510 MacroUpdates[ID].UndefLoc = ReadSourceLocation(F, Record, I);
2511 }
2512 break;
2513 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002514 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002515 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002516 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002517 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002518}
2519
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002520ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002521 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002522
Douglas Gregorc69a2922011-08-25 20:58:51 +00002523 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2524 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2525 unsigned Code = SLocEntryCursor.ReadCode();
2526 if (Code == llvm::bitc::END_BLOCK ||
2527 Code == llvm::bitc::ENTER_SUBBLOCK ||
2528 Code == llvm::bitc::DEFINE_ABBREV) {
2529 Error("incorrectly-formatted source location entry in AST file");
2530 return Failure;
2531 }
2532
2533 RecordData Record;
2534 const char *BlobStart;
2535 unsigned BlobLen;
2536 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2537 default:
2538 Error("incorrectly-formatted source location entry in AST file");
2539 return Failure;
2540
2541 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002542 // If the buffer was overridden, the file need not exist.
2543 if (Record[6])
2544 break;
2545
Douglas Gregorc69a2922011-08-25 20:58:51 +00002546 StringRef Filename(BlobStart, BlobLen);
2547 const FileEntry *File = getFileEntry(Filename);
2548
2549 if (File == 0) {
2550 std::string ErrorStr = "could not find file '";
2551 ErrorStr += Filename;
2552 ErrorStr += "' referenced by AST file";
2553 Error(ErrorStr.c_str());
2554 return IgnorePCH;
2555 }
2556
Douglas Gregora081da52011-11-16 20:05:18 +00002557 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002558 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002559 return Failure;
2560 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002561
2562 off_t StoredSize = (off_t)Record[4];
2563 time_t StoredTime = (time_t)Record[5];
2564
2565 // Check if there was a request to override the contents of the file
2566 // that was part of the precompiled header. Overridding such a file
2567 // can lead to problems when lexing using the source locations from the
2568 // PCH.
2569 SourceManager &SM = getSourceManager();
2570 if (SM.isFileOverridden(File)) {
2571 Error(diag::err_fe_pch_file_overridden, Filename);
2572 // After emitting the diagnostic, recover by disabling the override so
2573 // that the original file will be used.
2574 SM.disableFileContentsOverride(File);
2575 // The FileEntry is a virtual file entry with the size of the contents
2576 // that would override the original contents. Set it to the original's
2577 // size/time.
2578 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2579 StoredSize, StoredTime);
2580 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002581
Douglas Gregorc69a2922011-08-25 20:58:51 +00002582 // The stat info from the FileEntry came from the cached stat
2583 // info of the PCH, so we cannot trust it.
2584 struct stat StatBuf;
2585 if (::stat(File->getName(), &StatBuf) != 0) {
2586 StatBuf.st_size = File->getSize();
2587 StatBuf.st_mtime = File->getModificationTime();
2588 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002589
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002590 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002591#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002592 // In our regression testing, the Windows file system seems to
2593 // have inconsistent modification times that sometimes
2594 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002595 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002596#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002597 )) {
2598 Error(diag::err_fe_pch_file_modified, Filename);
2599 return IgnorePCH;
2600 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002601
Douglas Gregorc69a2922011-08-25 20:58:51 +00002602 break;
2603 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002604 }
2605 }
2606
2607 return Success;
2608}
2609
Douglas Gregorecc2c092011-12-01 22:20:10 +00002610void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002611 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002612 if (Names[I].isDecl()) {
2613 Names[I].getDecl()->Hidden = false;
2614 continue;
2615 }
2616
2617 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2618 Macro.second->setHidden(!Macro.second->isPublic());
2619 if (Macro.second->isDefined()) {
2620 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2621 if (DeserializationListener)
2622 DeserializationListener->MacroVisible(Macro.first);
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002623 }
Douglas Gregor13292642011-12-02 15:45:10 +00002624 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002625}
2626
Douglas Gregor5e356932011-12-01 17:11:21 +00002627void ASTReader::makeModuleVisible(Module *Mod,
2628 Module::NameVisibilityKind NameVisibility) {
2629 llvm::SmallPtrSet<Module *, 4> Visited;
2630 llvm::SmallVector<Module *, 4> Stack;
2631 Stack.push_back(Mod);
2632 while (!Stack.empty()) {
2633 Mod = Stack.back();
2634 Stack.pop_back();
2635
2636 if (NameVisibility <= Mod->NameVisibility) {
2637 // This module already has this level of visibility (or greater), so
2638 // there is nothing more to do.
2639 continue;
2640 }
2641
Douglas Gregor51f564f2011-12-31 04:05:44 +00002642 if (!Mod->isAvailable()) {
2643 // Modules that aren't available cannot be made visible.
2644 continue;
2645 }
2646
Douglas Gregor5e356932011-12-01 17:11:21 +00002647 // Update the module's name visibility.
2648 Mod->NameVisibility = NameVisibility;
2649
Douglas Gregorecc2c092011-12-01 22:20:10 +00002650 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002651 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002652 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2653 if (Hidden != HiddenNamesMap.end()) {
2654 makeNamesVisible(Hidden->second);
2655 HiddenNamesMap.erase(Hidden);
2656 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002657
2658 // Push any non-explicit submodules onto the stack to be marked as
2659 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002660 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2661 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002662 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002663 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2664 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002665 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002666
2667 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002668 bool AnyWildcard = false;
2669 bool UnrestrictedWildcard = false;
2670 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002671 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2672 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002673 if (!Mod->Exports[I].getInt()) {
2674 // Export a named module directly; no wildcards involved.
2675 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002676 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002677
2678 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002679 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002680
2681 // Wildcard export: export all of the imported modules that match
2682 // the given pattern.
2683 AnyWildcard = true;
2684 if (UnrestrictedWildcard)
2685 continue;
2686
2687 if (Module *Restriction = Mod->Exports[I].getPointer())
2688 WildcardRestrictions.push_back(Restriction);
2689 else {
2690 WildcardRestrictions.clear();
2691 UnrestrictedWildcard = true;
2692 }
2693 }
2694
2695 // If there were any wildcards, push any imported modules that were
2696 // re-exported by the wildcard restriction.
2697 if (!AnyWildcard)
2698 continue;
2699
2700 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2701 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002702 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002703 continue;
2704
2705 bool Acceptable = UnrestrictedWildcard;
2706 if (!Acceptable) {
2707 // Check whether this module meets one of the restrictions.
2708 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2709 Module *Restriction = WildcardRestrictions[R];
2710 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2711 Acceptable = true;
2712 break;
2713 }
2714 }
2715 }
2716
2717 if (!Acceptable)
2718 continue;
2719
Douglas Gregor0adaa882011-12-05 17:28:06 +00002720 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002721 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002722 }
2723}
2724
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002725ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002726 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002727 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002728 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002729
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002730 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002731 case Failure: return Failure;
2732 case IgnorePCH: return IgnorePCH;
2733 case Success: break;
2734 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002735
2736 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002737
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002738 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002739 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002740 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2741 // if DisableValidation is true, defines that were set on command-line
2742 // but not in the PCH file will not be added to SuggestedPredefines.
2743 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002744 return IgnorePCH;
2745
Douglas Gregoreee242f2011-10-27 09:33:13 +00002746 // Mark all of the identifiers in the identifier table as being out of date,
2747 // so that various accessors know to check the loaded modules when the
2748 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002749 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2750 IdEnd = PP.getIdentifierTable().end();
2751 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002752 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002753
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002754 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002755 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2756 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2757 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002758 Module *ResolvedMod = getSubmodule(GlobalID);
2759
2760 if (Unresolved.IsImport) {
2761 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002762 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002763 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002764 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002765
2766 if (ResolvedMod || Unresolved.IsWildcard)
2767 Unresolved.Mod->Exports.push_back(
2768 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002769 }
Douglas Gregor55988682011-12-05 16:33:54 +00002770 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002771
Douglas Gregor35942772011-09-09 21:34:22 +00002772 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002773
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002774 if (DeserializationListener)
2775 DeserializationListener->ReaderInitialized(this);
2776
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002777 if (!OriginalFileID.isInvalid()) {
2778 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2779 + OriginalFileID.getOpaqueValue() - 1);
2780
2781 // If this AST file is a precompiled preamble, then set the preamble file ID
2782 // of the source manager to the file source file from which the preamble was
2783 // built.
2784 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002785 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002786 } else if (Type == MK_MainFile) {
2787 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002788 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002789 }
2790
Douglas Gregorcff9f262012-01-27 01:47:08 +00002791 // For any Objective-C class definitions we have already loaded, make sure
2792 // that we load any additional categories.
2793 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2794 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2795 ObjCClassesLoaded[I],
2796 PreviousGeneration);
2797 }
2798
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002799 return Success;
2800}
2801
Chris Lattner5f9e2722011-07-23 10:55:15 +00002802ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002803 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002804 ModuleFile *ImportedBy) {
2805 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002806 bool NewModule;
2807 std::string ErrorStr;
2808 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002809 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002810
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002811 if (!M) {
2812 // We couldn't load the module.
2813 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2814 + ErrorStr;
2815 Error(Msg);
2816 return Failure;
2817 }
2818
2819 if (!NewModule) {
2820 // We've already loaded this module.
2821 return Success;
2822 }
2823
2824 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2825 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002826 if (FileName != "-") {
2827 CurrentDir = llvm::sys::path::parent_path(FileName);
2828 if (CurrentDir.empty()) CurrentDir = ".";
2829 }
2830
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002831 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002832 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002833 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002834 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002835
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002836 // Sniff for the signature.
2837 if (Stream.Read(8) != 'C' ||
2838 Stream.Read(8) != 'P' ||
2839 Stream.Read(8) != 'C' ||
2840 Stream.Read(8) != 'H') {
2841 Diag(diag::err_not_a_pch_file) << FileName;
2842 return Failure;
2843 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002844
Douglas Gregor2cf26342009-04-09 22:27:44 +00002845 while (!Stream.AtEndOfStream()) {
2846 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002847
Douglas Gregore1d918e2009-04-10 23:10:45 +00002848 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002849 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002850 return Failure;
2851 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002852
2853 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002854
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002855 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002856 switch (BlockID) {
2857 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002858 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002859 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002860 return Failure;
2861 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002862 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002863 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002864 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002865 case Success:
2866 break;
2867
2868 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002869 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002870
2871 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002872 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002873 // AST block, skipping subblocks, to see if there are other
2874 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002875
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002876 // FIXME: We can't clear loaded slocentries anymore.
2877 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002878
2879 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002880 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002881 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002882
Douglas Gregore1d918e2009-04-10 23:10:45 +00002883 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002884 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002885 break;
2886 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002887 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002888 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002889 return Failure;
2890 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002891 break;
2892 }
Mike Stump1eb44332009-09-09 15:08:12 +00002893 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002894
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002895 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002896 // bits of all files we've seen.
2897 F.GlobalBitOffset = TotalModulesSizeInBits;
2898 TotalModulesSizeInBits += F.SizeInBits;
2899 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002900
2901 // Make sure that the files this module was built against are still available.
2902 if (!DisableValidation) {
2903 switch(validateFileEntries(*M)) {
2904 case Failure: return Failure;
2905 case IgnorePCH: return IgnorePCH;
2906 case Success: break;
2907 }
2908 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002909
2910 // Preload SLocEntries.
2911 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2912 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002913 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2914 // directly because the entry may have already been loaded in which case
2915 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2916 // SourceManager.
2917 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002918 }
2919
Douglas Gregorc69a2922011-08-25 20:58:51 +00002920
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002921 return Success;
2922}
2923
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002924void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002925 // If there's a listener, notify them that we "read" the translation unit.
2926 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002927 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2928 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002929
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002930 // Make sure we load the declaration update records for the translation unit,
2931 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002932 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2933 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002934
Douglas Gregor5f957282011-08-11 22:18:49 +00002935 // FIXME: Find a better way to deal with collisions between these
2936 // built-in types. Right now, we just ignore the problem.
2937
2938 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002939 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002940 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2941 if (!Context.CFConstantStringTypeDecl)
2942 Context.setCFConstantStringType(GetType(String));
2943 }
2944
2945 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2946 QualType FileType = GetType(File);
2947 if (FileType.isNull()) {
2948 Error("FILE type is NULL");
2949 return;
2950 }
2951
2952 if (!Context.FILEDecl) {
2953 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2954 Context.setFILEDecl(Typedef->getDecl());
2955 else {
2956 const TagType *Tag = FileType->getAs<TagType>();
2957 if (!Tag) {
2958 Error("Invalid FILE type in AST file");
2959 return;
2960 }
2961 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002962 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002963 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002964 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002965
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002966 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002967 QualType Jmp_bufType = GetType(Jmp_buf);
2968 if (Jmp_bufType.isNull()) {
2969 Error("jmp_buf type is NULL");
2970 return;
2971 }
2972
2973 if (!Context.jmp_bufDecl) {
2974 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2975 Context.setjmp_bufDecl(Typedef->getDecl());
2976 else {
2977 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2978 if (!Tag) {
2979 Error("Invalid jmp_buf type in AST file");
2980 return;
2981 }
2982 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002983 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002984 }
Mike Stump782fa302009-07-28 02:25:19 +00002985 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002986
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002987 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002988 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2989 if (Sigjmp_bufType.isNull()) {
2990 Error("sigjmp_buf type is NULL");
2991 return;
2992 }
2993
2994 if (!Context.sigjmp_bufDecl) {
2995 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2996 Context.setsigjmp_bufDecl(Typedef->getDecl());
2997 else {
2998 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2999 assert(Tag && "Invalid sigjmp_buf type in AST file");
3000 Context.setsigjmp_bufDecl(Tag->getDecl());
3001 }
3002 }
3003 }
3004
3005 if (unsigned ObjCIdRedef
3006 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3007 if (Context.ObjCIdRedefinitionType.isNull())
3008 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3009 }
3010
3011 if (unsigned ObjCClassRedef
3012 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3013 if (Context.ObjCClassRedefinitionType.isNull())
3014 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3015 }
3016
3017 if (unsigned ObjCSelRedef
3018 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3019 if (Context.ObjCSelRedefinitionType.isNull())
3020 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3021 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003022
3023 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3024 QualType Ucontext_tType = GetType(Ucontext_t);
3025 if (Ucontext_tType.isNull()) {
3026 Error("ucontext_t type is NULL");
3027 return;
3028 }
3029
3030 if (!Context.ucontext_tDecl) {
3031 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3032 Context.setucontext_tDecl(Typedef->getDecl());
3033 else {
3034 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3035 assert(Tag && "Invalid ucontext_t type in AST file");
3036 Context.setucontext_tDecl(Tag->getDecl());
3037 }
3038 }
3039 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003040 }
3041
Douglas Gregor35942772011-09-09 21:34:22 +00003042 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003043
3044 // If there were any CUDA special declarations, deserialize them.
3045 if (!CUDASpecialDeclRefs.empty()) {
3046 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003047 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003048 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3049 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003050
3051 // Re-export any modules that were imported by a non-module AST file.
3052 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3053 if (Module *Imported = getSubmodule(ImportedModules[I]))
3054 makeModuleVisible(Imported, Module::AllVisible);
3055 }
3056 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003057}
3058
Douglas Gregorecc2c092011-12-01 22:20:10 +00003059void ASTReader::finalizeForWriting() {
3060 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3061 HiddenEnd = HiddenNamesMap.end();
3062 Hidden != HiddenEnd; ++Hidden) {
3063 makeNamesVisible(Hidden->second);
3064 }
3065 HiddenNamesMap.clear();
3066}
3067
Douglas Gregorb64c1932009-05-12 01:31:05 +00003068/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003069/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003070/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003071std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003072 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003073 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003074 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003075 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003076 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003077 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003078 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003079 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003080 return std::string();
3081 }
3082
3083 // Initialize the stream
3084 llvm::BitstreamReader StreamFile;
3085 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003086 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003087 (const unsigned char *)Buffer->getBufferEnd());
3088 Stream.init(StreamFile);
3089
3090 // Sniff for the signature.
3091 if (Stream.Read(8) != 'C' ||
3092 Stream.Read(8) != 'P' ||
3093 Stream.Read(8) != 'C' ||
3094 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003095 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003096 return std::string();
3097 }
3098
3099 RecordData Record;
3100 while (!Stream.AtEndOfStream()) {
3101 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Douglas Gregorb64c1932009-05-12 01:31:05 +00003103 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3104 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003105
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003106 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003107 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003108 case AST_BLOCK_ID:
3109 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003110 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003111 return std::string();
3112 }
3113 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003114
Douglas Gregorb64c1932009-05-12 01:31:05 +00003115 default:
3116 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003117 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003118 return std::string();
3119 }
3120 break;
3121 }
3122 continue;
3123 }
3124
3125 if (Code == llvm::bitc::END_BLOCK) {
3126 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003127 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003128 return std::string();
3129 }
3130 continue;
3131 }
3132
3133 if (Code == llvm::bitc::DEFINE_ABBREV) {
3134 Stream.ReadAbbrevRecord();
3135 continue;
3136 }
3137
3138 Record.clear();
3139 const char *BlobStart = 0;
3140 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003141 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003142 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003143 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003144 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003145
3146 return std::string();
3147}
3148
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003149ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003150 // Enter the submodule block.
3151 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3152 Error("malformed submodule block record in AST file");
3153 return Failure;
3154 }
3155
3156 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003157 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003158 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003159 RecordData Record;
3160 while (true) {
3161 unsigned Code = F.Stream.ReadCode();
3162 if (Code == llvm::bitc::END_BLOCK) {
3163 if (F.Stream.ReadBlockEnd()) {
3164 Error("error at end of submodule block in AST file");
3165 return Failure;
3166 }
3167 return Success;
3168 }
3169
3170 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3171 // No known subblocks, always skip them.
3172 F.Stream.ReadSubBlockID();
3173 if (F.Stream.SkipBlock()) {
3174 Error("malformed block record in AST file");
3175 return Failure;
3176 }
3177 continue;
3178 }
3179
3180 if (Code == llvm::bitc::DEFINE_ABBREV) {
3181 F.Stream.ReadAbbrevRecord();
3182 continue;
3183 }
3184
3185 // Read a record.
3186 const char *BlobStart;
3187 unsigned BlobLen;
3188 Record.clear();
3189 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3190 default: // Default behavior: ignore.
3191 break;
3192
3193 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003194 if (First) {
3195 Error("missing submodule metadata record at beginning of block");
3196 return Failure;
3197 }
3198
Douglas Gregore209e502011-12-06 01:10:29 +00003199 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003200 Error("malformed module definition");
3201 return Failure;
3202 }
3203
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003204 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003205 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3206 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3207 bool IsFramework = Record[2];
3208 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003209 bool IsSystem = Record[4];
3210 bool InferSubmodules = Record[5];
3211 bool InferExplicitSubmodules = Record[6];
3212 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003213
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003214 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003215 if (Parent)
3216 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003217
3218 // Retrieve this (sub)module from the module map, creating it if
3219 // necessary.
3220 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3221 IsFramework,
3222 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003223 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3224 if (GlobalIndex >= SubmodulesLoaded.size() ||
3225 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003226 Error("too many submodules");
3227 return Failure;
3228 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003229
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003230 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003231 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003232 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003233 CurrentModule->InferSubmodules = InferSubmodules;
3234 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3235 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003236 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003237 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003238
Douglas Gregore209e502011-12-06 01:10:29 +00003239 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003240 break;
3241 }
3242
Douglas Gregor77d029f2011-12-08 19:11:24 +00003243 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003244 if (First) {
3245 Error("missing submodule metadata record at beginning of block");
3246 return Failure;
3247 }
3248
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003249 if (!CurrentModule)
3250 break;
3251
3252 StringRef FileName(BlobStart, BlobLen);
3253 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003254 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003255 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003256 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003257 Error("mismatched umbrella headers in submodule");
3258 return Failure;
3259 }
3260 }
3261 break;
3262 }
3263
3264 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003265 if (First) {
3266 Error("missing submodule metadata record at beginning of block");
3267 return Failure;
3268 }
3269
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003270 if (!CurrentModule)
3271 break;
3272
3273 // FIXME: Be more lazy about this!
3274 StringRef FileName(BlobStart, BlobLen);
3275 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3276 if (std::find(CurrentModule->Headers.begin(),
3277 CurrentModule->Headers.end(),
3278 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003279 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003280 }
3281 break;
3282 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003283
3284 case SUBMODULE_TOPHEADER: {
3285 if (First) {
3286 Error("missing submodule metadata record at beginning of block");
3287 return Failure;
3288 }
3289
3290 if (!CurrentModule)
3291 break;
3292
3293 // FIXME: Be more lazy about this!
3294 StringRef FileName(BlobStart, BlobLen);
3295 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3296 CurrentModule->TopHeaders.insert(File);
3297 break;
3298 }
3299
Douglas Gregor77d029f2011-12-08 19:11:24 +00003300 case SUBMODULE_UMBRELLA_DIR: {
3301 if (First) {
3302 Error("missing submodule metadata record at beginning of block");
3303 return Failure;
3304 }
3305
3306 if (!CurrentModule)
3307 break;
3308
3309 StringRef DirName(BlobStart, BlobLen);
3310 if (const DirectoryEntry *Umbrella
3311 = PP.getFileManager().getDirectory(DirName)) {
3312 if (!CurrentModule->getUmbrellaDir())
3313 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3314 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3315 Error("mismatched umbrella directories in submodule");
3316 return Failure;
3317 }
3318 }
3319 break;
3320 }
3321
Douglas Gregor26ced122011-12-01 00:59:36 +00003322 case SUBMODULE_METADATA: {
3323 if (!First) {
3324 Error("submodule metadata record not at beginning of block");
3325 return Failure;
3326 }
3327 First = false;
3328
3329 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003330 F.LocalNumSubmodules = Record[0];
3331 unsigned LocalBaseSubmoduleID = Record[1];
3332 if (F.LocalNumSubmodules > 0) {
3333 // Introduce the global -> local mapping for submodules within this
3334 // module.
3335 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3336
3337 // Introduce the local -> global mapping for submodules within this
3338 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003339 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003340 std::make_pair(LocalBaseSubmoduleID,
3341 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3342
3343 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3344 }
3345 break;
3346 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003347
Douglas Gregor55988682011-12-05 16:33:54 +00003348 case SUBMODULE_IMPORTS: {
3349 if (First) {
3350 Error("missing submodule metadata record at beginning of block");
3351 return Failure;
3352 }
3353
3354 if (!CurrentModule)
3355 break;
3356
3357 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3358 UnresolvedModuleImportExport Unresolved;
3359 Unresolved.File = &F;
3360 Unresolved.Mod = CurrentModule;
3361 Unresolved.ID = Record[Idx];
3362 Unresolved.IsImport = true;
3363 Unresolved.IsWildcard = false;
3364 UnresolvedModuleImportExports.push_back(Unresolved);
3365 }
3366 break;
3367 }
3368
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003369 case SUBMODULE_EXPORTS: {
3370 if (First) {
3371 Error("missing submodule metadata record at beginning of block");
3372 return Failure;
3373 }
3374
3375 if (!CurrentModule)
3376 break;
3377
3378 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003379 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003380 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003381 Unresolved.Mod = CurrentModule;
3382 Unresolved.ID = Record[Idx];
3383 Unresolved.IsImport = false;
3384 Unresolved.IsWildcard = Record[Idx + 1];
3385 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003386 }
3387
3388 // Once we've loaded the set of exports, there's no reason to keep
3389 // the parsed, unresolved exports around.
3390 CurrentModule->UnresolvedExports.clear();
3391 break;
3392 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003393 case SUBMODULE_REQUIRES: {
3394 if (First) {
3395 Error("missing submodule metadata record at beginning of block");
3396 return Failure;
3397 }
3398
3399 if (!CurrentModule)
3400 break;
3401
3402 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003403 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003404 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003405 break;
3406 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003407 }
3408 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003409}
3410
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003411/// \brief Parse the record that corresponds to a LangOptions data
3412/// structure.
3413///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003414/// This routine parses the language options from the AST file and then gives
3415/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003416///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003417/// \returns true if the listener deems the file unacceptable, false otherwise.
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003418bool ASTReader::ParseLanguageOptions(const ModuleFile &M,
3419 const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003420 if (Listener) {
3421 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003422 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003423#define LANGOPT(Name, Bits, Default, Description) \
3424 LangOpts.Name = Record[Idx++];
3425#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3426 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3427#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003428
3429 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3430 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3431 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003432
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003433 unsigned Length = Record[Idx++];
3434 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3435 Record.begin() + Idx + Length);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003436 return Listener->ReadLanguageOptions(M, LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003437 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003438
3439 return false;
3440}
3441
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003442std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003443ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003444 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003445 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003446 assert(I != GlobalPreprocessedEntityMap.end() &&
3447 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003448 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003449 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3450 return std::make_pair(M, LocalIndex);
3451}
3452
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003453std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3454ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3455 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3456 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3457 Mod.NumPreprocessedEntities);
3458
3459 return std::make_pair(PreprocessingRecord::iterator(),
3460 PreprocessingRecord::iterator());
3461}
3462
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003463std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3464ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3465 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3466 ModuleDeclIterator(this, &Mod,
3467 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3468}
3469
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003470PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3471 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003472 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3473 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003474 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003475 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003476
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003477 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003478 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003479
3480 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3481 switch (Code) {
3482 case llvm::bitc::END_BLOCK:
3483 return 0;
3484
3485 case llvm::bitc::ENTER_SUBBLOCK:
3486 Error("unexpected subblock record in preprocessor detail block");
3487 return 0;
3488
3489 case llvm::bitc::DEFINE_ABBREV:
3490 Error("unexpected abbrevation record in preprocessor detail block");
3491 return 0;
3492
3493 default:
3494 break;
3495 }
3496
3497 if (!PP.getPreprocessingRecord()) {
3498 Error("no preprocessing record");
3499 return 0;
3500 }
3501
3502 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003503 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3504 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003505 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3506 const char *BlobStart = 0;
3507 unsigned BlobLen = 0;
3508 RecordData Record;
3509 PreprocessorDetailRecordTypes RecType =
3510 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3511 Code, Record, BlobStart, BlobLen);
3512 switch (RecType) {
3513 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003514 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003515 IdentifierInfo *Name = 0;
3516 MacroDefinition *Def = 0;
3517 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003518 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003519 else {
3520 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003521 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003522 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3523 }
3524
3525 MacroExpansion *ME;
3526 if (isBuiltin)
3527 ME = new (PPRec) MacroExpansion(Name, Range);
3528 else
3529 ME = new (PPRec) MacroExpansion(Def, Range);
3530
3531 return ME;
3532 }
3533
3534 case PPD_MACRO_DEFINITION: {
3535 // Decode the identifier info and then check again; if the macro is
3536 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003537 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003538 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003539 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003540
3541 if (DeserializationListener)
3542 DeserializationListener->MacroDefinitionRead(PPID, MD);
3543
3544 return MD;
3545 }
3546
3547 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003548 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003549 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3550 const FileEntry *File = 0;
3551 if (!FullFileName.empty())
3552 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003553
3554 // FIXME: Stable encoding
3555 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003556 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003557 InclusionDirective *ID
3558 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003559 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003560 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003561 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003562 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003563 return ID;
3564 }
3565 }
David Blaikie7530c032012-01-17 06:56:22 +00003566
3567 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003568}
3569
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003570/// \brief \arg SLocMapI points at a chunk of a module that contains no
3571/// preprocessed entities or the entities it contains are not the ones we are
3572/// looking for. Find the next module that contains entities and return the ID
3573/// of the first entry.
3574PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3575 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3576 ++SLocMapI;
3577 for (GlobalSLocOffsetMapType::const_iterator
3578 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003579 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003580 if (M.NumPreprocessedEntities)
3581 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3582 }
3583
3584 return getTotalNumPreprocessedEntities();
3585}
3586
3587namespace {
3588
3589template <unsigned PPEntityOffset::*PPLoc>
3590struct PPEntityComp {
3591 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003592 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003593
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003594 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003595
Benjamin Kramer88df1252011-09-21 06:42:26 +00003596 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3597 SourceLocation LHS = getLoc(L);
3598 SourceLocation RHS = getLoc(R);
3599 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3600 }
3601
3602 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003603 SourceLocation LHS = getLoc(L);
3604 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3605 }
3606
Benjamin Kramer88df1252011-09-21 06:42:26 +00003607 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003608 SourceLocation RHS = getLoc(R);
3609 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3610 }
3611
3612 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3613 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3614 }
3615};
3616
3617}
3618
3619/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3620PreprocessedEntityID
3621ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3622 if (SourceMgr.isLocalSourceLocation(BLoc))
3623 return getTotalNumPreprocessedEntities();
3624
3625 GlobalSLocOffsetMapType::const_iterator
3626 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3627 BLoc.getOffset());
3628 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3629 "Corrupted global sloc offset map");
3630
3631 if (SLocMapI->second->NumPreprocessedEntities == 0)
3632 return findNextPreprocessedEntity(SLocMapI);
3633
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003634 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003635 typedef const PPEntityOffset *pp_iterator;
3636 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3637 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003638
3639 size_t Count = M.NumPreprocessedEntities;
3640 size_t Half;
3641 pp_iterator First = pp_begin;
3642 pp_iterator PPI;
3643
3644 // Do a binary search manually instead of using std::lower_bound because
3645 // The end locations of entities may be unordered (when a macro expansion
3646 // is inside another macro argument), but for this case it is not important
3647 // whether we get the first macro expansion or its containing macro.
3648 while (Count > 0) {
3649 Half = Count/2;
3650 PPI = First;
3651 std::advance(PPI, Half);
3652 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3653 BLoc)){
3654 First = PPI;
3655 ++First;
3656 Count = Count - Half - 1;
3657 } else
3658 Count = Half;
3659 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003660
3661 if (PPI == pp_end)
3662 return findNextPreprocessedEntity(SLocMapI);
3663
3664 return getGlobalPreprocessedEntityID(M,
3665 M.BasePreprocessedEntityID + (PPI - pp_begin));
3666}
3667
3668/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3669PreprocessedEntityID
3670ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3671 if (SourceMgr.isLocalSourceLocation(ELoc))
3672 return getTotalNumPreprocessedEntities();
3673
3674 GlobalSLocOffsetMapType::const_iterator
3675 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3676 ELoc.getOffset());
3677 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3678 "Corrupted global sloc offset map");
3679
3680 if (SLocMapI->second->NumPreprocessedEntities == 0)
3681 return findNextPreprocessedEntity(SLocMapI);
3682
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003683 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003684 typedef const PPEntityOffset *pp_iterator;
3685 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3686 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3687 pp_iterator PPI =
3688 std::upper_bound(pp_begin, pp_end, ELoc,
3689 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3690
3691 if (PPI == pp_end)
3692 return findNextPreprocessedEntity(SLocMapI);
3693
3694 return getGlobalPreprocessedEntityID(M,
3695 M.BasePreprocessedEntityID + (PPI - pp_begin));
3696}
3697
3698/// \brief Returns a pair of [Begin, End) indices of preallocated
3699/// preprocessed entities that \arg Range encompasses.
3700std::pair<unsigned, unsigned>
3701 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3702 if (Range.isInvalid())
3703 return std::make_pair(0,0);
3704 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3705
3706 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3707 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3708 return std::make_pair(BeginID, EndID);
3709}
3710
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003711/// \brief Optionally returns true or false if the preallocated preprocessed
3712/// entity with index \arg Index came from file \arg FID.
3713llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3714 FileID FID) {
3715 if (FID.isInvalid())
3716 return false;
3717
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003718 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3719 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003720 unsigned LocalIndex = PPInfo.second;
3721 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3722
3723 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3724 if (Loc.isInvalid())
3725 return false;
3726
3727 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3728 return true;
3729 else
3730 return false;
3731}
3732
Douglas Gregord10a3812011-08-25 18:14:34 +00003733namespace {
3734 /// \brief Visitor used to search for information about a header file.
3735 class HeaderFileInfoVisitor {
3736 ASTReader &Reader;
3737 const FileEntry *FE;
3738
3739 llvm::Optional<HeaderFileInfo> HFI;
3740
3741 public:
3742 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3743 : Reader(Reader), FE(FE) { }
3744
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003745 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003746 HeaderFileInfoVisitor *This
3747 = static_cast<HeaderFileInfoVisitor *>(UserData);
3748
3749 HeaderFileInfoTrait Trait(This->Reader, M,
3750 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3751 M.HeaderFileFrameworkStrings,
3752 This->FE->getName());
3753
3754 HeaderFileInfoLookupTable *Table
3755 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3756 if (!Table)
3757 return false;
3758
3759 // Look in the on-disk hash table for an entry for this file name.
3760 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3761 &Trait);
3762 if (Pos == Table->end())
3763 return false;
3764
3765 This->HFI = *Pos;
3766 return true;
3767 }
3768
3769 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3770 };
3771}
3772
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003773HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003774 HeaderFileInfoVisitor Visitor(*this, FE);
3775 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3776 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003777 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003778 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3779 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003780 }
3781
3782 return HeaderFileInfo();
3783}
3784
David Blaikied6471f72011-09-25 23:23:43 +00003785void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003786 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003787 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003788 unsigned Idx = 0;
3789 while (Idx < F.PragmaDiagMappings.size()) {
3790 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003791 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3792 Diag.DiagStatePoints.push_back(
3793 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3794 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003795 while (1) {
3796 assert(Idx < F.PragmaDiagMappings.size() &&
3797 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3798 if (Idx >= F.PragmaDiagMappings.size()) {
3799 break; // Something is messed up but at least avoid infinite loop in
3800 // release build.
3801 }
3802 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3803 if (DiagID == (unsigned)-1) {
3804 break; // no more diag/map pairs for this location.
3805 }
3806 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003807 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3808 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003809 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003810 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003811 }
3812}
3813
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003814/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003815ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003816 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003817 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003818 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003819 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003820}
3821
3822/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003823///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003824/// The index is the type ID, shifted and minus the number of predefs. This
3825/// routine actually reads the record corresponding to the type at the given
3826/// location. It is a helper routine for GetType, which deals with reading type
3827/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003828QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003829 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003830 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003831
Douglas Gregor0b748912009-04-14 21:18:50 +00003832 // Keep track of where we are in the stream, then jump back there
3833 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003834 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003835
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003836 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003837
Douglas Gregord89275b2009-07-06 18:54:52 +00003838 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003839 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003840
Douglas Gregor393f2492011-07-22 00:38:23 +00003841 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003842 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003843 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003844 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003845 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3846 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003847 if (Record.size() != 2) {
3848 Error("Incorrect encoding of extended qualifier type");
3849 return QualType();
3850 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003851 QualType Base = readType(*Loc.F, Record, Idx);
3852 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003853 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003854 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003855
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003856 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003857 if (Record.size() != 1) {
3858 Error("Incorrect encoding of complex type");
3859 return QualType();
3860 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003861 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003862 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003863 }
3864
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003865 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003866 if (Record.size() != 1) {
3867 Error("Incorrect encoding of pointer type");
3868 return QualType();
3869 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003870 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003871 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003872 }
3873
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003874 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003875 if (Record.size() != 1) {
3876 Error("Incorrect encoding of block pointer type");
3877 return QualType();
3878 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003879 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003880 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003881 }
3882
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003883 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003884 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003885 Error("Incorrect encoding of lvalue reference type");
3886 return QualType();
3887 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003888 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003889 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003890 }
3891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003892 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003893 if (Record.size() != 1) {
3894 Error("Incorrect encoding of rvalue reference type");
3895 return QualType();
3896 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003897 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003898 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003899 }
3900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003901 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003902 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003903 Error("Incorrect encoding of member pointer type");
3904 return QualType();
3905 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003906 QualType PointeeType = readType(*Loc.F, Record, Idx);
3907 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003908 if (PointeeType.isNull() || ClassType.isNull())
3909 return QualType();
3910
Douglas Gregor35942772011-09-09 21:34:22 +00003911 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003912 }
3913
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003914 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003915 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003916 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3917 unsigned IndexTypeQuals = Record[2];
3918 unsigned Idx = 3;
3919 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003920 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003921 ASM, IndexTypeQuals);
3922 }
3923
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003924 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003925 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003926 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3927 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003928 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003929 }
3930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003931 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003932 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003933 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3934 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003935 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3936 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003937 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003938 ASM, IndexTypeQuals,
3939 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003940 }
3941
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003942 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003943 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003944 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003945 return QualType();
3946 }
3947
Douglas Gregor393f2492011-07-22 00:38:23 +00003948 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003949 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003950 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003951 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003952 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003953 }
3954
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003955 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003956 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003957 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003958 return QualType();
3959 }
3960
Douglas Gregor393f2492011-07-22 00:38:23 +00003961 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003962 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003963 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003964 }
3965
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003966 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003967 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003968 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003969 return QualType();
3970 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003971 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003972 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3973 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003974 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003975 }
3976
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003977 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003978 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003979
3980 FunctionProtoType::ExtProtoInfo EPI;
3981 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003982 /*hasregparm*/ Record[2],
3983 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003984 static_cast<CallingConv>(Record[4]),
3985 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003986
John McCallf85e1932011-06-15 23:02:42 +00003987 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003988 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003989 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003990 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003991 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003992
3993 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00003994 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00003995 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003996 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003997 ExceptionSpecificationType EST =
3998 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3999 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004000 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004001 if (EST == EST_Dynamic) {
4002 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004003 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004004 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004005 EPI.Exceptions = Exceptions.data();
4006 } else if (EST == EST_ComputedNoexcept) {
4007 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004008 } else if (EST == EST_Uninstantiated) {
4009 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4010 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004011 } else if (EST == EST_Unevaluated) {
4012 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004013 }
Douglas Gregor35942772011-09-09 21:34:22 +00004014 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004015 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004016 }
4017
Douglas Gregor409448c2011-07-21 22:35:25 +00004018 case TYPE_UNRESOLVED_USING: {
4019 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004020 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004021 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4022 }
4023
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004024 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004025 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004026 Error("incorrect encoding of typedef type");
4027 return QualType();
4028 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004029 unsigned Idx = 0;
4030 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004031 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004032 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004033 Canonical = Context.getCanonicalType(Canonical);
4034 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004035 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004036
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004037 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004038 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004039
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004040 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004041 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004042 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004043 return QualType();
4044 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004045 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004046 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004047 }
Mike Stump1eb44332009-09-09 15:08:12 +00004048
Douglas Gregorf8af9822012-02-12 18:42:33 +00004049 case TYPE_DECLTYPE: {
4050 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4051 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4052 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004053
Sean Huntca63c202011-05-24 22:41:36 +00004054 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004055 QualType BaseType = readType(*Loc.F, Record, Idx);
4056 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004057 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004058 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004059 }
4060
Richard Smith34b41d92011-02-20 03:19:35 +00004061 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004062 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004063
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004064 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004065 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004066 Error("incorrect encoding of record type");
4067 return QualType();
4068 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004069 unsigned Idx = 0;
4070 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004071 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4072 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4073 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004074 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004075 return T;
4076 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004077
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004078 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004079 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004080 Error("incorrect encoding of enum type");
4081 return QualType();
4082 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004083 unsigned Idx = 0;
4084 bool IsDependent = Record[Idx++];
4085 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004086 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004087 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004088 return T;
4089 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004090
John McCall9d156a72011-01-06 01:58:22 +00004091 case TYPE_ATTRIBUTED: {
4092 if (Record.size() != 3) {
4093 Error("incorrect encoding of attributed type");
4094 return QualType();
4095 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004096 QualType modifiedType = readType(*Loc.F, Record, Idx);
4097 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004098 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004099 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004100 }
4101
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004102 case TYPE_PAREN: {
4103 if (Record.size() != 1) {
4104 Error("incorrect encoding of paren type");
4105 return QualType();
4106 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004107 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004108 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004109 }
4110
Douglas Gregor7536dd52010-12-20 02:24:11 +00004111 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004112 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004113 Error("incorrect encoding of pack expansion type");
4114 return QualType();
4115 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004116 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004117 if (Pattern.isNull())
4118 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004119 llvm::Optional<unsigned> NumExpansions;
4120 if (Record[1])
4121 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004122 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004123 }
4124
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004125 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004126 unsigned Idx = 0;
4127 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004128 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004129 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004130 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004131 }
4132
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004133 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004134 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004135 ObjCInterfaceDecl *ItfD
4136 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004137 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004138 }
4139
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004140 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004141 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004142 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004143 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004144 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004145 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004146 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004147 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004148 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004149
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004150 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004151 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004152 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004153 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004154 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004155
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004156 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004157 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004158 QualType Parm = readType(*Loc.F, Record, Idx);
4159 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004160 return
Douglas Gregor35942772011-09-09 21:34:22 +00004161 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004162 Replacement);
4163 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004164
Douglas Gregorc3069d62011-01-14 02:55:32 +00004165 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4166 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004167 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004168 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004169 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004170 cast<TemplateTypeParmType>(Parm),
4171 ArgPack);
4172 }
4173
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004174 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004175 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004176 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004177 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004178 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004179 return
Douglas Gregor35942772011-09-09 21:34:22 +00004180 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004181 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004182
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004183 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004184 unsigned Idx = 0;
4185 unsigned Depth = Record[Idx++];
4186 unsigned Index = Record[Idx++];
4187 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004188 TemplateTypeParmDecl *D
4189 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004190 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004191 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004192
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004193 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004194 unsigned Idx = 0;
4195 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004196 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004197 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004198 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004199 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004200 Canon = Context.getCanonicalType(Canon);
4201 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004202 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004203
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004204 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004205 unsigned Idx = 0;
4206 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004207 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004208 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004209 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004210 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004211 Args.reserve(NumArgs);
4212 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004213 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004214 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004215 Args.size(), Args.data());
4216 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004217
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004218 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004219 unsigned Idx = 0;
4220
4221 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004222 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004223 ArrayType::ArraySizeModifier ASM
4224 = (ArrayType::ArraySizeModifier)Record[Idx++];
4225 unsigned IndexTypeQuals = Record[Idx++];
4226
4227 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004228 Expr *NumElts = ReadExpr(*Loc.F);
4229 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004230
Douglas Gregor35942772011-09-09 21:34:22 +00004231 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004232 IndexTypeQuals, Brackets);
4233 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004234
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004235 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004236 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004237 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004238 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004239 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004240 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004241 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004242 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004243 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004244 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004245 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004246 else
Douglas Gregor35942772011-09-09 21:34:22 +00004247 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004248 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004249 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004250 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004251 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004252
4253 case TYPE_ATOMIC: {
4254 if (Record.size() != 1) {
4255 Error("Incorrect encoding of atomic type");
4256 return QualType();
4257 }
4258 QualType ValueType = readType(*Loc.F, Record, Idx);
4259 return Context.getAtomicType(ValueType);
4260 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004261 }
David Blaikie7530c032012-01-17 06:56:22 +00004262 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004263}
4264
Sebastian Redlc3632732010-10-05 15:59:54 +00004265class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004266 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004267 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004268 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004269 unsigned &Idx;
4270
Sebastian Redlc3632732010-10-05 15:59:54 +00004271 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4272 unsigned &I) {
4273 return Reader.ReadSourceLocation(F, R, I);
4274 }
4275
Douglas Gregor409448c2011-07-21 22:35:25 +00004276 template<typename T>
4277 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4278 return Reader.ReadDeclAs<T>(F, Record, Idx);
4279 }
4280
John McCalla1ee0c52009-10-16 21:56:05 +00004281public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004282 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004283 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004284 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004285 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004286
John McCall51bd8032009-10-18 01:05:36 +00004287 // We want compile-time assurance that we've enumerated all of
4288 // these, so unfortunately we have to declare them first, then
4289 // define them out-of-line.
4290#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004291#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004292 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004293#include "clang/AST/TypeLocNodes.def"
4294
John McCall51bd8032009-10-18 01:05:36 +00004295 void VisitFunctionTypeLoc(FunctionTypeLoc);
4296 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004297};
4298
John McCall51bd8032009-10-18 01:05:36 +00004299void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004300 // nothing to do
4301}
John McCall51bd8032009-10-18 01:05:36 +00004302void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004303 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004304 if (TL.needsExtraLocalData()) {
4305 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4306 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4307 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4308 TL.setModeAttr(Record[Idx++]);
4309 }
John McCalla1ee0c52009-10-16 21:56:05 +00004310}
John McCall51bd8032009-10-18 01:05:36 +00004311void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004312 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004313}
John McCall51bd8032009-10-18 01:05:36 +00004314void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004315 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004316}
John McCall51bd8032009-10-18 01:05:36 +00004317void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004318 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004319}
John McCall51bd8032009-10-18 01:05:36 +00004320void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004321 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004322}
John McCall51bd8032009-10-18 01:05:36 +00004323void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004324 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004325}
John McCall51bd8032009-10-18 01:05:36 +00004326void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004327 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004328 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004329}
John McCall51bd8032009-10-18 01:05:36 +00004330void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004331 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4332 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004333 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004334 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004335 else
John McCall51bd8032009-10-18 01:05:36 +00004336 TL.setSizeExpr(0);
4337}
4338void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4339 VisitArrayTypeLoc(TL);
4340}
4341void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4342 VisitArrayTypeLoc(TL);
4343}
4344void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4345 VisitArrayTypeLoc(TL);
4346}
4347void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4348 DependentSizedArrayTypeLoc TL) {
4349 VisitArrayTypeLoc(TL);
4350}
4351void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4352 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004353 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004354}
4355void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004356 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004357}
4358void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004359 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004360}
4361void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004362 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004363 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4364 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004365 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004366 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004367 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004368 }
4369}
4370void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4371 VisitFunctionTypeLoc(TL);
4372}
4373void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4374 VisitFunctionTypeLoc(TL);
4375}
John McCalled976492009-12-04 22:46:56 +00004376void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004377 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004378}
John McCall51bd8032009-10-18 01:05:36 +00004379void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004380 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004381}
4382void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004383 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4384 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4385 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004386}
4387void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004388 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4389 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4390 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4391 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004392}
4393void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004394 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004395}
Sean Huntca63c202011-05-24 22:41:36 +00004396void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4397 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4398 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4399 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4400 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4401}
Richard Smith34b41d92011-02-20 03:19:35 +00004402void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4403 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4404}
John McCall51bd8032009-10-18 01:05:36 +00004405void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004406 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004407}
4408void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004409 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004410}
John McCall9d156a72011-01-06 01:58:22 +00004411void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4412 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4413 if (TL.hasAttrOperand()) {
4414 SourceRange range;
4415 range.setBegin(ReadSourceLocation(Record, Idx));
4416 range.setEnd(ReadSourceLocation(Record, Idx));
4417 TL.setAttrOperandParensRange(range);
4418 }
4419 if (TL.hasAttrExprOperand()) {
4420 if (Record[Idx++])
4421 TL.setAttrExprOperand(Reader.ReadExpr(F));
4422 else
4423 TL.setAttrExprOperand(0);
4424 } else if (TL.hasAttrEnumOperand())
4425 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4426}
John McCall51bd8032009-10-18 01:05:36 +00004427void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004428 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004429}
John McCall49a832b2009-10-18 09:09:24 +00004430void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4431 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004432 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004433}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004434void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4435 SubstTemplateTypeParmPackTypeLoc TL) {
4436 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4437}
John McCall51bd8032009-10-18 01:05:36 +00004438void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4439 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004440 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004441 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4442 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4443 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004444 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4445 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004446 Reader.GetTemplateArgumentLocInfo(F,
4447 TL.getTypePtr()->getArg(i).getKind(),
4448 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004449}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004450void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4451 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4452 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4453}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004454void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004455 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004456 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004457}
John McCall3cb0ebd2010-03-10 03:28:59 +00004458void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004459 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004460}
Douglas Gregor4714c122010-03-31 17:34:00 +00004461void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004462 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004463 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004464 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004465}
John McCall33500952010-06-11 00:33:02 +00004466void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4467 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004468 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004469 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004470 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004471 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004472 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4473 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004474 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4475 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004476 Reader.GetTemplateArgumentLocInfo(F,
4477 TL.getTypePtr()->getArg(I).getKind(),
4478 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004479}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004480void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4481 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4482}
John McCall51bd8032009-10-18 01:05:36 +00004483void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004484 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004485}
4486void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4487 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004488 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4489 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004490 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004491 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004492}
John McCall54e14c42009-10-22 22:37:11 +00004493void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004494 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004495}
Eli Friedmanb001de72011-10-06 23:00:33 +00004496void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4497 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4498 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4499 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4500}
John McCalla1ee0c52009-10-16 21:56:05 +00004501
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004502TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004503 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004504 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004505 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004506 if (InfoTy.isNull())
4507 return 0;
4508
Douglas Gregor35942772011-09-09 21:34:22 +00004509 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004510 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004511 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004512 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004513 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004514}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004515
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004516QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004517 unsigned FastQuals = ID & Qualifiers::FastMask;
4518 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004519
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004520 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004521 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004522 switch ((PredefinedTypeIDs)Index) {
4523 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004524 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4525 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004526
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004527 case PREDEF_TYPE_CHAR_U_ID:
4528 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004529 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004530 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004531 break;
4532
Douglas Gregor35942772011-09-09 21:34:22 +00004533 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4534 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4535 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4536 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4537 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4538 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4539 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4540 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4541 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4542 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4543 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4544 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4545 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004546 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004547 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4548 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4549 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4550 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4551 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004552 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004553 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4554 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4555 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4556 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4557 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4558 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4559 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4560 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4561 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004562
4563 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004564 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004565 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004566
4567 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4568 T = Context.ARCUnbridgedCastTy;
4569 break;
4570
Meador Ingefb40e3f2012-07-01 15:57:25 +00004571 case PREDEF_TYPE_VA_LIST_TAG:
4572 T = Context.getVaListTagType();
4573 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004574
4575 case PREDEF_TYPE_BUILTIN_FN:
4576 T = Context.BuiltinFnTy;
4577 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004578 }
4579
4580 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004581 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004582 }
4583
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004584 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004585 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004586 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004587 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004588 if (TypesLoaded[Index].isNull())
4589 return QualType();
4590
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004591 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004592 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004593 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004594 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004595 }
Mike Stump1eb44332009-09-09 15:08:12 +00004596
John McCall0953e762009-09-24 19:53:00 +00004597 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004598}
4599
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004600QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004601 return GetType(getGlobalTypeID(F, LocalID));
4602}
4603
4604serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004605ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004606 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4607 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4608
4609 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4610 return LocalID;
4611
4612 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4613 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4614 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4615
4616 unsigned GlobalIndex = LocalIndex + I->second;
4617 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4618}
4619
John McCall833ca992009-10-29 08:12:44 +00004620TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004621ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004622 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004623 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004624 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004625 switch (Kind) {
4626 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004627 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004628 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004629 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004630 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004631 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4632 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004633 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004634 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004635 SourceLocation());
4636 }
4637 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004638 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4639 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004640 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004641 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004642 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004643 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004644 }
John McCall833ca992009-10-29 08:12:44 +00004645 case TemplateArgument::Null:
4646 case TemplateArgument::Integral:
4647 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004648 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004649 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004650 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004651 return TemplateArgumentLocInfo();
4652 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004653 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004654}
4655
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004656TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004657ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004658 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004659 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004660
4661 if (Arg.getKind() == TemplateArgument::Expression) {
4662 if (Record[Index++]) // bool InfoHasSameExpr.
4663 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4664 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004665 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004666 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004667}
4668
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004669Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004670 return GetDecl(ID);
4671}
4672
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004673uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004674 unsigned &Idx){
4675 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004676 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004677
Douglas Gregore92b8a12011-08-04 00:01:48 +00004678 unsigned LocalID = Record[Idx++];
4679 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004680}
4681
4682CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004683 RecordLocation Loc = getLocalBitOffset(Offset);
4684 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004685 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004686 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004687 ReadingKindTracker ReadingKind(Read_Decl, *this);
4688 RecordData Record;
4689 unsigned Code = Cursor.ReadCode();
4690 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4691 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4692 Error("Malformed AST file: missing C++ base specifiers");
4693 return 0;
4694 }
4695
4696 unsigned Idx = 0;
4697 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004698 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004699 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4700 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004701 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004702 return Bases;
4703}
4704
Douglas Gregor409448c2011-07-21 22:35:25 +00004705serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004706ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004707 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004708 return LocalID;
4709
4710 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004711 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004712 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4713
4714 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004715}
4716
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004717bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004718 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004719 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4720 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4721 return &M == I->second;
4722}
4723
Douglas Gregorcff9f262012-01-27 01:47:08 +00004724ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4725 if (!D->isFromASTFile())
4726 return 0;
4727 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4728 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4729 return I->second;
4730}
4731
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004732SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4733 if (ID < NUM_PREDEF_DECL_IDS)
4734 return SourceLocation();
4735
4736 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4737
4738 if (Index > DeclsLoaded.size()) {
4739 Error("declaration ID out-of-range for AST file");
4740 return SourceLocation();
4741 }
4742
4743 if (Decl *D = DeclsLoaded[Index])
4744 return D->getLocation();
4745
4746 unsigned RawLocation = 0;
4747 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4748 return ReadSourceLocation(*Rec.F, RawLocation);
4749}
4750
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004751Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004752 if (ID < NUM_PREDEF_DECL_IDS) {
4753 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004754 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004755 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004756
4757 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004758 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004759
4760 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004761 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004762
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004763 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004764 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004765
Douglas Gregor79d67262011-08-12 05:59:41 +00004766 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004767 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004768
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004769 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4770 return Context.getObjCProtocolDecl();
4771
Douglas Gregor772eeae2011-08-12 06:49:56 +00004772 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004773 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004774
4775 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004776 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004777
4778 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004779 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004780
4781 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4782 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004783 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004784 }
4785
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004786 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4787
Richard Smith2fbf3732011-12-20 04:39:57 +00004788 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004789 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004790 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004791 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004792 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004793
Douglas Gregorfd002a72011-12-16 22:37:11 +00004794 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004795 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004796 if (DeserializationListener)
4797 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4798 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004799
4800 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004801}
4802
Douglas Gregora1be2782011-12-17 23:38:30 +00004803DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4804 DeclID GlobalID) {
4805 if (GlobalID < NUM_PREDEF_DECL_IDS)
4806 return GlobalID;
4807
4808 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4809 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4810 ModuleFile *Owner = I->second;
4811
4812 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4813 = M.GlobalToLocalDeclIDs.find(Owner);
4814 if (Pos == M.GlobalToLocalDeclIDs.end())
4815 return 0;
4816
4817 return GlobalID - Owner->BaseDeclID + Pos->second;
4818}
4819
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004820serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004821 const RecordData &Record,
4822 unsigned &Idx) {
4823 if (Idx >= Record.size()) {
4824 Error("Corrupted AST file");
4825 return 0;
4826 }
4827
4828 return getGlobalDeclID(F, Record[Idx++]);
4829}
4830
Chris Lattner887e2b32009-04-27 05:46:25 +00004831/// \brief Resolve the offset of a statement into a statement.
4832///
4833/// This operation will read a new statement from the external
4834/// source each time it is called, and is meant to be used via a
4835/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004836Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004837 // Switch case IDs are per Decl.
4838 ClearSwitchCaseIDs();
4839
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004840 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004841 RecordLocation Loc = getLocalBitOffset(Offset);
4842 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4843 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004844}
4845
Douglas Gregor851c75a2011-08-24 21:27:34 +00004846namespace {
4847 class FindExternalLexicalDeclsVisitor {
4848 ASTReader &Reader;
4849 const DeclContext *DC;
4850 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004851
Douglas Gregor851c75a2011-08-24 21:27:34 +00004852 SmallVectorImpl<Decl*> &Decls;
4853 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4854
4855 public:
4856 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4857 bool (*isKindWeWant)(Decl::Kind),
4858 SmallVectorImpl<Decl*> &Decls)
4859 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4860 {
4861 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4862 PredefsVisited[I] = false;
4863 }
4864
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004865 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004866 if (Preorder)
4867 return false;
4868
4869 FindExternalLexicalDeclsVisitor *This
4870 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4871
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004872 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004873 = M.DeclContextInfos.find(This->DC);
4874 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4875 return false;
4876
4877 // Load all of the declaration IDs
4878 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4879 *IDE = ID + Info->second.NumLexicalDecls;
4880 ID != IDE; ++ID) {
4881 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4882 continue;
4883
4884 // Don't add predefined declarations to the lexical context more
4885 // than once.
4886 if (ID->second < NUM_PREDEF_DECL_IDS) {
4887 if (This->PredefsVisited[ID->second])
4888 continue;
4889
4890 This->PredefsVisited[ID->second] = true;
4891 }
4892
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004893 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4894 if (!This->DC->isDeclInLexicalTraversal(D))
4895 This->Decls.push_back(D);
4896 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004897 }
4898
4899 return false;
4900 }
4901 };
4902}
4903
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004904ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004905 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004906 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004907 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004908 // least. Walk all of the modules in the order they were loaded.
4909 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4910 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004911 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004912 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004913}
4914
Douglas Gregor0d95f772011-08-24 19:03:07 +00004915namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004916
4917class DeclIDComp {
4918 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004919 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004920
4921public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004922 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004923
4924 bool operator()(LocalDeclID L, LocalDeclID R) const {
4925 SourceLocation LHS = getLocation(L);
4926 SourceLocation RHS = getLocation(R);
4927 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4928 }
4929
4930 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4931 SourceLocation RHS = getLocation(R);
4932 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4933 }
4934
4935 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4936 SourceLocation LHS = getLocation(L);
4937 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4938 }
4939
4940 SourceLocation getLocation(LocalDeclID ID) const {
4941 return Reader.getSourceManager().getFileLoc(
4942 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4943 }
4944};
4945
4946}
4947
4948void ASTReader::FindFileRegionDecls(FileID File,
4949 unsigned Offset, unsigned Length,
4950 SmallVectorImpl<Decl *> &Decls) {
4951 SourceManager &SM = getSourceManager();
4952
4953 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4954 if (I == FileDeclIDs.end())
4955 return;
4956
4957 FileDeclsInfo &DInfo = I->second;
4958 if (DInfo.Decls.empty())
4959 return;
4960
4961 SourceLocation
4962 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4963 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4964
4965 DeclIDComp DIDComp(*this, *DInfo.Mod);
4966 ArrayRef<serialization::LocalDeclID>::iterator
4967 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4968 BeginLoc, DIDComp);
4969 if (BeginIt != DInfo.Decls.begin())
4970 --BeginIt;
4971
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004972 // If we are pointing at a top-level decl inside an objc container, we need
4973 // to backtrack until we find it otherwise we will fail to report that the
4974 // region overlaps with an objc container.
4975 while (BeginIt != DInfo.Decls.begin() &&
4976 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4977 ->isTopLevelDeclInObjCContainer())
4978 --BeginIt;
4979
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004980 ArrayRef<serialization::LocalDeclID>::iterator
4981 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4982 EndLoc, DIDComp);
4983 if (EndIt != DInfo.Decls.end())
4984 ++EndIt;
4985
4986 for (ArrayRef<serialization::LocalDeclID>::iterator
4987 DIt = BeginIt; DIt != EndIt; ++DIt)
4988 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4989}
4990
4991namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004992 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00004993 /// declaration context.
4994 class DeclContextNameLookupVisitor {
4995 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004996 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004997 DeclarationName Name;
4998 SmallVectorImpl<NamedDecl *> &Decls;
4999
5000 public:
5001 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005002 SmallVectorImpl<const DeclContext *> &Contexts,
5003 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005004 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005005 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005006
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005007 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005008 DeclContextNameLookupVisitor *This
5009 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5010
5011 // Check whether we have any visible declaration information for
5012 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005013 ModuleFile::DeclContextInfosMap::iterator Info;
5014 bool FoundInfo = false;
5015 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5016 Info = M.DeclContextInfos.find(This->Contexts[I]);
5017 if (Info != M.DeclContextInfos.end() &&
5018 Info->second.NameLookupTableData) {
5019 FoundInfo = true;
5020 break;
5021 }
5022 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005023
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005024 if (!FoundInfo)
5025 return false;
5026
Douglas Gregor0d95f772011-08-24 19:03:07 +00005027 // Look for this name within this module.
5028 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005029 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005030 ASTDeclContextNameLookupTable::iterator Pos
5031 = LookupTable->find(This->Name);
5032 if (Pos == LookupTable->end())
5033 return false;
5034
5035 bool FoundAnything = false;
5036 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5037 for (; Data.first != Data.second; ++Data.first) {
5038 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5039 if (!ND)
5040 continue;
5041
5042 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005043 // A name might be null because the decl's redeclarable part is
5044 // currently read before reading its name. The lookup is triggered by
5045 // building that decl (likely indirectly), and so it is later in the
5046 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005047 continue;
5048 }
5049
5050 // Record this declaration.
5051 FoundAnything = true;
5052 This->Decls.push_back(ND);
5053 }
5054
5055 return FoundAnything;
5056 }
5057 };
5058}
5059
John McCall76bd1f32010-06-01 09:23:16 +00005060DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005061ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005062 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005063 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005064 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005065 if (!Name)
5066 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5067 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005068
Chris Lattner5f9e2722011-07-23 10:55:15 +00005069 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005070
5071 // Compute the declaration contexts we need to look into. Multiple such
5072 // declaration contexts occur when two declaration contexts from disjoint
5073 // modules get merged, e.g., when two namespaces with the same name are
5074 // independently defined in separate modules.
5075 SmallVector<const DeclContext *, 2> Contexts;
5076 Contexts.push_back(DC);
5077
5078 if (DC->isNamespace()) {
5079 MergedDeclsMap::iterator Merged
5080 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5081 if (Merged != MergedDecls.end()) {
5082 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5083 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5084 }
5085 }
5086
5087 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005088 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005089 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005090 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005091 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005092}
5093
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005094namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005095 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005096 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005097 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005098 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005099 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005100 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005101
5102 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005103 DeclContextAllNamesVisitor(ASTReader &Reader,
5104 SmallVectorImpl<const DeclContext *> &Contexts,
5105 llvm::DenseMap<DeclarationName,
5106 SmallVector<NamedDecl *, 8> > &Decls)
5107 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005108
5109 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005110 DeclContextAllNamesVisitor *This
5111 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005112
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005113 // Check whether we have any visible declaration information for
5114 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005115 ModuleFile::DeclContextInfosMap::iterator Info;
5116 bool FoundInfo = false;
5117 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5118 Info = M.DeclContextInfos.find(This->Contexts[I]);
5119 if (Info != M.DeclContextInfos.end() &&
5120 Info->second.NameLookupTableData) {
5121 FoundInfo = true;
5122 break;
5123 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005124 }
5125
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005126 if (!FoundInfo)
5127 return false;
5128
5129 ASTDeclContextNameLookupTable *LookupTable =
5130 Info->second.NameLookupTableData;
5131 bool FoundAnything = false;
5132 for (ASTDeclContextNameLookupTable::data_iterator
5133 I = LookupTable->data_begin(), E = LookupTable->data_end();
5134 I != E; ++I) {
5135 ASTDeclContextNameLookupTrait::data_type Data = *I;
5136 for (; Data.first != Data.second; ++Data.first) {
5137 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5138 *Data.first);
5139 if (!ND)
5140 continue;
5141
5142 // Record this declaration.
5143 FoundAnything = true;
5144 This->Decls[ND->getDeclName()].push_back(ND);
5145 }
5146 }
5147
5148 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005149 }
5150 };
5151}
5152
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005153void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005154 if (!DC->hasExternalVisibleStorage())
5155 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005156 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5157
5158 // Compute the declaration contexts we need to look into. Multiple such
5159 // declaration contexts occur when two declaration contexts from disjoint
5160 // modules get merged, e.g., when two namespaces with the same name are
5161 // independently defined in separate modules.
5162 SmallVector<const DeclContext *, 2> Contexts;
5163 Contexts.push_back(DC);
5164
5165 if (DC->isNamespace()) {
5166 MergedDeclsMap::iterator Merged
5167 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5168 if (Merged != MergedDecls.end()) {
5169 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5170 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5171 }
5172 }
5173
5174 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5175 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5176 ++NumVisibleDeclContextsRead;
5177
5178 for (llvm::DenseMap<DeclarationName,
5179 llvm::SmallVector<NamedDecl*, 8> >::iterator
5180 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5181 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5182 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005183 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005184}
5185
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005186/// \brief Under non-PCH compilation the consumer receives the objc methods
5187/// before receiving the implementation, and codegen depends on this.
5188/// We simulate this by deserializing and passing to consumer the methods of the
5189/// implementation before passing the deserialized implementation decl.
5190static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5191 ASTConsumer *Consumer) {
5192 assert(ImplD && Consumer);
5193
5194 for (ObjCImplDecl::method_iterator
5195 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005196 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005197
5198 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5199}
5200
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005201void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005202 assert(Consumer);
5203 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005204 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005205 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005206
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005207 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005208 }
5209}
5210
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005211void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5212 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5213 PassObjCImplDeclToConsumer(ImplD, Consumer);
5214 else
5215 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5216}
5217
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005218void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005219 this->Consumer = Consumer;
5220
Douglas Gregorfdd01722009-04-14 00:24:19 +00005221 if (!Consumer)
5222 return;
5223
5224 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005225 // Force deserialization of this decl, which will cause it to be queued for
5226 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005227 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005228 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005229 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005230
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005231 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005232}
5233
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005234void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005235 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005236
Mike Stump1eb44332009-09-09 15:08:12 +00005237 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005238 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005239 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005240 unsigned NumDeclsLoaded
5241 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5242 (Decl *)0);
5243 unsigned NumIdentifiersLoaded
5244 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5245 IdentifiersLoaded.end(),
5246 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005247 unsigned NumMacrosLoaded
5248 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5249 MacrosLoaded.end(),
5250 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005251 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005252 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5253 SelectorsLoaded.end(),
5254 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005255
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005256 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5257 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005258 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005259 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5260 NumSLocEntriesRead, TotalNumSLocEntries,
5261 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005262 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005263 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005264 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5265 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5266 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005267 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005268 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5269 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005270 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005271 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005272 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5273 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005274 if (!MacrosLoaded.empty())
5275 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5276 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5277 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005278 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005279 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005280 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5281 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005282 if (TotalNumStatements)
5283 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5284 NumStatementsRead, TotalNumStatements,
5285 ((float)NumStatementsRead/TotalNumStatements * 100));
5286 if (TotalNumMacros)
5287 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5288 NumMacrosRead, TotalNumMacros,
5289 ((float)NumMacrosRead/TotalNumMacros * 100));
5290 if (TotalLexicalDeclContexts)
5291 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5292 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5293 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5294 * 100));
5295 if (TotalVisibleDeclContexts)
5296 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5297 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5298 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5299 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005300 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005301 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005302 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5303 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005304 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005305 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005306 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005307 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005308 dump();
5309 std::fprintf(stderr, "\n");
5310}
5311
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005312template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005313static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005314dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005315 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005316 InitialCapacity> &Map) {
5317 if (Map.begin() == Map.end())
5318 return;
5319
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005320 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005321 llvm::errs() << Name << ":\n";
5322 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5323 I != IEnd; ++I) {
5324 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5325 << "\n";
5326 }
5327}
5328
Douglas Gregor23d7df52011-07-21 19:50:14 +00005329void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005330 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005331 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005332 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005333 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005334 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005335 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005336 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005337 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005338 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005339 dumpModuleIDMap("Global preprocessed entity map",
5340 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005341
5342 llvm::errs() << "\n*** PCH/Modules Loaded:";
5343 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5344 MEnd = ModuleMgr.end();
5345 M != MEnd; ++M)
5346 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005347}
5348
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005349/// Return the amount of memory used by memory buffers, breaking down
5350/// by heap-backed versus mmap'ed memory.
5351void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005352 for (ModuleConstIterator I = ModuleMgr.begin(),
5353 E = ModuleMgr.end(); I != E; ++I) {
5354 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005355 size_t bytes = buf->getBufferSize();
5356 switch (buf->getBufferKind()) {
5357 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5358 sizes.malloc_bytes += bytes;
5359 break;
5360 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5361 sizes.mmap_bytes += bytes;
5362 break;
5363 }
5364 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005365 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005366}
5367
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005368void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005369 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005370 S.ExternalSource = this;
5371
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005372 // Makes sure any declarations that were deserialized "too early"
5373 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005374 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005375 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5376 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005377 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005378 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005379
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005380 // Load the offsets of the declarations that Sema references.
5381 // They will be lazily deserialized when needed.
5382 if (!SemaDeclRefs.empty()) {
5383 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005384 if (!SemaObj->StdNamespace)
5385 SemaObj->StdNamespace = SemaDeclRefs[0];
5386 if (!SemaObj->StdBadAlloc)
5387 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005388 }
5389
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005390 if (!FPPragmaOptions.empty()) {
5391 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5392 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5393 }
5394
5395 if (!OpenCLExtensions.empty()) {
5396 unsigned I = 0;
5397#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5398#include "clang/Basic/OpenCLExtensions.def"
5399
5400 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5401 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005402}
5403
Douglas Gregor211f6e82011-08-20 04:39:52 +00005404IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005405 // Note that we are loading an identifier.
5406 Deserializing AnIdentifier(this);
5407
Douglas Gregor057df202012-01-18 20:56:22 +00005408 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5409 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005410 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005411 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005412 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005413 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005414}
5415
Douglas Gregor95f42922010-10-14 22:11:03 +00005416namespace clang {
5417 /// \brief An identifier-lookup iterator that enumerates all of the
5418 /// identifiers stored within a set of AST files.
5419 class ASTIdentifierIterator : public IdentifierIterator {
5420 /// \brief The AST reader whose identifiers are being enumerated.
5421 const ASTReader &Reader;
5422
5423 /// \brief The current index into the chain of AST files stored in
5424 /// the AST reader.
5425 unsigned Index;
5426
5427 /// \brief The current position within the identifier lookup table
5428 /// of the current AST file.
5429 ASTIdentifierLookupTable::key_iterator Current;
5430
5431 /// \brief The end position within the identifier lookup table of
5432 /// the current AST file.
5433 ASTIdentifierLookupTable::key_iterator End;
5434
5435 public:
5436 explicit ASTIdentifierIterator(const ASTReader &Reader);
5437
Chris Lattner5f9e2722011-07-23 10:55:15 +00005438 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005439 };
5440}
5441
5442ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005443 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005444 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005445 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005446 Current = IdTable->key_begin();
5447 End = IdTable->key_end();
5448}
5449
Chris Lattner5f9e2722011-07-23 10:55:15 +00005450StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005451 while (Current == End) {
5452 // If we have exhausted all of our AST files, we're done.
5453 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005454 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005455
5456 --Index;
5457 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005458 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5459 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005460 Current = IdTable->key_begin();
5461 End = IdTable->key_end();
5462 }
5463
5464 // We have any identifiers remaining in the current AST file; return
5465 // the next one.
5466 std::pair<const char*, unsigned> Key = *Current;
5467 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005468 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005469}
5470
5471IdentifierIterator *ASTReader::getIdentifiers() const {
5472 return new ASTIdentifierIterator(*this);
5473}
5474
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005475namespace clang { namespace serialization {
5476 class ReadMethodPoolVisitor {
5477 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005478 Selector Sel;
5479 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005480 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5481 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005482
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005483 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005484 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5485 unsigned PriorGeneration)
5486 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005487
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005488 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005489 ReadMethodPoolVisitor *This
5490 = static_cast<ReadMethodPoolVisitor *>(UserData);
5491
5492 if (!M.SelectorLookupTable)
5493 return false;
5494
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005495 // If we've already searched this module file, skip it now.
5496 if (M.Generation <= This->PriorGeneration)
5497 return true;
5498
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005499 ASTSelectorLookupTable *PoolTable
5500 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5501 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5502 if (Pos == PoolTable->end())
5503 return false;
5504
5505 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005506 // FIXME: Not quite happy with the statistics here. We probably should
5507 // disable this tracking when called via LoadSelector.
5508 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005509 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005510 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005511 if (This->Reader.DeserializationListener)
5512 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5513 This->Sel);
5514
5515 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5516 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5517 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005518 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005519
5520 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005521 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5522 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005523 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005524
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005525 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005526 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5527 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005528 }
5529 };
5530} } // end namespace clang::serialization
5531
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005532/// \brief Add the given set of methods to the method list.
5533static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5534 ObjCMethodList &List) {
5535 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5536 S.addMethodToGlobalList(&List, Methods[I]);
5537 }
5538}
5539
5540void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005541 // Get the selector generation and update it to the current generation.
5542 unsigned &Generation = SelectorGeneration[Sel];
5543 unsigned PriorGeneration = Generation;
5544 Generation = CurrentGeneration;
5545
5546 // Search for methods defined with this selector.
5547 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005548 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005549
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005550 if (Visitor.getInstanceMethods().empty() &&
5551 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005552 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005553 return;
5554 }
5555
5556 if (!getSema())
5557 return;
5558
5559 Sema &S = *getSema();
5560 Sema::GlobalMethodPool::iterator Pos
5561 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5562
5563 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5564 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005565}
5566
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005567void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005568 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005569 Namespaces.clear();
5570
5571 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5572 if (NamespaceDecl *Namespace
5573 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5574 Namespaces.push_back(Namespace);
5575 }
5576}
5577
Douglas Gregora8623202011-07-27 20:58:46 +00005578void ASTReader::ReadTentativeDefinitions(
5579 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5580 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5581 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5582 if (Var)
5583 TentativeDefs.push_back(Var);
5584 }
5585 TentativeDefinitions.clear();
5586}
5587
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005588void ASTReader::ReadUnusedFileScopedDecls(
5589 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5590 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5591 DeclaratorDecl *D
5592 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5593 if (D)
5594 Decls.push_back(D);
5595 }
5596 UnusedFileScopedDecls.clear();
5597}
5598
Douglas Gregor0129b562011-07-27 21:57:17 +00005599void ASTReader::ReadDelegatingConstructors(
5600 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5601 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5602 CXXConstructorDecl *D
5603 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5604 if (D)
5605 Decls.push_back(D);
5606 }
5607 DelegatingCtorDecls.clear();
5608}
5609
Douglas Gregord58a0a52011-07-28 00:39:29 +00005610void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5611 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5612 TypedefNameDecl *D
5613 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5614 if (D)
5615 Decls.push_back(D);
5616 }
5617 ExtVectorDecls.clear();
5618}
5619
Douglas Gregora126f172011-07-28 00:53:40 +00005620void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5621 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5622 CXXRecordDecl *D
5623 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5624 if (D)
5625 Decls.push_back(D);
5626 }
5627 DynamicClasses.clear();
5628}
5629
Douglas Gregorec12ce22011-07-28 14:20:37 +00005630void
5631ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5632 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5633 NamedDecl *D
5634 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5635 if (D)
5636 Decls.push_back(D);
5637 }
5638 LocallyScopedExternalDecls.clear();
5639}
5640
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005641void ASTReader::ReadReferencedSelectors(
5642 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5643 if (ReferencedSelectorsData.empty())
5644 return;
5645
5646 // If there are @selector references added them to its pool. This is for
5647 // implementation of -Wselector.
5648 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5649 unsigned I = 0;
5650 while (I < DataSize) {
5651 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5652 SourceLocation SelLoc
5653 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5654 Sels.push_back(std::make_pair(Sel, SelLoc));
5655 }
5656 ReferencedSelectorsData.clear();
5657}
5658
Douglas Gregor31e37b22011-07-28 18:09:57 +00005659void ASTReader::ReadWeakUndeclaredIdentifiers(
5660 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5661 if (WeakUndeclaredIdentifiers.empty())
5662 return;
5663
5664 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5665 IdentifierInfo *WeakId
5666 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5667 IdentifierInfo *AliasId
5668 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5669 SourceLocation Loc
5670 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5671 bool Used = WeakUndeclaredIdentifiers[I++];
5672 WeakInfo WI(AliasId, Loc);
5673 WI.setUsed(Used);
5674 WeakIDs.push_back(std::make_pair(WeakId, WI));
5675 }
5676 WeakUndeclaredIdentifiers.clear();
5677}
5678
Douglas Gregordfe65432011-07-28 19:11:31 +00005679void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5680 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5681 ExternalVTableUse VT;
5682 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5683 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5684 VT.DefinitionRequired = VTableUses[Idx++];
5685 VTables.push_back(VT);
5686 }
5687
5688 VTableUses.clear();
5689}
5690
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005691void ASTReader::ReadPendingInstantiations(
5692 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5693 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5694 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5695 SourceLocation Loc
5696 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005697
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005698 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005699 }
5700 PendingInstantiations.clear();
5701}
5702
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005703void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005704 // It would be complicated to avoid reading the methods anyway. So don't.
5705 ReadMethodPool(Sel);
5706}
5707
Douglas Gregor95eab172011-07-28 20:55:49 +00005708void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005709 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005710 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005711 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005712 if (DeserializationListener)
5713 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005714}
5715
Douglas Gregord89275b2009-07-06 18:54:52 +00005716/// \brief Set the globally-visible declarations associated with the given
5717/// identifier.
5718///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005719/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005720/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005721/// them.
5722///
5723/// \param II an IdentifierInfo that refers to one or more globally-visible
5724/// declarations.
5725///
5726/// \param DeclIDs the set of declaration IDs with the name @p II that are
5727/// visible at global scope.
5728///
5729/// \param Nonrecursive should be true to indicate that the caller knows that
5730/// this call is non-recursive, and therefore the globally-visible declarations
5731/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005732void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005733ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005734 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005735 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005736 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005737 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5738 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5739 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005740 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005741 return;
5742 }
Mike Stump1eb44332009-09-09 15:08:12 +00005743
Douglas Gregord89275b2009-07-06 18:54:52 +00005744 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5745 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5746 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005747 // Introduce this declaration into the translation-unit scope
5748 // and add it to the declaration chain for this identifier, so
5749 // that (unqualified) name lookup will find it.
5750 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005751 } else {
5752 // Queue this declaration so that it will be added to the
5753 // translation unit scope and identifier's declaration chain
5754 // once a Sema object is known.
5755 PreloadedDecls.push_back(D);
5756 }
5757 }
5758}
5759
Douglas Gregor95eab172011-07-28 20:55:49 +00005760IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005761 if (ID == 0)
5762 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005763
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005764 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005765 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005766 return 0;
5767 }
Mike Stump1eb44332009-09-09 15:08:12 +00005768
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005769 ID -= 1;
5770 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005771 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5772 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005773 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005774 unsigned Index = ID - M->BaseIdentifierID;
5775 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005776
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005777 // All of the strings in the AST file are preceded by a 16-bit length.
5778 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005779 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5780 // unsigned integers. This is important to avoid integer overflow when
5781 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005782 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005783 unsigned StrLen = (((unsigned) StrLenPtr[0])
5784 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005785 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005786 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005787 if (DeserializationListener)
5788 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005789 }
Mike Stump1eb44332009-09-09 15:08:12 +00005790
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005791 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005792}
5793
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005794IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005795 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5796}
5797
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005798IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005799 if (LocalID < NUM_PREDEF_IDENT_IDS)
5800 return LocalID;
5801
5802 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5803 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5804 assert(I != M.IdentifierRemap.end()
5805 && "Invalid index into identifier index remap");
5806
5807 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005808}
5809
Douglas Gregora8235d62012-10-09 23:05:51 +00005810MacroInfo *ASTReader::getMacro(MacroID ID) {
5811 if (ID == 0)
5812 return 0;
5813
5814 if (MacrosLoaded.empty()) {
5815 Error("no macro table in AST file");
5816 return 0;
5817 }
5818
5819 ID -= NUM_PREDEF_MACRO_IDS;
5820 if (!MacrosLoaded[ID]) {
5821 GlobalMacroMapType::iterator I
5822 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
5823 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
5824 ModuleFile *M = I->second;
5825 unsigned Index = ID - M->BaseMacroID;
5826 ReadMacroRecord(*M, M->MacroOffsets[Index]);
5827 }
5828
5829 return MacrosLoaded[ID];
5830}
5831
5832MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
5833 if (LocalID < NUM_PREDEF_MACRO_IDS)
5834 return LocalID;
5835
5836 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5837 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
5838 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
5839
5840 return LocalID + I->second;
5841}
5842
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005843bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005844 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005845}
5846
Douglas Gregor26ced122011-12-01 00:59:36 +00005847serialization::SubmoduleID
5848ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5849 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5850 return LocalID;
5851
5852 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5853 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5854 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005855 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00005856
5857 return LocalID + I->second;
5858}
5859
5860Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5861 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5862 assert(GlobalID == 0 && "Unhandled global submodule ID");
5863 return 0;
5864 }
5865
5866 if (GlobalID > SubmodulesLoaded.size()) {
5867 Error("submodule ID out of range in AST file");
5868 return 0;
5869 }
5870
5871 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5872}
5873
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005874Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005875 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5876}
5877
5878Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005879 if (ID == 0)
5880 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005881
Sebastian Redl725cd962010-08-04 20:40:17 +00005882 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005883 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005884 return Selector();
5885 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005886
Sebastian Redl725cd962010-08-04 20:40:17 +00005887 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005888 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005889 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5890 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005891 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005892 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005893 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005894 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005895 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005896 if (DeserializationListener)
5897 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005898 }
5899
Sebastian Redl725cd962010-08-04 20:40:17 +00005900 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005901}
5902
Douglas Gregor8451ec72011-07-28 14:41:43 +00005903Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005904 return DecodeSelector(ID);
5905}
5906
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005907uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005908 // ID 0 (the null selector) is considered an external selector.
5909 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005910}
5911
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005912serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005913ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005914 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5915 return LocalID;
5916
5917 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5918 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5919 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005920 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005921
5922 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005923}
5924
Mike Stump1eb44332009-09-09 15:08:12 +00005925DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005926ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005927 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005928 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5929 switch (Kind) {
5930 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005931 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005932
5933 case DeclarationName::ObjCZeroArgSelector:
5934 case DeclarationName::ObjCOneArgSelector:
5935 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005936 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005937
5938 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005939 return Context.DeclarationNames.getCXXConstructorName(
5940 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005941
5942 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005943 return Context.DeclarationNames.getCXXDestructorName(
5944 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005945
5946 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005947 return Context.DeclarationNames.getCXXConversionFunctionName(
5948 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005949
5950 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005951 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005952 (OverloadedOperatorKind)Record[Idx++]);
5953
Sean Hunt3e518bd2009-11-29 07:34:05 +00005954 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005955 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005956 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005957
Douglas Gregor2cf26342009-04-09 22:27:44 +00005958 case DeclarationName::CXXUsingDirective:
5959 return DeclarationName::getUsingDirectiveName();
5960 }
5961
David Blaikie7530c032012-01-17 06:56:22 +00005962 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005963}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005964
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005965void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005966 DeclarationNameLoc &DNLoc,
5967 DeclarationName Name,
5968 const RecordData &Record, unsigned &Idx) {
5969 switch (Name.getNameKind()) {
5970 case DeclarationName::CXXConstructorName:
5971 case DeclarationName::CXXDestructorName:
5972 case DeclarationName::CXXConversionFunctionName:
5973 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5974 break;
5975
5976 case DeclarationName::CXXOperatorName:
5977 DNLoc.CXXOperatorName.BeginOpNameLoc
5978 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5979 DNLoc.CXXOperatorName.EndOpNameLoc
5980 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5981 break;
5982
5983 case DeclarationName::CXXLiteralOperatorName:
5984 DNLoc.CXXLiteralOperatorName.OpNameLoc
5985 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5986 break;
5987
5988 case DeclarationName::Identifier:
5989 case DeclarationName::ObjCZeroArgSelector:
5990 case DeclarationName::ObjCOneArgSelector:
5991 case DeclarationName::ObjCMultiArgSelector:
5992 case DeclarationName::CXXUsingDirective:
5993 break;
5994 }
5995}
5996
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005997void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005998 DeclarationNameInfo &NameInfo,
5999 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006000 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006001 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6002 DeclarationNameLoc DNLoc;
6003 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6004 NameInfo.setInfo(DNLoc);
6005}
6006
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006007void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006008 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006009 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006010 unsigned NumTPLists = Record[Idx++];
6011 Info.NumTemplParamLists = NumTPLists;
6012 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006013 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006014 for (unsigned i=0; i != NumTPLists; ++i)
6015 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6016 }
6017}
6018
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006019TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006020ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006021 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006022 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006023 switch (Kind) {
6024 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006025 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006026
6027 case TemplateName::OverloadedTemplate: {
6028 unsigned size = Record[Idx++];
6029 UnresolvedSet<8> Decls;
6030 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006031 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006032
Douglas Gregor35942772011-09-09 21:34:22 +00006033 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006034 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006035
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006036 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006037 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006038 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006039 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006040 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006041 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006042
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006043 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006044 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006045 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006046 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006047 GetIdentifierInfo(F, Record,
6048 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006049 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006050 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006051 }
John McCall14606042011-06-30 08:33:18 +00006052
6053 case TemplateName::SubstTemplateTemplateParm: {
6054 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006055 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006056 if (!param) return TemplateName();
6057 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006058 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006059 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006060
6061 case TemplateName::SubstTemplateTemplateParmPack: {
6062 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006063 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006064 if (!Param)
6065 return TemplateName();
6066
6067 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6068 if (ArgPack.getKind() != TemplateArgument::Pack)
6069 return TemplateName();
6070
Douglas Gregor35942772011-09-09 21:34:22 +00006071 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006072 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006073 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006074
David Blaikieb219cfc2011-09-23 05:06:16 +00006075 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006076}
6077
6078TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006079ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006080 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006081 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6082 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006083 case TemplateArgument::Null:
6084 return TemplateArgument();
6085 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006086 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006087 case TemplateArgument::Declaration: {
6088 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6089 bool ForReferenceParam = Record[Idx++];
6090 return TemplateArgument(D, ForReferenceParam);
6091 }
6092 case TemplateArgument::NullPtr:
6093 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006094 case TemplateArgument::Integral: {
6095 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006096 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006097 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006098 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006099 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006100 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006101 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006102 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006103 llvm::Optional<unsigned> NumTemplateExpansions;
6104 if (unsigned NumExpansions = Record[Idx++])
6105 NumTemplateExpansions = NumExpansions - 1;
6106 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006107 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006108 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006109 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006110 case TemplateArgument::Pack: {
6111 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006112 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006113 for (unsigned I = 0; I != NumArgs; ++I)
6114 Args[I] = ReadTemplateArgument(F, Record, Idx);
6115 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006116 }
6117 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006118
David Blaikieb219cfc2011-09-23 05:06:16 +00006119 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006120}
6121
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006122TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006123ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006124 const RecordData &Record, unsigned &Idx) {
6125 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6126 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6127 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006128
6129 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006130 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006131 Params.reserve(NumParams);
6132 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006133 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006134
6135 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006136 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006137 Params.data(), Params.size(), RAngleLoc);
6138 return TemplateParams;
6139}
6140
6141void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006142ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006143ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006144 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006145 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006146 unsigned NumTemplateArgs = Record[Idx++];
6147 TemplArgs.reserve(NumTemplateArgs);
6148 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006149 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006150}
6151
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006152/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006153void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006154 const RecordData &Record, unsigned &Idx) {
6155 unsigned NumDecls = Record[Idx++];
6156 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006157 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006158 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6159 Set.addDecl(D, AS);
6160 }
6161}
6162
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006163CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006164ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006165 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006166 bool isVirtual = static_cast<bool>(Record[Idx++]);
6167 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6168 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006169 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006170 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6171 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006172 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006173 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006174 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006175 Result.setInheritConstructors(inheritConstructors);
6176 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006177}
6178
Sean Huntcbb67482011-01-08 20:30:50 +00006179std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006180ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006181 unsigned &Idx) {
6182 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006183 unsigned NumInitializers = Record[Idx++];
6184 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006185 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006186 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006187 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006188 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006189 bool IsBaseVirtual = false;
6190 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006191 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006192
Sean Hunt156b6402011-05-04 01:19:08 +00006193 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6194 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006195 case CTOR_INITIALIZER_BASE:
6196 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006197 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006198 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006199
6200 case CTOR_INITIALIZER_DELEGATING:
6201 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006202 break;
6203
6204 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006205 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006206 break;
6207
6208 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006209 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006210 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006211 }
Sean Hunt156b6402011-05-04 01:19:08 +00006212
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006213 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006214 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006215 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6216 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006217 bool IsWritten = Record[Idx++];
6218 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006219 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006220 if (IsWritten) {
6221 SourceOrderOrNumArrayIndices = Record[Idx++];
6222 } else {
6223 SourceOrderOrNumArrayIndices = Record[Idx++];
6224 Indices.reserve(SourceOrderOrNumArrayIndices);
6225 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006226 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006227 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006228
Sean Huntcbb67482011-01-08 20:30:50 +00006229 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006230 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006231 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006232 LParenLoc, Init, RParenLoc,
6233 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006234 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006235 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6236 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006237 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006238 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006239 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006240 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006241 else
Douglas Gregor35942772011-09-09 21:34:22 +00006242 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006243 MemberOrEllipsisLoc, LParenLoc,
6244 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006245 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006246 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006247 LParenLoc, Init, RParenLoc,
6248 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006249 }
6250
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006251 if (IsWritten)
6252 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006253 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006254 }
6255 }
6256
Sean Huntcbb67482011-01-08 20:30:50 +00006257 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006258}
6259
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006260NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006261ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006262 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006263 unsigned N = Record[Idx++];
6264 NestedNameSpecifier *NNS = 0, *Prev = 0;
6265 for (unsigned I = 0; I != N; ++I) {
6266 NestedNameSpecifier::SpecifierKind Kind
6267 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6268 switch (Kind) {
6269 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006270 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006271 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006272 break;
6273 }
6274
6275 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006276 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006277 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006278 break;
6279 }
6280
Douglas Gregor14aba762011-02-24 02:36:08 +00006281 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006282 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006283 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006284 break;
6285 }
6286
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006287 case NestedNameSpecifier::TypeSpec:
6288 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006289 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006290 if (!T)
6291 return 0;
6292
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006293 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006294 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006295 break;
6296 }
6297
6298 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006299 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006300 // No associated value, and there can't be a prefix.
6301 break;
6302 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006303 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006304 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006305 }
6306 return NNS;
6307}
6308
Douglas Gregordc355712011-02-25 00:36:19 +00006309NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006310ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006311 unsigned &Idx) {
6312 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006313 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006314 for (unsigned I = 0; I != N; ++I) {
6315 NestedNameSpecifier::SpecifierKind Kind
6316 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6317 switch (Kind) {
6318 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006319 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006320 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006321 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006322 break;
6323 }
6324
6325 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006326 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006327 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006328 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006329 break;
6330 }
6331
6332 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006333 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006334 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006335 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006336 break;
6337 }
6338
6339 case NestedNameSpecifier::TypeSpec:
6340 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006341 bool Template = Record[Idx++];
6342 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6343 if (!T)
6344 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006345 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006346
6347 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006348 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006349 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6350 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006351 break;
6352 }
6353
6354 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006355 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006356 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006357 break;
6358 }
6359 }
Douglas Gregordc355712011-02-25 00:36:19 +00006360 }
6361
Douglas Gregor35942772011-09-09 21:34:22 +00006362 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006363}
6364
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006365SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006366ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006367 unsigned &Idx) {
6368 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6369 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006370 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006371}
6372
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006373/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006374llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006375 unsigned BitWidth = Record[Idx++];
6376 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6377 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6378 Idx += NumWords;
6379 return Result;
6380}
6381
6382/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006383llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006384 bool isUnsigned = Record[Idx++];
6385 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6386}
6387
Douglas Gregor17fc2232009-04-14 21:55:33 +00006388/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006389llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006390 return llvm::APFloat(ReadAPInt(Record, Idx));
6391}
6392
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006393// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006394std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006395 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006396 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006397 Idx += Len;
6398 return Result;
6399}
6400
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006401VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6402 unsigned &Idx) {
6403 unsigned Major = Record[Idx++];
6404 unsigned Minor = Record[Idx++];
6405 unsigned Subminor = Record[Idx++];
6406 if (Minor == 0)
6407 return VersionTuple(Major);
6408 if (Subminor == 0)
6409 return VersionTuple(Major, Minor - 1);
6410 return VersionTuple(Major, Minor - 1, Subminor - 1);
6411}
6412
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006413CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006414 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006415 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006416 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006417 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006418}
6419
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006420DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006421 return Diag(SourceLocation(), DiagID);
6422}
6423
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006424DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006425 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006426}
Douglas Gregor025452f2009-04-17 00:04:06 +00006427
Douglas Gregor668c1a42009-04-21 22:25:48 +00006428/// \brief Retrieve the identifier table associated with the
6429/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006430IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006431 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006432}
6433
Douglas Gregor025452f2009-04-17 00:04:06 +00006434/// \brief Record that the given ID maps to the given switch-case
6435/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006436void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006437 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6438 "Already have a SwitchCase with this ID");
6439 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006440}
6441
6442/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006443SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006444 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6445 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006446}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006447
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006448void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006449 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006450}
6451
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006452void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006453 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006454 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6455 serialization::ModuleFile *> >::iterator
6456 I = CommentsCursors.begin(),
6457 E = CommentsCursors.end();
6458 I != E; ++I) {
6459 llvm::BitstreamCursor &Cursor = I->first;
6460 serialization::ModuleFile &F = *I->second;
6461 SavedStreamPosition SavedPosition(Cursor);
6462
6463 RecordData Record;
6464 while (true) {
6465 unsigned Code = Cursor.ReadCode();
6466 if (Code == llvm::bitc::END_BLOCK)
6467 break;
6468
6469 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6470 // No known subblocks, always skip them.
6471 Cursor.ReadSubBlockID();
6472 if (Cursor.SkipBlock()) {
6473 Error("malformed block record in AST file");
6474 return;
6475 }
6476 continue;
6477 }
6478
6479 if (Code == llvm::bitc::DEFINE_ABBREV) {
6480 Cursor.ReadAbbrevRecord();
6481 continue;
6482 }
6483
6484 // Read a record.
6485 Record.clear();
6486 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006487 case COMMENTS_RAW_COMMENT: {
6488 unsigned Idx = 0;
6489 SourceRange SR = ReadSourceRange(F, Record, Idx);
6490 RawComment::CommentKind Kind =
6491 (RawComment::CommentKind) Record[Idx++];
6492 bool IsTrailingComment = Record[Idx++];
6493 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006494 Comments.push_back(new (Context) RawComment(SR, Kind,
6495 IsTrailingComment,
6496 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006497 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006498 }
6499 }
6500 }
6501 }
6502 Context.Comments.addCommentsToFront(Comments);
6503}
6504
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006505void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006506 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6507 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006508 // If any identifiers with corresponding top-level declarations have
6509 // been loaded, load those declarations now.
6510 while (!PendingIdentifierInfos.empty()) {
6511 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6512 PendingIdentifierInfos.front().DeclIDs, true);
6513 PendingIdentifierInfos.pop_front();
6514 }
6515
Douglas Gregora1be2782011-12-17 23:38:30 +00006516 // Load pending declaration chains.
6517 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6518 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006519 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006520 }
6521 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006522
6523 // Load any pending macro definitions.
6524 // FIXME: Non-determinism here.
6525 while (!PendingMacroIDs.empty())
6526 LoadMacroDefinition(PendingMacroIDs.begin());
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006527 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006528
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006529 // If we deserialized any C++ or Objective-C class definitions, any
6530 // Objective-C protocol definitions, or any redeclarable templates, make sure
6531 // that all redeclarations point to the definitions. Note that this can only
6532 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006533 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6534 DEnd = PendingDefinitions.end();
6535 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006536 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6537 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6538 // Make sure that the TagType points at the definition.
6539 const_cast<TagType*>(TagT)->decl = TD;
6540 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006541
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006542 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6543 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6544 REnd = RD->redecls_end();
6545 R != REnd; ++R)
6546 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6547
6548 }
6549
Douglas Gregorfc529f72011-12-19 19:00:47 +00006550 continue;
6551 }
6552
Douglas Gregor1d784b22012-01-01 19:51:50 +00006553 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006554 // Make sure that the ObjCInterfaceType points at the definition.
6555 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6556 ->Decl = ID;
6557
Douglas Gregor1d784b22012-01-01 19:51:50 +00006558 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6559 REnd = ID->redecls_end();
6560 R != REnd; ++R)
6561 R->Data = ID->Data;
6562
6563 continue;
6564 }
6565
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006566 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6567 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6568 REnd = PD->redecls_end();
6569 R != REnd; ++R)
6570 R->Data = PD->Data;
6571
6572 continue;
6573 }
6574
6575 RedeclarableTemplateDecl *RTD
6576 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6577 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6578 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006579 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006580 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006581 }
6582 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006583
6584 // Load the bodies of any functions or methods we've encountered. We do
6585 // this now (delayed) so that we can be sure that the declaration chains
6586 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006587 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6588 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006589 PB != PBEnd; ++PB) {
6590 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6591 // FIXME: Check for =delete/=default?
6592 // FIXME: Complain about ODR violations here?
6593 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6594 FD->setLazyBody(PB->second);
6595 continue;
6596 }
6597
6598 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6599 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6600 MD->setLazyBody(PB->second);
6601 }
6602 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006603}
6604
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006605void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006606 assert(NumCurrentElementsDeserializing &&
6607 "FinishedDeserializing not paired with StartedDeserializing");
6608 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006609 // We decrease NumCurrentElementsDeserializing only after pending actions
6610 // are finished, to avoid recursively re-calling finishPendingActions().
6611 finishPendingActions();
6612 }
6613 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006614
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006615 if (NumCurrentElementsDeserializing == 0 &&
6616 Consumer && !PassingDeclsToConsumer) {
6617 // Guard variable to avoid recursively redoing the process of passing
6618 // decls to consumer.
6619 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6620 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006621
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006622 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006623 // We are not in recursive loading, so it's safe to pass the "interesting"
6624 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006625 Decl *D = InterestingDecls.front();
6626 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006627 PassInterestingDeclToConsumer(D);
6628 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006629 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006630}
Douglas Gregor501c1032010-08-19 00:28:17 +00006631
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006632ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006633 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006634 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006635 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6636 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006637 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006638 Consumer(0), ModuleMgr(PP.getFileManager()),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006639 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006640 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006641 DisableStatCache(DisableStatCache),
6642 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006643 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6644 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006645 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006646 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6647 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6648 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6649 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006650 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6651 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006652 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006653 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006654{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006655 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006656}
6657
Sebastian Redle1dde812010-08-24 00:50:04 +00006658ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006659 for (DeclContextVisibleUpdatesPending::iterator
6660 I = PendingVisibleUpdates.begin(),
6661 E = PendingVisibleUpdates.end();
6662 I != E; ++I) {
6663 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6664 F = I->second.end();
6665 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006666 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006667 }
6668}