blob: 736f082d6f38f61628eb4f21bbf3f5d274af2206 [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 Gregor668c1a42009-04-21 22:25:48 +0000543
Douglas Gregor98339b92011-08-25 20:47:51 +0000544 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000545 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000546
Douglas Gregor98339b92011-08-25 20:47:51 +0000547 // Build the IdentifierInfo itself and link the identifier ID with
548 // the new IdentifierInfo.
549 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000550 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000551 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000552 KnownII = II;
553 }
Douglas Gregor057df202012-01-18 20:56:22 +0000554 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000555 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000556
Douglas Gregor98339b92011-08-25 20:47:51 +0000557 // Set or check the various bits in the IdentifierInfo structure.
558 // Token IDs are read-only.
559 if (HasRevertedTokenIDToIdentifier)
560 II->RevertTokenIDToIdentifier();
561 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
562 assert(II->isExtensionToken() == ExtensionToken &&
563 "Incorrect extension token flag");
564 (void)ExtensionToken;
565 if (Poisoned)
566 II->setIsPoisoned(true);
567 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
568 "Incorrect C++ operator keyword flag");
569 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000570
Douglas Gregor98339b92011-08-25 20:47:51 +0000571 // If this identifier is a macro, deserialize the macro
572 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000573 if (hadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000574 SmallVector<MacroID, 4> MacroIDs;
575 while (uint32_t LocalID = ReadUnalignedLE32(d)) {
576 MacroIDs.push_back(Reader.getGlobalMacroID(F, LocalID));
577 DataLen -= 4;
Douglas Gregor13292642011-12-02 15:45:10 +0000578 }
Douglas Gregor6c6c54a2012-10-11 00:46:49 +0000579 DataLen -= 4;
580 Reader.setIdentifierIsMacro(II, MacroIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000581 }
582
Douglas Gregoreee242f2011-10-27 09:33:13 +0000583 Reader.SetIdentifierInfo(ID, II);
584
Douglas Gregor98339b92011-08-25 20:47:51 +0000585 // Read all of the declarations visible at global scope with this
586 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000587 if (DataLen > 0) {
588 SmallVector<uint32_t, 4> DeclIDs;
589 for (; DataLen > 0; DataLen -= 4)
590 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
591 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000592 }
593
Douglas Gregor98339b92011-08-25 20:47:51 +0000594 return II;
595}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000596
Douglas Gregor98339b92011-08-25 20:47:51 +0000597unsigned
598ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
599 llvm::FoldingSetNodeID ID;
600 ID.AddInteger(Key.Kind);
601
602 switch (Key.Kind) {
603 case DeclarationName::Identifier:
604 case DeclarationName::CXXLiteralOperatorName:
605 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
606 break;
607 case DeclarationName::ObjCZeroArgSelector:
608 case DeclarationName::ObjCOneArgSelector:
609 case DeclarationName::ObjCMultiArgSelector:
610 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
611 break;
612 case DeclarationName::CXXOperatorName:
613 ID.AddInteger((OverloadedOperatorKind)Key.Data);
614 break;
615 case DeclarationName::CXXConstructorName:
616 case DeclarationName::CXXDestructorName:
617 case DeclarationName::CXXConversionFunctionName:
618 case DeclarationName::CXXUsingDirective:
619 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000620 }
621
Douglas Gregor98339b92011-08-25 20:47:51 +0000622 return ID.ComputeHash();
623}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000624
Douglas Gregor98339b92011-08-25 20:47:51 +0000625ASTDeclContextNameLookupTrait::internal_key_type
626ASTDeclContextNameLookupTrait::GetInternalKey(
627 const external_key_type& Name) const {
628 DeclNameKey Key;
629 Key.Kind = Name.getNameKind();
630 switch (Name.getNameKind()) {
631 case DeclarationName::Identifier:
632 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
633 break;
634 case DeclarationName::ObjCZeroArgSelector:
635 case DeclarationName::ObjCOneArgSelector:
636 case DeclarationName::ObjCMultiArgSelector:
637 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
638 break;
639 case DeclarationName::CXXOperatorName:
640 Key.Data = Name.getCXXOverloadedOperator();
641 break;
642 case DeclarationName::CXXLiteralOperatorName:
643 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
644 break;
645 case DeclarationName::CXXConstructorName:
646 case DeclarationName::CXXDestructorName:
647 case DeclarationName::CXXConversionFunctionName:
648 case DeclarationName::CXXUsingDirective:
649 Key.Data = 0;
650 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000651 }
652
Douglas Gregor98339b92011-08-25 20:47:51 +0000653 return Key;
654}
655
Douglas Gregor98339b92011-08-25 20:47:51 +0000656std::pair<unsigned, unsigned>
657ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
658 using namespace clang::io;
659 unsigned KeyLen = ReadUnalignedLE16(d);
660 unsigned DataLen = ReadUnalignedLE16(d);
661 return std::make_pair(KeyLen, DataLen);
662}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000663
Douglas Gregor98339b92011-08-25 20:47:51 +0000664ASTDeclContextNameLookupTrait::internal_key_type
665ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
666 using namespace clang::io;
667
668 DeclNameKey Key;
669 Key.Kind = (DeclarationName::NameKind)*d++;
670 switch (Key.Kind) {
671 case DeclarationName::Identifier:
672 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
673 break;
674 case DeclarationName::ObjCZeroArgSelector:
675 case DeclarationName::ObjCOneArgSelector:
676 case DeclarationName::ObjCMultiArgSelector:
677 Key.Data =
678 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
679 .getAsOpaquePtr();
680 break;
681 case DeclarationName::CXXOperatorName:
682 Key.Data = *d++; // OverloadedOperatorKind
683 break;
684 case DeclarationName::CXXLiteralOperatorName:
685 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
686 break;
687 case DeclarationName::CXXConstructorName:
688 case DeclarationName::CXXDestructorName:
689 case DeclarationName::CXXConversionFunctionName:
690 case DeclarationName::CXXUsingDirective:
691 Key.Data = 0;
692 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000693 }
694
Douglas Gregor98339b92011-08-25 20:47:51 +0000695 return Key;
696}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000697
Douglas Gregor98339b92011-08-25 20:47:51 +0000698ASTDeclContextNameLookupTrait::data_type
699ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
700 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000701 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000702 using namespace clang::io;
703 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000704 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000705 return std::make_pair(Start, Start + NumDecls);
706}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000707
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000708bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000709 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000710 const std::pair<uint64_t, uint64_t> &Offsets,
711 DeclContextInfo &Info) {
712 SavedStreamPosition SavedPosition(Cursor);
713 // First the lexical decls.
714 if (Offsets.first != 0) {
715 Cursor.JumpToBit(Offsets.first);
716
717 RecordData Record;
718 const char *Blob;
719 unsigned BlobLen;
720 unsigned Code = Cursor.ReadCode();
721 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
722 if (RecCode != DECL_CONTEXT_LEXICAL) {
723 Error("Expected lexical block");
724 return true;
725 }
726
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000727 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
728 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000729 }
730
731 // Now the lookup table.
732 if (Offsets.second != 0) {
733 Cursor.JumpToBit(Offsets.second);
734
735 RecordData Record;
736 const char *Blob;
737 unsigned BlobLen;
738 unsigned Code = Cursor.ReadCode();
739 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
740 if (RecCode != DECL_CONTEXT_VISIBLE) {
741 Error("Expected visible lookup table block");
742 return true;
743 }
744 Info.NameLookupTableData
745 = ASTDeclContextNameLookupTable::Create(
746 (const unsigned char *)Blob + Record[0],
747 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000748 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000749 }
750
751 return false;
752}
753
Chris Lattner5f9e2722011-07-23 10:55:15 +0000754void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000755 Error(diag::err_fe_pch_malformed, Msg);
756}
757
758void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000759 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000760 if (Diags.isDiagnosticInFlight())
761 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
762 else
763 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000764}
765
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000766/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000767bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000768 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000769 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000770 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000771 SuggestedPredefines,
772 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000773 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000774}
775
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000776//===----------------------------------------------------------------------===//
777// Source Manager Deserialization
778//===----------------------------------------------------------------------===//
779
Douglas Gregorbd945002009-04-13 16:31:14 +0000780/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000781/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000782bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000783 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000784 unsigned Idx = 0;
785 LineTableInfo &LineTable = SourceMgr.getLineTable();
786
787 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000788 std::map<int, int> FileIDs;
789 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000790 // Extract the file name
791 unsigned FilenameLen = Record[Idx++];
792 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
793 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000794 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000795 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000796 }
797
798 // Parse the line entries
799 std::vector<LineEntry> Entries;
800 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000801 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000802 assert(FID >= 0 && "Serialized line entries for non-local file.");
803 // Remap FileID from 1-based old view.
804 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000805
806 // Extract the line entries
807 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000808 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000809 Entries.clear();
810 Entries.reserve(NumEntries);
811 for (unsigned I = 0; I != NumEntries; ++I) {
812 unsigned FileOffset = Record[Idx++];
813 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000814 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000815 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000816 = (SrcMgr::CharacteristicKind)Record[Idx++];
817 unsigned IncludeOffset = Record[Idx++];
818 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
819 FileKind, IncludeOffset));
820 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000821 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000822 }
823
824 return false;
825}
826
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000827namespace {
828
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000829class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000830public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000831 const ino_t ino;
832 const dev_t dev;
833 const mode_t mode;
834 const time_t mtime;
835 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000837 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000838 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000839};
840
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000841class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000842 public:
843 typedef const char *external_key_type;
844 typedef const char *internal_key_type;
845
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000846 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847
848 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000849 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850 }
851
852 static internal_key_type GetInternalKey(const char *path) { return path; }
853
854 static bool EqualKey(internal_key_type a, internal_key_type b) {
855 return strcmp(a, b) == 0;
856 }
857
858 static std::pair<unsigned, unsigned>
859 ReadKeyDataLength(const unsigned char*& d) {
860 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
861 unsigned DataLen = (unsigned) *d++;
862 return std::make_pair(KeyLen + 1, DataLen);
863 }
864
865 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
866 return (const char *)d;
867 }
868
869 static data_type ReadData(const internal_key_type, const unsigned char *d,
870 unsigned /*DataLen*/) {
871 using namespace clang::io;
872
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000873 ino_t ino = (ino_t) ReadUnalignedLE32(d);
874 dev_t dev = (dev_t) ReadUnalignedLE32(d);
875 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000876 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000877 off_t size = (off_t) ReadUnalignedLE64(d);
878 return data_type(ino, dev, mode, mtime, size);
879 }
880};
881
882/// \brief stat() cache for precompiled headers.
883///
884/// This cache is very similar to the stat cache used by pretokenized
885/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000886class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000887 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000888 CacheTy *Cache;
889
890 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000891public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000892 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
893 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000894 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
895 Cache = CacheTy::Create(Buckets, Base);
896 }
897
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000898 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Chris Lattner898a0612010-11-23 21:17:56 +0000900 LookupResult getStat(const char *Path, struct stat &StatBuf,
901 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000902 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000903 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000904
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000905 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000906 if (I == Cache->end()) {
907 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000908 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000912 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattner10e286a2010-11-23 19:19:34 +0000914 StatBuf.st_ino = Data.ino;
915 StatBuf.st_dev = Data.dev;
916 StatBuf.st_mtime = Data.mtime;
917 StatBuf.st_mode = Data.mode;
918 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000919 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000920 }
921};
922} // end anonymous namespace
923
924
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000925/// \brief Read a source manager block
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000926ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000927 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000928
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000929 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000930
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000931 // Set the source-location entry cursor to the current position in
932 // the stream. This cursor will be used to read the contents of the
933 // source manager block initially, and then lazily read
934 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000935 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000936
937 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000938 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000939 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000940 return Failure;
941 }
942
943 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000944 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000945 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000946 return Failure;
947 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000948
Douglas Gregor14f79002009-04-10 03:52:48 +0000949 RecordData Record;
950 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000951 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000952 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000953 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000954 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000955 return Failure;
956 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000957 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregor14f79002009-04-10 03:52:48 +0000960 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
961 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000962 SLocEntryCursor.ReadSubBlockID();
963 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000964 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000965 return Failure;
966 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000967 continue;
968 }
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Douglas Gregor14f79002009-04-10 03:52:48 +0000970 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000971 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000972 continue;
973 }
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregor14f79002009-04-10 03:52:48 +0000975 // Read a record.
976 const char *BlobStart;
977 unsigned BlobLen;
978 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000979 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000980 default: // Default behavior: ignore.
981 break;
982
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000983 case SM_SLOC_FILE_ENTRY:
984 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000985 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000986 // Once we hit one of the source location entries, we're done.
987 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000988 }
989 }
990}
991
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000992/// \brief If a header file is not found at the path that we expect it to be
993/// and the PCH file was moved from its original location, try to resolve the
994/// file by assuming that header+PCH were moved together and the header is in
995/// the same place relative to the PCH.
996static std::string
997resolveFileRelativeToOriginalDir(const std::string &Filename,
998 const std::string &OriginalDir,
999 const std::string &CurrDir) {
1000 assert(OriginalDir != CurrDir &&
1001 "No point trying to resolve the file if the PCH dir didn't change");
1002 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001003 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001004 fs::make_absolute(filePath);
1005 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001006 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001007
1008 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1009 fileDirE = path::end(path::parent_path(filePath));
1010 path::const_iterator origDirI = path::begin(OriginalDir),
1011 origDirE = path::end(OriginalDir);
1012 // Skip the common path components from filePath and OriginalDir.
1013 while (fileDirI != fileDirE && origDirI != origDirE &&
1014 *fileDirI == *origDirI) {
1015 ++fileDirI;
1016 ++origDirI;
1017 }
1018 for (; origDirI != origDirE; ++origDirI)
1019 path::append(currPCHPath, "..");
1020 path::append(currPCHPath, fileDirI, fileDirE);
1021 path::append(currPCHPath, path::filename(Filename));
1022 return currPCHPath.str();
1023}
1024
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001025/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001026ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001027 if (ID == 0)
1028 return Success;
1029
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001030 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001031 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001032 return Failure;
1033 }
1034
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001035 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001036 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001037 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001038 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001039
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001040 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001041 unsigned Code = SLocEntryCursor.ReadCode();
1042 if (Code == llvm::bitc::END_BLOCK ||
1043 Code == llvm::bitc::ENTER_SUBBLOCK ||
1044 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001045 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001046 return Failure;
1047 }
1048
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001049 RecordData Record;
1050 const char *BlobStart;
1051 unsigned BlobLen;
1052 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1053 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001054 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001055 return Failure;
1056
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001057 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00001058 if (Record.size() < 7) {
1059 Error("source location entry is incorrect");
1060 return Failure;
1061 }
1062
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001063 // We will detect whether a file changed and return 'Failure' for it, but
1064 // we will also try to fail gracefully by setting up the SLocEntry.
1065 ASTReader::ASTReadResult Result = Success;
1066
Douglas Gregora081da52011-11-16 20:05:18 +00001067 bool OverriddenBuffer = Record[6];
1068
1069 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1070 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001071 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora4581a12011-11-17 19:08:51 +00001072 const FileEntry *File =
1073 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1074 (time_t)Record[5])
1075 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001076 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1077 OriginalDir != CurrentDir) {
1078 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1079 OriginalDir,
1080 CurrentDir);
1081 if (!resolved.empty())
1082 File = FileMgr.getFile(resolved);
1083 }
Axel Naumann04331162011-01-27 10:55:51 +00001084 if (File == 0)
1085 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1086 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001087 if (File == 0) {
1088 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001089 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001090 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001091 Error(ErrorStr.c_str());
1092 return Failure;
1093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +00001095 if (!DisableValidation && !OverriddenBuffer &&
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001096 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001097#if !defined(LLVM_ON_WIN32)
1098 // In our regression testing, the Windows file system seems to
1099 // have inconsistent modification times that sometimes
1100 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001101 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001102#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001103 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001104 Error(diag::err_fe_pch_file_modified, Filename);
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001105 Result = Failure;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001106 }
1107
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001108 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001109 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001110 // This is the module's main file.
1111 IncludeLoc = getImportLocation(F);
1112 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001113 SrcMgr::CharacteristicKind
1114 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1115 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001116 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001117 SrcMgr::FileInfo &FileInfo =
1118 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001119 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001121 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001122
Douglas Gregora081da52011-11-16 20:05:18 +00001123 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1124 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001125 if (NumFileDecls) {
1126 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001127 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1128 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001129 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001130
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001131 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001132 = SourceMgr.getOrCreateContentCache(File,
1133 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001134 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1135 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001136 unsigned Code = SLocEntryCursor.ReadCode();
1137 Record.clear();
1138 unsigned RecCode
1139 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1140
1141 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1142 Error("AST record has invalid code");
1143 return Failure;
1144 }
1145
1146 llvm::MemoryBuffer *Buffer
1147 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1148 Filename);
1149 SourceMgr.overrideFileContents(File, Buffer);
1150 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001151
1152 if (Result == Failure)
1153 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001154 break;
1155 }
1156
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001157 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001158 const char *Name = BlobStart;
1159 unsigned Offset = Record[0];
1160 unsigned Code = SLocEntryCursor.ReadCode();
1161 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001162 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001163 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001164
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001165 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001166 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001167 return Failure;
1168 }
1169
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001170 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001171 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1172 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001173 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1174 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Douglas Gregor6236a292011-12-02 21:56:05 +00001176 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001177 PCHPredefinesBlock Block = {
1178 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001179 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001180 };
1181 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001182 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001183
1184 break;
1185 }
1186
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001187 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001188 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001189 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001190 ReadSourceLocation(*F, Record[2]),
1191 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001192 Record[4],
1193 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001194 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001195 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001196 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001197 }
1198
1199 return Success;
1200}
1201
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001202/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001203SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001204 if (F->ImportLoc.isValid())
1205 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001206
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001207 // Otherwise we have a PCH. It's considered to be "imported" at the first
1208 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001209 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001210 // Main file is the importer. We assume that it is the first entry in the
1211 // entry table. We can't ask the manager, because at the time of PCH loading
1212 // the main file entry doesn't exist yet.
1213 // The very first entry is the invalid instantiation loc, which takes up
1214 // offsets 0 and 1.
1215 return SourceLocation::getFromRawEncoding(2U);
1216 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001217 //return F->Loaders[0]->FirstLoc;
1218 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001219}
1220
Chris Lattner6367f6d2009-04-27 01:05:14 +00001221/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1222/// specified cursor. Read the abbreviations that are at the top of the block
1223/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001224bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001225 unsigned BlockID) {
1226 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001227 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001228 return Failure;
1229 }
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Chris Lattner6367f6d2009-04-27 01:05:14 +00001231 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001232 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001233 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001234
Chris Lattner6367f6d2009-04-27 01:05:14 +00001235 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001236 if (Code != llvm::bitc::DEFINE_ABBREV) {
1237 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001238 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001239 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001240 Cursor.ReadAbbrevRecord();
1241 }
1242}
1243
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001244void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001245 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Douglas Gregor37e26842009-04-21 23:56:24 +00001247 // Keep track of where we are in the stream, then jump back there
1248 // after reading this macro.
1249 SavedStreamPosition SavedPosition(Stream);
1250
1251 Stream.JumpToBit(Offset);
1252 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001253 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001254 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Douglas Gregor37e26842009-04-21 23:56:24 +00001256 while (true) {
1257 unsigned Code = Stream.ReadCode();
1258 switch (Code) {
1259 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001260 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001261
1262 case llvm::bitc::ENTER_SUBBLOCK:
1263 // No known subblocks, always skip them.
1264 Stream.ReadSubBlockID();
1265 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001266 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001267 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001268 }
1269 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Douglas Gregor37e26842009-04-21 23:56:24 +00001271 case llvm::bitc::DEFINE_ABBREV:
1272 Stream.ReadAbbrevRecord();
1273 continue;
1274 default: break;
1275 }
1276
1277 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001278 const char *BlobStart = 0;
1279 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001280 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001281 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001282 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001283 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001284 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001285 case PP_MACRO_OBJECT_LIKE:
1286 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001287 // If we already have a macro, that means that we've hit the end
1288 // of the definition of the macro we were looking for. We're
1289 // done.
1290 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001291 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001292
Douglas Gregor95eab172011-07-28 20:55:49 +00001293 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001294 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001295 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001296 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001297 }
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Douglas Gregora8235d62012-10-09 23:05:51 +00001299 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1300
1301 // If this macro has already been loaded, don't do so again.
1302 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1303 return;
1304
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001305 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1306 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001307 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001308 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001309
Douglas Gregora8235d62012-10-09 23:05:51 +00001310 // Record this macro.
1311 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1312
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001313 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1314 if (UndefLoc.isValid())
1315 MI->setUndefLoc(UndefLoc);
1316
1317 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001318 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001320 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001321 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001322
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001323 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001324 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001325 bool isC99VarArgs = Record[NextIndex++];
1326 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001327 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001328 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001330 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001331
1332 // Install function-like macro info.
1333 MI->setIsFunctionLike();
1334 if (isC99VarArgs) MI->setIsC99Varargs();
1335 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001336 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001337 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001338 }
1339
Douglas Gregora8235d62012-10-09 23:05:51 +00001340 if (DeserializationListener)
1341 DeserializationListener->MacroRead(GlobalID, MI);
1342
1343 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001344 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001345 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1346 if (Update != MacroUpdates.end()) {
1347 if (MI->getUndefLoc().isInvalid()) {
1348 MI->setUndefLoc(Update->second.UndefLoc);
1349 if (PPMutationListener *Listener = PP.getPPMutationListener())
1350 Listener->UndefinedMacro(MI);
1351 }
1352 MacroUpdates.erase(Update);
1353 }
1354
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001355 // Determine whether this macro definition is visible.
1356 bool Hidden = !MI->isPublic();
1357 if (!Hidden && GlobalSubmoduleID) {
1358 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1359 if (Owner->NameVisibility == Module::Hidden) {
1360 // The owning module is not visible, and this macro definition
1361 // should not be, either.
1362 Hidden = true;
1363
1364 // Note that this macro definition was hidden because its owning
1365 // module is not yet visible.
1366 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1367 }
1368 }
1369 }
1370 MI->setHidden(Hidden);
1371
Douglas Gregor37e26842009-04-21 23:56:24 +00001372 // Finally, install the macro.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001373 PP.addLoadedMacroInfo(II, MI);
Douglas Gregor37e26842009-04-21 23:56:24 +00001374
1375 // Remember that we saw this macro last so that we add the tokens that
1376 // form its body to it.
1377 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001378
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001379 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1380 Record[NextIndex]) {
1381 // We have a macro definition. Register the association
1382 PreprocessedEntityID
1383 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1384 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1385 PPRec.RegisterMacroDefinition(Macro,
1386 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001387 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001388
Douglas Gregor37e26842009-04-21 23:56:24 +00001389 ++NumMacrosRead;
1390 break;
1391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001393 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001394 // If we see a TOKEN before a PP_MACRO_*, then the file is
1395 // erroneous, just pretend we didn't see this.
1396 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregor37e26842009-04-21 23:56:24 +00001398 Token Tok;
1399 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001400 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001401 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001402 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001403 Tok.setIdentifierInfo(II);
1404 Tok.setKind((tok::TokenKind)Record[3]);
1405 Tok.setFlag((Token::TokenFlags)Record[4]);
1406 Macro->AddTokenToBody(Tok);
1407 break;
1408 }
David Blaikie7530c032012-01-17 06:56:22 +00001409 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001410 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001411}
1412
Douglas Gregor86c67d82011-07-28 22:39:26 +00001413PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001414ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001415 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001416 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1417 assert(I != M.PreprocessedEntityRemap.end()
1418 && "Invalid index into preprocessed entity index remap");
1419
1420 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001421}
1422
Douglas Gregor98339b92011-08-25 20:47:51 +00001423unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1424 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001425}
Douglas Gregor98339b92011-08-25 20:47:51 +00001426
1427HeaderFileInfoTrait::internal_key_type
1428HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1429
1430bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1431 if (strcmp(a, b) == 0)
1432 return true;
1433
1434 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1435 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001436
1437 // Determine whether the actual files are equivalent.
1438 bool Result = false;
1439 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001440 return false;
1441
Douglas Gregor99a922b2011-12-09 16:22:07 +00001442 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001443}
1444
1445std::pair<unsigned, unsigned>
1446HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1447 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1448 unsigned DataLen = (unsigned) *d++;
1449 return std::make_pair(KeyLen + 1, DataLen);
1450}
1451
1452HeaderFileInfoTrait::data_type
1453HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1454 unsigned DataLen) {
1455 const unsigned char *End = d + DataLen;
1456 using namespace clang::io;
1457 HeaderFileInfo HFI;
1458 unsigned Flags = *d++;
1459 HFI.isImport = (Flags >> 5) & 0x01;
1460 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1461 HFI.DirInfo = (Flags >> 2) & 0x03;
1462 HFI.Resolved = (Flags >> 1) & 0x01;
1463 HFI.IndexHeaderMapHeader = Flags & 0x01;
1464 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001465 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1466 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001467 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1468 // The framework offset is 1 greater than the actual offset,
1469 // since 0 is used as an indicator for "no framework name".
1470 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1471 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1472 }
1473
1474 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1475 (void)End;
1476
1477 // This HeaderFileInfo was externally loaded.
1478 HFI.External = true;
1479 return HFI;
1480}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001481
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001482void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1483 II->setHadMacroDefinition(true);
1484 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1485 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001486}
1487
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001488void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001489 // Note that we are loading defined macros.
1490 Deserializing Macros(this);
1491
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001492 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1493 E = ModuleMgr.rend(); I != E; ++I) {
1494 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001495
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001496 // If there was no preprocessor block, skip this file.
1497 if (!MacroCursor.getBitStreamReader())
1498 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001499
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001500 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001501 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001502
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001503 RecordData Record;
1504 while (true) {
1505 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001506 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001507 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001508
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001509 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1510 // No known subblocks, always skip them.
1511 Cursor.ReadSubBlockID();
1512 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001513 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001514 return;
1515 }
1516 continue;
1517 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001518
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001519 if (Code == llvm::bitc::DEFINE_ABBREV) {
1520 Cursor.ReadAbbrevRecord();
1521 continue;
1522 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001523
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001524 // Read a record.
1525 const char *BlobStart;
1526 unsigned BlobLen;
1527 Record.clear();
1528 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1529 default: // Default behavior: ignore.
1530 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001531
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001532 case PP_MACRO_OBJECT_LIKE:
1533 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001534 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001535 break;
1536
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001537 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001538 // Ignore tokens.
1539 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001540 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001541 }
1542 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001543}
1544
Douglas Gregoreee242f2011-10-27 09:33:13 +00001545namespace {
1546 /// \brief Visitor class used to look up identifirs in an AST file.
1547 class IdentifierLookupVisitor {
1548 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001549 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001550 IdentifierInfo *Found;
1551 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001552 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1553 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001554
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001555 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001556 IdentifierLookupVisitor *This
1557 = static_cast<IdentifierLookupVisitor *>(UserData);
1558
Douglas Gregor057df202012-01-18 20:56:22 +00001559 // If we've already searched this module file, skip it now.
1560 if (M.Generation <= This->PriorGeneration)
1561 return true;
1562
Douglas Gregoreee242f2011-10-27 09:33:13 +00001563 ASTIdentifierLookupTable *IdTable
1564 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1565 if (!IdTable)
1566 return false;
1567
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001568 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1569 M, This->Found);
1570
Douglas Gregoreee242f2011-10-27 09:33:13 +00001571 std::pair<const char*, unsigned> Key(This->Name.begin(),
1572 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001573 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001574 if (Pos == IdTable->end())
1575 return false;
1576
1577 // Dereferencing the iterator has the effect of building the
1578 // IdentifierInfo node and populating it with the various
1579 // declarations it needs.
1580 This->Found = *Pos;
1581 return true;
1582 }
1583
1584 // \brief Retrieve the identifier info found within the module
1585 // files.
1586 IdentifierInfo *getIdentifierInfo() const { return Found; }
1587 };
1588}
1589
1590void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001591 // Note that we are loading an identifier.
1592 Deserializing AnIdentifier(this);
1593
Douglas Gregor057df202012-01-18 20:56:22 +00001594 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001595 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001596 PriorGeneration = IdentifierGeneration[&II];
1597
1598 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1599 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1600 markIdentifierUpToDate(&II);
1601}
1602
1603void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1604 if (!II)
1605 return;
1606
1607 II->setOutOfDate(false);
1608
1609 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001610 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001611 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001612}
1613
Chris Lattner5f9e2722011-07-23 10:55:15 +00001614const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001615 std::string Filename = filenameStrRef;
1616 MaybeAddSystemRootToFilename(Filename);
1617 const FileEntry *File = FileMgr.getFile(Filename);
1618 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1619 OriginalDir != CurrentDir) {
1620 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1621 OriginalDir,
1622 CurrentDir);
1623 if (!resolved.empty())
1624 File = FileMgr.getFile(resolved);
1625 }
1626
1627 return File;
1628}
1629
Douglas Gregore650c8c2009-07-07 00:12:59 +00001630/// \brief If we are loading a relocatable PCH file, and the filename is
1631/// not an absolute path, add the system root to the beginning of the file
1632/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001633void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001634 // If this is not a relocatable PCH file, there's nothing to do.
1635 if (!RelocatablePCH)
1636 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Michael J. Spencer256053b2010-12-17 21:22:22 +00001638 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001639 return;
1640
Douglas Gregor832d6202011-07-22 16:35:34 +00001641 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001642 // If no system root was given, default to '/'
1643 Filename.insert(Filename.begin(), '/');
1644 return;
1645 }
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Douglas Gregor832d6202011-07-22 16:35:34 +00001647 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001648 if (isysroot[Length - 1] != '/')
1649 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Douglas Gregor832d6202011-07-22 16:35:34 +00001651 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001652}
1653
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001654ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001655ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001656 llvm::BitstreamCursor &Stream = F.Stream;
1657
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001658 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001659 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001660 return Failure;
1661 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001662
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001663 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001664 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001665 while (!Stream.AtEndOfStream()) {
1666 unsigned Code = Stream.ReadCode();
1667 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001668 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001669 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001670 return Failure;
1671 }
Chris Lattner7356a312009-04-11 21:15:38 +00001672
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001673 DeclContext *DC = Context.getTranslationUnitDecl();
1674 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1675 DC->setMustBuildLookupTable();
1676
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001677 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001678 }
1679
1680 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1681 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001682 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001683 // We lazily load the decls block, but we want to set up the
1684 // DeclsCursor cursor to point into it. Clone our current bitcode
1685 // cursor to it, enter the block and read the abbrevs in that block.
1686 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001687 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001688 if (Stream.SkipBlock() || // Skip with the main cursor.
1689 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001690 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001691 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001692 return Failure;
1693 }
1694 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001696 case DECL_UPDATES_BLOCK_ID:
1697 if (Stream.SkipBlock()) {
1698 Error("malformed block record in AST file");
1699 return Failure;
1700 }
1701 break;
1702
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001703 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001704 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001705 if (!PP.getExternalSource())
1706 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001707
Douglas Gregorecdcb882010-10-20 22:00:55 +00001708 if (Stream.SkipBlock() ||
1709 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001710 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001711 return Failure;
1712 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001713 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001714 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001715
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001716 case PREPROCESSOR_DETAIL_BLOCK_ID:
1717 F.PreprocessorDetailCursor = Stream;
1718 if (Stream.SkipBlock() ||
1719 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1720 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1721 Error("malformed preprocessor detail record in AST file");
1722 return Failure;
1723 }
1724 F.PreprocessorDetailStartOffset
1725 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001726
1727 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001728 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001729 if (!PP.getPreprocessingRecord()->getExternalSource())
1730 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001731 break;
1732
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001733 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001734 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001735 case Success:
1736 break;
1737
1738 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001739 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001740 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001741
1742 case IgnorePCH:
1743 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001744 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001745 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001746
1747 case SUBMODULE_BLOCK_ID:
1748 switch (ReadSubmoduleBlock(F)) {
1749 case Success:
1750 break;
1751
1752 case Failure:
1753 Error("malformed submodule block in AST file");
1754 return Failure;
1755
1756 case IgnorePCH:
1757 return IgnorePCH;
1758 }
1759 break;
1760
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001761 case COMMENTS_BLOCK_ID: {
1762 llvm::BitstreamCursor C = Stream;
1763 if (Stream.SkipBlock() ||
1764 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1765 Error("malformed comments block in AST file");
1766 return Failure;
1767 }
1768 CommentsCursors.push_back(std::make_pair(C, &F));
1769 break;
1770 }
1771
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001772 default:
1773 if (!Stream.SkipBlock())
1774 break;
1775 Error("malformed block record in AST file");
1776 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001777 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001778 continue;
1779 }
1780
1781 if (Code == llvm::bitc::DEFINE_ABBREV) {
1782 Stream.ReadAbbrevRecord();
1783 continue;
1784 }
1785
1786 // Read and process a record.
1787 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001788 const char *BlobStart = 0;
1789 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001790 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001791 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001792 default: // Default behavior: ignore.
1793 break;
1794
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001795 case METADATA: {
1796 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1797 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001798 : diag::warn_pch_version_too_new);
1799 return IgnorePCH;
1800 }
1801
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001802 bool hasErrors = Record[5];
1803 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1804 Diag(diag::err_pch_with_compiler_errors);
1805 return IgnorePCH;
1806 }
1807
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001808 RelocatablePCH = Record[4];
1809 if (Listener) {
1810 std::string TargetTriple(BlobStart, BlobLen);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001811 if (Listener->ReadTargetTriple(F, TargetTriple))
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001812 return IgnorePCH;
1813 }
1814 break;
1815 }
1816
Douglas Gregore95b9192011-08-17 21:07:30 +00001817 case IMPORTS: {
1818 // Load each of the imported PCH files.
1819 unsigned Idx = 0, N = Record.size();
1820 while (Idx < N) {
1821 // Read information about the AST file.
1822 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1823 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001824 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001825 Record.begin() + Idx + Length);
1826 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001827
Douglas Gregore95b9192011-08-17 21:07:30 +00001828 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001829 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001830 case Failure: return Failure;
1831 // If we have to ignore the dependency, we'll have to ignore this too.
1832 case IgnorePCH: return IgnorePCH;
1833 case Success: break;
1834 }
1835 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001836 break;
1837 }
1838
Douglas Gregora119da02011-08-02 16:26:37 +00001839 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001840 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001841 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001842 return Failure;
1843 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001844 F.TypeOffsets = (const uint32_t *)BlobStart;
1845 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001846 unsigned LocalBaseTypeIndex = Record[1];
1847 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001848
Douglas Gregora119da02011-08-02 16:26:37 +00001849 if (F.LocalNumTypes > 0) {
1850 // Introduce the global -> local mapping for types within this module.
1851 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1852
1853 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001854 F.TypeRemap.insertOrReplace(
1855 std::make_pair(LocalBaseTypeIndex,
1856 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001857
1858 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1859 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001860 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001861 }
1862
Douglas Gregor496c7092011-08-03 15:48:04 +00001863 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001864 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001865 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001866 return Failure;
1867 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001868 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001869 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001870 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001871 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001872
Douglas Gregor496c7092011-08-03 15:48:04 +00001873 if (F.LocalNumDecls > 0) {
1874 // Introduce the global -> local mapping for declarations within this
1875 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001876 GlobalDeclMap.insert(
1877 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001878
1879 // Introduce the local -> global mapping for declarations within this
1880 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001881 F.DeclRemap.insertOrReplace(
1882 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001883
Douglas Gregora1be2782011-12-17 23:38:30 +00001884 // Introduce the global -> local mapping for declarations within this
1885 // module.
1886 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1887
Douglas Gregor496c7092011-08-03 15:48:04 +00001888 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1889 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001890 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001891 }
1892
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001893 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001894 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001895 DeclContextInfo &Info = F.DeclContextInfos[TU];
1896 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1897 Info.NumLexicalDecls
1898 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001899 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001900 break;
1901 }
1902
Sebastian Redle1dde812010-08-24 00:50:04 +00001903 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001904 unsigned Idx = 0;
1905 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001906 ASTDeclContextNameLookupTable *Table =
1907 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001908 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001909 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001910 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001911 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1912 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001913 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001914 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001915 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001916 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001917 break;
1918 }
1919
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001920 case LANGUAGE_OPTIONS:
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001921 if (ParseLanguageOptions(F, Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001922 return IgnorePCH;
1923 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001924
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001926 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001927 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001928 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001929 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001930 (const unsigned char *)F.IdentifierTableData + Record[0],
1931 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001932 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001933
1934 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001935 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001936 break;
1937
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001938 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001939 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001940 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001941 return Failure;
1942 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001943 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1944 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001945 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001946 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001947
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001948 if (F.LocalNumIdentifiers > 0) {
1949 // Introduce the global -> local mapping for identifiers within this
1950 // module.
1951 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1952 &F));
1953
1954 // Introduce the local -> global mapping for identifiers within this
1955 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001956 F.IdentifierRemap.insertOrReplace(
1957 std::make_pair(LocalBaseIdentifierID,
1958 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001959
1960 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1961 + F.LocalNumIdentifiers);
1962 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001963 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001964 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001965
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001966 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001967 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1968 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001969 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001970
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001971 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001972 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1973 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001974 break;
1975
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001977 TotalNumStatements += Record[0];
1978 TotalNumMacros += Record[1];
1979 TotalLexicalDeclContexts += Record[2];
1980 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001981 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001982
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001983 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001984 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1985 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001986 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001987
Sean Huntebcbe1d2011-05-04 23:29:54 +00001988 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001989 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1990 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001991 break;
1992
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001993 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001994 if (Record.size() % 4 != 0) {
1995 Error("invalid weak identifiers record");
1996 return Failure;
1997 }
1998
1999 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2000 // files. This isn't the way to do it :)
2001 WeakUndeclaredIdentifiers.clear();
2002
2003 // Translate the weak, undeclared identifiers into global IDs.
2004 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2005 WeakUndeclaredIdentifiers.push_back(
2006 getGlobalIdentifierID(F, Record[I++]));
2007 WeakUndeclaredIdentifiers.push_back(
2008 getGlobalIdentifierID(F, Record[I++]));
2009 WeakUndeclaredIdentifiers.push_back(
2010 ReadSourceLocation(F, Record, I).getRawEncoding());
2011 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2012 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002013 break;
2014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002015 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002016 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2017 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002018 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002019
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002020 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002021 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002022 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002023 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002024 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002025
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002026 if (F.LocalNumSelectors > 0) {
2027 // Introduce the global -> local mapping for selectors within this
2028 // module.
2029 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2030
2031 // Introduce the local -> global mapping for selectors within this
2032 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002033 F.SelectorRemap.insertOrReplace(
2034 std::make_pair(LocalBaseSelectorID,
2035 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002036
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002037 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2038 }
2039 break;
2040 }
2041
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002042 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002043 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002044 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002045 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002046 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002047 F.SelectorLookupTableData + Record[0],
2048 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002049 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002050 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002051 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002052
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002053 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002054 if (!Record.empty()) {
2055 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2056 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2057 Record[Idx++]));
2058 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2059 getRawEncoding());
2060 }
2061 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002062 break;
2063
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002064 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002065 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002066 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002067 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002068
2069 case FILE_SORTED_DECLS:
2070 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002071 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002072 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002073
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002074 case SOURCE_LOCATION_OFFSETS: {
2075 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002076 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002077 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002078 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002079 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2080 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002081 // Make our entry in the range map. BaseID is negative and growing, so
2082 // we invert it. Because we invert it, though, we need the other end of
2083 // the range.
2084 unsigned RangeStart =
2085 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2086 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2087 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2088
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002089 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2090 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2091 GlobalSLocOffsetMap.insert(
2092 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2093 - SLocSpaceSize,&F));
2094
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002095 // Initialize the remapping table.
2096 // Invalid stays invalid.
2097 F.SLocRemap.insert(std::make_pair(0U, 0));
2098 // This module. Base was 2 when being compiled.
2099 F.SLocRemap.insert(std::make_pair(2U,
2100 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002101
2102 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002103 break;
2104 }
2105
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002106 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002107 // Additional remapping information.
2108 const unsigned char *Data = (const unsigned char*)BlobStart;
2109 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002110
2111 // Continuous range maps we may be updating in our module.
2112 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002113 ContinuousRangeMap<uint32_t, int, 2>::Builder
2114 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002115 ContinuousRangeMap<uint32_t, int, 2>::Builder
2116 MacroRemap(F.MacroRemap);
2117 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002118 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2119 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002120 SubmoduleRemap(F.SubmoduleRemap);
2121 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002122 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002123 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002124 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2125
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002126 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002127 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002128 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002129 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002130 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002131 if (!OM) {
2132 Error("SourceLocation remap refers to unknown module");
2133 return Failure;
2134 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002135
2136 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2137 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002138 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002139 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002140 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002141 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2142 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002143 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002144
2145 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2146 SLocRemap.insert(std::make_pair(SLocOffset,
2147 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002148 IdentifierRemap.insert(
2149 std::make_pair(IdentifierIDOffset,
2150 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002151 MacroRemap.insert(std::make_pair(MacroIDOffset,
2152 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002153 PreprocessedEntityRemap.insert(
2154 std::make_pair(PreprocessedEntityIDOffset,
2155 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002156 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2157 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002158 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2159 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002160 DeclRemap.insert(std::make_pair(DeclIDOffset,
2161 OM->BaseDeclID - DeclIDOffset));
2162
Douglas Gregora119da02011-08-02 16:26:37 +00002163 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002164 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002165
2166 // Global -> local mappings.
2167 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002168 }
2169 break;
2170 }
2171
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002172 case SOURCE_MANAGER_LINE_TABLE:
2173 if (ParseLineTable(F, Record))
2174 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002175 break;
2176
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002177 case FILE_SOURCE_LOCATION_OFFSETS:
2178 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2179 F.LocalNumSLocFileEntries = Record[0];
2180 break;
2181
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002182 case SOURCE_LOCATION_PRELOADS: {
2183 // Need to transform from the local view (1-based IDs) to the global view,
2184 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002185 if (!F.PreloadSLocEntries.empty()) {
2186 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2187 return Failure;
2188 }
2189
2190 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002191 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002192 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002193
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002194 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002195 if (!DisableStatCache) {
2196 ASTStatCache *MyStatCache =
2197 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2198 (const unsigned char *)BlobStart,
2199 NumStatHits, NumStatMisses);
2200 FileMgr.addStatCache(MyStatCache);
2201 F.StatCache = MyStatCache;
2202 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002203 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002204 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002205
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002206 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002207 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2208 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002209 break;
2210
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002211 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002212 if (Record.size() % 3 != 0) {
2213 Error("Invalid VTABLE_USES record");
2214 return Failure;
2215 }
2216
Sebastian Redl40566802010-08-05 18:21:25 +00002217 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002218 // FIXME: Modules will have some trouble with this. This is clearly not
2219 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002220 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002221
2222 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2223 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2224 VTableUses.push_back(
2225 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2226 VTableUses.push_back(Record[Idx++]);
2227 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002228 break;
2229
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002230 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002231 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2232 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002233 break;
2234
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002235 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002236 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002237 Error("Invalid existing PendingInstantiations");
2238 return Failure;
2239 }
2240
2241 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002242 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2243 return Failure;
2244 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002245
Douglas Gregorf2abb522011-07-28 19:26:52 +00002246 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2247 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2248 PendingInstantiations.push_back(
2249 ReadSourceLocation(F, Record, I).getRawEncoding());
2250 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002251 break;
2252
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002253 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002254 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002255 // FIXME: Modules will have some trouble with this.
2256 SemaDeclRefs.clear();
2257 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2258 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002259 break;
2260
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002261 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002262 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002263 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002264 ActualOriginalFileName.assign(BlobStart, BlobLen);
2265 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002266 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002267 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Douglas Gregor31d375f2011-05-06 21:43:30 +00002269 case ORIGINAL_FILE_ID:
2270 OriginalFileID = FileID::get(Record[0]);
2271 break;
2272
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002273 case ORIGINAL_PCH_DIR:
2274 // The primary AST will be the last to get here, so it will be the one
2275 // that's used.
2276 OriginalDir.assign(BlobStart, BlobLen);
2277 break;
2278
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002279 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002280 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002281 StringRef ASTBranch(BlobStart, BlobLen);
2282 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002283 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002284 return IgnorePCH;
2285 }
2286 break;
2287 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002288
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002289 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002290 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2291 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2292 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002293
2294 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002295
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002296 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002297 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002298 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002299 if (!PP.getPreprocessingRecord()->getExternalSource())
2300 PP.getPreprocessingRecord()->SetExternalSource(*this);
2301 StartingID
2302 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002303 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002304 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002305
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002306 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002307 // Introduce the global -> local mapping for preprocessed entities in
2308 // this module.
2309 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2310
2311 // Introduce the local -> global mapping for preprocessed entities in
2312 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002313 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002314 std::make_pair(LocalBasePreprocessedEntityID,
2315 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2316 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002317
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002318 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002319 }
2320
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002321 case DECL_UPDATE_OFFSETS: {
2322 if (Record.size() % 2 != 0) {
2323 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2324 return Failure;
2325 }
2326 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002327 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2328 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002329 break;
2330 }
2331
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002332 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002333 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002334 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002335 return Failure;
2336 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002337 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002338 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002339 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002340 break;
2341 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002342
Douglas Gregorcff9f262012-01-27 01:47:08 +00002343 case OBJC_CATEGORIES_MAP: {
2344 if (F.LocalNumObjCCategoriesInMap != 0) {
2345 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002346 return Failure;
2347 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002348
2349 F.LocalNumObjCCategoriesInMap = Record[0];
2350 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002351 break;
2352 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002353
Douglas Gregorcff9f262012-01-27 01:47:08 +00002354 case OBJC_CATEGORIES:
2355 F.ObjCCategories.swap(Record);
2356 break;
2357
Douglas Gregor7c789c12010-10-29 22:39:52 +00002358 case CXX_BASE_SPECIFIER_OFFSETS: {
2359 if (F.LocalNumCXXBaseSpecifiers != 0) {
2360 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2361 return Failure;
2362 }
2363
2364 F.LocalNumCXXBaseSpecifiers = Record[0];
2365 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002366 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002367 break;
2368 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002369
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002370 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002371 if (Record.size() % 2 != 0) {
2372 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2373 return Failure;
2374 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002375
2376 if (F.PragmaDiagMappings.empty())
2377 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002378 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002379 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2380 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002381 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002382
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002383 case CUDA_SPECIAL_DECL_REFS:
2384 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002385 // FIXME: Modules will have trouble with this.
2386 CUDASpecialDeclRefs.clear();
2387 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2388 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002389 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002390
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002391 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002392 F.HeaderFileInfoTableData = BlobStart;
2393 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002394 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002395 if (Record[0]) {
2396 F.HeaderFileInfoTable
2397 = HeaderFileInfoLookupTable::Create(
2398 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002399 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002400 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002401 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002402 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002403
2404 PP.getHeaderSearchInfo().SetExternalSource(this);
2405 if (!PP.getHeaderSearchInfo().getExternalLookup())
2406 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002407 }
2408 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002409 }
2410
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002411 case FP_PRAGMA_OPTIONS:
2412 // Later tables overwrite earlier ones.
2413 FPPragmaOptions.swap(Record);
2414 break;
2415
2416 case OPENCL_EXTENSIONS:
2417 // Later tables overwrite earlier ones.
2418 OpenCLExtensions.swap(Record);
2419 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002420
2421 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002422 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2423 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002424 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002425
2426 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002427 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2428 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002429 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002430
2431 case IMPORTED_MODULES: {
2432 if (F.Kind != MK_Module) {
2433 // If we aren't loading a module (which has its own exports), make
2434 // all of the imported modules visible.
2435 // FIXME: Deal with macros-only imports.
2436 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2437 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2438 ImportedModules.push_back(GlobalID);
2439 }
2440 }
2441 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002442 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002443
Douglas Gregora1be2782011-12-17 23:38:30 +00002444 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002445 F.RedeclarationChains.swap(Record);
2446 break;
2447 }
2448
2449 case LOCAL_REDECLARATIONS_MAP: {
2450 if (F.LocalNumRedeclarationsInMap != 0) {
2451 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002452 return Failure;
2453 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002454
Douglas Gregor2171bf12012-01-15 16:58:34 +00002455 F.LocalNumRedeclarationsInMap = Record[0];
2456 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002457 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002458 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002459
2460 case MERGED_DECLARATIONS: {
2461 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2462 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2463 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2464 for (unsigned N = Record[Idx++]; N > 0; --N)
2465 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2466 }
2467 break;
2468 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002469
2470 case MACRO_OFFSET: {
2471 if (F.LocalNumMacros != 0) {
2472 Error("duplicate MACRO_OFFSET record in AST file");
2473 return Failure;
2474 }
2475 F.MacroOffsets = (const uint32_t *)BlobStart;
2476 F.LocalNumMacros = Record[0];
2477 unsigned LocalBaseMacroID = Record[1];
2478 F.BaseMacroID = getTotalNumMacros();
2479
2480 if (F.LocalNumMacros > 0) {
2481 // Introduce the global -> local mapping for macros within this module.
2482 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2483
2484 // Introduce the local -> global mapping for macros within this module.
2485 F.MacroRemap.insertOrReplace(
2486 std::make_pair(LocalBaseMacroID,
2487 F.BaseMacroID - LocalBaseMacroID));
2488
2489 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2490 }
2491 break;
2492 }
2493
2494 case MACRO_UPDATES: {
2495 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2496 MacroID ID = getGlobalMacroID(F, Record[I++]);
2497 if (I == N)
2498 break;
2499
2500 MacroUpdates[ID].UndefLoc = ReadSourceLocation(F, Record, I);
2501 }
2502 break;
2503 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002504 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002505 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002506 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002507 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002508}
2509
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002510ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002511 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002512
Douglas Gregorc69a2922011-08-25 20:58:51 +00002513 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2514 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2515 unsigned Code = SLocEntryCursor.ReadCode();
2516 if (Code == llvm::bitc::END_BLOCK ||
2517 Code == llvm::bitc::ENTER_SUBBLOCK ||
2518 Code == llvm::bitc::DEFINE_ABBREV) {
2519 Error("incorrectly-formatted source location entry in AST file");
2520 return Failure;
2521 }
2522
2523 RecordData Record;
2524 const char *BlobStart;
2525 unsigned BlobLen;
2526 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2527 default:
2528 Error("incorrectly-formatted source location entry in AST file");
2529 return Failure;
2530
2531 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002532 // If the buffer was overridden, the file need not exist.
2533 if (Record[6])
2534 break;
2535
Douglas Gregorc69a2922011-08-25 20:58:51 +00002536 StringRef Filename(BlobStart, BlobLen);
2537 const FileEntry *File = getFileEntry(Filename);
2538
2539 if (File == 0) {
2540 std::string ErrorStr = "could not find file '";
2541 ErrorStr += Filename;
2542 ErrorStr += "' referenced by AST file";
2543 Error(ErrorStr.c_str());
2544 return IgnorePCH;
2545 }
2546
Douglas Gregora081da52011-11-16 20:05:18 +00002547 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002548 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002549 return Failure;
2550 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002551
2552 off_t StoredSize = (off_t)Record[4];
2553 time_t StoredTime = (time_t)Record[5];
2554
2555 // Check if there was a request to override the contents of the file
2556 // that was part of the precompiled header. Overridding such a file
2557 // can lead to problems when lexing using the source locations from the
2558 // PCH.
2559 SourceManager &SM = getSourceManager();
2560 if (SM.isFileOverridden(File)) {
2561 Error(diag::err_fe_pch_file_overridden, Filename);
2562 // After emitting the diagnostic, recover by disabling the override so
2563 // that the original file will be used.
2564 SM.disableFileContentsOverride(File);
2565 // The FileEntry is a virtual file entry with the size of the contents
2566 // that would override the original contents. Set it to the original's
2567 // size/time.
2568 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2569 StoredSize, StoredTime);
2570 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002571
Douglas Gregorc69a2922011-08-25 20:58:51 +00002572 // The stat info from the FileEntry came from the cached stat
2573 // info of the PCH, so we cannot trust it.
2574 struct stat StatBuf;
2575 if (::stat(File->getName(), &StatBuf) != 0) {
2576 StatBuf.st_size = File->getSize();
2577 StatBuf.st_mtime = File->getModificationTime();
2578 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002579
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002580 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002581#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002582 // In our regression testing, the Windows file system seems to
2583 // have inconsistent modification times that sometimes
2584 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002585 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002586#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002587 )) {
2588 Error(diag::err_fe_pch_file_modified, Filename);
2589 return IgnorePCH;
2590 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002591
Douglas Gregorc69a2922011-08-25 20:58:51 +00002592 break;
2593 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002594 }
2595 }
2596
2597 return Success;
2598}
2599
Douglas Gregorecc2c092011-12-01 22:20:10 +00002600void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002601 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002602 if (Names[I].isDecl()) {
2603 Names[I].getDecl()->Hidden = false;
2604 continue;
2605 }
2606
2607 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2608 Macro.second->setHidden(!Macro.second->isPublic());
2609 if (Macro.second->isDefined()) {
2610 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002611 }
Douglas Gregor13292642011-12-02 15:45:10 +00002612 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002613}
2614
Douglas Gregor5e356932011-12-01 17:11:21 +00002615void ASTReader::makeModuleVisible(Module *Mod,
2616 Module::NameVisibilityKind NameVisibility) {
2617 llvm::SmallPtrSet<Module *, 4> Visited;
2618 llvm::SmallVector<Module *, 4> Stack;
2619 Stack.push_back(Mod);
2620 while (!Stack.empty()) {
2621 Mod = Stack.back();
2622 Stack.pop_back();
2623
2624 if (NameVisibility <= Mod->NameVisibility) {
2625 // This module already has this level of visibility (or greater), so
2626 // there is nothing more to do.
2627 continue;
2628 }
2629
Douglas Gregor51f564f2011-12-31 04:05:44 +00002630 if (!Mod->isAvailable()) {
2631 // Modules that aren't available cannot be made visible.
2632 continue;
2633 }
2634
Douglas Gregor5e356932011-12-01 17:11:21 +00002635 // Update the module's name visibility.
2636 Mod->NameVisibility = NameVisibility;
2637
Douglas Gregorecc2c092011-12-01 22:20:10 +00002638 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002639 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002640 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2641 if (Hidden != HiddenNamesMap.end()) {
2642 makeNamesVisible(Hidden->second);
2643 HiddenNamesMap.erase(Hidden);
2644 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002645
2646 // Push any non-explicit submodules onto the stack to be marked as
2647 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002648 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2649 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002650 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002651 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2652 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002653 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002654
2655 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002656 bool AnyWildcard = false;
2657 bool UnrestrictedWildcard = false;
2658 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002659 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2660 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002661 if (!Mod->Exports[I].getInt()) {
2662 // Export a named module directly; no wildcards involved.
2663 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002664 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002665
2666 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002667 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002668
2669 // Wildcard export: export all of the imported modules that match
2670 // the given pattern.
2671 AnyWildcard = true;
2672 if (UnrestrictedWildcard)
2673 continue;
2674
2675 if (Module *Restriction = Mod->Exports[I].getPointer())
2676 WildcardRestrictions.push_back(Restriction);
2677 else {
2678 WildcardRestrictions.clear();
2679 UnrestrictedWildcard = true;
2680 }
2681 }
2682
2683 // If there were any wildcards, push any imported modules that were
2684 // re-exported by the wildcard restriction.
2685 if (!AnyWildcard)
2686 continue;
2687
2688 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2689 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002690 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002691 continue;
2692
2693 bool Acceptable = UnrestrictedWildcard;
2694 if (!Acceptable) {
2695 // Check whether this module meets one of the restrictions.
2696 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2697 Module *Restriction = WildcardRestrictions[R];
2698 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2699 Acceptable = true;
2700 break;
2701 }
2702 }
2703 }
2704
2705 if (!Acceptable)
2706 continue;
2707
Douglas Gregor0adaa882011-12-05 17:28:06 +00002708 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002709 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002710 }
2711}
2712
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002713ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002714 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002715 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002716 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002717
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002718 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002719 case Failure: return Failure;
2720 case IgnorePCH: return IgnorePCH;
2721 case Success: break;
2722 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002723
2724 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002725
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002726 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002727 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002728 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2729 // if DisableValidation is true, defines that were set on command-line
2730 // but not in the PCH file will not be added to SuggestedPredefines.
2731 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002732 return IgnorePCH;
2733
Douglas Gregoreee242f2011-10-27 09:33:13 +00002734 // Mark all of the identifiers in the identifier table as being out of date,
2735 // so that various accessors know to check the loaded modules when the
2736 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002737 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2738 IdEnd = PP.getIdentifierTable().end();
2739 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002740 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002741
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002742 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002743 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2744 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2745 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002746 Module *ResolvedMod = getSubmodule(GlobalID);
2747
2748 if (Unresolved.IsImport) {
2749 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002750 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002751 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002752 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002753
2754 if (ResolvedMod || Unresolved.IsWildcard)
2755 Unresolved.Mod->Exports.push_back(
2756 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002757 }
Douglas Gregor55988682011-12-05 16:33:54 +00002758 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002759
Douglas Gregor35942772011-09-09 21:34:22 +00002760 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002761
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002762 if (DeserializationListener)
2763 DeserializationListener->ReaderInitialized(this);
2764
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002765 if (!OriginalFileID.isInvalid()) {
2766 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2767 + OriginalFileID.getOpaqueValue() - 1);
2768
2769 // If this AST file is a precompiled preamble, then set the preamble file ID
2770 // of the source manager to the file source file from which the preamble was
2771 // built.
2772 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002773 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002774 } else if (Type == MK_MainFile) {
2775 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002776 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002777 }
2778
Douglas Gregorcff9f262012-01-27 01:47:08 +00002779 // For any Objective-C class definitions we have already loaded, make sure
2780 // that we load any additional categories.
2781 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2782 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2783 ObjCClassesLoaded[I],
2784 PreviousGeneration);
2785 }
2786
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002787 return Success;
2788}
2789
Chris Lattner5f9e2722011-07-23 10:55:15 +00002790ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002791 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002792 ModuleFile *ImportedBy) {
2793 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002794 bool NewModule;
2795 std::string ErrorStr;
2796 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002797 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002798
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002799 if (!M) {
2800 // We couldn't load the module.
2801 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2802 + ErrorStr;
2803 Error(Msg);
2804 return Failure;
2805 }
2806
2807 if (!NewModule) {
2808 // We've already loaded this module.
2809 return Success;
2810 }
2811
2812 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2813 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002814 if (FileName != "-") {
2815 CurrentDir = llvm::sys::path::parent_path(FileName);
2816 if (CurrentDir.empty()) CurrentDir = ".";
2817 }
2818
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002819 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002820 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002821 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002822 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002823
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002824 // Sniff for the signature.
2825 if (Stream.Read(8) != 'C' ||
2826 Stream.Read(8) != 'P' ||
2827 Stream.Read(8) != 'C' ||
2828 Stream.Read(8) != 'H') {
2829 Diag(diag::err_not_a_pch_file) << FileName;
2830 return Failure;
2831 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002832
Douglas Gregor2cf26342009-04-09 22:27:44 +00002833 while (!Stream.AtEndOfStream()) {
2834 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002835
Douglas Gregore1d918e2009-04-10 23:10:45 +00002836 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002837 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002838 return Failure;
2839 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002840
2841 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002842
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002843 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002844 switch (BlockID) {
2845 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002846 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002847 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002848 return Failure;
2849 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002850 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002851 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002852 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002853 case Success:
2854 break;
2855
2856 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002857 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002858
2859 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002860 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002861 // AST block, skipping subblocks, to see if there are other
2862 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002863
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002864 // FIXME: We can't clear loaded slocentries anymore.
2865 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002866
2867 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002868 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002869 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002870
Douglas Gregore1d918e2009-04-10 23:10:45 +00002871 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002872 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002873 break;
2874 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002875 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002876 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002877 return Failure;
2878 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002879 break;
2880 }
Mike Stump1eb44332009-09-09 15:08:12 +00002881 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002882
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002883 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002884 // bits of all files we've seen.
2885 F.GlobalBitOffset = TotalModulesSizeInBits;
2886 TotalModulesSizeInBits += F.SizeInBits;
2887 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002888
2889 // Make sure that the files this module was built against are still available.
2890 if (!DisableValidation) {
2891 switch(validateFileEntries(*M)) {
2892 case Failure: return Failure;
2893 case IgnorePCH: return IgnorePCH;
2894 case Success: break;
2895 }
2896 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002897
2898 // Preload SLocEntries.
2899 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2900 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002901 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2902 // directly because the entry may have already been loaded in which case
2903 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2904 // SourceManager.
2905 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002906 }
2907
Douglas Gregorc69a2922011-08-25 20:58:51 +00002908
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002909 return Success;
2910}
2911
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002912void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002913 // If there's a listener, notify them that we "read" the translation unit.
2914 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002915 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2916 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002917
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002918 // Make sure we load the declaration update records for the translation unit,
2919 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002920 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2921 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002922
Douglas Gregor5f957282011-08-11 22:18:49 +00002923 // FIXME: Find a better way to deal with collisions between these
2924 // built-in types. Right now, we just ignore the problem.
2925
2926 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002927 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002928 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2929 if (!Context.CFConstantStringTypeDecl)
2930 Context.setCFConstantStringType(GetType(String));
2931 }
2932
2933 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2934 QualType FileType = GetType(File);
2935 if (FileType.isNull()) {
2936 Error("FILE type is NULL");
2937 return;
2938 }
2939
2940 if (!Context.FILEDecl) {
2941 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2942 Context.setFILEDecl(Typedef->getDecl());
2943 else {
2944 const TagType *Tag = FileType->getAs<TagType>();
2945 if (!Tag) {
2946 Error("Invalid FILE type in AST file");
2947 return;
2948 }
2949 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002950 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002951 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002952 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002953
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002954 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002955 QualType Jmp_bufType = GetType(Jmp_buf);
2956 if (Jmp_bufType.isNull()) {
2957 Error("jmp_buf type is NULL");
2958 return;
2959 }
2960
2961 if (!Context.jmp_bufDecl) {
2962 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2963 Context.setjmp_bufDecl(Typedef->getDecl());
2964 else {
2965 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2966 if (!Tag) {
2967 Error("Invalid jmp_buf type in AST file");
2968 return;
2969 }
2970 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002971 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002972 }
Mike Stump782fa302009-07-28 02:25:19 +00002973 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002974
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002975 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002976 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2977 if (Sigjmp_bufType.isNull()) {
2978 Error("sigjmp_buf type is NULL");
2979 return;
2980 }
2981
2982 if (!Context.sigjmp_bufDecl) {
2983 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2984 Context.setsigjmp_bufDecl(Typedef->getDecl());
2985 else {
2986 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2987 assert(Tag && "Invalid sigjmp_buf type in AST file");
2988 Context.setsigjmp_bufDecl(Tag->getDecl());
2989 }
2990 }
2991 }
2992
2993 if (unsigned ObjCIdRedef
2994 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2995 if (Context.ObjCIdRedefinitionType.isNull())
2996 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2997 }
2998
2999 if (unsigned ObjCClassRedef
3000 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3001 if (Context.ObjCClassRedefinitionType.isNull())
3002 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3003 }
3004
3005 if (unsigned ObjCSelRedef
3006 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3007 if (Context.ObjCSelRedefinitionType.isNull())
3008 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3009 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003010
3011 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3012 QualType Ucontext_tType = GetType(Ucontext_t);
3013 if (Ucontext_tType.isNull()) {
3014 Error("ucontext_t type is NULL");
3015 return;
3016 }
3017
3018 if (!Context.ucontext_tDecl) {
3019 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3020 Context.setucontext_tDecl(Typedef->getDecl());
3021 else {
3022 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3023 assert(Tag && "Invalid ucontext_t type in AST file");
3024 Context.setucontext_tDecl(Tag->getDecl());
3025 }
3026 }
3027 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003028 }
3029
Douglas Gregor35942772011-09-09 21:34:22 +00003030 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003031
3032 // If there were any CUDA special declarations, deserialize them.
3033 if (!CUDASpecialDeclRefs.empty()) {
3034 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003035 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003036 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3037 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003038
3039 // Re-export any modules that were imported by a non-module AST file.
3040 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3041 if (Module *Imported = getSubmodule(ImportedModules[I]))
3042 makeModuleVisible(Imported, Module::AllVisible);
3043 }
3044 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003045}
3046
Douglas Gregorecc2c092011-12-01 22:20:10 +00003047void ASTReader::finalizeForWriting() {
3048 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3049 HiddenEnd = HiddenNamesMap.end();
3050 Hidden != HiddenEnd; ++Hidden) {
3051 makeNamesVisible(Hidden->second);
3052 }
3053 HiddenNamesMap.clear();
3054}
3055
Douglas Gregorb64c1932009-05-12 01:31:05 +00003056/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003057/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003058/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003059std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003060 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003061 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003062 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003063 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003064 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003065 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003066 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003067 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003068 return std::string();
3069 }
3070
3071 // Initialize the stream
3072 llvm::BitstreamReader StreamFile;
3073 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003074 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003075 (const unsigned char *)Buffer->getBufferEnd());
3076 Stream.init(StreamFile);
3077
3078 // Sniff for the signature.
3079 if (Stream.Read(8) != 'C' ||
3080 Stream.Read(8) != 'P' ||
3081 Stream.Read(8) != 'C' ||
3082 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003083 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003084 return std::string();
3085 }
3086
3087 RecordData Record;
3088 while (!Stream.AtEndOfStream()) {
3089 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003090
Douglas Gregorb64c1932009-05-12 01:31:05 +00003091 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3092 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003093
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003094 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003095 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003096 case AST_BLOCK_ID:
3097 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003098 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003099 return std::string();
3100 }
3101 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Douglas Gregorb64c1932009-05-12 01:31:05 +00003103 default:
3104 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003105 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003106 return std::string();
3107 }
3108 break;
3109 }
3110 continue;
3111 }
3112
3113 if (Code == llvm::bitc::END_BLOCK) {
3114 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003115 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003116 return std::string();
3117 }
3118 continue;
3119 }
3120
3121 if (Code == llvm::bitc::DEFINE_ABBREV) {
3122 Stream.ReadAbbrevRecord();
3123 continue;
3124 }
3125
3126 Record.clear();
3127 const char *BlobStart = 0;
3128 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003129 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003130 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003131 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003132 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003133
3134 return std::string();
3135}
3136
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003137ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003138 // Enter the submodule block.
3139 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3140 Error("malformed submodule block record in AST file");
3141 return Failure;
3142 }
3143
3144 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003145 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003146 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003147 RecordData Record;
3148 while (true) {
3149 unsigned Code = F.Stream.ReadCode();
3150 if (Code == llvm::bitc::END_BLOCK) {
3151 if (F.Stream.ReadBlockEnd()) {
3152 Error("error at end of submodule block in AST file");
3153 return Failure;
3154 }
3155 return Success;
3156 }
3157
3158 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3159 // No known subblocks, always skip them.
3160 F.Stream.ReadSubBlockID();
3161 if (F.Stream.SkipBlock()) {
3162 Error("malformed block record in AST file");
3163 return Failure;
3164 }
3165 continue;
3166 }
3167
3168 if (Code == llvm::bitc::DEFINE_ABBREV) {
3169 F.Stream.ReadAbbrevRecord();
3170 continue;
3171 }
3172
3173 // Read a record.
3174 const char *BlobStart;
3175 unsigned BlobLen;
3176 Record.clear();
3177 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3178 default: // Default behavior: ignore.
3179 break;
3180
3181 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003182 if (First) {
3183 Error("missing submodule metadata record at beginning of block");
3184 return Failure;
3185 }
3186
Douglas Gregore209e502011-12-06 01:10:29 +00003187 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003188 Error("malformed module definition");
3189 return Failure;
3190 }
3191
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003192 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003193 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3194 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3195 bool IsFramework = Record[2];
3196 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003197 bool IsSystem = Record[4];
3198 bool InferSubmodules = Record[5];
3199 bool InferExplicitSubmodules = Record[6];
3200 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003201
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003202 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003203 if (Parent)
3204 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003205
3206 // Retrieve this (sub)module from the module map, creating it if
3207 // necessary.
3208 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3209 IsFramework,
3210 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003211 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3212 if (GlobalIndex >= SubmodulesLoaded.size() ||
3213 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003214 Error("too many submodules");
3215 return Failure;
3216 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003217
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003218 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003219 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003220 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003221 CurrentModule->InferSubmodules = InferSubmodules;
3222 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3223 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003224 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003225 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003226
Douglas Gregore209e502011-12-06 01:10:29 +00003227 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003228 break;
3229 }
3230
Douglas Gregor77d029f2011-12-08 19:11:24 +00003231 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003232 if (First) {
3233 Error("missing submodule metadata record at beginning of block");
3234 return Failure;
3235 }
3236
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003237 if (!CurrentModule)
3238 break;
3239
3240 StringRef FileName(BlobStart, BlobLen);
3241 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003242 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003243 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003244 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003245 Error("mismatched umbrella headers in submodule");
3246 return Failure;
3247 }
3248 }
3249 break;
3250 }
3251
3252 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003253 if (First) {
3254 Error("missing submodule metadata record at beginning of block");
3255 return Failure;
3256 }
3257
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003258 if (!CurrentModule)
3259 break;
3260
3261 // FIXME: Be more lazy about this!
3262 StringRef FileName(BlobStart, BlobLen);
3263 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3264 if (std::find(CurrentModule->Headers.begin(),
3265 CurrentModule->Headers.end(),
3266 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003267 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003268 }
3269 break;
3270 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003271
3272 case SUBMODULE_TOPHEADER: {
3273 if (First) {
3274 Error("missing submodule metadata record at beginning of block");
3275 return Failure;
3276 }
3277
3278 if (!CurrentModule)
3279 break;
3280
3281 // FIXME: Be more lazy about this!
3282 StringRef FileName(BlobStart, BlobLen);
3283 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3284 CurrentModule->TopHeaders.insert(File);
3285 break;
3286 }
3287
Douglas Gregor77d029f2011-12-08 19:11:24 +00003288 case SUBMODULE_UMBRELLA_DIR: {
3289 if (First) {
3290 Error("missing submodule metadata record at beginning of block");
3291 return Failure;
3292 }
3293
3294 if (!CurrentModule)
3295 break;
3296
3297 StringRef DirName(BlobStart, BlobLen);
3298 if (const DirectoryEntry *Umbrella
3299 = PP.getFileManager().getDirectory(DirName)) {
3300 if (!CurrentModule->getUmbrellaDir())
3301 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3302 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3303 Error("mismatched umbrella directories in submodule");
3304 return Failure;
3305 }
3306 }
3307 break;
3308 }
3309
Douglas Gregor26ced122011-12-01 00:59:36 +00003310 case SUBMODULE_METADATA: {
3311 if (!First) {
3312 Error("submodule metadata record not at beginning of block");
3313 return Failure;
3314 }
3315 First = false;
3316
3317 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003318 F.LocalNumSubmodules = Record[0];
3319 unsigned LocalBaseSubmoduleID = Record[1];
3320 if (F.LocalNumSubmodules > 0) {
3321 // Introduce the global -> local mapping for submodules within this
3322 // module.
3323 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3324
3325 // Introduce the local -> global mapping for submodules within this
3326 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003327 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003328 std::make_pair(LocalBaseSubmoduleID,
3329 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3330
3331 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3332 }
3333 break;
3334 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003335
Douglas Gregor55988682011-12-05 16:33:54 +00003336 case SUBMODULE_IMPORTS: {
3337 if (First) {
3338 Error("missing submodule metadata record at beginning of block");
3339 return Failure;
3340 }
3341
3342 if (!CurrentModule)
3343 break;
3344
3345 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3346 UnresolvedModuleImportExport Unresolved;
3347 Unresolved.File = &F;
3348 Unresolved.Mod = CurrentModule;
3349 Unresolved.ID = Record[Idx];
3350 Unresolved.IsImport = true;
3351 Unresolved.IsWildcard = false;
3352 UnresolvedModuleImportExports.push_back(Unresolved);
3353 }
3354 break;
3355 }
3356
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003357 case SUBMODULE_EXPORTS: {
3358 if (First) {
3359 Error("missing submodule metadata record at beginning of block");
3360 return Failure;
3361 }
3362
3363 if (!CurrentModule)
3364 break;
3365
3366 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003367 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003368 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003369 Unresolved.Mod = CurrentModule;
3370 Unresolved.ID = Record[Idx];
3371 Unresolved.IsImport = false;
3372 Unresolved.IsWildcard = Record[Idx + 1];
3373 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003374 }
3375
3376 // Once we've loaded the set of exports, there's no reason to keep
3377 // the parsed, unresolved exports around.
3378 CurrentModule->UnresolvedExports.clear();
3379 break;
3380 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003381 case SUBMODULE_REQUIRES: {
3382 if (First) {
3383 Error("missing submodule metadata record at beginning of block");
3384 return Failure;
3385 }
3386
3387 if (!CurrentModule)
3388 break;
3389
3390 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003391 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003392 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003393 break;
3394 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003395 }
3396 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003397}
3398
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003399/// \brief Parse the record that corresponds to a LangOptions data
3400/// structure.
3401///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003402/// This routine parses the language options from the AST file and then gives
3403/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003404///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003405/// \returns true if the listener deems the file unacceptable, false otherwise.
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003406bool ASTReader::ParseLanguageOptions(const ModuleFile &M,
3407 const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003408 if (Listener) {
3409 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003410 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003411#define LANGOPT(Name, Bits, Default, Description) \
3412 LangOpts.Name = Record[Idx++];
3413#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3414 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3415#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003416
3417 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3418 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3419 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003420
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003421 unsigned Length = Record[Idx++];
3422 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3423 Record.begin() + Idx + Length);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003424 return Listener->ReadLanguageOptions(M, LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003425 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003426
3427 return false;
3428}
3429
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003430std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003431ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003432 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003433 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003434 assert(I != GlobalPreprocessedEntityMap.end() &&
3435 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003436 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003437 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3438 return std::make_pair(M, LocalIndex);
3439}
3440
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003441std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3442ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3443 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3444 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3445 Mod.NumPreprocessedEntities);
3446
3447 return std::make_pair(PreprocessingRecord::iterator(),
3448 PreprocessingRecord::iterator());
3449}
3450
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003451std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3452ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3453 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3454 ModuleDeclIterator(this, &Mod,
3455 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3456}
3457
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003458PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3459 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003460 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3461 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003462 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003463 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003464
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003465 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003466 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003467
3468 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3469 switch (Code) {
3470 case llvm::bitc::END_BLOCK:
3471 return 0;
3472
3473 case llvm::bitc::ENTER_SUBBLOCK:
3474 Error("unexpected subblock record in preprocessor detail block");
3475 return 0;
3476
3477 case llvm::bitc::DEFINE_ABBREV:
3478 Error("unexpected abbrevation record in preprocessor detail block");
3479 return 0;
3480
3481 default:
3482 break;
3483 }
3484
3485 if (!PP.getPreprocessingRecord()) {
3486 Error("no preprocessing record");
3487 return 0;
3488 }
3489
3490 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003491 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3492 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003493 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3494 const char *BlobStart = 0;
3495 unsigned BlobLen = 0;
3496 RecordData Record;
3497 PreprocessorDetailRecordTypes RecType =
3498 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3499 Code, Record, BlobStart, BlobLen);
3500 switch (RecType) {
3501 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003502 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003503 IdentifierInfo *Name = 0;
3504 MacroDefinition *Def = 0;
3505 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003506 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003507 else {
3508 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003509 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003510 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3511 }
3512
3513 MacroExpansion *ME;
3514 if (isBuiltin)
3515 ME = new (PPRec) MacroExpansion(Name, Range);
3516 else
3517 ME = new (PPRec) MacroExpansion(Def, Range);
3518
3519 return ME;
3520 }
3521
3522 case PPD_MACRO_DEFINITION: {
3523 // Decode the identifier info and then check again; if the macro is
3524 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003525 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003526 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003527 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003528
3529 if (DeserializationListener)
3530 DeserializationListener->MacroDefinitionRead(PPID, MD);
3531
3532 return MD;
3533 }
3534
3535 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003536 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003537 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3538 const FileEntry *File = 0;
3539 if (!FullFileName.empty())
3540 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003541
3542 // FIXME: Stable encoding
3543 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003544 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003545 InclusionDirective *ID
3546 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003547 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003548 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003549 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003550 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003551 return ID;
3552 }
3553 }
David Blaikie7530c032012-01-17 06:56:22 +00003554
3555 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003556}
3557
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003558/// \brief \arg SLocMapI points at a chunk of a module that contains no
3559/// preprocessed entities or the entities it contains are not the ones we are
3560/// looking for. Find the next module that contains entities and return the ID
3561/// of the first entry.
3562PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3563 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3564 ++SLocMapI;
3565 for (GlobalSLocOffsetMapType::const_iterator
3566 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003567 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003568 if (M.NumPreprocessedEntities)
3569 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3570 }
3571
3572 return getTotalNumPreprocessedEntities();
3573}
3574
3575namespace {
3576
3577template <unsigned PPEntityOffset::*PPLoc>
3578struct PPEntityComp {
3579 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003580 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003581
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003582 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003583
Benjamin Kramer88df1252011-09-21 06:42:26 +00003584 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3585 SourceLocation LHS = getLoc(L);
3586 SourceLocation RHS = getLoc(R);
3587 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3588 }
3589
3590 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003591 SourceLocation LHS = getLoc(L);
3592 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3593 }
3594
Benjamin Kramer88df1252011-09-21 06:42:26 +00003595 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003596 SourceLocation RHS = getLoc(R);
3597 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3598 }
3599
3600 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3601 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3602 }
3603};
3604
3605}
3606
3607/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3608PreprocessedEntityID
3609ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3610 if (SourceMgr.isLocalSourceLocation(BLoc))
3611 return getTotalNumPreprocessedEntities();
3612
3613 GlobalSLocOffsetMapType::const_iterator
3614 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3615 BLoc.getOffset());
3616 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3617 "Corrupted global sloc offset map");
3618
3619 if (SLocMapI->second->NumPreprocessedEntities == 0)
3620 return findNextPreprocessedEntity(SLocMapI);
3621
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003622 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003623 typedef const PPEntityOffset *pp_iterator;
3624 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3625 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003626
3627 size_t Count = M.NumPreprocessedEntities;
3628 size_t Half;
3629 pp_iterator First = pp_begin;
3630 pp_iterator PPI;
3631
3632 // Do a binary search manually instead of using std::lower_bound because
3633 // The end locations of entities may be unordered (when a macro expansion
3634 // is inside another macro argument), but for this case it is not important
3635 // whether we get the first macro expansion or its containing macro.
3636 while (Count > 0) {
3637 Half = Count/2;
3638 PPI = First;
3639 std::advance(PPI, Half);
3640 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3641 BLoc)){
3642 First = PPI;
3643 ++First;
3644 Count = Count - Half - 1;
3645 } else
3646 Count = Half;
3647 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003648
3649 if (PPI == pp_end)
3650 return findNextPreprocessedEntity(SLocMapI);
3651
3652 return getGlobalPreprocessedEntityID(M,
3653 M.BasePreprocessedEntityID + (PPI - pp_begin));
3654}
3655
3656/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3657PreprocessedEntityID
3658ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3659 if (SourceMgr.isLocalSourceLocation(ELoc))
3660 return getTotalNumPreprocessedEntities();
3661
3662 GlobalSLocOffsetMapType::const_iterator
3663 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3664 ELoc.getOffset());
3665 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3666 "Corrupted global sloc offset map");
3667
3668 if (SLocMapI->second->NumPreprocessedEntities == 0)
3669 return findNextPreprocessedEntity(SLocMapI);
3670
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003671 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003672 typedef const PPEntityOffset *pp_iterator;
3673 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3674 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3675 pp_iterator PPI =
3676 std::upper_bound(pp_begin, pp_end, ELoc,
3677 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3678
3679 if (PPI == pp_end)
3680 return findNextPreprocessedEntity(SLocMapI);
3681
3682 return getGlobalPreprocessedEntityID(M,
3683 M.BasePreprocessedEntityID + (PPI - pp_begin));
3684}
3685
3686/// \brief Returns a pair of [Begin, End) indices of preallocated
3687/// preprocessed entities that \arg Range encompasses.
3688std::pair<unsigned, unsigned>
3689 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3690 if (Range.isInvalid())
3691 return std::make_pair(0,0);
3692 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3693
3694 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3695 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3696 return std::make_pair(BeginID, EndID);
3697}
3698
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003699/// \brief Optionally returns true or false if the preallocated preprocessed
3700/// entity with index \arg Index came from file \arg FID.
3701llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3702 FileID FID) {
3703 if (FID.isInvalid())
3704 return false;
3705
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003706 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3707 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003708 unsigned LocalIndex = PPInfo.second;
3709 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3710
3711 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3712 if (Loc.isInvalid())
3713 return false;
3714
3715 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3716 return true;
3717 else
3718 return false;
3719}
3720
Douglas Gregord10a3812011-08-25 18:14:34 +00003721namespace {
3722 /// \brief Visitor used to search for information about a header file.
3723 class HeaderFileInfoVisitor {
3724 ASTReader &Reader;
3725 const FileEntry *FE;
3726
3727 llvm::Optional<HeaderFileInfo> HFI;
3728
3729 public:
3730 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3731 : Reader(Reader), FE(FE) { }
3732
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003733 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003734 HeaderFileInfoVisitor *This
3735 = static_cast<HeaderFileInfoVisitor *>(UserData);
3736
3737 HeaderFileInfoTrait Trait(This->Reader, M,
3738 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3739 M.HeaderFileFrameworkStrings,
3740 This->FE->getName());
3741
3742 HeaderFileInfoLookupTable *Table
3743 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3744 if (!Table)
3745 return false;
3746
3747 // Look in the on-disk hash table for an entry for this file name.
3748 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3749 &Trait);
3750 if (Pos == Table->end())
3751 return false;
3752
3753 This->HFI = *Pos;
3754 return true;
3755 }
3756
3757 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3758 };
3759}
3760
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003761HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003762 HeaderFileInfoVisitor Visitor(*this, FE);
3763 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3764 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003765 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003766 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3767 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003768 }
3769
3770 return HeaderFileInfo();
3771}
3772
David Blaikied6471f72011-09-25 23:23:43 +00003773void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003774 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003775 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003776 unsigned Idx = 0;
3777 while (Idx < F.PragmaDiagMappings.size()) {
3778 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003779 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3780 Diag.DiagStatePoints.push_back(
3781 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3782 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003783 while (1) {
3784 assert(Idx < F.PragmaDiagMappings.size() &&
3785 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3786 if (Idx >= F.PragmaDiagMappings.size()) {
3787 break; // Something is messed up but at least avoid infinite loop in
3788 // release build.
3789 }
3790 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3791 if (DiagID == (unsigned)-1) {
3792 break; // no more diag/map pairs for this location.
3793 }
3794 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003795 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3796 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003797 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003798 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003799 }
3800}
3801
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003802/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003803ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003804 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003805 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003806 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003807 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003808}
3809
3810/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003811///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003812/// The index is the type ID, shifted and minus the number of predefs. This
3813/// routine actually reads the record corresponding to the type at the given
3814/// location. It is a helper routine for GetType, which deals with reading type
3815/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003816QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003817 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003818 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003819
Douglas Gregor0b748912009-04-14 21:18:50 +00003820 // Keep track of where we are in the stream, then jump back there
3821 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003822 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003823
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003824 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003825
Douglas Gregord89275b2009-07-06 18:54:52 +00003826 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003827 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003828
Douglas Gregor393f2492011-07-22 00:38:23 +00003829 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003830 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003831 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003832 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003833 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3834 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003835 if (Record.size() != 2) {
3836 Error("Incorrect encoding of extended qualifier type");
3837 return QualType();
3838 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003839 QualType Base = readType(*Loc.F, Record, Idx);
3840 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003841 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003842 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003843
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003844 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003845 if (Record.size() != 1) {
3846 Error("Incorrect encoding of complex type");
3847 return QualType();
3848 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003849 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003850 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003851 }
3852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003853 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003854 if (Record.size() != 1) {
3855 Error("Incorrect encoding of pointer type");
3856 return QualType();
3857 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003858 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003859 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003860 }
3861
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003862 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003863 if (Record.size() != 1) {
3864 Error("Incorrect encoding of block pointer type");
3865 return QualType();
3866 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003867 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003868 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003869 }
3870
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003871 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003872 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003873 Error("Incorrect encoding of lvalue reference type");
3874 return QualType();
3875 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003876 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003877 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003878 }
3879
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003880 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003881 if (Record.size() != 1) {
3882 Error("Incorrect encoding of rvalue reference type");
3883 return QualType();
3884 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003885 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003886 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003887 }
3888
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003889 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003890 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003891 Error("Incorrect encoding of member pointer type");
3892 return QualType();
3893 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003894 QualType PointeeType = readType(*Loc.F, Record, Idx);
3895 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003896 if (PointeeType.isNull() || ClassType.isNull())
3897 return QualType();
3898
Douglas Gregor35942772011-09-09 21:34:22 +00003899 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003900 }
3901
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003902 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003903 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003904 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3905 unsigned IndexTypeQuals = Record[2];
3906 unsigned Idx = 3;
3907 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003908 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003909 ASM, IndexTypeQuals);
3910 }
3911
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003912 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003913 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003914 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3915 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003916 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003917 }
3918
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003919 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003920 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003921 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3922 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003923 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3924 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003925 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003926 ASM, IndexTypeQuals,
3927 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003928 }
3929
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003930 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003931 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003932 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003933 return QualType();
3934 }
3935
Douglas Gregor393f2492011-07-22 00:38:23 +00003936 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003937 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003938 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003939 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003940 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003941 }
3942
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003943 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003944 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003945 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003946 return QualType();
3947 }
3948
Douglas Gregor393f2492011-07-22 00:38:23 +00003949 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003950 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003951 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003952 }
3953
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003954 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003955 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003956 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003957 return QualType();
3958 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003959 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003960 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3961 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003962 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003963 }
3964
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003965 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003966 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003967
3968 FunctionProtoType::ExtProtoInfo EPI;
3969 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003970 /*hasregparm*/ Record[2],
3971 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003972 static_cast<CallingConv>(Record[4]),
3973 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003974
John McCallf85e1932011-06-15 23:02:42 +00003975 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003976 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003977 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003978 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003979 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003980
3981 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00003982 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00003983 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003984 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003985 ExceptionSpecificationType EST =
3986 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3987 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00003988 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003989 if (EST == EST_Dynamic) {
3990 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00003991 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003992 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003993 EPI.Exceptions = Exceptions.data();
3994 } else if (EST == EST_ComputedNoexcept) {
3995 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00003996 } else if (EST == EST_Uninstantiated) {
3997 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
3998 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00003999 } else if (EST == EST_Unevaluated) {
4000 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004001 }
Douglas Gregor35942772011-09-09 21:34:22 +00004002 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004003 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004004 }
4005
Douglas Gregor409448c2011-07-21 22:35:25 +00004006 case TYPE_UNRESOLVED_USING: {
4007 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004008 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004009 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4010 }
4011
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004012 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004013 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004014 Error("incorrect encoding of typedef type");
4015 return QualType();
4016 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004017 unsigned Idx = 0;
4018 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004019 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004020 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004021 Canonical = Context.getCanonicalType(Canonical);
4022 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004023 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004024
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004025 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004026 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004027
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004028 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004029 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004030 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004031 return QualType();
4032 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004033 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004034 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004035 }
Mike Stump1eb44332009-09-09 15:08:12 +00004036
Douglas Gregorf8af9822012-02-12 18:42:33 +00004037 case TYPE_DECLTYPE: {
4038 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4039 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4040 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004041
Sean Huntca63c202011-05-24 22:41:36 +00004042 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004043 QualType BaseType = readType(*Loc.F, Record, Idx);
4044 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004045 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004046 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004047 }
4048
Richard Smith34b41d92011-02-20 03:19:35 +00004049 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004050 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004051
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004052 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004053 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004054 Error("incorrect encoding of record type");
4055 return QualType();
4056 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004057 unsigned Idx = 0;
4058 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004059 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4060 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4061 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004062 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004063 return T;
4064 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004065
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004066 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004067 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004068 Error("incorrect encoding of enum type");
4069 return QualType();
4070 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004071 unsigned Idx = 0;
4072 bool IsDependent = Record[Idx++];
4073 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004074 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004075 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004076 return T;
4077 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004078
John McCall9d156a72011-01-06 01:58:22 +00004079 case TYPE_ATTRIBUTED: {
4080 if (Record.size() != 3) {
4081 Error("incorrect encoding of attributed type");
4082 return QualType();
4083 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004084 QualType modifiedType = readType(*Loc.F, Record, Idx);
4085 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004086 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004087 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004088 }
4089
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004090 case TYPE_PAREN: {
4091 if (Record.size() != 1) {
4092 Error("incorrect encoding of paren type");
4093 return QualType();
4094 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004095 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004096 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004097 }
4098
Douglas Gregor7536dd52010-12-20 02:24:11 +00004099 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004100 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004101 Error("incorrect encoding of pack expansion type");
4102 return QualType();
4103 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004104 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004105 if (Pattern.isNull())
4106 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004107 llvm::Optional<unsigned> NumExpansions;
4108 if (Record[1])
4109 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004110 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004111 }
4112
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004113 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004114 unsigned Idx = 0;
4115 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004116 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004117 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004118 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004119 }
4120
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004121 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004122 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004123 ObjCInterfaceDecl *ItfD
4124 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004125 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004126 }
4127
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004128 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004129 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004130 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004131 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004132 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004133 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004134 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004135 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004136 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004137
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004138 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004139 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004140 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004141 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004142 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004144 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004145 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004146 QualType Parm = readType(*Loc.F, Record, Idx);
4147 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004148 return
Douglas Gregor35942772011-09-09 21:34:22 +00004149 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004150 Replacement);
4151 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004152
Douglas Gregorc3069d62011-01-14 02:55:32 +00004153 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4154 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004155 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004156 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004157 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004158 cast<TemplateTypeParmType>(Parm),
4159 ArgPack);
4160 }
4161
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004162 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004163 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004164 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004165 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004166 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004167 return
Douglas Gregor35942772011-09-09 21:34:22 +00004168 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004169 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004170
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004171 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004172 unsigned Idx = 0;
4173 unsigned Depth = Record[Idx++];
4174 unsigned Index = Record[Idx++];
4175 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004176 TemplateTypeParmDecl *D
4177 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004178 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004179 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004180
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004181 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004182 unsigned Idx = 0;
4183 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004184 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004185 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004186 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004187 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004188 Canon = Context.getCanonicalType(Canon);
4189 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004190 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004191
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004192 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004193 unsigned Idx = 0;
4194 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004195 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004196 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004197 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004198 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004199 Args.reserve(NumArgs);
4200 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004201 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004202 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004203 Args.size(), Args.data());
4204 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004205
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004206 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004207 unsigned Idx = 0;
4208
4209 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004210 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004211 ArrayType::ArraySizeModifier ASM
4212 = (ArrayType::ArraySizeModifier)Record[Idx++];
4213 unsigned IndexTypeQuals = Record[Idx++];
4214
4215 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004216 Expr *NumElts = ReadExpr(*Loc.F);
4217 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004218
Douglas Gregor35942772011-09-09 21:34:22 +00004219 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004220 IndexTypeQuals, Brackets);
4221 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004222
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004223 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004224 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004225 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004226 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004227 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004228 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004229 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004230 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004231 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004232 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004233 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004234 else
Douglas Gregor35942772011-09-09 21:34:22 +00004235 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004236 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004237 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004238 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004239 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004240
4241 case TYPE_ATOMIC: {
4242 if (Record.size() != 1) {
4243 Error("Incorrect encoding of atomic type");
4244 return QualType();
4245 }
4246 QualType ValueType = readType(*Loc.F, Record, Idx);
4247 return Context.getAtomicType(ValueType);
4248 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004249 }
David Blaikie7530c032012-01-17 06:56:22 +00004250 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004251}
4252
Sebastian Redlc3632732010-10-05 15:59:54 +00004253class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004254 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004255 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004256 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004257 unsigned &Idx;
4258
Sebastian Redlc3632732010-10-05 15:59:54 +00004259 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4260 unsigned &I) {
4261 return Reader.ReadSourceLocation(F, R, I);
4262 }
4263
Douglas Gregor409448c2011-07-21 22:35:25 +00004264 template<typename T>
4265 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4266 return Reader.ReadDeclAs<T>(F, Record, Idx);
4267 }
4268
John McCalla1ee0c52009-10-16 21:56:05 +00004269public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004270 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004271 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004272 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004273 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004274
John McCall51bd8032009-10-18 01:05:36 +00004275 // We want compile-time assurance that we've enumerated all of
4276 // these, so unfortunately we have to declare them first, then
4277 // define them out-of-line.
4278#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004279#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004280 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004281#include "clang/AST/TypeLocNodes.def"
4282
John McCall51bd8032009-10-18 01:05:36 +00004283 void VisitFunctionTypeLoc(FunctionTypeLoc);
4284 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004285};
4286
John McCall51bd8032009-10-18 01:05:36 +00004287void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004288 // nothing to do
4289}
John McCall51bd8032009-10-18 01:05:36 +00004290void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004291 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004292 if (TL.needsExtraLocalData()) {
4293 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4294 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4295 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4296 TL.setModeAttr(Record[Idx++]);
4297 }
John McCalla1ee0c52009-10-16 21:56:05 +00004298}
John McCall51bd8032009-10-18 01:05:36 +00004299void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004300 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004301}
John McCall51bd8032009-10-18 01:05:36 +00004302void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004303 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004304}
John McCall51bd8032009-10-18 01:05:36 +00004305void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004306 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004307}
John McCall51bd8032009-10-18 01:05:36 +00004308void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004309 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004310}
John McCall51bd8032009-10-18 01:05:36 +00004311void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004312 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004313}
John McCall51bd8032009-10-18 01:05:36 +00004314void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004315 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004316 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004317}
John McCall51bd8032009-10-18 01:05:36 +00004318void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004319 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4320 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004321 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004322 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004323 else
John McCall51bd8032009-10-18 01:05:36 +00004324 TL.setSizeExpr(0);
4325}
4326void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4327 VisitArrayTypeLoc(TL);
4328}
4329void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4330 VisitArrayTypeLoc(TL);
4331}
4332void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4333 VisitArrayTypeLoc(TL);
4334}
4335void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4336 DependentSizedArrayTypeLoc TL) {
4337 VisitArrayTypeLoc(TL);
4338}
4339void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4340 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004341 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004342}
4343void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004344 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004345}
4346void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004347 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004348}
4349void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004350 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004351 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4352 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004353 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004354 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004355 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004356 }
4357}
4358void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4359 VisitFunctionTypeLoc(TL);
4360}
4361void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4362 VisitFunctionTypeLoc(TL);
4363}
John McCalled976492009-12-04 22:46:56 +00004364void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004365 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004366}
John McCall51bd8032009-10-18 01:05:36 +00004367void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004368 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004369}
4370void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004371 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4372 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4373 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004374}
4375void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004376 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4377 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4378 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4379 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004380}
4381void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004382 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004383}
Sean Huntca63c202011-05-24 22:41:36 +00004384void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4385 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4386 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4387 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4388 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4389}
Richard Smith34b41d92011-02-20 03:19:35 +00004390void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4391 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4392}
John McCall51bd8032009-10-18 01:05:36 +00004393void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004394 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004395}
4396void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004397 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004398}
John McCall9d156a72011-01-06 01:58:22 +00004399void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4400 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4401 if (TL.hasAttrOperand()) {
4402 SourceRange range;
4403 range.setBegin(ReadSourceLocation(Record, Idx));
4404 range.setEnd(ReadSourceLocation(Record, Idx));
4405 TL.setAttrOperandParensRange(range);
4406 }
4407 if (TL.hasAttrExprOperand()) {
4408 if (Record[Idx++])
4409 TL.setAttrExprOperand(Reader.ReadExpr(F));
4410 else
4411 TL.setAttrExprOperand(0);
4412 } else if (TL.hasAttrEnumOperand())
4413 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4414}
John McCall51bd8032009-10-18 01:05:36 +00004415void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004416 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004417}
John McCall49a832b2009-10-18 09:09:24 +00004418void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4419 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004420 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004421}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004422void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4423 SubstTemplateTypeParmPackTypeLoc TL) {
4424 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4425}
John McCall51bd8032009-10-18 01:05:36 +00004426void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4427 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004428 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004429 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4430 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4431 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004432 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4433 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004434 Reader.GetTemplateArgumentLocInfo(F,
4435 TL.getTypePtr()->getArg(i).getKind(),
4436 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004437}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004438void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4439 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4440 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4441}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004442void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004443 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004444 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004445}
John McCall3cb0ebd2010-03-10 03:28:59 +00004446void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004447 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004448}
Douglas Gregor4714c122010-03-31 17:34:00 +00004449void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004450 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004451 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004452 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004453}
John McCall33500952010-06-11 00:33:02 +00004454void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4455 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004456 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004457 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004458 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004459 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004460 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4461 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004462 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4463 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004464 Reader.GetTemplateArgumentLocInfo(F,
4465 TL.getTypePtr()->getArg(I).getKind(),
4466 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004467}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004468void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4469 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4470}
John McCall51bd8032009-10-18 01:05:36 +00004471void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004472 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004473}
4474void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4475 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004476 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4477 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004478 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004479 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004480}
John McCall54e14c42009-10-22 22:37:11 +00004481void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004482 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004483}
Eli Friedmanb001de72011-10-06 23:00:33 +00004484void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4485 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4486 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4487 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4488}
John McCalla1ee0c52009-10-16 21:56:05 +00004489
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004490TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004491 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004492 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004493 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004494 if (InfoTy.isNull())
4495 return 0;
4496
Douglas Gregor35942772011-09-09 21:34:22 +00004497 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004498 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004499 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004500 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004501 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004502}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004503
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004504QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004505 unsigned FastQuals = ID & Qualifiers::FastMask;
4506 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004507
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004508 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004509 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004510 switch ((PredefinedTypeIDs)Index) {
4511 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004512 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4513 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004514
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004515 case PREDEF_TYPE_CHAR_U_ID:
4516 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004517 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004518 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004519 break;
4520
Douglas Gregor35942772011-09-09 21:34:22 +00004521 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4522 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4523 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4524 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4525 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4526 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4527 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4528 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4529 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4530 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4531 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4532 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4533 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004534 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004535 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4536 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4537 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4538 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4539 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004540 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004541 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4542 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4543 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4544 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4545 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4546 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4547 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4548 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4549 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004550
4551 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004552 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004553 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004554
4555 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4556 T = Context.ARCUnbridgedCastTy;
4557 break;
4558
Meador Ingefb40e3f2012-07-01 15:57:25 +00004559 case PREDEF_TYPE_VA_LIST_TAG:
4560 T = Context.getVaListTagType();
4561 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004562
4563 case PREDEF_TYPE_BUILTIN_FN:
4564 T = Context.BuiltinFnTy;
4565 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004566 }
4567
4568 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004569 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004570 }
4571
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004572 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004573 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004574 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004575 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004576 if (TypesLoaded[Index].isNull())
4577 return QualType();
4578
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004579 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004580 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004581 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004582 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004583 }
Mike Stump1eb44332009-09-09 15:08:12 +00004584
John McCall0953e762009-09-24 19:53:00 +00004585 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004586}
4587
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004588QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004589 return GetType(getGlobalTypeID(F, LocalID));
4590}
4591
4592serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004593ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004594 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4595 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4596
4597 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4598 return LocalID;
4599
4600 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4601 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4602 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4603
4604 unsigned GlobalIndex = LocalIndex + I->second;
4605 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4606}
4607
John McCall833ca992009-10-29 08:12:44 +00004608TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004609ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004610 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004611 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004612 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004613 switch (Kind) {
4614 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004615 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004616 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004617 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004618 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004619 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4620 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004621 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004622 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004623 SourceLocation());
4624 }
4625 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004626 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4627 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004628 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004629 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004630 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004631 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004632 }
John McCall833ca992009-10-29 08:12:44 +00004633 case TemplateArgument::Null:
4634 case TemplateArgument::Integral:
4635 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004636 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004637 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004638 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004639 return TemplateArgumentLocInfo();
4640 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004641 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004642}
4643
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004644TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004645ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004646 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004647 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004648
4649 if (Arg.getKind() == TemplateArgument::Expression) {
4650 if (Record[Index++]) // bool InfoHasSameExpr.
4651 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4652 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004653 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004654 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004655}
4656
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004657Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004658 return GetDecl(ID);
4659}
4660
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004661uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004662 unsigned &Idx){
4663 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004664 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004665
Douglas Gregore92b8a12011-08-04 00:01:48 +00004666 unsigned LocalID = Record[Idx++];
4667 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004668}
4669
4670CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004671 RecordLocation Loc = getLocalBitOffset(Offset);
4672 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004673 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004674 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004675 ReadingKindTracker ReadingKind(Read_Decl, *this);
4676 RecordData Record;
4677 unsigned Code = Cursor.ReadCode();
4678 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4679 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4680 Error("Malformed AST file: missing C++ base specifiers");
4681 return 0;
4682 }
4683
4684 unsigned Idx = 0;
4685 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004686 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004687 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4688 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004689 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004690 return Bases;
4691}
4692
Douglas Gregor409448c2011-07-21 22:35:25 +00004693serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004694ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004695 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004696 return LocalID;
4697
4698 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004699 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004700 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4701
4702 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004703}
4704
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004705bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004706 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004707 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4708 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4709 return &M == I->second;
4710}
4711
Douglas Gregorcff9f262012-01-27 01:47:08 +00004712ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4713 if (!D->isFromASTFile())
4714 return 0;
4715 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4716 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4717 return I->second;
4718}
4719
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004720SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4721 if (ID < NUM_PREDEF_DECL_IDS)
4722 return SourceLocation();
4723
4724 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4725
4726 if (Index > DeclsLoaded.size()) {
4727 Error("declaration ID out-of-range for AST file");
4728 return SourceLocation();
4729 }
4730
4731 if (Decl *D = DeclsLoaded[Index])
4732 return D->getLocation();
4733
4734 unsigned RawLocation = 0;
4735 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4736 return ReadSourceLocation(*Rec.F, RawLocation);
4737}
4738
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004739Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004740 if (ID < NUM_PREDEF_DECL_IDS) {
4741 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004742 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004743 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004744
4745 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004746 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004747
4748 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004749 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004750
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004751 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004752 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004753
Douglas Gregor79d67262011-08-12 05:59:41 +00004754 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004755 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004756
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004757 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4758 return Context.getObjCProtocolDecl();
4759
Douglas Gregor772eeae2011-08-12 06:49:56 +00004760 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004761 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004762
4763 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004764 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004765
4766 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004767 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004768
4769 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4770 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004771 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004772 }
4773
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004774 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4775
Richard Smith2fbf3732011-12-20 04:39:57 +00004776 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004777 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004778 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004779 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004780 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004781
Douglas Gregorfd002a72011-12-16 22:37:11 +00004782 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004783 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004784 if (DeserializationListener)
4785 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4786 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004787
4788 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004789}
4790
Douglas Gregora1be2782011-12-17 23:38:30 +00004791DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4792 DeclID GlobalID) {
4793 if (GlobalID < NUM_PREDEF_DECL_IDS)
4794 return GlobalID;
4795
4796 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4797 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4798 ModuleFile *Owner = I->second;
4799
4800 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4801 = M.GlobalToLocalDeclIDs.find(Owner);
4802 if (Pos == M.GlobalToLocalDeclIDs.end())
4803 return 0;
4804
4805 return GlobalID - Owner->BaseDeclID + Pos->second;
4806}
4807
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004808serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004809 const RecordData &Record,
4810 unsigned &Idx) {
4811 if (Idx >= Record.size()) {
4812 Error("Corrupted AST file");
4813 return 0;
4814 }
4815
4816 return getGlobalDeclID(F, Record[Idx++]);
4817}
4818
Chris Lattner887e2b32009-04-27 05:46:25 +00004819/// \brief Resolve the offset of a statement into a statement.
4820///
4821/// This operation will read a new statement from the external
4822/// source each time it is called, and is meant to be used via a
4823/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004824Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004825 // Switch case IDs are per Decl.
4826 ClearSwitchCaseIDs();
4827
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004828 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004829 RecordLocation Loc = getLocalBitOffset(Offset);
4830 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4831 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004832}
4833
Douglas Gregor851c75a2011-08-24 21:27:34 +00004834namespace {
4835 class FindExternalLexicalDeclsVisitor {
4836 ASTReader &Reader;
4837 const DeclContext *DC;
4838 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004839
Douglas Gregor851c75a2011-08-24 21:27:34 +00004840 SmallVectorImpl<Decl*> &Decls;
4841 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4842
4843 public:
4844 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4845 bool (*isKindWeWant)(Decl::Kind),
4846 SmallVectorImpl<Decl*> &Decls)
4847 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4848 {
4849 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4850 PredefsVisited[I] = false;
4851 }
4852
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004853 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004854 if (Preorder)
4855 return false;
4856
4857 FindExternalLexicalDeclsVisitor *This
4858 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4859
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004860 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004861 = M.DeclContextInfos.find(This->DC);
4862 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4863 return false;
4864
4865 // Load all of the declaration IDs
4866 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4867 *IDE = ID + Info->second.NumLexicalDecls;
4868 ID != IDE; ++ID) {
4869 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4870 continue;
4871
4872 // Don't add predefined declarations to the lexical context more
4873 // than once.
4874 if (ID->second < NUM_PREDEF_DECL_IDS) {
4875 if (This->PredefsVisited[ID->second])
4876 continue;
4877
4878 This->PredefsVisited[ID->second] = true;
4879 }
4880
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004881 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4882 if (!This->DC->isDeclInLexicalTraversal(D))
4883 This->Decls.push_back(D);
4884 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004885 }
4886
4887 return false;
4888 }
4889 };
4890}
4891
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004892ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004893 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004894 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004895 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004896 // least. Walk all of the modules in the order they were loaded.
4897 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4898 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004899 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004900 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004901}
4902
Douglas Gregor0d95f772011-08-24 19:03:07 +00004903namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004904
4905class DeclIDComp {
4906 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004907 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004908
4909public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004910 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004911
4912 bool operator()(LocalDeclID L, LocalDeclID R) const {
4913 SourceLocation LHS = getLocation(L);
4914 SourceLocation RHS = getLocation(R);
4915 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4916 }
4917
4918 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4919 SourceLocation RHS = getLocation(R);
4920 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4921 }
4922
4923 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4924 SourceLocation LHS = getLocation(L);
4925 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4926 }
4927
4928 SourceLocation getLocation(LocalDeclID ID) const {
4929 return Reader.getSourceManager().getFileLoc(
4930 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4931 }
4932};
4933
4934}
4935
4936void ASTReader::FindFileRegionDecls(FileID File,
4937 unsigned Offset, unsigned Length,
4938 SmallVectorImpl<Decl *> &Decls) {
4939 SourceManager &SM = getSourceManager();
4940
4941 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4942 if (I == FileDeclIDs.end())
4943 return;
4944
4945 FileDeclsInfo &DInfo = I->second;
4946 if (DInfo.Decls.empty())
4947 return;
4948
4949 SourceLocation
4950 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4951 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4952
4953 DeclIDComp DIDComp(*this, *DInfo.Mod);
4954 ArrayRef<serialization::LocalDeclID>::iterator
4955 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4956 BeginLoc, DIDComp);
4957 if (BeginIt != DInfo.Decls.begin())
4958 --BeginIt;
4959
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004960 // If we are pointing at a top-level decl inside an objc container, we need
4961 // to backtrack until we find it otherwise we will fail to report that the
4962 // region overlaps with an objc container.
4963 while (BeginIt != DInfo.Decls.begin() &&
4964 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4965 ->isTopLevelDeclInObjCContainer())
4966 --BeginIt;
4967
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004968 ArrayRef<serialization::LocalDeclID>::iterator
4969 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4970 EndLoc, DIDComp);
4971 if (EndIt != DInfo.Decls.end())
4972 ++EndIt;
4973
4974 for (ArrayRef<serialization::LocalDeclID>::iterator
4975 DIt = BeginIt; DIt != EndIt; ++DIt)
4976 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4977}
4978
4979namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004980 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00004981 /// declaration context.
4982 class DeclContextNameLookupVisitor {
4983 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004984 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004985 DeclarationName Name;
4986 SmallVectorImpl<NamedDecl *> &Decls;
4987
4988 public:
4989 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004990 SmallVectorImpl<const DeclContext *> &Contexts,
4991 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00004992 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004993 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004994
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004995 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004996 DeclContextNameLookupVisitor *This
4997 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4998
4999 // Check whether we have any visible declaration information for
5000 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005001 ModuleFile::DeclContextInfosMap::iterator Info;
5002 bool FoundInfo = false;
5003 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5004 Info = M.DeclContextInfos.find(This->Contexts[I]);
5005 if (Info != M.DeclContextInfos.end() &&
5006 Info->second.NameLookupTableData) {
5007 FoundInfo = true;
5008 break;
5009 }
5010 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005011
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005012 if (!FoundInfo)
5013 return false;
5014
Douglas Gregor0d95f772011-08-24 19:03:07 +00005015 // Look for this name within this module.
5016 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005017 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005018 ASTDeclContextNameLookupTable::iterator Pos
5019 = LookupTable->find(This->Name);
5020 if (Pos == LookupTable->end())
5021 return false;
5022
5023 bool FoundAnything = false;
5024 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5025 for (; Data.first != Data.second; ++Data.first) {
5026 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5027 if (!ND)
5028 continue;
5029
5030 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005031 // A name might be null because the decl's redeclarable part is
5032 // currently read before reading its name. The lookup is triggered by
5033 // building that decl (likely indirectly), and so it is later in the
5034 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005035 continue;
5036 }
5037
5038 // Record this declaration.
5039 FoundAnything = true;
5040 This->Decls.push_back(ND);
5041 }
5042
5043 return FoundAnything;
5044 }
5045 };
5046}
5047
John McCall76bd1f32010-06-01 09:23:16 +00005048DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005049ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005050 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005051 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005052 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005053 if (!Name)
5054 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5055 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005056
Chris Lattner5f9e2722011-07-23 10:55:15 +00005057 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005058
5059 // Compute the declaration contexts we need to look into. Multiple such
5060 // declaration contexts occur when two declaration contexts from disjoint
5061 // modules get merged, e.g., when two namespaces with the same name are
5062 // independently defined in separate modules.
5063 SmallVector<const DeclContext *, 2> Contexts;
5064 Contexts.push_back(DC);
5065
5066 if (DC->isNamespace()) {
5067 MergedDeclsMap::iterator Merged
5068 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5069 if (Merged != MergedDecls.end()) {
5070 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5071 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5072 }
5073 }
5074
5075 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005076 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005077 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005078 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005079 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005080}
5081
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005082namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005083 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005084 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005085 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005086 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005087 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005088 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005089
5090 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005091 DeclContextAllNamesVisitor(ASTReader &Reader,
5092 SmallVectorImpl<const DeclContext *> &Contexts,
5093 llvm::DenseMap<DeclarationName,
5094 SmallVector<NamedDecl *, 8> > &Decls)
5095 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005096
5097 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005098 DeclContextAllNamesVisitor *This
5099 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005100
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005101 // Check whether we have any visible declaration information for
5102 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005103 ModuleFile::DeclContextInfosMap::iterator Info;
5104 bool FoundInfo = false;
5105 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5106 Info = M.DeclContextInfos.find(This->Contexts[I]);
5107 if (Info != M.DeclContextInfos.end() &&
5108 Info->second.NameLookupTableData) {
5109 FoundInfo = true;
5110 break;
5111 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005112 }
5113
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005114 if (!FoundInfo)
5115 return false;
5116
5117 ASTDeclContextNameLookupTable *LookupTable =
5118 Info->second.NameLookupTableData;
5119 bool FoundAnything = false;
5120 for (ASTDeclContextNameLookupTable::data_iterator
5121 I = LookupTable->data_begin(), E = LookupTable->data_end();
5122 I != E; ++I) {
5123 ASTDeclContextNameLookupTrait::data_type Data = *I;
5124 for (; Data.first != Data.second; ++Data.first) {
5125 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5126 *Data.first);
5127 if (!ND)
5128 continue;
5129
5130 // Record this declaration.
5131 FoundAnything = true;
5132 This->Decls[ND->getDeclName()].push_back(ND);
5133 }
5134 }
5135
5136 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005137 }
5138 };
5139}
5140
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005141void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005142 if (!DC->hasExternalVisibleStorage())
5143 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005144 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5145
5146 // Compute the declaration contexts we need to look into. Multiple such
5147 // declaration contexts occur when two declaration contexts from disjoint
5148 // modules get merged, e.g., when two namespaces with the same name are
5149 // independently defined in separate modules.
5150 SmallVector<const DeclContext *, 2> Contexts;
5151 Contexts.push_back(DC);
5152
5153 if (DC->isNamespace()) {
5154 MergedDeclsMap::iterator Merged
5155 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5156 if (Merged != MergedDecls.end()) {
5157 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5158 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5159 }
5160 }
5161
5162 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5163 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5164 ++NumVisibleDeclContextsRead;
5165
5166 for (llvm::DenseMap<DeclarationName,
5167 llvm::SmallVector<NamedDecl*, 8> >::iterator
5168 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5169 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5170 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005171 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005172}
5173
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005174/// \brief Under non-PCH compilation the consumer receives the objc methods
5175/// before receiving the implementation, and codegen depends on this.
5176/// We simulate this by deserializing and passing to consumer the methods of the
5177/// implementation before passing the deserialized implementation decl.
5178static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5179 ASTConsumer *Consumer) {
5180 assert(ImplD && Consumer);
5181
5182 for (ObjCImplDecl::method_iterator
5183 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005184 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005185
5186 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5187}
5188
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005189void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005190 assert(Consumer);
5191 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005192 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005193 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005194
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005195 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005196 }
5197}
5198
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005199void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5200 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5201 PassObjCImplDeclToConsumer(ImplD, Consumer);
5202 else
5203 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5204}
5205
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005206void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005207 this->Consumer = Consumer;
5208
Douglas Gregorfdd01722009-04-14 00:24:19 +00005209 if (!Consumer)
5210 return;
5211
5212 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005213 // Force deserialization of this decl, which will cause it to be queued for
5214 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005215 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005216 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005217 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005218
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005219 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005220}
5221
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005222void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005223 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005224
Mike Stump1eb44332009-09-09 15:08:12 +00005225 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005226 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005227 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005228 unsigned NumDeclsLoaded
5229 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5230 (Decl *)0);
5231 unsigned NumIdentifiersLoaded
5232 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5233 IdentifiersLoaded.end(),
5234 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005235 unsigned NumMacrosLoaded
5236 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5237 MacrosLoaded.end(),
5238 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005239 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005240 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5241 SelectorsLoaded.end(),
5242 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005243
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005244 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5245 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005246 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005247 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5248 NumSLocEntriesRead, TotalNumSLocEntries,
5249 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005250 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005251 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005252 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5253 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5254 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005255 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005256 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5257 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005258 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005259 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005260 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5261 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005262 if (!MacrosLoaded.empty())
5263 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5264 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5265 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005266 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005267 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005268 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5269 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005270 if (TotalNumStatements)
5271 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5272 NumStatementsRead, TotalNumStatements,
5273 ((float)NumStatementsRead/TotalNumStatements * 100));
5274 if (TotalNumMacros)
5275 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5276 NumMacrosRead, TotalNumMacros,
5277 ((float)NumMacrosRead/TotalNumMacros * 100));
5278 if (TotalLexicalDeclContexts)
5279 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5280 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5281 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5282 * 100));
5283 if (TotalVisibleDeclContexts)
5284 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5285 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5286 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5287 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005288 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005289 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005290 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5291 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005292 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005293 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005294 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005295 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005296 dump();
5297 std::fprintf(stderr, "\n");
5298}
5299
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005300template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005301static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005302dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005303 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005304 InitialCapacity> &Map) {
5305 if (Map.begin() == Map.end())
5306 return;
5307
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005308 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005309 llvm::errs() << Name << ":\n";
5310 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5311 I != IEnd; ++I) {
5312 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5313 << "\n";
5314 }
5315}
5316
Douglas Gregor23d7df52011-07-21 19:50:14 +00005317void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005318 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005319 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005320 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005321 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005322 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005323 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005324 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005325 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005326 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005327 dumpModuleIDMap("Global preprocessed entity map",
5328 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005329
5330 llvm::errs() << "\n*** PCH/Modules Loaded:";
5331 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5332 MEnd = ModuleMgr.end();
5333 M != MEnd; ++M)
5334 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005335}
5336
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005337/// Return the amount of memory used by memory buffers, breaking down
5338/// by heap-backed versus mmap'ed memory.
5339void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005340 for (ModuleConstIterator I = ModuleMgr.begin(),
5341 E = ModuleMgr.end(); I != E; ++I) {
5342 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005343 size_t bytes = buf->getBufferSize();
5344 switch (buf->getBufferKind()) {
5345 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5346 sizes.malloc_bytes += bytes;
5347 break;
5348 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5349 sizes.mmap_bytes += bytes;
5350 break;
5351 }
5352 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005353 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005354}
5355
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005356void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005357 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005358 S.ExternalSource = this;
5359
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005360 // Makes sure any declarations that were deserialized "too early"
5361 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005362 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005363 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5364 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005365 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005366 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005367
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005368 // Load the offsets of the declarations that Sema references.
5369 // They will be lazily deserialized when needed.
5370 if (!SemaDeclRefs.empty()) {
5371 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005372 if (!SemaObj->StdNamespace)
5373 SemaObj->StdNamespace = SemaDeclRefs[0];
5374 if (!SemaObj->StdBadAlloc)
5375 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005376 }
5377
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005378 if (!FPPragmaOptions.empty()) {
5379 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5380 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5381 }
5382
5383 if (!OpenCLExtensions.empty()) {
5384 unsigned I = 0;
5385#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5386#include "clang/Basic/OpenCLExtensions.def"
5387
5388 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5389 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005390}
5391
Douglas Gregor211f6e82011-08-20 04:39:52 +00005392IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005393 // Note that we are loading an identifier.
5394 Deserializing AnIdentifier(this);
5395
Douglas Gregor057df202012-01-18 20:56:22 +00005396 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5397 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005398 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005399 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005400 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005401 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005402}
5403
Douglas Gregor95f42922010-10-14 22:11:03 +00005404namespace clang {
5405 /// \brief An identifier-lookup iterator that enumerates all of the
5406 /// identifiers stored within a set of AST files.
5407 class ASTIdentifierIterator : public IdentifierIterator {
5408 /// \brief The AST reader whose identifiers are being enumerated.
5409 const ASTReader &Reader;
5410
5411 /// \brief The current index into the chain of AST files stored in
5412 /// the AST reader.
5413 unsigned Index;
5414
5415 /// \brief The current position within the identifier lookup table
5416 /// of the current AST file.
5417 ASTIdentifierLookupTable::key_iterator Current;
5418
5419 /// \brief The end position within the identifier lookup table of
5420 /// the current AST file.
5421 ASTIdentifierLookupTable::key_iterator End;
5422
5423 public:
5424 explicit ASTIdentifierIterator(const ASTReader &Reader);
5425
Chris Lattner5f9e2722011-07-23 10:55:15 +00005426 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005427 };
5428}
5429
5430ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005431 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005432 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005433 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005434 Current = IdTable->key_begin();
5435 End = IdTable->key_end();
5436}
5437
Chris Lattner5f9e2722011-07-23 10:55:15 +00005438StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005439 while (Current == End) {
5440 // If we have exhausted all of our AST files, we're done.
5441 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005442 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005443
5444 --Index;
5445 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005446 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5447 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005448 Current = IdTable->key_begin();
5449 End = IdTable->key_end();
5450 }
5451
5452 // We have any identifiers remaining in the current AST file; return
5453 // the next one.
5454 std::pair<const char*, unsigned> Key = *Current;
5455 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005456 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005457}
5458
5459IdentifierIterator *ASTReader::getIdentifiers() const {
5460 return new ASTIdentifierIterator(*this);
5461}
5462
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005463namespace clang { namespace serialization {
5464 class ReadMethodPoolVisitor {
5465 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005466 Selector Sel;
5467 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005468 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5469 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005470
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005471 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005472 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5473 unsigned PriorGeneration)
5474 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005475
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005476 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005477 ReadMethodPoolVisitor *This
5478 = static_cast<ReadMethodPoolVisitor *>(UserData);
5479
5480 if (!M.SelectorLookupTable)
5481 return false;
5482
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005483 // If we've already searched this module file, skip it now.
5484 if (M.Generation <= This->PriorGeneration)
5485 return true;
5486
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005487 ASTSelectorLookupTable *PoolTable
5488 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5489 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5490 if (Pos == PoolTable->end())
5491 return false;
5492
5493 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005494 // FIXME: Not quite happy with the statistics here. We probably should
5495 // disable this tracking when called via LoadSelector.
5496 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005497 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005498 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005499 if (This->Reader.DeserializationListener)
5500 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5501 This->Sel);
5502
5503 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5504 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5505 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005506 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005507
5508 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005509 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5510 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005511 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005512
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005513 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005514 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5515 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005516 }
5517 };
5518} } // end namespace clang::serialization
5519
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005520/// \brief Add the given set of methods to the method list.
5521static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5522 ObjCMethodList &List) {
5523 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5524 S.addMethodToGlobalList(&List, Methods[I]);
5525 }
5526}
5527
5528void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005529 // Get the selector generation and update it to the current generation.
5530 unsigned &Generation = SelectorGeneration[Sel];
5531 unsigned PriorGeneration = Generation;
5532 Generation = CurrentGeneration;
5533
5534 // Search for methods defined with this selector.
5535 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005536 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005537
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005538 if (Visitor.getInstanceMethods().empty() &&
5539 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005540 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005541 return;
5542 }
5543
5544 if (!getSema())
5545 return;
5546
5547 Sema &S = *getSema();
5548 Sema::GlobalMethodPool::iterator Pos
5549 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5550
5551 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5552 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005553}
5554
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005555void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005556 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005557 Namespaces.clear();
5558
5559 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5560 if (NamespaceDecl *Namespace
5561 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5562 Namespaces.push_back(Namespace);
5563 }
5564}
5565
Douglas Gregora8623202011-07-27 20:58:46 +00005566void ASTReader::ReadTentativeDefinitions(
5567 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5568 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5569 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5570 if (Var)
5571 TentativeDefs.push_back(Var);
5572 }
5573 TentativeDefinitions.clear();
5574}
5575
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005576void ASTReader::ReadUnusedFileScopedDecls(
5577 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5578 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5579 DeclaratorDecl *D
5580 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5581 if (D)
5582 Decls.push_back(D);
5583 }
5584 UnusedFileScopedDecls.clear();
5585}
5586
Douglas Gregor0129b562011-07-27 21:57:17 +00005587void ASTReader::ReadDelegatingConstructors(
5588 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5589 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5590 CXXConstructorDecl *D
5591 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5592 if (D)
5593 Decls.push_back(D);
5594 }
5595 DelegatingCtorDecls.clear();
5596}
5597
Douglas Gregord58a0a52011-07-28 00:39:29 +00005598void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5599 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5600 TypedefNameDecl *D
5601 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5602 if (D)
5603 Decls.push_back(D);
5604 }
5605 ExtVectorDecls.clear();
5606}
5607
Douglas Gregora126f172011-07-28 00:53:40 +00005608void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5609 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5610 CXXRecordDecl *D
5611 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5612 if (D)
5613 Decls.push_back(D);
5614 }
5615 DynamicClasses.clear();
5616}
5617
Douglas Gregorec12ce22011-07-28 14:20:37 +00005618void
5619ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5620 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5621 NamedDecl *D
5622 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5623 if (D)
5624 Decls.push_back(D);
5625 }
5626 LocallyScopedExternalDecls.clear();
5627}
5628
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005629void ASTReader::ReadReferencedSelectors(
5630 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5631 if (ReferencedSelectorsData.empty())
5632 return;
5633
5634 // If there are @selector references added them to its pool. This is for
5635 // implementation of -Wselector.
5636 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5637 unsigned I = 0;
5638 while (I < DataSize) {
5639 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5640 SourceLocation SelLoc
5641 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5642 Sels.push_back(std::make_pair(Sel, SelLoc));
5643 }
5644 ReferencedSelectorsData.clear();
5645}
5646
Douglas Gregor31e37b22011-07-28 18:09:57 +00005647void ASTReader::ReadWeakUndeclaredIdentifiers(
5648 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5649 if (WeakUndeclaredIdentifiers.empty())
5650 return;
5651
5652 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5653 IdentifierInfo *WeakId
5654 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5655 IdentifierInfo *AliasId
5656 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5657 SourceLocation Loc
5658 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5659 bool Used = WeakUndeclaredIdentifiers[I++];
5660 WeakInfo WI(AliasId, Loc);
5661 WI.setUsed(Used);
5662 WeakIDs.push_back(std::make_pair(WeakId, WI));
5663 }
5664 WeakUndeclaredIdentifiers.clear();
5665}
5666
Douglas Gregordfe65432011-07-28 19:11:31 +00005667void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5668 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5669 ExternalVTableUse VT;
5670 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5671 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5672 VT.DefinitionRequired = VTableUses[Idx++];
5673 VTables.push_back(VT);
5674 }
5675
5676 VTableUses.clear();
5677}
5678
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005679void ASTReader::ReadPendingInstantiations(
5680 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5681 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5682 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5683 SourceLocation Loc
5684 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005685
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005686 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005687 }
5688 PendingInstantiations.clear();
5689}
5690
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005691void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005692 // It would be complicated to avoid reading the methods anyway. So don't.
5693 ReadMethodPool(Sel);
5694}
5695
Douglas Gregor95eab172011-07-28 20:55:49 +00005696void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005697 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005698 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005699 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005700 if (DeserializationListener)
5701 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005702}
5703
Douglas Gregord89275b2009-07-06 18:54:52 +00005704/// \brief Set the globally-visible declarations associated with the given
5705/// identifier.
5706///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005707/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005708/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005709/// them.
5710///
5711/// \param II an IdentifierInfo that refers to one or more globally-visible
5712/// declarations.
5713///
5714/// \param DeclIDs the set of declaration IDs with the name @p II that are
5715/// visible at global scope.
5716///
5717/// \param Nonrecursive should be true to indicate that the caller knows that
5718/// this call is non-recursive, and therefore the globally-visible declarations
5719/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005720void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005721ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005722 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005723 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005724 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005725 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5726 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5727 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005728 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005729 return;
5730 }
Mike Stump1eb44332009-09-09 15:08:12 +00005731
Douglas Gregord89275b2009-07-06 18:54:52 +00005732 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5733 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5734 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005735 // Introduce this declaration into the translation-unit scope
5736 // and add it to the declaration chain for this identifier, so
5737 // that (unqualified) name lookup will find it.
5738 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005739 } else {
5740 // Queue this declaration so that it will be added to the
5741 // translation unit scope and identifier's declaration chain
5742 // once a Sema object is known.
5743 PreloadedDecls.push_back(D);
5744 }
5745 }
5746}
5747
Douglas Gregor95eab172011-07-28 20:55:49 +00005748IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005749 if (ID == 0)
5750 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005751
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005752 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005753 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005754 return 0;
5755 }
Mike Stump1eb44332009-09-09 15:08:12 +00005756
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005757 ID -= 1;
5758 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005759 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5760 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005761 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005762 unsigned Index = ID - M->BaseIdentifierID;
5763 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005764
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005765 // All of the strings in the AST file are preceded by a 16-bit length.
5766 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005767 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5768 // unsigned integers. This is important to avoid integer overflow when
5769 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005770 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005771 unsigned StrLen = (((unsigned) StrLenPtr[0])
5772 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005773 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005774 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005775 if (DeserializationListener)
5776 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005777 }
Mike Stump1eb44332009-09-09 15:08:12 +00005778
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005779 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005780}
5781
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005782IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005783 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5784}
5785
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005786IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005787 if (LocalID < NUM_PREDEF_IDENT_IDS)
5788 return LocalID;
5789
5790 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5791 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5792 assert(I != M.IdentifierRemap.end()
5793 && "Invalid index into identifier index remap");
5794
5795 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005796}
5797
Douglas Gregora8235d62012-10-09 23:05:51 +00005798MacroInfo *ASTReader::getMacro(MacroID ID) {
5799 if (ID == 0)
5800 return 0;
5801
5802 if (MacrosLoaded.empty()) {
5803 Error("no macro table in AST file");
5804 return 0;
5805 }
5806
5807 ID -= NUM_PREDEF_MACRO_IDS;
5808 if (!MacrosLoaded[ID]) {
5809 GlobalMacroMapType::iterator I
5810 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
5811 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
5812 ModuleFile *M = I->second;
5813 unsigned Index = ID - M->BaseMacroID;
5814 ReadMacroRecord(*M, M->MacroOffsets[Index]);
5815 }
5816
5817 return MacrosLoaded[ID];
5818}
5819
5820MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
5821 if (LocalID < NUM_PREDEF_MACRO_IDS)
5822 return LocalID;
5823
5824 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5825 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
5826 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
5827
5828 return LocalID + I->second;
5829}
5830
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005831bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005832 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005833}
5834
Douglas Gregor26ced122011-12-01 00:59:36 +00005835serialization::SubmoduleID
5836ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5837 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5838 return LocalID;
5839
5840 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5841 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5842 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005843 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00005844
5845 return LocalID + I->second;
5846}
5847
5848Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5849 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5850 assert(GlobalID == 0 && "Unhandled global submodule ID");
5851 return 0;
5852 }
5853
5854 if (GlobalID > SubmodulesLoaded.size()) {
5855 Error("submodule ID out of range in AST file");
5856 return 0;
5857 }
5858
5859 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5860}
5861
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005862Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005863 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5864}
5865
5866Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005867 if (ID == 0)
5868 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005869
Sebastian Redl725cd962010-08-04 20:40:17 +00005870 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005871 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005872 return Selector();
5873 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005874
Sebastian Redl725cd962010-08-04 20:40:17 +00005875 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005876 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005877 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5878 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005879 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005880 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005881 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005882 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005883 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005884 if (DeserializationListener)
5885 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005886 }
5887
Sebastian Redl725cd962010-08-04 20:40:17 +00005888 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005889}
5890
Douglas Gregor8451ec72011-07-28 14:41:43 +00005891Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005892 return DecodeSelector(ID);
5893}
5894
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005895uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005896 // ID 0 (the null selector) is considered an external selector.
5897 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005898}
5899
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005900serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005901ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005902 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5903 return LocalID;
5904
5905 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5906 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5907 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005908 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005909
5910 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005911}
5912
Mike Stump1eb44332009-09-09 15:08:12 +00005913DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005914ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005915 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005916 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5917 switch (Kind) {
5918 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005919 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005920
5921 case DeclarationName::ObjCZeroArgSelector:
5922 case DeclarationName::ObjCOneArgSelector:
5923 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005924 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005925
5926 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005927 return Context.DeclarationNames.getCXXConstructorName(
5928 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005929
5930 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005931 return Context.DeclarationNames.getCXXDestructorName(
5932 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005933
5934 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005935 return Context.DeclarationNames.getCXXConversionFunctionName(
5936 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005937
5938 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005939 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005940 (OverloadedOperatorKind)Record[Idx++]);
5941
Sean Hunt3e518bd2009-11-29 07:34:05 +00005942 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005943 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005944 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005945
Douglas Gregor2cf26342009-04-09 22:27:44 +00005946 case DeclarationName::CXXUsingDirective:
5947 return DeclarationName::getUsingDirectiveName();
5948 }
5949
David Blaikie7530c032012-01-17 06:56:22 +00005950 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005951}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005952
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005953void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005954 DeclarationNameLoc &DNLoc,
5955 DeclarationName Name,
5956 const RecordData &Record, unsigned &Idx) {
5957 switch (Name.getNameKind()) {
5958 case DeclarationName::CXXConstructorName:
5959 case DeclarationName::CXXDestructorName:
5960 case DeclarationName::CXXConversionFunctionName:
5961 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5962 break;
5963
5964 case DeclarationName::CXXOperatorName:
5965 DNLoc.CXXOperatorName.BeginOpNameLoc
5966 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5967 DNLoc.CXXOperatorName.EndOpNameLoc
5968 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5969 break;
5970
5971 case DeclarationName::CXXLiteralOperatorName:
5972 DNLoc.CXXLiteralOperatorName.OpNameLoc
5973 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5974 break;
5975
5976 case DeclarationName::Identifier:
5977 case DeclarationName::ObjCZeroArgSelector:
5978 case DeclarationName::ObjCOneArgSelector:
5979 case DeclarationName::ObjCMultiArgSelector:
5980 case DeclarationName::CXXUsingDirective:
5981 break;
5982 }
5983}
5984
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005985void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005986 DeclarationNameInfo &NameInfo,
5987 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005988 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005989 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5990 DeclarationNameLoc DNLoc;
5991 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5992 NameInfo.setInfo(DNLoc);
5993}
5994
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005995void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005996 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005997 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005998 unsigned NumTPLists = Record[Idx++];
5999 Info.NumTemplParamLists = NumTPLists;
6000 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006001 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006002 for (unsigned i=0; i != NumTPLists; ++i)
6003 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6004 }
6005}
6006
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006007TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006008ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006009 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006010 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006011 switch (Kind) {
6012 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006013 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006014
6015 case TemplateName::OverloadedTemplate: {
6016 unsigned size = Record[Idx++];
6017 UnresolvedSet<8> Decls;
6018 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006019 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006020
Douglas Gregor35942772011-09-09 21:34:22 +00006021 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006022 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006023
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006024 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006025 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006026 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006027 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006028 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006029 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006030
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006031 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006032 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006033 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006034 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006035 GetIdentifierInfo(F, Record,
6036 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006037 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006038 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006039 }
John McCall14606042011-06-30 08:33:18 +00006040
6041 case TemplateName::SubstTemplateTemplateParm: {
6042 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006043 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006044 if (!param) return TemplateName();
6045 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006046 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006047 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006048
6049 case TemplateName::SubstTemplateTemplateParmPack: {
6050 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006051 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006052 if (!Param)
6053 return TemplateName();
6054
6055 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6056 if (ArgPack.getKind() != TemplateArgument::Pack)
6057 return TemplateName();
6058
Douglas Gregor35942772011-09-09 21:34:22 +00006059 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006060 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006061 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006062
David Blaikieb219cfc2011-09-23 05:06:16 +00006063 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006064}
6065
6066TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006067ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006068 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006069 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6070 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006071 case TemplateArgument::Null:
6072 return TemplateArgument();
6073 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006074 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006075 case TemplateArgument::Declaration: {
6076 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6077 bool ForReferenceParam = Record[Idx++];
6078 return TemplateArgument(D, ForReferenceParam);
6079 }
6080 case TemplateArgument::NullPtr:
6081 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006082 case TemplateArgument::Integral: {
6083 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006084 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006085 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006086 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006087 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006088 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006089 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006090 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006091 llvm::Optional<unsigned> NumTemplateExpansions;
6092 if (unsigned NumExpansions = Record[Idx++])
6093 NumTemplateExpansions = NumExpansions - 1;
6094 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006095 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006096 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006097 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006098 case TemplateArgument::Pack: {
6099 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006100 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006101 for (unsigned I = 0; I != NumArgs; ++I)
6102 Args[I] = ReadTemplateArgument(F, Record, Idx);
6103 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006104 }
6105 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006106
David Blaikieb219cfc2011-09-23 05:06:16 +00006107 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006108}
6109
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006110TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006111ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006112 const RecordData &Record, unsigned &Idx) {
6113 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6114 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6115 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006116
6117 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006118 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006119 Params.reserve(NumParams);
6120 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006121 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006122
6123 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006124 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006125 Params.data(), Params.size(), RAngleLoc);
6126 return TemplateParams;
6127}
6128
6129void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006130ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006131ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006132 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006133 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006134 unsigned NumTemplateArgs = Record[Idx++];
6135 TemplArgs.reserve(NumTemplateArgs);
6136 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006137 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006138}
6139
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006140/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006141void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006142 const RecordData &Record, unsigned &Idx) {
6143 unsigned NumDecls = Record[Idx++];
6144 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006145 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006146 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6147 Set.addDecl(D, AS);
6148 }
6149}
6150
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006151CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006152ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006153 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006154 bool isVirtual = static_cast<bool>(Record[Idx++]);
6155 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6156 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006157 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006158 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6159 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006160 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006161 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006162 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006163 Result.setInheritConstructors(inheritConstructors);
6164 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006165}
6166
Sean Huntcbb67482011-01-08 20:30:50 +00006167std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006168ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006169 unsigned &Idx) {
6170 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006171 unsigned NumInitializers = Record[Idx++];
6172 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006173 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006174 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006175 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006176 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006177 bool IsBaseVirtual = false;
6178 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006179 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006180
Sean Hunt156b6402011-05-04 01:19:08 +00006181 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6182 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006183 case CTOR_INITIALIZER_BASE:
6184 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006185 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006186 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006187
6188 case CTOR_INITIALIZER_DELEGATING:
6189 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006190 break;
6191
6192 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006193 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006194 break;
6195
6196 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006197 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006198 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006199 }
Sean Hunt156b6402011-05-04 01:19:08 +00006200
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006201 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006202 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006203 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6204 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006205 bool IsWritten = Record[Idx++];
6206 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006207 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006208 if (IsWritten) {
6209 SourceOrderOrNumArrayIndices = Record[Idx++];
6210 } else {
6211 SourceOrderOrNumArrayIndices = Record[Idx++];
6212 Indices.reserve(SourceOrderOrNumArrayIndices);
6213 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006214 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006215 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006216
Sean Huntcbb67482011-01-08 20:30:50 +00006217 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006218 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006219 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006220 LParenLoc, Init, RParenLoc,
6221 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006222 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006223 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6224 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006225 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006226 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006227 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006228 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006229 else
Douglas Gregor35942772011-09-09 21:34:22 +00006230 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006231 MemberOrEllipsisLoc, LParenLoc,
6232 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006233 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006234 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006235 LParenLoc, Init, RParenLoc,
6236 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006237 }
6238
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006239 if (IsWritten)
6240 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006241 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006242 }
6243 }
6244
Sean Huntcbb67482011-01-08 20:30:50 +00006245 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006246}
6247
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006248NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006249ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006250 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006251 unsigned N = Record[Idx++];
6252 NestedNameSpecifier *NNS = 0, *Prev = 0;
6253 for (unsigned I = 0; I != N; ++I) {
6254 NestedNameSpecifier::SpecifierKind Kind
6255 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6256 switch (Kind) {
6257 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006258 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006259 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006260 break;
6261 }
6262
6263 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006264 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006265 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006266 break;
6267 }
6268
Douglas Gregor14aba762011-02-24 02:36:08 +00006269 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006270 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006271 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006272 break;
6273 }
6274
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006275 case NestedNameSpecifier::TypeSpec:
6276 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006277 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006278 if (!T)
6279 return 0;
6280
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006281 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006282 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006283 break;
6284 }
6285
6286 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006287 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006288 // No associated value, and there can't be a prefix.
6289 break;
6290 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006291 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006292 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006293 }
6294 return NNS;
6295}
6296
Douglas Gregordc355712011-02-25 00:36:19 +00006297NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006298ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006299 unsigned &Idx) {
6300 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006301 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006302 for (unsigned I = 0; I != N; ++I) {
6303 NestedNameSpecifier::SpecifierKind Kind
6304 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6305 switch (Kind) {
6306 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006307 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006308 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006309 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006310 break;
6311 }
6312
6313 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006314 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006315 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006316 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006317 break;
6318 }
6319
6320 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006321 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006322 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006323 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006324 break;
6325 }
6326
6327 case NestedNameSpecifier::TypeSpec:
6328 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006329 bool Template = Record[Idx++];
6330 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6331 if (!T)
6332 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006333 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006334
6335 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006336 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006337 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6338 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006339 break;
6340 }
6341
6342 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006343 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006344 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006345 break;
6346 }
6347 }
Douglas Gregordc355712011-02-25 00:36:19 +00006348 }
6349
Douglas Gregor35942772011-09-09 21:34:22 +00006350 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006351}
6352
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006353SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006354ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006355 unsigned &Idx) {
6356 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6357 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006358 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006359}
6360
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006361/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006362llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006363 unsigned BitWidth = Record[Idx++];
6364 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6365 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6366 Idx += NumWords;
6367 return Result;
6368}
6369
6370/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006371llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006372 bool isUnsigned = Record[Idx++];
6373 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6374}
6375
Douglas Gregor17fc2232009-04-14 21:55:33 +00006376/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006377llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006378 return llvm::APFloat(ReadAPInt(Record, Idx));
6379}
6380
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006381// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006382std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006383 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006384 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006385 Idx += Len;
6386 return Result;
6387}
6388
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006389VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6390 unsigned &Idx) {
6391 unsigned Major = Record[Idx++];
6392 unsigned Minor = Record[Idx++];
6393 unsigned Subminor = Record[Idx++];
6394 if (Minor == 0)
6395 return VersionTuple(Major);
6396 if (Subminor == 0)
6397 return VersionTuple(Major, Minor - 1);
6398 return VersionTuple(Major, Minor - 1, Subminor - 1);
6399}
6400
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006401CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006402 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006403 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006404 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006405 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006406}
6407
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006408DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006409 return Diag(SourceLocation(), DiagID);
6410}
6411
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006412DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006413 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006414}
Douglas Gregor025452f2009-04-17 00:04:06 +00006415
Douglas Gregor668c1a42009-04-21 22:25:48 +00006416/// \brief Retrieve the identifier table associated with the
6417/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006418IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006419 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006420}
6421
Douglas Gregor025452f2009-04-17 00:04:06 +00006422/// \brief Record that the given ID maps to the given switch-case
6423/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006424void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006425 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6426 "Already have a SwitchCase with this ID");
6427 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006428}
6429
6430/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006431SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006432 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6433 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006434}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006435
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006436void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006437 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006438}
6439
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006440void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006441 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006442 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6443 serialization::ModuleFile *> >::iterator
6444 I = CommentsCursors.begin(),
6445 E = CommentsCursors.end();
6446 I != E; ++I) {
6447 llvm::BitstreamCursor &Cursor = I->first;
6448 serialization::ModuleFile &F = *I->second;
6449 SavedStreamPosition SavedPosition(Cursor);
6450
6451 RecordData Record;
6452 while (true) {
6453 unsigned Code = Cursor.ReadCode();
6454 if (Code == llvm::bitc::END_BLOCK)
6455 break;
6456
6457 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6458 // No known subblocks, always skip them.
6459 Cursor.ReadSubBlockID();
6460 if (Cursor.SkipBlock()) {
6461 Error("malformed block record in AST file");
6462 return;
6463 }
6464 continue;
6465 }
6466
6467 if (Code == llvm::bitc::DEFINE_ABBREV) {
6468 Cursor.ReadAbbrevRecord();
6469 continue;
6470 }
6471
6472 // Read a record.
6473 Record.clear();
6474 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006475 case COMMENTS_RAW_COMMENT: {
6476 unsigned Idx = 0;
6477 SourceRange SR = ReadSourceRange(F, Record, Idx);
6478 RawComment::CommentKind Kind =
6479 (RawComment::CommentKind) Record[Idx++];
6480 bool IsTrailingComment = Record[Idx++];
6481 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006482 Comments.push_back(new (Context) RawComment(SR, Kind,
6483 IsTrailingComment,
6484 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006485 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006486 }
6487 }
6488 }
6489 }
6490 Context.Comments.addCommentsToFront(Comments);
6491}
6492
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006493void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006494 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6495 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006496 // If any identifiers with corresponding top-level declarations have
6497 // been loaded, load those declarations now.
6498 while (!PendingIdentifierInfos.empty()) {
6499 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6500 PendingIdentifierInfos.front().DeclIDs, true);
6501 PendingIdentifierInfos.pop_front();
6502 }
6503
Douglas Gregora1be2782011-12-17 23:38:30 +00006504 // Load pending declaration chains.
6505 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6506 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006507 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006508 }
6509 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006510
6511 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00006512 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6513 // FIXME: std::move here
6514 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
6515 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6516 ++IDIdx) {
6517 getMacro(GlobalIDs[IDIdx]);
6518 }
6519 }
6520 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006521 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006522
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006523 // If we deserialized any C++ or Objective-C class definitions, any
6524 // Objective-C protocol definitions, or any redeclarable templates, make sure
6525 // that all redeclarations point to the definitions. Note that this can only
6526 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006527 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6528 DEnd = PendingDefinitions.end();
6529 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006530 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6531 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6532 // Make sure that the TagType points at the definition.
6533 const_cast<TagType*>(TagT)->decl = TD;
6534 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006535
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006536 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6537 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6538 REnd = RD->redecls_end();
6539 R != REnd; ++R)
6540 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6541
6542 }
6543
Douglas Gregorfc529f72011-12-19 19:00:47 +00006544 continue;
6545 }
6546
Douglas Gregor1d784b22012-01-01 19:51:50 +00006547 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006548 // Make sure that the ObjCInterfaceType points at the definition.
6549 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6550 ->Decl = ID;
6551
Douglas Gregor1d784b22012-01-01 19:51:50 +00006552 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6553 REnd = ID->redecls_end();
6554 R != REnd; ++R)
6555 R->Data = ID->Data;
6556
6557 continue;
6558 }
6559
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006560 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6561 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6562 REnd = PD->redecls_end();
6563 R != REnd; ++R)
6564 R->Data = PD->Data;
6565
6566 continue;
6567 }
6568
6569 RedeclarableTemplateDecl *RTD
6570 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6571 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6572 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006573 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006574 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006575 }
6576 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006577
6578 // Load the bodies of any functions or methods we've encountered. We do
6579 // this now (delayed) so that we can be sure that the declaration chains
6580 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006581 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6582 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006583 PB != PBEnd; ++PB) {
6584 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6585 // FIXME: Check for =delete/=default?
6586 // FIXME: Complain about ODR violations here?
6587 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6588 FD->setLazyBody(PB->second);
6589 continue;
6590 }
6591
6592 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6593 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6594 MD->setLazyBody(PB->second);
6595 }
6596 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006597}
6598
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006599void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006600 assert(NumCurrentElementsDeserializing &&
6601 "FinishedDeserializing not paired with StartedDeserializing");
6602 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006603 // We decrease NumCurrentElementsDeserializing only after pending actions
6604 // are finished, to avoid recursively re-calling finishPendingActions().
6605 finishPendingActions();
6606 }
6607 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006608
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006609 if (NumCurrentElementsDeserializing == 0 &&
6610 Consumer && !PassingDeclsToConsumer) {
6611 // Guard variable to avoid recursively redoing the process of passing
6612 // decls to consumer.
6613 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6614 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006615
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006616 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006617 // We are not in recursive loading, so it's safe to pass the "interesting"
6618 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006619 Decl *D = InterestingDecls.front();
6620 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006621 PassInterestingDeclToConsumer(D);
6622 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006623 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006624}
Douglas Gregor501c1032010-08-19 00:28:17 +00006625
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006626ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006627 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006628 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006629 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6630 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006631 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006632 Consumer(0), ModuleMgr(PP.getFileManager()),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006633 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006634 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006635 DisableStatCache(DisableStatCache),
6636 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006637 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6638 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006639 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006640 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6641 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6642 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6643 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006644 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6645 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006646 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006647 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006648{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006649 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006650}
6651
Sebastian Redle1dde812010-08-24 00:50:04 +00006652ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006653 for (DeclContextVisibleUpdatesPending::iterator
6654 I = PendingVisibleUpdates.begin(),
6655 E = PendingVisibleUpdates.end();
6656 I != E; ++I) {
6657 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6658 F = I->second.end();
6659 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006660 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006661 }
6662}