blob: a62f2578e9be6e6d3d499f9cd13c67e25dbc6eaf [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 Gregor3ab50fe2012-10-11 17:41:54 +00001244void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset,
1245 MacroInfo *Hint) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001246 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Douglas Gregor37e26842009-04-21 23:56:24 +00001248 // Keep track of where we are in the stream, then jump back there
1249 // after reading this macro.
1250 SavedStreamPosition SavedPosition(Stream);
1251
1252 Stream.JumpToBit(Offset);
1253 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001254 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001255 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Douglas Gregore8219a62012-10-11 21:07:39 +00001257 // RAII object to add the loaded macro information once we're done
1258 // adding tokens.
1259 struct AddLoadedMacroInfoRAII {
1260 Preprocessor &PP;
1261 MacroInfo *Hint;
1262 MacroInfo *MI;
1263 IdentifierInfo *II;
1264
1265 AddLoadedMacroInfoRAII(Preprocessor &PP, MacroInfo *Hint)
1266 : PP(PP), Hint(Hint), MI(), II() { }
1267 ~AddLoadedMacroInfoRAII( ) {
1268 if (MI) {
1269 // Finally, install the macro.
1270 PP.addLoadedMacroInfo(II, MI, Hint);
1271 }
1272 }
1273 } AddLoadedMacroInfo(PP, Hint);
1274
Douglas Gregor37e26842009-04-21 23:56:24 +00001275 while (true) {
1276 unsigned Code = Stream.ReadCode();
1277 switch (Code) {
1278 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001279 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001280
1281 case llvm::bitc::ENTER_SUBBLOCK:
1282 // No known subblocks, always skip them.
1283 Stream.ReadSubBlockID();
1284 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001285 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001286 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001287 }
1288 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregor37e26842009-04-21 23:56:24 +00001290 case llvm::bitc::DEFINE_ABBREV:
1291 Stream.ReadAbbrevRecord();
1292 continue;
1293 default: break;
1294 }
1295
1296 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001297 const char *BlobStart = 0;
1298 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001299 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001300 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001301 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001302 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001303 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001304 case PP_MACRO_OBJECT_LIKE:
1305 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001306 // If we already have a macro, that means that we've hit the end
1307 // of the definition of the macro we were looking for. We're
1308 // done.
1309 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001310 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001311
Douglas Gregor95eab172011-07-28 20:55:49 +00001312 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001313 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001314 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001315 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001316 }
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Douglas Gregora8235d62012-10-09 23:05:51 +00001318 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1319
1320 // If this macro has already been loaded, don't do so again.
1321 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1322 return;
1323
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001324 SubmoduleID GlobalSubmoduleID = getGlobalSubmoduleID(F, Record[2]);
1325 unsigned NextIndex = 3;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001326 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001327 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001328
Douglas Gregora8235d62012-10-09 23:05:51 +00001329 // Record this macro.
1330 MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS] = MI;
1331
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001332 SourceLocation UndefLoc = ReadSourceLocation(F, Record, NextIndex);
1333 if (UndefLoc.isValid())
1334 MI->setUndefLoc(UndefLoc);
1335
1336 MI->setIsUsed(Record[NextIndex++]);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001337 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001339 bool IsPublic = Record[NextIndex++];
Douglas Gregoraa93a872011-10-17 15:32:29 +00001340 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001341
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001342 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001343 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001344 bool isC99VarArgs = Record[NextIndex++];
1345 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001346 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001347 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001348 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001349 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001350
1351 // Install function-like macro info.
1352 MI->setIsFunctionLike();
1353 if (isC99VarArgs) MI->setIsC99Varargs();
1354 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001355 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001356 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001357 }
1358
Douglas Gregora8235d62012-10-09 23:05:51 +00001359 if (DeserializationListener)
1360 DeserializationListener->MacroRead(GlobalID, MI);
1361
1362 // If an update record marked this as undefined, do so now.
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001363 // FIXME: Only if the submodule this update came from is visible?
Douglas Gregora8235d62012-10-09 23:05:51 +00001364 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1365 if (Update != MacroUpdates.end()) {
1366 if (MI->getUndefLoc().isInvalid()) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00001367 for (unsigned I = 0, N = Update->second.size(); I != N; ++I) {
1368 bool Hidden = false;
1369 if (unsigned SubmoduleID = Update->second[I].first) {
1370 if (Module *Owner = getSubmodule(SubmoduleID)) {
1371 if (Owner->NameVisibility == Module::Hidden) {
1372 // Note that this #undef is hidden.
1373 Hidden = true;
1374
1375 // Record this hiding for later.
1376 HiddenNamesMap[Owner].push_back(
1377 HiddenName(II, MI, Update->second[I].second.UndefLoc));
1378 }
1379 }
1380 }
1381
1382 if (!Hidden) {
1383 MI->setUndefLoc(Update->second[I].second.UndefLoc);
1384 if (PPMutationListener *Listener = PP.getPPMutationListener())
1385 Listener->UndefinedMacro(MI);
1386 break;
1387 }
1388 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001389 }
1390 MacroUpdates.erase(Update);
1391 }
1392
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001393 // Determine whether this macro definition is visible.
1394 bool Hidden = !MI->isPublic();
1395 if (!Hidden && GlobalSubmoduleID) {
1396 if (Module *Owner = getSubmodule(GlobalSubmoduleID)) {
1397 if (Owner->NameVisibility == Module::Hidden) {
1398 // The owning module is not visible, and this macro definition
1399 // should not be, either.
1400 Hidden = true;
1401
1402 // Note that this macro definition was hidden because its owning
1403 // module is not yet visible.
1404 HiddenNamesMap[Owner].push_back(HiddenName(II, MI));
1405 }
1406 }
1407 }
1408 MI->setHidden(Hidden);
1409
Douglas Gregore8219a62012-10-11 21:07:39 +00001410 // Make sure we install the macro once we're done.
1411 AddLoadedMacroInfo.MI = MI;
1412 AddLoadedMacroInfo.II = II;
Douglas Gregor37e26842009-04-21 23:56:24 +00001413
1414 // Remember that we saw this macro last so that we add the tokens that
1415 // form its body to it.
1416 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001417
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001418 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1419 Record[NextIndex]) {
1420 // We have a macro definition. Register the association
1421 PreprocessedEntityID
1422 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1423 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1424 PPRec.RegisterMacroDefinition(Macro,
1425 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001426 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001427
Douglas Gregor37e26842009-04-21 23:56:24 +00001428 ++NumMacrosRead;
1429 break;
1430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001432 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001433 // If we see a TOKEN before a PP_MACRO_*, then the file is
1434 // erroneous, just pretend we didn't see this.
1435 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregor37e26842009-04-21 23:56:24 +00001437 Token Tok;
1438 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001439 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001440 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001441 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001442 Tok.setIdentifierInfo(II);
1443 Tok.setKind((tok::TokenKind)Record[3]);
1444 Tok.setFlag((Token::TokenFlags)Record[4]);
1445 Macro->AddTokenToBody(Tok);
1446 break;
1447 }
David Blaikie7530c032012-01-17 06:56:22 +00001448 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001449 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001450}
1451
Douglas Gregor86c67d82011-07-28 22:39:26 +00001452PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001453ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001454 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001455 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1456 assert(I != M.PreprocessedEntityRemap.end()
1457 && "Invalid index into preprocessed entity index remap");
1458
1459 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001460}
1461
Douglas Gregor98339b92011-08-25 20:47:51 +00001462unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1463 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001464}
Douglas Gregor98339b92011-08-25 20:47:51 +00001465
1466HeaderFileInfoTrait::internal_key_type
1467HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1468
1469bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1470 if (strcmp(a, b) == 0)
1471 return true;
1472
1473 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1474 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001475
1476 // Determine whether the actual files are equivalent.
1477 bool Result = false;
1478 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001479 return false;
1480
Douglas Gregor99a922b2011-12-09 16:22:07 +00001481 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001482}
1483
1484std::pair<unsigned, unsigned>
1485HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1486 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1487 unsigned DataLen = (unsigned) *d++;
1488 return std::make_pair(KeyLen + 1, DataLen);
1489}
1490
1491HeaderFileInfoTrait::data_type
1492HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1493 unsigned DataLen) {
1494 const unsigned char *End = d + DataLen;
1495 using namespace clang::io;
1496 HeaderFileInfo HFI;
1497 unsigned Flags = *d++;
1498 HFI.isImport = (Flags >> 5) & 0x01;
1499 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1500 HFI.DirInfo = (Flags >> 2) & 0x03;
1501 HFI.Resolved = (Flags >> 1) & 0x01;
1502 HFI.IndexHeaderMapHeader = Flags & 0x01;
1503 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001504 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1505 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001506 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1507 // The framework offset is 1 greater than the actual offset,
1508 // since 0 is used as an indicator for "no framework name".
1509 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1510 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1511 }
1512
1513 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1514 (void)End;
1515
1516 // This HeaderFileInfo was externally loaded.
1517 HFI.External = true;
1518 return HFI;
1519}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001520
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001521void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ArrayRef<MacroID> IDs){
1522 II->setHadMacroDefinition(true);
1523 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1524 PendingMacroIDs[II].append(IDs.begin(), IDs.end());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001525}
1526
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001527void ASTReader::ReadDefinedMacros() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001528 // Note that we are loading defined macros.
1529 Deserializing Macros(this);
1530
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001531 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1532 E = ModuleMgr.rend(); I != E; ++I) {
1533 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001534
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001535 // If there was no preprocessor block, skip this file.
1536 if (!MacroCursor.getBitStreamReader())
1537 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001538
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001539 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001540 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001541
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001542 RecordData Record;
1543 while (true) {
1544 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001545 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001546 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001547
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001548 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1549 // No known subblocks, always skip them.
1550 Cursor.ReadSubBlockID();
1551 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001552 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001553 return;
1554 }
1555 continue;
1556 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001557
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001558 if (Code == llvm::bitc::DEFINE_ABBREV) {
1559 Cursor.ReadAbbrevRecord();
1560 continue;
1561 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001562
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001563 // Read a record.
1564 const char *BlobStart;
1565 unsigned BlobLen;
1566 Record.clear();
1567 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1568 default: // Default behavior: ignore.
1569 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001570
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001571 case PP_MACRO_OBJECT_LIKE:
1572 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001573 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001574 break;
1575
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001576 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001577 // Ignore tokens.
1578 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001579 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001580 }
1581 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001582}
1583
Douglas Gregoreee242f2011-10-27 09:33:13 +00001584namespace {
1585 /// \brief Visitor class used to look up identifirs in an AST file.
1586 class IdentifierLookupVisitor {
1587 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001588 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001589 IdentifierInfo *Found;
1590 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001591 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1592 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001593
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001594 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001595 IdentifierLookupVisitor *This
1596 = static_cast<IdentifierLookupVisitor *>(UserData);
1597
Douglas Gregor057df202012-01-18 20:56:22 +00001598 // If we've already searched this module file, skip it now.
1599 if (M.Generation <= This->PriorGeneration)
1600 return true;
1601
Douglas Gregoreee242f2011-10-27 09:33:13 +00001602 ASTIdentifierLookupTable *IdTable
1603 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1604 if (!IdTable)
1605 return false;
1606
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001607 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1608 M, This->Found);
1609
Douglas Gregoreee242f2011-10-27 09:33:13 +00001610 std::pair<const char*, unsigned> Key(This->Name.begin(),
1611 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001612 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001613 if (Pos == IdTable->end())
1614 return false;
1615
1616 // Dereferencing the iterator has the effect of building the
1617 // IdentifierInfo node and populating it with the various
1618 // declarations it needs.
1619 This->Found = *Pos;
1620 return true;
1621 }
1622
1623 // \brief Retrieve the identifier info found within the module
1624 // files.
1625 IdentifierInfo *getIdentifierInfo() const { return Found; }
1626 };
1627}
1628
1629void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001630 // Note that we are loading an identifier.
1631 Deserializing AnIdentifier(this);
1632
Douglas Gregor057df202012-01-18 20:56:22 +00001633 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001634 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001635 PriorGeneration = IdentifierGeneration[&II];
1636
1637 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1638 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1639 markIdentifierUpToDate(&II);
1640}
1641
1642void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1643 if (!II)
1644 return;
1645
1646 II->setOutOfDate(false);
1647
1648 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001649 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001650 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001651}
1652
Chris Lattner5f9e2722011-07-23 10:55:15 +00001653const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001654 std::string Filename = filenameStrRef;
1655 MaybeAddSystemRootToFilename(Filename);
1656 const FileEntry *File = FileMgr.getFile(Filename);
1657 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1658 OriginalDir != CurrentDir) {
1659 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1660 OriginalDir,
1661 CurrentDir);
1662 if (!resolved.empty())
1663 File = FileMgr.getFile(resolved);
1664 }
1665
1666 return File;
1667}
1668
Douglas Gregore650c8c2009-07-07 00:12:59 +00001669/// \brief If we are loading a relocatable PCH file, and the filename is
1670/// not an absolute path, add the system root to the beginning of the file
1671/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001672void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001673 // If this is not a relocatable PCH file, there's nothing to do.
1674 if (!RelocatablePCH)
1675 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Michael J. Spencer256053b2010-12-17 21:22:22 +00001677 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001678 return;
1679
Douglas Gregor832d6202011-07-22 16:35:34 +00001680 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001681 // If no system root was given, default to '/'
1682 Filename.insert(Filename.begin(), '/');
1683 return;
1684 }
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Douglas Gregor832d6202011-07-22 16:35:34 +00001686 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001687 if (isysroot[Length - 1] != '/')
1688 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001689
Douglas Gregor832d6202011-07-22 16:35:34 +00001690 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001691}
1692
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001693ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001694ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001695 llvm::BitstreamCursor &Stream = F.Stream;
1696
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001697 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001698 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001699 return Failure;
1700 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001701
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001702 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001703 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001704 while (!Stream.AtEndOfStream()) {
1705 unsigned Code = Stream.ReadCode();
1706 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001707 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001708 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001709 return Failure;
1710 }
Chris Lattner7356a312009-04-11 21:15:38 +00001711
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001712 DeclContext *DC = Context.getTranslationUnitDecl();
1713 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1714 DC->setMustBuildLookupTable();
1715
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001716 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001717 }
1718
1719 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1720 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001721 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001722 // We lazily load the decls block, but we want to set up the
1723 // DeclsCursor cursor to point into it. Clone our current bitcode
1724 // cursor to it, enter the block and read the abbrevs in that block.
1725 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001726 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001727 if (Stream.SkipBlock() || // Skip with the main cursor.
1728 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001729 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001730 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001731 return Failure;
1732 }
1733 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001735 case DECL_UPDATES_BLOCK_ID:
1736 if (Stream.SkipBlock()) {
1737 Error("malformed block record in AST file");
1738 return Failure;
1739 }
1740 break;
1741
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001742 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001743 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001744 if (!PP.getExternalSource())
1745 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001746
Douglas Gregorecdcb882010-10-20 22:00:55 +00001747 if (Stream.SkipBlock() ||
1748 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001749 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001750 return Failure;
1751 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001752 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001753 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001754
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001755 case PREPROCESSOR_DETAIL_BLOCK_ID:
1756 F.PreprocessorDetailCursor = Stream;
1757 if (Stream.SkipBlock() ||
1758 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1759 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1760 Error("malformed preprocessor detail record in AST file");
1761 return Failure;
1762 }
1763 F.PreprocessorDetailStartOffset
1764 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001765
1766 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001767 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001768 if (!PP.getPreprocessingRecord()->getExternalSource())
1769 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001770 break;
1771
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001772 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001773 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001774 case Success:
1775 break;
1776
1777 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001778 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001779 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001780
1781 case IgnorePCH:
1782 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001783 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001784 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001785
1786 case SUBMODULE_BLOCK_ID:
1787 switch (ReadSubmoduleBlock(F)) {
1788 case Success:
1789 break;
1790
1791 case Failure:
1792 Error("malformed submodule block in AST file");
1793 return Failure;
1794
1795 case IgnorePCH:
1796 return IgnorePCH;
1797 }
1798 break;
1799
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001800 case COMMENTS_BLOCK_ID: {
1801 llvm::BitstreamCursor C = Stream;
1802 if (Stream.SkipBlock() ||
1803 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1804 Error("malformed comments block in AST file");
1805 return Failure;
1806 }
1807 CommentsCursors.push_back(std::make_pair(C, &F));
1808 break;
1809 }
1810
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001811 default:
1812 if (!Stream.SkipBlock())
1813 break;
1814 Error("malformed block record in AST file");
1815 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001816 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001817 continue;
1818 }
1819
1820 if (Code == llvm::bitc::DEFINE_ABBREV) {
1821 Stream.ReadAbbrevRecord();
1822 continue;
1823 }
1824
1825 // Read and process a record.
1826 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001827 const char *BlobStart = 0;
1828 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001829 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001830 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001831 default: // Default behavior: ignore.
1832 break;
1833
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001834 case METADATA: {
1835 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1836 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001837 : diag::warn_pch_version_too_new);
1838 return IgnorePCH;
1839 }
1840
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001841 bool hasErrors = Record[5];
1842 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1843 Diag(diag::err_pch_with_compiler_errors);
1844 return IgnorePCH;
1845 }
1846
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001847 RelocatablePCH = Record[4];
1848 if (Listener) {
1849 std::string TargetTriple(BlobStart, BlobLen);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001850 if (Listener->ReadTargetTriple(F, TargetTriple))
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001851 return IgnorePCH;
1852 }
1853 break;
1854 }
1855
Douglas Gregore95b9192011-08-17 21:07:30 +00001856 case IMPORTS: {
1857 // Load each of the imported PCH files.
1858 unsigned Idx = 0, N = Record.size();
1859 while (Idx < N) {
1860 // Read information about the AST file.
1861 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1862 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001863 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001864 Record.begin() + Idx + Length);
1865 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001866
Douglas Gregore95b9192011-08-17 21:07:30 +00001867 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001868 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001869 case Failure: return Failure;
1870 // If we have to ignore the dependency, we'll have to ignore this too.
1871 case IgnorePCH: return IgnorePCH;
1872 case Success: break;
1873 }
1874 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001875 break;
1876 }
1877
Douglas Gregora119da02011-08-02 16:26:37 +00001878 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001879 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001880 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001881 return Failure;
1882 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001883 F.TypeOffsets = (const uint32_t *)BlobStart;
1884 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001885 unsigned LocalBaseTypeIndex = Record[1];
1886 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001887
Douglas Gregora119da02011-08-02 16:26:37 +00001888 if (F.LocalNumTypes > 0) {
1889 // Introduce the global -> local mapping for types within this module.
1890 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1891
1892 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001893 F.TypeRemap.insertOrReplace(
1894 std::make_pair(LocalBaseTypeIndex,
1895 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001896
1897 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1898 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001899 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001900 }
1901
Douglas Gregor496c7092011-08-03 15:48:04 +00001902 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001903 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001904 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001905 return Failure;
1906 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001907 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001908 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001909 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001910 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001911
Douglas Gregor496c7092011-08-03 15:48:04 +00001912 if (F.LocalNumDecls > 0) {
1913 // Introduce the global -> local mapping for declarations within this
1914 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001915 GlobalDeclMap.insert(
1916 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001917
1918 // Introduce the local -> global mapping for declarations within this
1919 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001920 F.DeclRemap.insertOrReplace(
1921 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001922
Douglas Gregora1be2782011-12-17 23:38:30 +00001923 // Introduce the global -> local mapping for declarations within this
1924 // module.
1925 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1926
Douglas Gregor496c7092011-08-03 15:48:04 +00001927 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1928 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001929 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001930 }
1931
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001932 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001933 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001934 DeclContextInfo &Info = F.DeclContextInfos[TU];
1935 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1936 Info.NumLexicalDecls
1937 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001938 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001939 break;
1940 }
1941
Sebastian Redle1dde812010-08-24 00:50:04 +00001942 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001943 unsigned Idx = 0;
1944 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001945 ASTDeclContextNameLookupTable *Table =
1946 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001947 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001948 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001949 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001950 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1951 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001952 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001953 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001954 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001955 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001956 break;
1957 }
1958
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001959 case LANGUAGE_OPTIONS:
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001960 if (ParseLanguageOptions(F, Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001961 return IgnorePCH;
1962 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001963
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001964 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001965 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001966 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001967 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001968 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001969 (const unsigned char *)F.IdentifierTableData + Record[0],
1970 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001971 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001972
1973 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001974 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001975 break;
1976
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001977 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001978 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001979 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001980 return Failure;
1981 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001982 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1983 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001984 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001985 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001986
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001987 if (F.LocalNumIdentifiers > 0) {
1988 // Introduce the global -> local mapping for identifiers within this
1989 // module.
1990 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1991 &F));
1992
1993 // Introduce the local -> global mapping for identifiers within this
1994 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001995 F.IdentifierRemap.insertOrReplace(
1996 std::make_pair(LocalBaseIdentifierID,
1997 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001998
1999 IdentifiersLoaded.resize(IdentifiersLoaded.size()
2000 + F.LocalNumIdentifiers);
2001 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002002 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002003 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002004
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002005 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002006 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2007 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00002008 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00002009
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002010 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00002011 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2012 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00002013 break;
2014
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002015 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002016 TotalNumStatements += Record[0];
2017 TotalNumMacros += Record[1];
2018 TotalLexicalDeclContexts += Record[2];
2019 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002020 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002021
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002022 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002023 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2024 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002025 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002026
Sean Huntebcbe1d2011-05-04 23:29:54 +00002027 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002028 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2029 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002030 break;
2031
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002032 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002033 if (Record.size() % 4 != 0) {
2034 Error("invalid weak identifiers record");
2035 return Failure;
2036 }
2037
2038 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2039 // files. This isn't the way to do it :)
2040 WeakUndeclaredIdentifiers.clear();
2041
2042 // Translate the weak, undeclared identifiers into global IDs.
2043 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2044 WeakUndeclaredIdentifiers.push_back(
2045 getGlobalIdentifierID(F, Record[I++]));
2046 WeakUndeclaredIdentifiers.push_back(
2047 getGlobalIdentifierID(F, Record[I++]));
2048 WeakUndeclaredIdentifiers.push_back(
2049 ReadSourceLocation(F, Record, I).getRawEncoding());
2050 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2051 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002052 break;
2053
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002054 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002055 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2056 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002057 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002058
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002059 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002060 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002061 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002062 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002063 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002064
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002065 if (F.LocalNumSelectors > 0) {
2066 // Introduce the global -> local mapping for selectors within this
2067 // module.
2068 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2069
2070 // Introduce the local -> global mapping for selectors within this
2071 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002072 F.SelectorRemap.insertOrReplace(
2073 std::make_pair(LocalBaseSelectorID,
2074 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002075
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002076 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2077 }
2078 break;
2079 }
2080
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002081 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002082 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002083 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002084 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002085 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002086 F.SelectorLookupTableData + Record[0],
2087 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002088 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002089 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002090 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002091
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002092 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002093 if (!Record.empty()) {
2094 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2095 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2096 Record[Idx++]));
2097 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2098 getRawEncoding());
2099 }
2100 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002101 break;
2102
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002103 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002104 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002105 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002106 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002107
2108 case FILE_SORTED_DECLS:
2109 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002110 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002111 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002112
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002113 case SOURCE_LOCATION_OFFSETS: {
2114 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002115 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002116 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002117 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002118 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2119 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002120 // Make our entry in the range map. BaseID is negative and growing, so
2121 // we invert it. Because we invert it, though, we need the other end of
2122 // the range.
2123 unsigned RangeStart =
2124 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2125 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2126 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2127
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002128 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2129 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2130 GlobalSLocOffsetMap.insert(
2131 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2132 - SLocSpaceSize,&F));
2133
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002134 // Initialize the remapping table.
2135 // Invalid stays invalid.
2136 F.SLocRemap.insert(std::make_pair(0U, 0));
2137 // This module. Base was 2 when being compiled.
2138 F.SLocRemap.insert(std::make_pair(2U,
2139 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002140
2141 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002142 break;
2143 }
2144
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002145 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002146 // Additional remapping information.
2147 const unsigned char *Data = (const unsigned char*)BlobStart;
2148 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002149
2150 // Continuous range maps we may be updating in our module.
2151 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002152 ContinuousRangeMap<uint32_t, int, 2>::Builder
2153 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002154 ContinuousRangeMap<uint32_t, int, 2>::Builder
2155 MacroRemap(F.MacroRemap);
2156 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002157 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2158 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002159 SubmoduleRemap(F.SubmoduleRemap);
2160 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002161 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002162 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002163 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2164
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002165 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002166 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002167 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002168 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002169 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002170 if (!OM) {
2171 Error("SourceLocation remap refers to unknown module");
2172 return Failure;
2173 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002174
2175 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2176 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002177 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002178 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002179 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002180 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2181 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002182 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002183
2184 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2185 SLocRemap.insert(std::make_pair(SLocOffset,
2186 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002187 IdentifierRemap.insert(
2188 std::make_pair(IdentifierIDOffset,
2189 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002190 MacroRemap.insert(std::make_pair(MacroIDOffset,
2191 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002192 PreprocessedEntityRemap.insert(
2193 std::make_pair(PreprocessedEntityIDOffset,
2194 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002195 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2196 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002197 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2198 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002199 DeclRemap.insert(std::make_pair(DeclIDOffset,
2200 OM->BaseDeclID - DeclIDOffset));
2201
Douglas Gregora119da02011-08-02 16:26:37 +00002202 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002203 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002204
2205 // Global -> local mappings.
2206 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002207 }
2208 break;
2209 }
2210
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002211 case SOURCE_MANAGER_LINE_TABLE:
2212 if (ParseLineTable(F, Record))
2213 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002214 break;
2215
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002216 case FILE_SOURCE_LOCATION_OFFSETS:
2217 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2218 F.LocalNumSLocFileEntries = Record[0];
2219 break;
2220
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002221 case SOURCE_LOCATION_PRELOADS: {
2222 // Need to transform from the local view (1-based IDs) to the global view,
2223 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002224 if (!F.PreloadSLocEntries.empty()) {
2225 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2226 return Failure;
2227 }
2228
2229 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002230 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002231 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002232
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002233 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002234 if (!DisableStatCache) {
2235 ASTStatCache *MyStatCache =
2236 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2237 (const unsigned char *)BlobStart,
2238 NumStatHits, NumStatMisses);
2239 FileMgr.addStatCache(MyStatCache);
2240 F.StatCache = MyStatCache;
2241 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002242 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002243 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002245 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002246 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2247 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002248 break;
2249
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002250 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002251 if (Record.size() % 3 != 0) {
2252 Error("Invalid VTABLE_USES record");
2253 return Failure;
2254 }
2255
Sebastian Redl40566802010-08-05 18:21:25 +00002256 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002257 // FIXME: Modules will have some trouble with this. This is clearly not
2258 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002259 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002260
2261 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2262 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2263 VTableUses.push_back(
2264 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2265 VTableUses.push_back(Record[Idx++]);
2266 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002267 break;
2268
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002269 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002270 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2271 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002272 break;
2273
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002274 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002275 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002276 Error("Invalid existing PendingInstantiations");
2277 return Failure;
2278 }
2279
2280 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002281 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2282 return Failure;
2283 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002284
Douglas Gregorf2abb522011-07-28 19:26:52 +00002285 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2286 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2287 PendingInstantiations.push_back(
2288 ReadSourceLocation(F, Record, I).getRawEncoding());
2289 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002290 break;
2291
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002292 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002293 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002294 // FIXME: Modules will have some trouble with this.
2295 SemaDeclRefs.clear();
2296 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2297 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002298 break;
2299
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002300 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002301 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002302 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002303 ActualOriginalFileName.assign(BlobStart, BlobLen);
2304 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002305 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002306 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Douglas Gregor31d375f2011-05-06 21:43:30 +00002308 case ORIGINAL_FILE_ID:
2309 OriginalFileID = FileID::get(Record[0]);
2310 break;
2311
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002312 case ORIGINAL_PCH_DIR:
2313 // The primary AST will be the last to get here, so it will be the one
2314 // that's used.
2315 OriginalDir.assign(BlobStart, BlobLen);
2316 break;
2317
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002318 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002319 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002320 StringRef ASTBranch(BlobStart, BlobLen);
2321 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002322 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002323 return IgnorePCH;
2324 }
2325 break;
2326 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002327
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002328 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002329 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2330 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2331 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002332
2333 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002334
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002335 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002336 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002337 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002338 if (!PP.getPreprocessingRecord()->getExternalSource())
2339 PP.getPreprocessingRecord()->SetExternalSource(*this);
2340 StartingID
2341 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002342 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002343 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002344
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002345 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002346 // Introduce the global -> local mapping for preprocessed entities in
2347 // this module.
2348 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2349
2350 // Introduce the local -> global mapping for preprocessed entities in
2351 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002352 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002353 std::make_pair(LocalBasePreprocessedEntityID,
2354 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2355 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002356
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002357 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002358 }
2359
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002360 case DECL_UPDATE_OFFSETS: {
2361 if (Record.size() % 2 != 0) {
2362 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2363 return Failure;
2364 }
2365 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002366 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2367 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002368 break;
2369 }
2370
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002371 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002372 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002373 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002374 return Failure;
2375 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002376 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002377 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002378 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002379 break;
2380 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002381
Douglas Gregorcff9f262012-01-27 01:47:08 +00002382 case OBJC_CATEGORIES_MAP: {
2383 if (F.LocalNumObjCCategoriesInMap != 0) {
2384 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002385 return Failure;
2386 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002387
2388 F.LocalNumObjCCategoriesInMap = Record[0];
2389 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002390 break;
2391 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002392
Douglas Gregorcff9f262012-01-27 01:47:08 +00002393 case OBJC_CATEGORIES:
2394 F.ObjCCategories.swap(Record);
2395 break;
2396
Douglas Gregor7c789c12010-10-29 22:39:52 +00002397 case CXX_BASE_SPECIFIER_OFFSETS: {
2398 if (F.LocalNumCXXBaseSpecifiers != 0) {
2399 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2400 return Failure;
2401 }
2402
2403 F.LocalNumCXXBaseSpecifiers = Record[0];
2404 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002405 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002406 break;
2407 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002408
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002409 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002410 if (Record.size() % 2 != 0) {
2411 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2412 return Failure;
2413 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002414
2415 if (F.PragmaDiagMappings.empty())
2416 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002417 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002418 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2419 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002420 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002421
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002422 case CUDA_SPECIAL_DECL_REFS:
2423 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002424 // FIXME: Modules will have trouble with this.
2425 CUDASpecialDeclRefs.clear();
2426 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2427 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002428 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002429
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002430 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002431 F.HeaderFileInfoTableData = BlobStart;
2432 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002433 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002434 if (Record[0]) {
2435 F.HeaderFileInfoTable
2436 = HeaderFileInfoLookupTable::Create(
2437 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002438 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002439 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002440 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002441 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002442
2443 PP.getHeaderSearchInfo().SetExternalSource(this);
2444 if (!PP.getHeaderSearchInfo().getExternalLookup())
2445 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002446 }
2447 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002448 }
2449
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002450 case FP_PRAGMA_OPTIONS:
2451 // Later tables overwrite earlier ones.
2452 FPPragmaOptions.swap(Record);
2453 break;
2454
2455 case OPENCL_EXTENSIONS:
2456 // Later tables overwrite earlier ones.
2457 OpenCLExtensions.swap(Record);
2458 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002459
2460 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002461 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2462 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002463 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002464
2465 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002466 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2467 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002468 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002469
2470 case IMPORTED_MODULES: {
2471 if (F.Kind != MK_Module) {
2472 // If we aren't loading a module (which has its own exports), make
2473 // all of the imported modules visible.
2474 // FIXME: Deal with macros-only imports.
2475 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2476 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2477 ImportedModules.push_back(GlobalID);
2478 }
2479 }
2480 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002481 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002482
Douglas Gregora1be2782011-12-17 23:38:30 +00002483 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002484 F.RedeclarationChains.swap(Record);
2485 break;
2486 }
2487
2488 case LOCAL_REDECLARATIONS_MAP: {
2489 if (F.LocalNumRedeclarationsInMap != 0) {
2490 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002491 return Failure;
2492 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002493
Douglas Gregor2171bf12012-01-15 16:58:34 +00002494 F.LocalNumRedeclarationsInMap = Record[0];
2495 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002496 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002497 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002498
2499 case MERGED_DECLARATIONS: {
2500 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2501 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2502 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2503 for (unsigned N = Record[Idx++]; N > 0; --N)
2504 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2505 }
2506 break;
2507 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002508
2509 case MACRO_OFFSET: {
2510 if (F.LocalNumMacros != 0) {
2511 Error("duplicate MACRO_OFFSET record in AST file");
2512 return Failure;
2513 }
2514 F.MacroOffsets = (const uint32_t *)BlobStart;
2515 F.LocalNumMacros = Record[0];
2516 unsigned LocalBaseMacroID = Record[1];
2517 F.BaseMacroID = getTotalNumMacros();
2518
2519 if (F.LocalNumMacros > 0) {
2520 // Introduce the global -> local mapping for macros within this module.
2521 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2522
2523 // Introduce the local -> global mapping for macros within this module.
2524 F.MacroRemap.insertOrReplace(
2525 std::make_pair(LocalBaseMacroID,
2526 F.BaseMacroID - LocalBaseMacroID));
2527
2528 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2529 }
2530 break;
2531 }
2532
2533 case MACRO_UPDATES: {
2534 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2535 MacroID ID = getGlobalMacroID(F, Record[I++]);
2536 if (I == N)
2537 break;
2538
Douglas Gregor54c8a402012-10-12 00:16:50 +00002539 SourceLocation UndefLoc = ReadSourceLocation(F, Record, I);
2540 SubmoduleID SubmoduleID = getGlobalSubmoduleID(F, Record[I++]);;
2541 MacroUpdate Update;
2542 Update.UndefLoc = UndefLoc;
2543 MacroUpdates[ID].push_back(std::make_pair(SubmoduleID, Update));
Douglas Gregora8235d62012-10-09 23:05:51 +00002544 }
2545 break;
2546 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002547 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002548 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002549 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002550 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551}
2552
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002553ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002554 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002555
Douglas Gregorc69a2922011-08-25 20:58:51 +00002556 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2557 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2558 unsigned Code = SLocEntryCursor.ReadCode();
2559 if (Code == llvm::bitc::END_BLOCK ||
2560 Code == llvm::bitc::ENTER_SUBBLOCK ||
2561 Code == llvm::bitc::DEFINE_ABBREV) {
2562 Error("incorrectly-formatted source location entry in AST file");
2563 return Failure;
2564 }
2565
2566 RecordData Record;
2567 const char *BlobStart;
2568 unsigned BlobLen;
2569 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2570 default:
2571 Error("incorrectly-formatted source location entry in AST file");
2572 return Failure;
2573
2574 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002575 // If the buffer was overridden, the file need not exist.
2576 if (Record[6])
2577 break;
2578
Douglas Gregorc69a2922011-08-25 20:58:51 +00002579 StringRef Filename(BlobStart, BlobLen);
2580 const FileEntry *File = getFileEntry(Filename);
2581
2582 if (File == 0) {
2583 std::string ErrorStr = "could not find file '";
2584 ErrorStr += Filename;
2585 ErrorStr += "' referenced by AST file";
2586 Error(ErrorStr.c_str());
2587 return IgnorePCH;
2588 }
2589
Douglas Gregora081da52011-11-16 20:05:18 +00002590 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002591 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002592 return Failure;
2593 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002594
2595 off_t StoredSize = (off_t)Record[4];
2596 time_t StoredTime = (time_t)Record[5];
2597
2598 // Check if there was a request to override the contents of the file
2599 // that was part of the precompiled header. Overridding such a file
2600 // can lead to problems when lexing using the source locations from the
2601 // PCH.
2602 SourceManager &SM = getSourceManager();
2603 if (SM.isFileOverridden(File)) {
2604 Error(diag::err_fe_pch_file_overridden, Filename);
2605 // After emitting the diagnostic, recover by disabling the override so
2606 // that the original file will be used.
2607 SM.disableFileContentsOverride(File);
2608 // The FileEntry is a virtual file entry with the size of the contents
2609 // that would override the original contents. Set it to the original's
2610 // size/time.
2611 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2612 StoredSize, StoredTime);
2613 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002614
Douglas Gregorc69a2922011-08-25 20:58:51 +00002615 // The stat info from the FileEntry came from the cached stat
2616 // info of the PCH, so we cannot trust it.
2617 struct stat StatBuf;
2618 if (::stat(File->getName(), &StatBuf) != 0) {
2619 StatBuf.st_size = File->getSize();
2620 StatBuf.st_mtime = File->getModificationTime();
2621 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002622
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002623 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002624#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002625 // In our regression testing, the Windows file system seems to
2626 // have inconsistent modification times that sometimes
2627 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002628 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002629#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002630 )) {
2631 Error(diag::err_fe_pch_file_modified, Filename);
2632 return IgnorePCH;
2633 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002634
Douglas Gregorc69a2922011-08-25 20:58:51 +00002635 break;
2636 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002637 }
2638 }
2639
2640 return Success;
2641}
2642
Douglas Gregorecc2c092011-12-01 22:20:10 +00002643void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002644 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
Douglas Gregor54c8a402012-10-12 00:16:50 +00002645 switch (Names[I].getKind()) {
2646 case HiddenName::Declaration:
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002647 Names[I].getDecl()->Hidden = false;
Douglas Gregor54c8a402012-10-12 00:16:50 +00002648 break;
2649
2650 case HiddenName::MacroVisibility: {
2651 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2652 Macro.second->setHidden(!Macro.second->isPublic());
2653 if (Macro.second->isDefined()) {
2654 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2655 }
2656 break;
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002657 }
2658
Douglas Gregor54c8a402012-10-12 00:16:50 +00002659 case HiddenName::MacroUndef: {
2660 std::pair<IdentifierInfo *, MacroInfo *> Macro = Names[I].getMacro();
2661 if (Macro.second->isDefined()) {
2662 Macro.second->setUndefLoc(Names[I].getMacroUndefLoc());
2663 if (PPMutationListener *Listener = PP.getPPMutationListener())
2664 Listener->UndefinedMacro(Macro.second);
2665 PP.makeLoadedMacroInfoVisible(Macro.first, Macro.second);
2666 }
2667 break;
2668 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002669 }
Douglas Gregor13292642011-12-02 15:45:10 +00002670 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002671}
2672
Douglas Gregor5e356932011-12-01 17:11:21 +00002673void ASTReader::makeModuleVisible(Module *Mod,
2674 Module::NameVisibilityKind NameVisibility) {
2675 llvm::SmallPtrSet<Module *, 4> Visited;
2676 llvm::SmallVector<Module *, 4> Stack;
2677 Stack.push_back(Mod);
2678 while (!Stack.empty()) {
2679 Mod = Stack.back();
2680 Stack.pop_back();
2681
2682 if (NameVisibility <= Mod->NameVisibility) {
2683 // This module already has this level of visibility (or greater), so
2684 // there is nothing more to do.
2685 continue;
2686 }
2687
Douglas Gregor51f564f2011-12-31 04:05:44 +00002688 if (!Mod->isAvailable()) {
2689 // Modules that aren't available cannot be made visible.
2690 continue;
2691 }
2692
Douglas Gregor5e356932011-12-01 17:11:21 +00002693 // Update the module's name visibility.
2694 Mod->NameVisibility = NameVisibility;
2695
Douglas Gregorecc2c092011-12-01 22:20:10 +00002696 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002697 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002698 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2699 if (Hidden != HiddenNamesMap.end()) {
2700 makeNamesVisible(Hidden->second);
2701 HiddenNamesMap.erase(Hidden);
2702 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002703
2704 // Push any non-explicit submodules onto the stack to be marked as
2705 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002706 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2707 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002708 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002709 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2710 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002711 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002712
2713 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002714 bool AnyWildcard = false;
2715 bool UnrestrictedWildcard = false;
2716 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002717 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2718 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002719 if (!Mod->Exports[I].getInt()) {
2720 // Export a named module directly; no wildcards involved.
2721 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002722 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002723
2724 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002725 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002726
2727 // Wildcard export: export all of the imported modules that match
2728 // the given pattern.
2729 AnyWildcard = true;
2730 if (UnrestrictedWildcard)
2731 continue;
2732
2733 if (Module *Restriction = Mod->Exports[I].getPointer())
2734 WildcardRestrictions.push_back(Restriction);
2735 else {
2736 WildcardRestrictions.clear();
2737 UnrestrictedWildcard = true;
2738 }
2739 }
2740
2741 // If there were any wildcards, push any imported modules that were
2742 // re-exported by the wildcard restriction.
2743 if (!AnyWildcard)
2744 continue;
2745
2746 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2747 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002748 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002749 continue;
2750
2751 bool Acceptable = UnrestrictedWildcard;
2752 if (!Acceptable) {
2753 // Check whether this module meets one of the restrictions.
2754 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2755 Module *Restriction = WildcardRestrictions[R];
2756 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2757 Acceptable = true;
2758 break;
2759 }
2760 }
2761 }
2762
2763 if (!Acceptable)
2764 continue;
2765
Douglas Gregor0adaa882011-12-05 17:28:06 +00002766 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002767 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002768 }
2769}
2770
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002771ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002772 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002773 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002774 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002775
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002776 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002777 case Failure: return Failure;
2778 case IgnorePCH: return IgnorePCH;
2779 case Success: break;
2780 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002781
2782 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002783
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002784 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002785 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002786 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2787 // if DisableValidation is true, defines that were set on command-line
2788 // but not in the PCH file will not be added to SuggestedPredefines.
2789 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002790 return IgnorePCH;
2791
Douglas Gregoreee242f2011-10-27 09:33:13 +00002792 // Mark all of the identifiers in the identifier table as being out of date,
2793 // so that various accessors know to check the loaded modules when the
2794 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002795 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2796 IdEnd = PP.getIdentifierTable().end();
2797 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002798 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002799
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002800 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002801 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2802 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2803 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002804 Module *ResolvedMod = getSubmodule(GlobalID);
2805
2806 if (Unresolved.IsImport) {
2807 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002808 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002809 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002810 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002811
2812 if (ResolvedMod || Unresolved.IsWildcard)
2813 Unresolved.Mod->Exports.push_back(
2814 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002815 }
Douglas Gregor55988682011-12-05 16:33:54 +00002816 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002817
Douglas Gregor35942772011-09-09 21:34:22 +00002818 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002819
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002820 if (DeserializationListener)
2821 DeserializationListener->ReaderInitialized(this);
2822
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002823 if (!OriginalFileID.isInvalid()) {
2824 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2825 + OriginalFileID.getOpaqueValue() - 1);
2826
2827 // If this AST file is a precompiled preamble, then set the preamble file ID
2828 // of the source manager to the file source file from which the preamble was
2829 // built.
2830 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002831 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002832 } else if (Type == MK_MainFile) {
2833 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002834 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002835 }
2836
Douglas Gregorcff9f262012-01-27 01:47:08 +00002837 // For any Objective-C class definitions we have already loaded, make sure
2838 // that we load any additional categories.
2839 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2840 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2841 ObjCClassesLoaded[I],
2842 PreviousGeneration);
2843 }
2844
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002845 return Success;
2846}
2847
Chris Lattner5f9e2722011-07-23 10:55:15 +00002848ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002849 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002850 ModuleFile *ImportedBy) {
2851 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002852 bool NewModule;
2853 std::string ErrorStr;
2854 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002855 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002856
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002857 if (!M) {
2858 // We couldn't load the module.
2859 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2860 + ErrorStr;
2861 Error(Msg);
2862 return Failure;
2863 }
2864
2865 if (!NewModule) {
2866 // We've already loaded this module.
2867 return Success;
2868 }
2869
2870 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2871 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002872 if (FileName != "-") {
2873 CurrentDir = llvm::sys::path::parent_path(FileName);
2874 if (CurrentDir.empty()) CurrentDir = ".";
2875 }
2876
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002877 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002878 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002879 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002880 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002881
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002882 // Sniff for the signature.
2883 if (Stream.Read(8) != 'C' ||
2884 Stream.Read(8) != 'P' ||
2885 Stream.Read(8) != 'C' ||
2886 Stream.Read(8) != 'H') {
2887 Diag(diag::err_not_a_pch_file) << FileName;
2888 return Failure;
2889 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002890
Douglas Gregor2cf26342009-04-09 22:27:44 +00002891 while (!Stream.AtEndOfStream()) {
2892 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002893
Douglas Gregore1d918e2009-04-10 23:10:45 +00002894 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002895 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002896 return Failure;
2897 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002898
2899 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002900
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002901 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002902 switch (BlockID) {
2903 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002904 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002905 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002906 return Failure;
2907 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002908 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002909 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002910 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002911 case Success:
2912 break;
2913
2914 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002915 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002916
2917 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002918 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002919 // AST block, skipping subblocks, to see if there are other
2920 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002921
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002922 // FIXME: We can't clear loaded slocentries anymore.
2923 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002924
2925 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002926 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002927 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002928
Douglas Gregore1d918e2009-04-10 23:10:45 +00002929 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002930 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002931 break;
2932 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002933 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002934 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002935 return Failure;
2936 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002937 break;
2938 }
Mike Stump1eb44332009-09-09 15:08:12 +00002939 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002940
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002941 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002942 // bits of all files we've seen.
2943 F.GlobalBitOffset = TotalModulesSizeInBits;
2944 TotalModulesSizeInBits += F.SizeInBits;
2945 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002946
2947 // Make sure that the files this module was built against are still available.
2948 if (!DisableValidation) {
2949 switch(validateFileEntries(*M)) {
2950 case Failure: return Failure;
2951 case IgnorePCH: return IgnorePCH;
2952 case Success: break;
2953 }
2954 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002955
2956 // Preload SLocEntries.
2957 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2958 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002959 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2960 // directly because the entry may have already been loaded in which case
2961 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2962 // SourceManager.
2963 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002964 }
2965
Douglas Gregorc69a2922011-08-25 20:58:51 +00002966
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002967 return Success;
2968}
2969
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002970void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002971 // If there's a listener, notify them that we "read" the translation unit.
2972 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002973 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2974 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002975
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002976 // Make sure we load the declaration update records for the translation unit,
2977 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002978 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2979 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002980
Douglas Gregor5f957282011-08-11 22:18:49 +00002981 // FIXME: Find a better way to deal with collisions between these
2982 // built-in types. Right now, we just ignore the problem.
2983
2984 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002985 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002986 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2987 if (!Context.CFConstantStringTypeDecl)
2988 Context.setCFConstantStringType(GetType(String));
2989 }
2990
2991 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2992 QualType FileType = GetType(File);
2993 if (FileType.isNull()) {
2994 Error("FILE type is NULL");
2995 return;
2996 }
2997
2998 if (!Context.FILEDecl) {
2999 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
3000 Context.setFILEDecl(Typedef->getDecl());
3001 else {
3002 const TagType *Tag = FileType->getAs<TagType>();
3003 if (!Tag) {
3004 Error("Invalid FILE type in AST file");
3005 return;
3006 }
3007 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003008 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003009 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003010 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003011
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003012 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003013 QualType Jmp_bufType = GetType(Jmp_buf);
3014 if (Jmp_bufType.isNull()) {
3015 Error("jmp_buf type is NULL");
3016 return;
3017 }
3018
3019 if (!Context.jmp_bufDecl) {
3020 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
3021 Context.setjmp_bufDecl(Typedef->getDecl());
3022 else {
3023 const TagType *Tag = Jmp_bufType->getAs<TagType>();
3024 if (!Tag) {
3025 Error("Invalid jmp_buf type in AST file");
3026 return;
3027 }
3028 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003029 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003030 }
Mike Stump782fa302009-07-28 02:25:19 +00003031 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003032
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003033 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003034 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3035 if (Sigjmp_bufType.isNull()) {
3036 Error("sigjmp_buf type is NULL");
3037 return;
3038 }
3039
3040 if (!Context.sigjmp_bufDecl) {
3041 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3042 Context.setsigjmp_bufDecl(Typedef->getDecl());
3043 else {
3044 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3045 assert(Tag && "Invalid sigjmp_buf type in AST file");
3046 Context.setsigjmp_bufDecl(Tag->getDecl());
3047 }
3048 }
3049 }
3050
3051 if (unsigned ObjCIdRedef
3052 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3053 if (Context.ObjCIdRedefinitionType.isNull())
3054 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3055 }
3056
3057 if (unsigned ObjCClassRedef
3058 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3059 if (Context.ObjCClassRedefinitionType.isNull())
3060 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3061 }
3062
3063 if (unsigned ObjCSelRedef
3064 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3065 if (Context.ObjCSelRedefinitionType.isNull())
3066 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3067 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003068
3069 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3070 QualType Ucontext_tType = GetType(Ucontext_t);
3071 if (Ucontext_tType.isNull()) {
3072 Error("ucontext_t type is NULL");
3073 return;
3074 }
3075
3076 if (!Context.ucontext_tDecl) {
3077 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3078 Context.setucontext_tDecl(Typedef->getDecl());
3079 else {
3080 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3081 assert(Tag && "Invalid ucontext_t type in AST file");
3082 Context.setucontext_tDecl(Tag->getDecl());
3083 }
3084 }
3085 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003086 }
3087
Douglas Gregor35942772011-09-09 21:34:22 +00003088 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003089
3090 // If there were any CUDA special declarations, deserialize them.
3091 if (!CUDASpecialDeclRefs.empty()) {
3092 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003093 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003094 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3095 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003096
3097 // Re-export any modules that were imported by a non-module AST file.
3098 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3099 if (Module *Imported = getSubmodule(ImportedModules[I]))
3100 makeModuleVisible(Imported, Module::AllVisible);
3101 }
3102 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003103}
3104
Douglas Gregorecc2c092011-12-01 22:20:10 +00003105void ASTReader::finalizeForWriting() {
3106 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3107 HiddenEnd = HiddenNamesMap.end();
3108 Hidden != HiddenEnd; ++Hidden) {
3109 makeNamesVisible(Hidden->second);
3110 }
3111 HiddenNamesMap.clear();
3112}
3113
Douglas Gregorb64c1932009-05-12 01:31:05 +00003114/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003115/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003116/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003117std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003118 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003119 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003120 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003121 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003122 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003123 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003124 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003125 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003126 return std::string();
3127 }
3128
3129 // Initialize the stream
3130 llvm::BitstreamReader StreamFile;
3131 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003132 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003133 (const unsigned char *)Buffer->getBufferEnd());
3134 Stream.init(StreamFile);
3135
3136 // Sniff for the signature.
3137 if (Stream.Read(8) != 'C' ||
3138 Stream.Read(8) != 'P' ||
3139 Stream.Read(8) != 'C' ||
3140 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003141 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003142 return std::string();
3143 }
3144
3145 RecordData Record;
3146 while (!Stream.AtEndOfStream()) {
3147 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003148
Douglas Gregorb64c1932009-05-12 01:31:05 +00003149 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3150 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003151
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003152 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003153 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003154 case AST_BLOCK_ID:
3155 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003156 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003157 return std::string();
3158 }
3159 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003160
Douglas Gregorb64c1932009-05-12 01:31:05 +00003161 default:
3162 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003163 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003164 return std::string();
3165 }
3166 break;
3167 }
3168 continue;
3169 }
3170
3171 if (Code == llvm::bitc::END_BLOCK) {
3172 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003173 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003174 return std::string();
3175 }
3176 continue;
3177 }
3178
3179 if (Code == llvm::bitc::DEFINE_ABBREV) {
3180 Stream.ReadAbbrevRecord();
3181 continue;
3182 }
3183
3184 Record.clear();
3185 const char *BlobStart = 0;
3186 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003187 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003188 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003189 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003190 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003191
3192 return std::string();
3193}
3194
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003195ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003196 // Enter the submodule block.
3197 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3198 Error("malformed submodule block record in AST file");
3199 return Failure;
3200 }
3201
3202 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003203 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003204 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003205 RecordData Record;
3206 while (true) {
3207 unsigned Code = F.Stream.ReadCode();
3208 if (Code == llvm::bitc::END_BLOCK) {
3209 if (F.Stream.ReadBlockEnd()) {
3210 Error("error at end of submodule block in AST file");
3211 return Failure;
3212 }
3213 return Success;
3214 }
3215
3216 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3217 // No known subblocks, always skip them.
3218 F.Stream.ReadSubBlockID();
3219 if (F.Stream.SkipBlock()) {
3220 Error("malformed block record in AST file");
3221 return Failure;
3222 }
3223 continue;
3224 }
3225
3226 if (Code == llvm::bitc::DEFINE_ABBREV) {
3227 F.Stream.ReadAbbrevRecord();
3228 continue;
3229 }
3230
3231 // Read a record.
3232 const char *BlobStart;
3233 unsigned BlobLen;
3234 Record.clear();
3235 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3236 default: // Default behavior: ignore.
3237 break;
3238
3239 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003240 if (First) {
3241 Error("missing submodule metadata record at beginning of block");
3242 return Failure;
3243 }
3244
Douglas Gregore209e502011-12-06 01:10:29 +00003245 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003246 Error("malformed module definition");
3247 return Failure;
3248 }
3249
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003250 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003251 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3252 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3253 bool IsFramework = Record[2];
3254 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003255 bool IsSystem = Record[4];
3256 bool InferSubmodules = Record[5];
3257 bool InferExplicitSubmodules = Record[6];
3258 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003259
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003260 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003261 if (Parent)
3262 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003263
3264 // Retrieve this (sub)module from the module map, creating it if
3265 // necessary.
3266 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3267 IsFramework,
3268 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003269 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3270 if (GlobalIndex >= SubmodulesLoaded.size() ||
3271 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003272 Error("too many submodules");
3273 return Failure;
3274 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003275
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003276 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003277 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003278 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003279 CurrentModule->InferSubmodules = InferSubmodules;
3280 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3281 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003282 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003283 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003284
Douglas Gregore209e502011-12-06 01:10:29 +00003285 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003286 break;
3287 }
3288
Douglas Gregor77d029f2011-12-08 19:11:24 +00003289 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003290 if (First) {
3291 Error("missing submodule metadata record at beginning of block");
3292 return Failure;
3293 }
3294
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003295 if (!CurrentModule)
3296 break;
3297
3298 StringRef FileName(BlobStart, BlobLen);
3299 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003300 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003301 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003302 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003303 Error("mismatched umbrella headers in submodule");
3304 return Failure;
3305 }
3306 }
3307 break;
3308 }
3309
3310 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003311 if (First) {
3312 Error("missing submodule metadata record at beginning of block");
3313 return Failure;
3314 }
3315
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003316 if (!CurrentModule)
3317 break;
3318
3319 // FIXME: Be more lazy about this!
3320 StringRef FileName(BlobStart, BlobLen);
3321 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3322 if (std::find(CurrentModule->Headers.begin(),
3323 CurrentModule->Headers.end(),
3324 File) == CurrentModule->Headers.end())
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00003325 ModMap.addHeader(CurrentModule, File, false);
3326 }
3327 break;
3328 }
3329
3330 case SUBMODULE_EXCLUDED_HEADER: {
3331 if (First) {
3332 Error("missing submodule metadata record at beginning of block");
3333 return Failure;
3334 }
3335
3336 if (!CurrentModule)
3337 break;
3338
3339 // FIXME: Be more lazy about this!
3340 StringRef FileName(BlobStart, BlobLen);
3341 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3342 if (std::find(CurrentModule->Headers.begin(),
3343 CurrentModule->Headers.end(),
3344 File) == CurrentModule->Headers.end())
3345 ModMap.addHeader(CurrentModule, File, true);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003346 }
3347 break;
3348 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003349
3350 case SUBMODULE_TOPHEADER: {
3351 if (First) {
3352 Error("missing submodule metadata record at beginning of block");
3353 return Failure;
3354 }
3355
3356 if (!CurrentModule)
3357 break;
3358
3359 // FIXME: Be more lazy about this!
3360 StringRef FileName(BlobStart, BlobLen);
3361 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3362 CurrentModule->TopHeaders.insert(File);
3363 break;
3364 }
3365
Douglas Gregor77d029f2011-12-08 19:11:24 +00003366 case SUBMODULE_UMBRELLA_DIR: {
3367 if (First) {
3368 Error("missing submodule metadata record at beginning of block");
3369 return Failure;
3370 }
3371
3372 if (!CurrentModule)
3373 break;
3374
3375 StringRef DirName(BlobStart, BlobLen);
3376 if (const DirectoryEntry *Umbrella
3377 = PP.getFileManager().getDirectory(DirName)) {
3378 if (!CurrentModule->getUmbrellaDir())
3379 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3380 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3381 Error("mismatched umbrella directories in submodule");
3382 return Failure;
3383 }
3384 }
3385 break;
3386 }
3387
Douglas Gregor26ced122011-12-01 00:59:36 +00003388 case SUBMODULE_METADATA: {
3389 if (!First) {
3390 Error("submodule metadata record not at beginning of block");
3391 return Failure;
3392 }
3393 First = false;
3394
3395 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003396 F.LocalNumSubmodules = Record[0];
3397 unsigned LocalBaseSubmoduleID = Record[1];
3398 if (F.LocalNumSubmodules > 0) {
3399 // Introduce the global -> local mapping for submodules within this
3400 // module.
3401 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3402
3403 // Introduce the local -> global mapping for submodules within this
3404 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003405 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003406 std::make_pair(LocalBaseSubmoduleID,
3407 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3408
3409 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3410 }
3411 break;
3412 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003413
Douglas Gregor55988682011-12-05 16:33:54 +00003414 case SUBMODULE_IMPORTS: {
3415 if (First) {
3416 Error("missing submodule metadata record at beginning of block");
3417 return Failure;
3418 }
3419
3420 if (!CurrentModule)
3421 break;
3422
3423 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3424 UnresolvedModuleImportExport Unresolved;
3425 Unresolved.File = &F;
3426 Unresolved.Mod = CurrentModule;
3427 Unresolved.ID = Record[Idx];
3428 Unresolved.IsImport = true;
3429 Unresolved.IsWildcard = false;
3430 UnresolvedModuleImportExports.push_back(Unresolved);
3431 }
3432 break;
3433 }
3434
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003435 case SUBMODULE_EXPORTS: {
3436 if (First) {
3437 Error("missing submodule metadata record at beginning of block");
3438 return Failure;
3439 }
3440
3441 if (!CurrentModule)
3442 break;
3443
3444 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003445 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003446 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003447 Unresolved.Mod = CurrentModule;
3448 Unresolved.ID = Record[Idx];
3449 Unresolved.IsImport = false;
3450 Unresolved.IsWildcard = Record[Idx + 1];
3451 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003452 }
3453
3454 // Once we've loaded the set of exports, there's no reason to keep
3455 // the parsed, unresolved exports around.
3456 CurrentModule->UnresolvedExports.clear();
3457 break;
3458 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003459 case SUBMODULE_REQUIRES: {
3460 if (First) {
3461 Error("missing submodule metadata record at beginning of block");
3462 return Failure;
3463 }
3464
3465 if (!CurrentModule)
3466 break;
3467
3468 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003469 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003470 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003471 break;
3472 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003473 }
3474 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003475}
3476
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003477/// \brief Parse the record that corresponds to a LangOptions data
3478/// structure.
3479///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003480/// This routine parses the language options from the AST file and then gives
3481/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003482///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003483/// \returns true if the listener deems the file unacceptable, false otherwise.
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003484bool ASTReader::ParseLanguageOptions(const ModuleFile &M,
3485 const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003486 if (Listener) {
3487 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003488 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003489#define LANGOPT(Name, Bits, Default, Description) \
3490 LangOpts.Name = Record[Idx++];
3491#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3492 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3493#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003494
3495 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3496 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3497 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003498
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003499 unsigned Length = Record[Idx++];
3500 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3501 Record.begin() + Idx + Length);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003502 return Listener->ReadLanguageOptions(M, LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003503 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003504
3505 return false;
3506}
3507
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003508std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003509ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003510 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003511 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003512 assert(I != GlobalPreprocessedEntityMap.end() &&
3513 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003514 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003515 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3516 return std::make_pair(M, LocalIndex);
3517}
3518
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003519std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3520ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3521 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3522 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3523 Mod.NumPreprocessedEntities);
3524
3525 return std::make_pair(PreprocessingRecord::iterator(),
3526 PreprocessingRecord::iterator());
3527}
3528
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003529std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3530ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3531 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3532 ModuleDeclIterator(this, &Mod,
3533 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3534}
3535
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003536PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3537 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003538 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3539 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003540 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003541 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003542
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003543 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003544 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003545
3546 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3547 switch (Code) {
3548 case llvm::bitc::END_BLOCK:
3549 return 0;
3550
3551 case llvm::bitc::ENTER_SUBBLOCK:
3552 Error("unexpected subblock record in preprocessor detail block");
3553 return 0;
3554
3555 case llvm::bitc::DEFINE_ABBREV:
3556 Error("unexpected abbrevation record in preprocessor detail block");
3557 return 0;
3558
3559 default:
3560 break;
3561 }
3562
3563 if (!PP.getPreprocessingRecord()) {
3564 Error("no preprocessing record");
3565 return 0;
3566 }
3567
3568 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003569 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3570 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003571 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3572 const char *BlobStart = 0;
3573 unsigned BlobLen = 0;
3574 RecordData Record;
3575 PreprocessorDetailRecordTypes RecType =
3576 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3577 Code, Record, BlobStart, BlobLen);
3578 switch (RecType) {
3579 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003580 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003581 IdentifierInfo *Name = 0;
3582 MacroDefinition *Def = 0;
3583 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003584 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003585 else {
3586 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003587 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003588 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3589 }
3590
3591 MacroExpansion *ME;
3592 if (isBuiltin)
3593 ME = new (PPRec) MacroExpansion(Name, Range);
3594 else
3595 ME = new (PPRec) MacroExpansion(Def, Range);
3596
3597 return ME;
3598 }
3599
3600 case PPD_MACRO_DEFINITION: {
3601 // Decode the identifier info and then check again; if the macro is
3602 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003603 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003604 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003605 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003606
3607 if (DeserializationListener)
3608 DeserializationListener->MacroDefinitionRead(PPID, MD);
3609
3610 return MD;
3611 }
3612
3613 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003614 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003615 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3616 const FileEntry *File = 0;
3617 if (!FullFileName.empty())
3618 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003619
3620 // FIXME: Stable encoding
3621 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003622 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003623 InclusionDirective *ID
3624 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003625 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003626 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003627 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003628 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003629 return ID;
3630 }
3631 }
David Blaikie7530c032012-01-17 06:56:22 +00003632
3633 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003634}
3635
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003636/// \brief \arg SLocMapI points at a chunk of a module that contains no
3637/// preprocessed entities or the entities it contains are not the ones we are
3638/// looking for. Find the next module that contains entities and return the ID
3639/// of the first entry.
3640PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3641 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3642 ++SLocMapI;
3643 for (GlobalSLocOffsetMapType::const_iterator
3644 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003645 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003646 if (M.NumPreprocessedEntities)
3647 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3648 }
3649
3650 return getTotalNumPreprocessedEntities();
3651}
3652
3653namespace {
3654
3655template <unsigned PPEntityOffset::*PPLoc>
3656struct PPEntityComp {
3657 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003658 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003659
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003660 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003661
Benjamin Kramer88df1252011-09-21 06:42:26 +00003662 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3663 SourceLocation LHS = getLoc(L);
3664 SourceLocation RHS = getLoc(R);
3665 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3666 }
3667
3668 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003669 SourceLocation LHS = getLoc(L);
3670 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3671 }
3672
Benjamin Kramer88df1252011-09-21 06:42:26 +00003673 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003674 SourceLocation RHS = getLoc(R);
3675 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3676 }
3677
3678 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3679 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3680 }
3681};
3682
3683}
3684
3685/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3686PreprocessedEntityID
3687ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3688 if (SourceMgr.isLocalSourceLocation(BLoc))
3689 return getTotalNumPreprocessedEntities();
3690
3691 GlobalSLocOffsetMapType::const_iterator
3692 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3693 BLoc.getOffset());
3694 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3695 "Corrupted global sloc offset map");
3696
3697 if (SLocMapI->second->NumPreprocessedEntities == 0)
3698 return findNextPreprocessedEntity(SLocMapI);
3699
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003700 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003701 typedef const PPEntityOffset *pp_iterator;
3702 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3703 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003704
3705 size_t Count = M.NumPreprocessedEntities;
3706 size_t Half;
3707 pp_iterator First = pp_begin;
3708 pp_iterator PPI;
3709
3710 // Do a binary search manually instead of using std::lower_bound because
3711 // The end locations of entities may be unordered (when a macro expansion
3712 // is inside another macro argument), but for this case it is not important
3713 // whether we get the first macro expansion or its containing macro.
3714 while (Count > 0) {
3715 Half = Count/2;
3716 PPI = First;
3717 std::advance(PPI, Half);
3718 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3719 BLoc)){
3720 First = PPI;
3721 ++First;
3722 Count = Count - Half - 1;
3723 } else
3724 Count = Half;
3725 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003726
3727 if (PPI == pp_end)
3728 return findNextPreprocessedEntity(SLocMapI);
3729
3730 return getGlobalPreprocessedEntityID(M,
3731 M.BasePreprocessedEntityID + (PPI - pp_begin));
3732}
3733
3734/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3735PreprocessedEntityID
3736ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3737 if (SourceMgr.isLocalSourceLocation(ELoc))
3738 return getTotalNumPreprocessedEntities();
3739
3740 GlobalSLocOffsetMapType::const_iterator
3741 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3742 ELoc.getOffset());
3743 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3744 "Corrupted global sloc offset map");
3745
3746 if (SLocMapI->second->NumPreprocessedEntities == 0)
3747 return findNextPreprocessedEntity(SLocMapI);
3748
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003749 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003750 typedef const PPEntityOffset *pp_iterator;
3751 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3752 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3753 pp_iterator PPI =
3754 std::upper_bound(pp_begin, pp_end, ELoc,
3755 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3756
3757 if (PPI == pp_end)
3758 return findNextPreprocessedEntity(SLocMapI);
3759
3760 return getGlobalPreprocessedEntityID(M,
3761 M.BasePreprocessedEntityID + (PPI - pp_begin));
3762}
3763
3764/// \brief Returns a pair of [Begin, End) indices of preallocated
3765/// preprocessed entities that \arg Range encompasses.
3766std::pair<unsigned, unsigned>
3767 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3768 if (Range.isInvalid())
3769 return std::make_pair(0,0);
3770 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3771
3772 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3773 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3774 return std::make_pair(BeginID, EndID);
3775}
3776
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003777/// \brief Optionally returns true or false if the preallocated preprocessed
3778/// entity with index \arg Index came from file \arg FID.
3779llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3780 FileID FID) {
3781 if (FID.isInvalid())
3782 return false;
3783
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003784 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3785 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003786 unsigned LocalIndex = PPInfo.second;
3787 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3788
3789 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3790 if (Loc.isInvalid())
3791 return false;
3792
3793 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3794 return true;
3795 else
3796 return false;
3797}
3798
Douglas Gregord10a3812011-08-25 18:14:34 +00003799namespace {
3800 /// \brief Visitor used to search for information about a header file.
3801 class HeaderFileInfoVisitor {
3802 ASTReader &Reader;
3803 const FileEntry *FE;
3804
3805 llvm::Optional<HeaderFileInfo> HFI;
3806
3807 public:
3808 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3809 : Reader(Reader), FE(FE) { }
3810
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003811 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003812 HeaderFileInfoVisitor *This
3813 = static_cast<HeaderFileInfoVisitor *>(UserData);
3814
3815 HeaderFileInfoTrait Trait(This->Reader, M,
3816 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3817 M.HeaderFileFrameworkStrings,
3818 This->FE->getName());
3819
3820 HeaderFileInfoLookupTable *Table
3821 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3822 if (!Table)
3823 return false;
3824
3825 // Look in the on-disk hash table for an entry for this file name.
3826 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3827 &Trait);
3828 if (Pos == Table->end())
3829 return false;
3830
3831 This->HFI = *Pos;
3832 return true;
3833 }
3834
3835 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3836 };
3837}
3838
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003839HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003840 HeaderFileInfoVisitor Visitor(*this, FE);
3841 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3842 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003843 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003844 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3845 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003846 }
3847
3848 return HeaderFileInfo();
3849}
3850
David Blaikied6471f72011-09-25 23:23:43 +00003851void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003852 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003853 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003854 unsigned Idx = 0;
3855 while (Idx < F.PragmaDiagMappings.size()) {
3856 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003857 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3858 Diag.DiagStatePoints.push_back(
3859 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3860 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003861 while (1) {
3862 assert(Idx < F.PragmaDiagMappings.size() &&
3863 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3864 if (Idx >= F.PragmaDiagMappings.size()) {
3865 break; // Something is messed up but at least avoid infinite loop in
3866 // release build.
3867 }
3868 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3869 if (DiagID == (unsigned)-1) {
3870 break; // no more diag/map pairs for this location.
3871 }
3872 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003873 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3874 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003875 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003876 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003877 }
3878}
3879
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003880/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003881ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003882 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003883 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003884 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003885 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003886}
3887
3888/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003889///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003890/// The index is the type ID, shifted and minus the number of predefs. This
3891/// routine actually reads the record corresponding to the type at the given
3892/// location. It is a helper routine for GetType, which deals with reading type
3893/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003894QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003895 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003896 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003897
Douglas Gregor0b748912009-04-14 21:18:50 +00003898 // Keep track of where we are in the stream, then jump back there
3899 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003900 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003901
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003902 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003903
Douglas Gregord89275b2009-07-06 18:54:52 +00003904 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003905 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003906
Douglas Gregor393f2492011-07-22 00:38:23 +00003907 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003908 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003909 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003910 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003911 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3912 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003913 if (Record.size() != 2) {
3914 Error("Incorrect encoding of extended qualifier type");
3915 return QualType();
3916 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003917 QualType Base = readType(*Loc.F, Record, Idx);
3918 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003919 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003920 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003921
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003922 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003923 if (Record.size() != 1) {
3924 Error("Incorrect encoding of complex type");
3925 return QualType();
3926 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003927 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003928 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003929 }
3930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003931 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003932 if (Record.size() != 1) {
3933 Error("Incorrect encoding of pointer type");
3934 return QualType();
3935 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003936 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003937 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003938 }
3939
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003940 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003941 if (Record.size() != 1) {
3942 Error("Incorrect encoding of block pointer type");
3943 return QualType();
3944 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003945 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003946 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003947 }
3948
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003949 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003950 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003951 Error("Incorrect encoding of lvalue reference type");
3952 return QualType();
3953 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003954 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003955 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003956 }
3957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003958 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003959 if (Record.size() != 1) {
3960 Error("Incorrect encoding of rvalue reference type");
3961 return QualType();
3962 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003963 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003964 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003965 }
3966
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003967 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003968 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003969 Error("Incorrect encoding of member pointer type");
3970 return QualType();
3971 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003972 QualType PointeeType = readType(*Loc.F, Record, Idx);
3973 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003974 if (PointeeType.isNull() || ClassType.isNull())
3975 return QualType();
3976
Douglas Gregor35942772011-09-09 21:34:22 +00003977 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003978 }
3979
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003980 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003981 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003982 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3983 unsigned IndexTypeQuals = Record[2];
3984 unsigned Idx = 3;
3985 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003986 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003987 ASM, IndexTypeQuals);
3988 }
3989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003990 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003991 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003992 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3993 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003994 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003995 }
3996
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003997 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003998 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003999 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
4000 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00004001 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
4002 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00004003 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004004 ASM, IndexTypeQuals,
4005 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004006 }
4007
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004008 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004009 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004010 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004011 return QualType();
4012 }
4013
Douglas Gregor393f2492011-07-22 00:38:23 +00004014 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004015 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00004016 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004017 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00004018 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004019 }
4020
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004021 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00004022 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004023 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004024 return QualType();
4025 }
4026
Douglas Gregor393f2492011-07-22 00:38:23 +00004027 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004028 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00004029 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004030 }
4031
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004032 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00004033 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00004034 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004035 return QualType();
4036 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004037 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00004038 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
4039 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00004040 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004041 }
4042
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004043 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004044 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00004045
4046 FunctionProtoType::ExtProtoInfo EPI;
4047 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00004048 /*hasregparm*/ Record[2],
4049 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004050 static_cast<CallingConv>(Record[4]),
4051 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004052
John McCallf85e1932011-06-15 23:02:42 +00004053 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004054 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004055 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004056 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004057 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004058
4059 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004060 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004061 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004062 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004063 ExceptionSpecificationType EST =
4064 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4065 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004066 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004067 if (EST == EST_Dynamic) {
4068 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004069 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004070 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004071 EPI.Exceptions = Exceptions.data();
4072 } else if (EST == EST_ComputedNoexcept) {
4073 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004074 } else if (EST == EST_Uninstantiated) {
4075 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4076 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004077 } else if (EST == EST_Unevaluated) {
4078 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004079 }
Douglas Gregor35942772011-09-09 21:34:22 +00004080 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004081 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004082 }
4083
Douglas Gregor409448c2011-07-21 22:35:25 +00004084 case TYPE_UNRESOLVED_USING: {
4085 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004086 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004087 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4088 }
4089
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004090 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004091 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004092 Error("incorrect encoding of typedef type");
4093 return QualType();
4094 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004095 unsigned Idx = 0;
4096 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004097 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004098 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004099 Canonical = Context.getCanonicalType(Canonical);
4100 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004101 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004102
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004103 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004104 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004105
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004106 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004107 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004108 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004109 return QualType();
4110 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004111 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004112 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004113 }
Mike Stump1eb44332009-09-09 15:08:12 +00004114
Douglas Gregorf8af9822012-02-12 18:42:33 +00004115 case TYPE_DECLTYPE: {
4116 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4117 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4118 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004119
Sean Huntca63c202011-05-24 22:41:36 +00004120 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004121 QualType BaseType = readType(*Loc.F, Record, Idx);
4122 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004123 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004124 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004125 }
4126
Richard Smith34b41d92011-02-20 03:19:35 +00004127 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004128 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004129
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004130 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004131 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004132 Error("incorrect encoding of record type");
4133 return QualType();
4134 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004135 unsigned Idx = 0;
4136 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004137 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4138 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4139 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004140 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004141 return T;
4142 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004143
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004144 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004145 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004146 Error("incorrect encoding of enum type");
4147 return QualType();
4148 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004149 unsigned Idx = 0;
4150 bool IsDependent = Record[Idx++];
4151 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004152 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004153 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004154 return T;
4155 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004156
John McCall9d156a72011-01-06 01:58:22 +00004157 case TYPE_ATTRIBUTED: {
4158 if (Record.size() != 3) {
4159 Error("incorrect encoding of attributed type");
4160 return QualType();
4161 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004162 QualType modifiedType = readType(*Loc.F, Record, Idx);
4163 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004164 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004165 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004166 }
4167
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004168 case TYPE_PAREN: {
4169 if (Record.size() != 1) {
4170 Error("incorrect encoding of paren type");
4171 return QualType();
4172 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004173 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004174 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004175 }
4176
Douglas Gregor7536dd52010-12-20 02:24:11 +00004177 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004178 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004179 Error("incorrect encoding of pack expansion type");
4180 return QualType();
4181 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004182 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004183 if (Pattern.isNull())
4184 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004185 llvm::Optional<unsigned> NumExpansions;
4186 if (Record[1])
4187 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004188 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004189 }
4190
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004191 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004192 unsigned Idx = 0;
4193 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004194 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004195 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004196 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004197 }
4198
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004199 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004200 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004201 ObjCInterfaceDecl *ItfD
4202 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004203 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004204 }
4205
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004206 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004207 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004208 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004209 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004210 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004211 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004212 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004213 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004214 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004216 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004217 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004218 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004219 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004220 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004221
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004222 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004223 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004224 QualType Parm = readType(*Loc.F, Record, Idx);
4225 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004226 return
Douglas Gregor35942772011-09-09 21:34:22 +00004227 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004228 Replacement);
4229 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004230
Douglas Gregorc3069d62011-01-14 02:55:32 +00004231 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4232 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004233 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004234 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004235 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004236 cast<TemplateTypeParmType>(Parm),
4237 ArgPack);
4238 }
4239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004240 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004241 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004242 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004243 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004244 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004245 return
Douglas Gregor35942772011-09-09 21:34:22 +00004246 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004247 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004248
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004249 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004250 unsigned Idx = 0;
4251 unsigned Depth = Record[Idx++];
4252 unsigned Index = Record[Idx++];
4253 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004254 TemplateTypeParmDecl *D
4255 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004256 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004257 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004258
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004259 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004260 unsigned Idx = 0;
4261 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004262 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004263 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004264 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004265 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004266 Canon = Context.getCanonicalType(Canon);
4267 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004268 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004269
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004270 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004271 unsigned Idx = 0;
4272 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004273 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004274 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004275 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004276 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004277 Args.reserve(NumArgs);
4278 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004279 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004280 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004281 Args.size(), Args.data());
4282 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004283
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004284 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004285 unsigned Idx = 0;
4286
4287 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004288 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004289 ArrayType::ArraySizeModifier ASM
4290 = (ArrayType::ArraySizeModifier)Record[Idx++];
4291 unsigned IndexTypeQuals = Record[Idx++];
4292
4293 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004294 Expr *NumElts = ReadExpr(*Loc.F);
4295 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004296
Douglas Gregor35942772011-09-09 21:34:22 +00004297 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004298 IndexTypeQuals, Brackets);
4299 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004300
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004301 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004302 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004303 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004304 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004305 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004306 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004307 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004308 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004309 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004310 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004311 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004312 else
Douglas Gregor35942772011-09-09 21:34:22 +00004313 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004314 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004315 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004316 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004317 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004318
4319 case TYPE_ATOMIC: {
4320 if (Record.size() != 1) {
4321 Error("Incorrect encoding of atomic type");
4322 return QualType();
4323 }
4324 QualType ValueType = readType(*Loc.F, Record, Idx);
4325 return Context.getAtomicType(ValueType);
4326 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004327 }
David Blaikie7530c032012-01-17 06:56:22 +00004328 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004329}
4330
Sebastian Redlc3632732010-10-05 15:59:54 +00004331class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004332 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004333 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004334 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004335 unsigned &Idx;
4336
Sebastian Redlc3632732010-10-05 15:59:54 +00004337 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4338 unsigned &I) {
4339 return Reader.ReadSourceLocation(F, R, I);
4340 }
4341
Douglas Gregor409448c2011-07-21 22:35:25 +00004342 template<typename T>
4343 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4344 return Reader.ReadDeclAs<T>(F, Record, Idx);
4345 }
4346
John McCalla1ee0c52009-10-16 21:56:05 +00004347public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004348 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004349 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004350 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004351 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004352
John McCall51bd8032009-10-18 01:05:36 +00004353 // We want compile-time assurance that we've enumerated all of
4354 // these, so unfortunately we have to declare them first, then
4355 // define them out-of-line.
4356#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004357#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004358 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004359#include "clang/AST/TypeLocNodes.def"
4360
John McCall51bd8032009-10-18 01:05:36 +00004361 void VisitFunctionTypeLoc(FunctionTypeLoc);
4362 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004363};
4364
John McCall51bd8032009-10-18 01:05:36 +00004365void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004366 // nothing to do
4367}
John McCall51bd8032009-10-18 01:05:36 +00004368void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004369 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004370 if (TL.needsExtraLocalData()) {
4371 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4372 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4373 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4374 TL.setModeAttr(Record[Idx++]);
4375 }
John McCalla1ee0c52009-10-16 21:56:05 +00004376}
John McCall51bd8032009-10-18 01:05:36 +00004377void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004378 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004379}
John McCall51bd8032009-10-18 01:05:36 +00004380void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004381 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004382}
John McCall51bd8032009-10-18 01:05:36 +00004383void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004384 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004385}
John McCall51bd8032009-10-18 01:05:36 +00004386void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004387 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004388}
John McCall51bd8032009-10-18 01:05:36 +00004389void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004390 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004391}
John McCall51bd8032009-10-18 01:05:36 +00004392void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004393 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004394 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004395}
John McCall51bd8032009-10-18 01:05:36 +00004396void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004397 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4398 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004399 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004400 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004401 else
John McCall51bd8032009-10-18 01:05:36 +00004402 TL.setSizeExpr(0);
4403}
4404void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4405 VisitArrayTypeLoc(TL);
4406}
4407void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4408 VisitArrayTypeLoc(TL);
4409}
4410void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4411 VisitArrayTypeLoc(TL);
4412}
4413void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4414 DependentSizedArrayTypeLoc TL) {
4415 VisitArrayTypeLoc(TL);
4416}
4417void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4418 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004419 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004420}
4421void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004422 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004423}
4424void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004425 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004426}
4427void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004428 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004429 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4430 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004431 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004432 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004433 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004434 }
4435}
4436void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4437 VisitFunctionTypeLoc(TL);
4438}
4439void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4440 VisitFunctionTypeLoc(TL);
4441}
John McCalled976492009-12-04 22:46:56 +00004442void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004443 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004444}
John McCall51bd8032009-10-18 01:05:36 +00004445void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004446 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004447}
4448void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004449 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4450 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4451 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004452}
4453void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004454 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4455 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4456 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4457 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004458}
4459void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004460 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004461}
Sean Huntca63c202011-05-24 22:41:36 +00004462void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4463 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4464 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4465 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4466 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4467}
Richard Smith34b41d92011-02-20 03:19:35 +00004468void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4469 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4470}
John McCall51bd8032009-10-18 01:05:36 +00004471void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004472 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004473}
4474void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004475 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004476}
John McCall9d156a72011-01-06 01:58:22 +00004477void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4478 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4479 if (TL.hasAttrOperand()) {
4480 SourceRange range;
4481 range.setBegin(ReadSourceLocation(Record, Idx));
4482 range.setEnd(ReadSourceLocation(Record, Idx));
4483 TL.setAttrOperandParensRange(range);
4484 }
4485 if (TL.hasAttrExprOperand()) {
4486 if (Record[Idx++])
4487 TL.setAttrExprOperand(Reader.ReadExpr(F));
4488 else
4489 TL.setAttrExprOperand(0);
4490 } else if (TL.hasAttrEnumOperand())
4491 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4492}
John McCall51bd8032009-10-18 01:05:36 +00004493void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004494 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004495}
John McCall49a832b2009-10-18 09:09:24 +00004496void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4497 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004498 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004499}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004500void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4501 SubstTemplateTypeParmPackTypeLoc TL) {
4502 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4503}
John McCall51bd8032009-10-18 01:05:36 +00004504void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4505 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004506 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004507 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4508 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4509 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004510 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4511 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004512 Reader.GetTemplateArgumentLocInfo(F,
4513 TL.getTypePtr()->getArg(i).getKind(),
4514 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004515}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004516void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4517 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4518 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4519}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004520void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004521 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004522 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004523}
John McCall3cb0ebd2010-03-10 03:28:59 +00004524void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004525 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004526}
Douglas Gregor4714c122010-03-31 17:34:00 +00004527void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004528 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004529 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004530 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004531}
John McCall33500952010-06-11 00:33:02 +00004532void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4533 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004534 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004535 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004536 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004537 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004538 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4539 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004540 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4541 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004542 Reader.GetTemplateArgumentLocInfo(F,
4543 TL.getTypePtr()->getArg(I).getKind(),
4544 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004545}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004546void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4547 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4548}
John McCall51bd8032009-10-18 01:05:36 +00004549void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004550 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004551}
4552void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4553 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004554 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4555 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004556 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004557 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004558}
John McCall54e14c42009-10-22 22:37:11 +00004559void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004560 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004561}
Eli Friedmanb001de72011-10-06 23:00:33 +00004562void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4563 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4564 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4565 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4566}
John McCalla1ee0c52009-10-16 21:56:05 +00004567
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004568TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004569 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004570 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004571 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004572 if (InfoTy.isNull())
4573 return 0;
4574
Douglas Gregor35942772011-09-09 21:34:22 +00004575 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004576 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004577 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004578 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004579 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004580}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004581
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004582QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004583 unsigned FastQuals = ID & Qualifiers::FastMask;
4584 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004585
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004586 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004587 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004588 switch ((PredefinedTypeIDs)Index) {
4589 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004590 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4591 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004592
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004593 case PREDEF_TYPE_CHAR_U_ID:
4594 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004595 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004596 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004597 break;
4598
Douglas Gregor35942772011-09-09 21:34:22 +00004599 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4600 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4601 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4602 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4603 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4604 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4605 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4606 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4607 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4608 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4609 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4610 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4611 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004612 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004613 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4614 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4615 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4616 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4617 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004618 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004619 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4620 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4621 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4622 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4623 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4624 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4625 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4626 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4627 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004628
4629 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004630 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004631 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004632
4633 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4634 T = Context.ARCUnbridgedCastTy;
4635 break;
4636
Meador Ingefb40e3f2012-07-01 15:57:25 +00004637 case PREDEF_TYPE_VA_LIST_TAG:
4638 T = Context.getVaListTagType();
4639 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004640
4641 case PREDEF_TYPE_BUILTIN_FN:
4642 T = Context.BuiltinFnTy;
4643 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004644 }
4645
4646 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004647 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004648 }
4649
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004650 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004651 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004652 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004653 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004654 if (TypesLoaded[Index].isNull())
4655 return QualType();
4656
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004657 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004658 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004659 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004660 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004661 }
Mike Stump1eb44332009-09-09 15:08:12 +00004662
John McCall0953e762009-09-24 19:53:00 +00004663 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004664}
4665
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004666QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004667 return GetType(getGlobalTypeID(F, LocalID));
4668}
4669
4670serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004671ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004672 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4673 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4674
4675 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4676 return LocalID;
4677
4678 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4679 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4680 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4681
4682 unsigned GlobalIndex = LocalIndex + I->second;
4683 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4684}
4685
John McCall833ca992009-10-29 08:12:44 +00004686TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004687ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004688 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004689 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004690 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004691 switch (Kind) {
4692 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004693 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004694 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004695 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004696 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004697 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4698 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004699 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004700 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004701 SourceLocation());
4702 }
4703 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004704 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4705 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004706 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004707 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004708 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004709 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004710 }
John McCall833ca992009-10-29 08:12:44 +00004711 case TemplateArgument::Null:
4712 case TemplateArgument::Integral:
4713 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004714 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004715 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004716 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004717 return TemplateArgumentLocInfo();
4718 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004719 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004720}
4721
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004722TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004723ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004724 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004725 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004726
4727 if (Arg.getKind() == TemplateArgument::Expression) {
4728 if (Record[Index++]) // bool InfoHasSameExpr.
4729 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4730 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004731 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004732 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004733}
4734
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004735Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004736 return GetDecl(ID);
4737}
4738
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004739uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004740 unsigned &Idx){
4741 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004742 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004743
Douglas Gregore92b8a12011-08-04 00:01:48 +00004744 unsigned LocalID = Record[Idx++];
4745 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004746}
4747
4748CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004749 RecordLocation Loc = getLocalBitOffset(Offset);
4750 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004751 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004752 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004753 ReadingKindTracker ReadingKind(Read_Decl, *this);
4754 RecordData Record;
4755 unsigned Code = Cursor.ReadCode();
4756 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4757 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4758 Error("Malformed AST file: missing C++ base specifiers");
4759 return 0;
4760 }
4761
4762 unsigned Idx = 0;
4763 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004764 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004765 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4766 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004767 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004768 return Bases;
4769}
4770
Douglas Gregor409448c2011-07-21 22:35:25 +00004771serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004772ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004773 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004774 return LocalID;
4775
4776 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004777 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004778 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4779
4780 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004781}
4782
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004783bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004784 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004785 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4786 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4787 return &M == I->second;
4788}
4789
Douglas Gregorcff9f262012-01-27 01:47:08 +00004790ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4791 if (!D->isFromASTFile())
4792 return 0;
4793 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4794 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4795 return I->second;
4796}
4797
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004798SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4799 if (ID < NUM_PREDEF_DECL_IDS)
4800 return SourceLocation();
4801
4802 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4803
4804 if (Index > DeclsLoaded.size()) {
4805 Error("declaration ID out-of-range for AST file");
4806 return SourceLocation();
4807 }
4808
4809 if (Decl *D = DeclsLoaded[Index])
4810 return D->getLocation();
4811
4812 unsigned RawLocation = 0;
4813 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4814 return ReadSourceLocation(*Rec.F, RawLocation);
4815}
4816
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004817Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004818 if (ID < NUM_PREDEF_DECL_IDS) {
4819 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004820 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004821 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004822
4823 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004824 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004825
4826 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004827 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004828
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004829 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004830 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004831
Douglas Gregor79d67262011-08-12 05:59:41 +00004832 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004833 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004834
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004835 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4836 return Context.getObjCProtocolDecl();
4837
Douglas Gregor772eeae2011-08-12 06:49:56 +00004838 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004839 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004840
4841 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004842 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004843
4844 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004845 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004846
4847 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4848 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004849 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004850 }
4851
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004852 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4853
Richard Smith2fbf3732011-12-20 04:39:57 +00004854 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004855 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004856 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004857 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004858 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004859
Douglas Gregorfd002a72011-12-16 22:37:11 +00004860 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004861 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004862 if (DeserializationListener)
4863 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4864 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004865
4866 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004867}
4868
Douglas Gregora1be2782011-12-17 23:38:30 +00004869DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4870 DeclID GlobalID) {
4871 if (GlobalID < NUM_PREDEF_DECL_IDS)
4872 return GlobalID;
4873
4874 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4875 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4876 ModuleFile *Owner = I->second;
4877
4878 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4879 = M.GlobalToLocalDeclIDs.find(Owner);
4880 if (Pos == M.GlobalToLocalDeclIDs.end())
4881 return 0;
4882
4883 return GlobalID - Owner->BaseDeclID + Pos->second;
4884}
4885
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004886serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004887 const RecordData &Record,
4888 unsigned &Idx) {
4889 if (Idx >= Record.size()) {
4890 Error("Corrupted AST file");
4891 return 0;
4892 }
4893
4894 return getGlobalDeclID(F, Record[Idx++]);
4895}
4896
Chris Lattner887e2b32009-04-27 05:46:25 +00004897/// \brief Resolve the offset of a statement into a statement.
4898///
4899/// This operation will read a new statement from the external
4900/// source each time it is called, and is meant to be used via a
4901/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004902Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004903 // Switch case IDs are per Decl.
4904 ClearSwitchCaseIDs();
4905
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004906 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004907 RecordLocation Loc = getLocalBitOffset(Offset);
4908 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4909 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004910}
4911
Douglas Gregor851c75a2011-08-24 21:27:34 +00004912namespace {
4913 class FindExternalLexicalDeclsVisitor {
4914 ASTReader &Reader;
4915 const DeclContext *DC;
4916 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004917
Douglas Gregor851c75a2011-08-24 21:27:34 +00004918 SmallVectorImpl<Decl*> &Decls;
4919 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4920
4921 public:
4922 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4923 bool (*isKindWeWant)(Decl::Kind),
4924 SmallVectorImpl<Decl*> &Decls)
4925 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4926 {
4927 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4928 PredefsVisited[I] = false;
4929 }
4930
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004931 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004932 if (Preorder)
4933 return false;
4934
4935 FindExternalLexicalDeclsVisitor *This
4936 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4937
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004938 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004939 = M.DeclContextInfos.find(This->DC);
4940 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4941 return false;
4942
4943 // Load all of the declaration IDs
4944 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4945 *IDE = ID + Info->second.NumLexicalDecls;
4946 ID != IDE; ++ID) {
4947 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4948 continue;
4949
4950 // Don't add predefined declarations to the lexical context more
4951 // than once.
4952 if (ID->second < NUM_PREDEF_DECL_IDS) {
4953 if (This->PredefsVisited[ID->second])
4954 continue;
4955
4956 This->PredefsVisited[ID->second] = true;
4957 }
4958
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004959 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4960 if (!This->DC->isDeclInLexicalTraversal(D))
4961 This->Decls.push_back(D);
4962 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004963 }
4964
4965 return false;
4966 }
4967 };
4968}
4969
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004970ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004971 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004972 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004973 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004974 // least. Walk all of the modules in the order they were loaded.
4975 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4976 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004977 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004978 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004979}
4980
Douglas Gregor0d95f772011-08-24 19:03:07 +00004981namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004982
4983class DeclIDComp {
4984 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004985 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004986
4987public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004988 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004989
4990 bool operator()(LocalDeclID L, LocalDeclID R) const {
4991 SourceLocation LHS = getLocation(L);
4992 SourceLocation RHS = getLocation(R);
4993 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4994 }
4995
4996 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4997 SourceLocation RHS = getLocation(R);
4998 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4999 }
5000
5001 bool operator()(LocalDeclID L, SourceLocation RHS) const {
5002 SourceLocation LHS = getLocation(L);
5003 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
5004 }
5005
5006 SourceLocation getLocation(LocalDeclID ID) const {
5007 return Reader.getSourceManager().getFileLoc(
5008 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
5009 }
5010};
5011
5012}
5013
5014void ASTReader::FindFileRegionDecls(FileID File,
5015 unsigned Offset, unsigned Length,
5016 SmallVectorImpl<Decl *> &Decls) {
5017 SourceManager &SM = getSourceManager();
5018
5019 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
5020 if (I == FileDeclIDs.end())
5021 return;
5022
5023 FileDeclsInfo &DInfo = I->second;
5024 if (DInfo.Decls.empty())
5025 return;
5026
5027 SourceLocation
5028 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
5029 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
5030
5031 DeclIDComp DIDComp(*this, *DInfo.Mod);
5032 ArrayRef<serialization::LocalDeclID>::iterator
5033 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5034 BeginLoc, DIDComp);
5035 if (BeginIt != DInfo.Decls.begin())
5036 --BeginIt;
5037
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00005038 // If we are pointing at a top-level decl inside an objc container, we need
5039 // to backtrack until we find it otherwise we will fail to report that the
5040 // region overlaps with an objc container.
5041 while (BeginIt != DInfo.Decls.begin() &&
5042 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
5043 ->isTopLevelDeclInObjCContainer())
5044 --BeginIt;
5045
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00005046 ArrayRef<serialization::LocalDeclID>::iterator
5047 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
5048 EndLoc, DIDComp);
5049 if (EndIt != DInfo.Decls.end())
5050 ++EndIt;
5051
5052 for (ArrayRef<serialization::LocalDeclID>::iterator
5053 DIt = BeginIt; DIt != EndIt; ++DIt)
5054 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5055}
5056
5057namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005058 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005059 /// declaration context.
5060 class DeclContextNameLookupVisitor {
5061 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005062 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005063 DeclarationName Name;
5064 SmallVectorImpl<NamedDecl *> &Decls;
5065
5066 public:
5067 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005068 SmallVectorImpl<const DeclContext *> &Contexts,
5069 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005070 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005071 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005072
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005073 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005074 DeclContextNameLookupVisitor *This
5075 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5076
5077 // Check whether we have any visible declaration information for
5078 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005079 ModuleFile::DeclContextInfosMap::iterator Info;
5080 bool FoundInfo = false;
5081 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5082 Info = M.DeclContextInfos.find(This->Contexts[I]);
5083 if (Info != M.DeclContextInfos.end() &&
5084 Info->second.NameLookupTableData) {
5085 FoundInfo = true;
5086 break;
5087 }
5088 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005089
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005090 if (!FoundInfo)
5091 return false;
5092
Douglas Gregor0d95f772011-08-24 19:03:07 +00005093 // Look for this name within this module.
5094 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005095 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005096 ASTDeclContextNameLookupTable::iterator Pos
5097 = LookupTable->find(This->Name);
5098 if (Pos == LookupTable->end())
5099 return false;
5100
5101 bool FoundAnything = false;
5102 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5103 for (; Data.first != Data.second; ++Data.first) {
5104 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5105 if (!ND)
5106 continue;
5107
5108 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005109 // A name might be null because the decl's redeclarable part is
5110 // currently read before reading its name. The lookup is triggered by
5111 // building that decl (likely indirectly), and so it is later in the
5112 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005113 continue;
5114 }
5115
5116 // Record this declaration.
5117 FoundAnything = true;
5118 This->Decls.push_back(ND);
5119 }
5120
5121 return FoundAnything;
5122 }
5123 };
5124}
5125
John McCall76bd1f32010-06-01 09:23:16 +00005126DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005127ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005128 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005129 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005130 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005131 if (!Name)
5132 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5133 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005134
Chris Lattner5f9e2722011-07-23 10:55:15 +00005135 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005136
5137 // Compute the declaration contexts we need to look into. Multiple such
5138 // declaration contexts occur when two declaration contexts from disjoint
5139 // modules get merged, e.g., when two namespaces with the same name are
5140 // independently defined in separate modules.
5141 SmallVector<const DeclContext *, 2> Contexts;
5142 Contexts.push_back(DC);
5143
5144 if (DC->isNamespace()) {
5145 MergedDeclsMap::iterator Merged
5146 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5147 if (Merged != MergedDecls.end()) {
5148 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5149 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5150 }
5151 }
5152
5153 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005154 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005155 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005156 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005157 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005158}
5159
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005160namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005161 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005162 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005163 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005164 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005165 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005166 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005167
5168 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005169 DeclContextAllNamesVisitor(ASTReader &Reader,
5170 SmallVectorImpl<const DeclContext *> &Contexts,
5171 llvm::DenseMap<DeclarationName,
5172 SmallVector<NamedDecl *, 8> > &Decls)
5173 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005174
5175 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005176 DeclContextAllNamesVisitor *This
5177 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005178
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005179 // Check whether we have any visible declaration information for
5180 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005181 ModuleFile::DeclContextInfosMap::iterator Info;
5182 bool FoundInfo = false;
5183 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5184 Info = M.DeclContextInfos.find(This->Contexts[I]);
5185 if (Info != M.DeclContextInfos.end() &&
5186 Info->second.NameLookupTableData) {
5187 FoundInfo = true;
5188 break;
5189 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005190 }
5191
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005192 if (!FoundInfo)
5193 return false;
5194
5195 ASTDeclContextNameLookupTable *LookupTable =
5196 Info->second.NameLookupTableData;
5197 bool FoundAnything = false;
5198 for (ASTDeclContextNameLookupTable::data_iterator
5199 I = LookupTable->data_begin(), E = LookupTable->data_end();
5200 I != E; ++I) {
5201 ASTDeclContextNameLookupTrait::data_type Data = *I;
5202 for (; Data.first != Data.second; ++Data.first) {
5203 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5204 *Data.first);
5205 if (!ND)
5206 continue;
5207
5208 // Record this declaration.
5209 FoundAnything = true;
5210 This->Decls[ND->getDeclName()].push_back(ND);
5211 }
5212 }
5213
5214 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005215 }
5216 };
5217}
5218
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005219void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005220 if (!DC->hasExternalVisibleStorage())
5221 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005222 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5223
5224 // Compute the declaration contexts we need to look into. Multiple such
5225 // declaration contexts occur when two declaration contexts from disjoint
5226 // modules get merged, e.g., when two namespaces with the same name are
5227 // independently defined in separate modules.
5228 SmallVector<const DeclContext *, 2> Contexts;
5229 Contexts.push_back(DC);
5230
5231 if (DC->isNamespace()) {
5232 MergedDeclsMap::iterator Merged
5233 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5234 if (Merged != MergedDecls.end()) {
5235 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5236 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5237 }
5238 }
5239
5240 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5241 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5242 ++NumVisibleDeclContextsRead;
5243
5244 for (llvm::DenseMap<DeclarationName,
5245 llvm::SmallVector<NamedDecl*, 8> >::iterator
5246 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5247 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5248 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005249 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005250}
5251
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005252/// \brief Under non-PCH compilation the consumer receives the objc methods
5253/// before receiving the implementation, and codegen depends on this.
5254/// We simulate this by deserializing and passing to consumer the methods of the
5255/// implementation before passing the deserialized implementation decl.
5256static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5257 ASTConsumer *Consumer) {
5258 assert(ImplD && Consumer);
5259
5260 for (ObjCImplDecl::method_iterator
5261 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005262 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005263
5264 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5265}
5266
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005267void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005268 assert(Consumer);
5269 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005270 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005271 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005272
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005273 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005274 }
5275}
5276
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005277void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5278 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5279 PassObjCImplDeclToConsumer(ImplD, Consumer);
5280 else
5281 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5282}
5283
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005284void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005285 this->Consumer = Consumer;
5286
Douglas Gregorfdd01722009-04-14 00:24:19 +00005287 if (!Consumer)
5288 return;
5289
5290 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005291 // Force deserialization of this decl, which will cause it to be queued for
5292 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005293 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005294 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005295 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005296
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005297 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005298}
5299
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005300void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005301 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005302
Mike Stump1eb44332009-09-09 15:08:12 +00005303 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005304 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005305 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005306 unsigned NumDeclsLoaded
5307 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5308 (Decl *)0);
5309 unsigned NumIdentifiersLoaded
5310 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5311 IdentifiersLoaded.end(),
5312 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005313 unsigned NumMacrosLoaded
5314 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5315 MacrosLoaded.end(),
5316 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005317 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005318 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5319 SelectorsLoaded.end(),
5320 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005321
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005322 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5323 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005324 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005325 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5326 NumSLocEntriesRead, TotalNumSLocEntries,
5327 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005328 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005329 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005330 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5331 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5332 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005333 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005334 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5335 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005336 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005337 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005338 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5339 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005340 if (!MacrosLoaded.empty())
5341 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5342 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5343 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005344 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005345 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005346 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5347 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005348 if (TotalNumStatements)
5349 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5350 NumStatementsRead, TotalNumStatements,
5351 ((float)NumStatementsRead/TotalNumStatements * 100));
5352 if (TotalNumMacros)
5353 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5354 NumMacrosRead, TotalNumMacros,
5355 ((float)NumMacrosRead/TotalNumMacros * 100));
5356 if (TotalLexicalDeclContexts)
5357 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5358 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5359 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5360 * 100));
5361 if (TotalVisibleDeclContexts)
5362 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5363 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5364 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5365 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005366 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005367 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005368 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5369 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005370 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005371 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005372 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005373 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005374 dump();
5375 std::fprintf(stderr, "\n");
5376}
5377
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005378template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005379static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005380dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005381 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005382 InitialCapacity> &Map) {
5383 if (Map.begin() == Map.end())
5384 return;
5385
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005386 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005387 llvm::errs() << Name << ":\n";
5388 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5389 I != IEnd; ++I) {
5390 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5391 << "\n";
5392 }
5393}
5394
Douglas Gregor23d7df52011-07-21 19:50:14 +00005395void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005396 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005397 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005398 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005399 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005400 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005401 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005402 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005403 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005404 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005405 dumpModuleIDMap("Global preprocessed entity map",
5406 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005407
5408 llvm::errs() << "\n*** PCH/Modules Loaded:";
5409 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5410 MEnd = ModuleMgr.end();
5411 M != MEnd; ++M)
5412 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005413}
5414
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005415/// Return the amount of memory used by memory buffers, breaking down
5416/// by heap-backed versus mmap'ed memory.
5417void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005418 for (ModuleConstIterator I = ModuleMgr.begin(),
5419 E = ModuleMgr.end(); I != E; ++I) {
5420 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005421 size_t bytes = buf->getBufferSize();
5422 switch (buf->getBufferKind()) {
5423 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5424 sizes.malloc_bytes += bytes;
5425 break;
5426 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5427 sizes.mmap_bytes += bytes;
5428 break;
5429 }
5430 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005431 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005432}
5433
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005434void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005435 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005436 S.ExternalSource = this;
5437
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005438 // Makes sure any declarations that were deserialized "too early"
5439 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005440 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005441 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5442 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005443 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005444 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005445
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005446 // Load the offsets of the declarations that Sema references.
5447 // They will be lazily deserialized when needed.
5448 if (!SemaDeclRefs.empty()) {
5449 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005450 if (!SemaObj->StdNamespace)
5451 SemaObj->StdNamespace = SemaDeclRefs[0];
5452 if (!SemaObj->StdBadAlloc)
5453 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005454 }
5455
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005456 if (!FPPragmaOptions.empty()) {
5457 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5458 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5459 }
5460
5461 if (!OpenCLExtensions.empty()) {
5462 unsigned I = 0;
5463#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5464#include "clang/Basic/OpenCLExtensions.def"
5465
5466 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5467 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005468}
5469
Douglas Gregor211f6e82011-08-20 04:39:52 +00005470IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00005471 // Note that we are loading an identifier.
5472 Deserializing AnIdentifier(this);
5473
Douglas Gregor057df202012-01-18 20:56:22 +00005474 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5475 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005476 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005477 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005478 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005479 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005480}
5481
Douglas Gregor95f42922010-10-14 22:11:03 +00005482namespace clang {
5483 /// \brief An identifier-lookup iterator that enumerates all of the
5484 /// identifiers stored within a set of AST files.
5485 class ASTIdentifierIterator : public IdentifierIterator {
5486 /// \brief The AST reader whose identifiers are being enumerated.
5487 const ASTReader &Reader;
5488
5489 /// \brief The current index into the chain of AST files stored in
5490 /// the AST reader.
5491 unsigned Index;
5492
5493 /// \brief The current position within the identifier lookup table
5494 /// of the current AST file.
5495 ASTIdentifierLookupTable::key_iterator Current;
5496
5497 /// \brief The end position within the identifier lookup table of
5498 /// the current AST file.
5499 ASTIdentifierLookupTable::key_iterator End;
5500
5501 public:
5502 explicit ASTIdentifierIterator(const ASTReader &Reader);
5503
Chris Lattner5f9e2722011-07-23 10:55:15 +00005504 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005505 };
5506}
5507
5508ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005509 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005510 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005511 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005512 Current = IdTable->key_begin();
5513 End = IdTable->key_end();
5514}
5515
Chris Lattner5f9e2722011-07-23 10:55:15 +00005516StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005517 while (Current == End) {
5518 // If we have exhausted all of our AST files, we're done.
5519 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005520 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005521
5522 --Index;
5523 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005524 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5525 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005526 Current = IdTable->key_begin();
5527 End = IdTable->key_end();
5528 }
5529
5530 // We have any identifiers remaining in the current AST file; return
5531 // the next one.
5532 std::pair<const char*, unsigned> Key = *Current;
5533 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005534 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005535}
5536
5537IdentifierIterator *ASTReader::getIdentifiers() const {
5538 return new ASTIdentifierIterator(*this);
5539}
5540
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005541namespace clang { namespace serialization {
5542 class ReadMethodPoolVisitor {
5543 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005544 Selector Sel;
5545 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005546 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5547 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005548
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005549 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005550 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5551 unsigned PriorGeneration)
5552 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005553
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005554 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005555 ReadMethodPoolVisitor *This
5556 = static_cast<ReadMethodPoolVisitor *>(UserData);
5557
5558 if (!M.SelectorLookupTable)
5559 return false;
5560
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005561 // If we've already searched this module file, skip it now.
5562 if (M.Generation <= This->PriorGeneration)
5563 return true;
5564
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005565 ASTSelectorLookupTable *PoolTable
5566 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5567 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5568 if (Pos == PoolTable->end())
5569 return false;
5570
5571 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005572 // FIXME: Not quite happy with the statistics here. We probably should
5573 // disable this tracking when called via LoadSelector.
5574 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005575 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005576 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005577 if (This->Reader.DeserializationListener)
5578 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5579 This->Sel);
5580
5581 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5582 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5583 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005584 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005585
5586 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005587 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5588 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005589 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005590
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005591 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005592 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5593 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005594 }
5595 };
5596} } // end namespace clang::serialization
5597
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005598/// \brief Add the given set of methods to the method list.
5599static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5600 ObjCMethodList &List) {
5601 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5602 S.addMethodToGlobalList(&List, Methods[I]);
5603 }
5604}
5605
5606void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005607 // Get the selector generation and update it to the current generation.
5608 unsigned &Generation = SelectorGeneration[Sel];
5609 unsigned PriorGeneration = Generation;
5610 Generation = CurrentGeneration;
5611
5612 // Search for methods defined with this selector.
5613 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005614 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005615
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005616 if (Visitor.getInstanceMethods().empty() &&
5617 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005618 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005619 return;
5620 }
5621
5622 if (!getSema())
5623 return;
5624
5625 Sema &S = *getSema();
5626 Sema::GlobalMethodPool::iterator Pos
5627 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5628
5629 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5630 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005631}
5632
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005633void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005634 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005635 Namespaces.clear();
5636
5637 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5638 if (NamespaceDecl *Namespace
5639 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5640 Namespaces.push_back(Namespace);
5641 }
5642}
5643
Douglas Gregora8623202011-07-27 20:58:46 +00005644void ASTReader::ReadTentativeDefinitions(
5645 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5646 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5647 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5648 if (Var)
5649 TentativeDefs.push_back(Var);
5650 }
5651 TentativeDefinitions.clear();
5652}
5653
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005654void ASTReader::ReadUnusedFileScopedDecls(
5655 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5656 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5657 DeclaratorDecl *D
5658 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5659 if (D)
5660 Decls.push_back(D);
5661 }
5662 UnusedFileScopedDecls.clear();
5663}
5664
Douglas Gregor0129b562011-07-27 21:57:17 +00005665void ASTReader::ReadDelegatingConstructors(
5666 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5667 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5668 CXXConstructorDecl *D
5669 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5670 if (D)
5671 Decls.push_back(D);
5672 }
5673 DelegatingCtorDecls.clear();
5674}
5675
Douglas Gregord58a0a52011-07-28 00:39:29 +00005676void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5677 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5678 TypedefNameDecl *D
5679 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5680 if (D)
5681 Decls.push_back(D);
5682 }
5683 ExtVectorDecls.clear();
5684}
5685
Douglas Gregora126f172011-07-28 00:53:40 +00005686void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5687 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5688 CXXRecordDecl *D
5689 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5690 if (D)
5691 Decls.push_back(D);
5692 }
5693 DynamicClasses.clear();
5694}
5695
Douglas Gregorec12ce22011-07-28 14:20:37 +00005696void
5697ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5698 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5699 NamedDecl *D
5700 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5701 if (D)
5702 Decls.push_back(D);
5703 }
5704 LocallyScopedExternalDecls.clear();
5705}
5706
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005707void ASTReader::ReadReferencedSelectors(
5708 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5709 if (ReferencedSelectorsData.empty())
5710 return;
5711
5712 // If there are @selector references added them to its pool. This is for
5713 // implementation of -Wselector.
5714 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5715 unsigned I = 0;
5716 while (I < DataSize) {
5717 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5718 SourceLocation SelLoc
5719 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5720 Sels.push_back(std::make_pair(Sel, SelLoc));
5721 }
5722 ReferencedSelectorsData.clear();
5723}
5724
Douglas Gregor31e37b22011-07-28 18:09:57 +00005725void ASTReader::ReadWeakUndeclaredIdentifiers(
5726 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5727 if (WeakUndeclaredIdentifiers.empty())
5728 return;
5729
5730 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5731 IdentifierInfo *WeakId
5732 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5733 IdentifierInfo *AliasId
5734 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5735 SourceLocation Loc
5736 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5737 bool Used = WeakUndeclaredIdentifiers[I++];
5738 WeakInfo WI(AliasId, Loc);
5739 WI.setUsed(Used);
5740 WeakIDs.push_back(std::make_pair(WeakId, WI));
5741 }
5742 WeakUndeclaredIdentifiers.clear();
5743}
5744
Douglas Gregordfe65432011-07-28 19:11:31 +00005745void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5746 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5747 ExternalVTableUse VT;
5748 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5749 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5750 VT.DefinitionRequired = VTableUses[Idx++];
5751 VTables.push_back(VT);
5752 }
5753
5754 VTableUses.clear();
5755}
5756
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005757void ASTReader::ReadPendingInstantiations(
5758 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5759 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5760 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5761 SourceLocation Loc
5762 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005763
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005764 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005765 }
5766 PendingInstantiations.clear();
5767}
5768
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005769void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005770 // It would be complicated to avoid reading the methods anyway. So don't.
5771 ReadMethodPool(Sel);
5772}
5773
Douglas Gregor95eab172011-07-28 20:55:49 +00005774void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005775 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005776 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005777 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005778 if (DeserializationListener)
5779 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005780}
5781
Douglas Gregord89275b2009-07-06 18:54:52 +00005782/// \brief Set the globally-visible declarations associated with the given
5783/// identifier.
5784///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005785/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005786/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005787/// them.
5788///
5789/// \param II an IdentifierInfo that refers to one or more globally-visible
5790/// declarations.
5791///
5792/// \param DeclIDs the set of declaration IDs with the name @p II that are
5793/// visible at global scope.
5794///
5795/// \param Nonrecursive should be true to indicate that the caller knows that
5796/// this call is non-recursive, and therefore the globally-visible declarations
5797/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005798void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005799ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005800 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005801 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005802 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005803 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5804 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5805 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005806 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005807 return;
5808 }
Mike Stump1eb44332009-09-09 15:08:12 +00005809
Douglas Gregord89275b2009-07-06 18:54:52 +00005810 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5811 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5812 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005813 // Introduce this declaration into the translation-unit scope
5814 // and add it to the declaration chain for this identifier, so
5815 // that (unqualified) name lookup will find it.
5816 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005817 } else {
5818 // Queue this declaration so that it will be added to the
5819 // translation unit scope and identifier's declaration chain
5820 // once a Sema object is known.
5821 PreloadedDecls.push_back(D);
5822 }
5823 }
5824}
5825
Douglas Gregor95eab172011-07-28 20:55:49 +00005826IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005827 if (ID == 0)
5828 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005829
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005830 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005831 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005832 return 0;
5833 }
Mike Stump1eb44332009-09-09 15:08:12 +00005834
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005835 ID -= 1;
5836 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005837 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5838 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005839 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005840 unsigned Index = ID - M->BaseIdentifierID;
5841 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005842
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005843 // All of the strings in the AST file are preceded by a 16-bit length.
5844 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005845 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5846 // unsigned integers. This is important to avoid integer overflow when
5847 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005848 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005849 unsigned StrLen = (((unsigned) StrLenPtr[0])
5850 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005851 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005852 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005853 if (DeserializationListener)
5854 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005855 }
Mike Stump1eb44332009-09-09 15:08:12 +00005856
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005857 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005858}
5859
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005860IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005861 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5862}
5863
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005864IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005865 if (LocalID < NUM_PREDEF_IDENT_IDS)
5866 return LocalID;
5867
5868 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5869 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5870 assert(I != M.IdentifierRemap.end()
5871 && "Invalid index into identifier index remap");
5872
5873 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005874}
5875
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00005876MacroInfo *ASTReader::getMacro(MacroID ID, MacroInfo *Hint) {
Douglas Gregora8235d62012-10-09 23:05:51 +00005877 if (ID == 0)
5878 return 0;
5879
5880 if (MacrosLoaded.empty()) {
5881 Error("no macro table in AST file");
5882 return 0;
5883 }
5884
5885 ID -= NUM_PREDEF_MACRO_IDS;
5886 if (!MacrosLoaded[ID]) {
5887 GlobalMacroMapType::iterator I
5888 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
5889 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
5890 ModuleFile *M = I->second;
5891 unsigned Index = ID - M->BaseMacroID;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00005892 ReadMacroRecord(*M, M->MacroOffsets[Index], Hint);
Douglas Gregora8235d62012-10-09 23:05:51 +00005893 }
5894
5895 return MacrosLoaded[ID];
5896}
5897
5898MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
5899 if (LocalID < NUM_PREDEF_MACRO_IDS)
5900 return LocalID;
5901
5902 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5903 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
5904 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
5905
5906 return LocalID + I->second;
5907}
5908
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005909bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005910 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005911}
5912
Douglas Gregor26ced122011-12-01 00:59:36 +00005913serialization::SubmoduleID
5914ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5915 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5916 return LocalID;
5917
5918 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5919 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5920 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005921 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00005922
5923 return LocalID + I->second;
5924}
5925
5926Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5927 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5928 assert(GlobalID == 0 && "Unhandled global submodule ID");
5929 return 0;
5930 }
5931
5932 if (GlobalID > SubmodulesLoaded.size()) {
5933 Error("submodule ID out of range in AST file");
5934 return 0;
5935 }
5936
5937 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5938}
5939
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005940Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005941 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5942}
5943
5944Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005945 if (ID == 0)
5946 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005947
Sebastian Redl725cd962010-08-04 20:40:17 +00005948 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005949 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005950 return Selector();
5951 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005952
Sebastian Redl725cd962010-08-04 20:40:17 +00005953 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005954 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005955 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5956 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005957 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005958 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005959 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005960 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005961 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005962 if (DeserializationListener)
5963 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005964 }
5965
Sebastian Redl725cd962010-08-04 20:40:17 +00005966 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005967}
5968
Douglas Gregor8451ec72011-07-28 14:41:43 +00005969Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005970 return DecodeSelector(ID);
5971}
5972
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005973uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005974 // ID 0 (the null selector) is considered an external selector.
5975 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005976}
5977
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005978serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005979ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005980 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5981 return LocalID;
5982
5983 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5984 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5985 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005986 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005987
5988 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005989}
5990
Mike Stump1eb44332009-09-09 15:08:12 +00005991DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005992ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005993 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005994 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5995 switch (Kind) {
5996 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005997 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005998
5999 case DeclarationName::ObjCZeroArgSelector:
6000 case DeclarationName::ObjCOneArgSelector:
6001 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00006002 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006003
6004 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006005 return Context.DeclarationNames.getCXXConstructorName(
6006 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006007
6008 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006009 return Context.DeclarationNames.getCXXDestructorName(
6010 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006011
6012 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00006013 return Context.DeclarationNames.getCXXConversionFunctionName(
6014 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00006015
6016 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006017 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00006018 (OverloadedOperatorKind)Record[Idx++]);
6019
Sean Hunt3e518bd2009-11-29 07:34:05 +00006020 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00006021 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00006022 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00006023
Douglas Gregor2cf26342009-04-09 22:27:44 +00006024 case DeclarationName::CXXUsingDirective:
6025 return DeclarationName::getUsingDirectiveName();
6026 }
6027
David Blaikie7530c032012-01-17 06:56:22 +00006028 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00006029}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006030
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006031void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006032 DeclarationNameLoc &DNLoc,
6033 DeclarationName Name,
6034 const RecordData &Record, unsigned &Idx) {
6035 switch (Name.getNameKind()) {
6036 case DeclarationName::CXXConstructorName:
6037 case DeclarationName::CXXDestructorName:
6038 case DeclarationName::CXXConversionFunctionName:
6039 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
6040 break;
6041
6042 case DeclarationName::CXXOperatorName:
6043 DNLoc.CXXOperatorName.BeginOpNameLoc
6044 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6045 DNLoc.CXXOperatorName.EndOpNameLoc
6046 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6047 break;
6048
6049 case DeclarationName::CXXLiteralOperatorName:
6050 DNLoc.CXXLiteralOperatorName.OpNameLoc
6051 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
6052 break;
6053
6054 case DeclarationName::Identifier:
6055 case DeclarationName::ObjCZeroArgSelector:
6056 case DeclarationName::ObjCOneArgSelector:
6057 case DeclarationName::ObjCMultiArgSelector:
6058 case DeclarationName::CXXUsingDirective:
6059 break;
6060 }
6061}
6062
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006063void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006064 DeclarationNameInfo &NameInfo,
6065 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006066 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006067 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6068 DeclarationNameLoc DNLoc;
6069 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6070 NameInfo.setInfo(DNLoc);
6071}
6072
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006073void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006074 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006075 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006076 unsigned NumTPLists = Record[Idx++];
6077 Info.NumTemplParamLists = NumTPLists;
6078 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006079 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006080 for (unsigned i=0; i != NumTPLists; ++i)
6081 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6082 }
6083}
6084
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006085TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006086ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006087 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006088 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006089 switch (Kind) {
6090 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006091 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006092
6093 case TemplateName::OverloadedTemplate: {
6094 unsigned size = Record[Idx++];
6095 UnresolvedSet<8> Decls;
6096 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006097 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006098
Douglas Gregor35942772011-09-09 21:34:22 +00006099 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006100 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006101
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006102 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006103 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006104 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006105 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006106 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006107 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006108
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006109 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006110 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006111 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006112 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006113 GetIdentifierInfo(F, Record,
6114 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006115 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006116 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006117 }
John McCall14606042011-06-30 08:33:18 +00006118
6119 case TemplateName::SubstTemplateTemplateParm: {
6120 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006121 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006122 if (!param) return TemplateName();
6123 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006124 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006125 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006126
6127 case TemplateName::SubstTemplateTemplateParmPack: {
6128 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006129 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006130 if (!Param)
6131 return TemplateName();
6132
6133 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6134 if (ArgPack.getKind() != TemplateArgument::Pack)
6135 return TemplateName();
6136
Douglas Gregor35942772011-09-09 21:34:22 +00006137 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006138 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006139 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006140
David Blaikieb219cfc2011-09-23 05:06:16 +00006141 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006142}
6143
6144TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006145ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006146 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006147 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6148 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006149 case TemplateArgument::Null:
6150 return TemplateArgument();
6151 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006152 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006153 case TemplateArgument::Declaration: {
6154 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6155 bool ForReferenceParam = Record[Idx++];
6156 return TemplateArgument(D, ForReferenceParam);
6157 }
6158 case TemplateArgument::NullPtr:
6159 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006160 case TemplateArgument::Integral: {
6161 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006162 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006163 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006164 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006165 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006166 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006167 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006168 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006169 llvm::Optional<unsigned> NumTemplateExpansions;
6170 if (unsigned NumExpansions = Record[Idx++])
6171 NumTemplateExpansions = NumExpansions - 1;
6172 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006173 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006174 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006175 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006176 case TemplateArgument::Pack: {
6177 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006178 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006179 for (unsigned I = 0; I != NumArgs; ++I)
6180 Args[I] = ReadTemplateArgument(F, Record, Idx);
6181 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006182 }
6183 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006184
David Blaikieb219cfc2011-09-23 05:06:16 +00006185 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006186}
6187
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006188TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006189ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006190 const RecordData &Record, unsigned &Idx) {
6191 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6192 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6193 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006194
6195 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006196 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006197 Params.reserve(NumParams);
6198 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006199 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006200
6201 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006202 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006203 Params.data(), Params.size(), RAngleLoc);
6204 return TemplateParams;
6205}
6206
6207void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006208ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006209ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006210 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006211 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006212 unsigned NumTemplateArgs = Record[Idx++];
6213 TemplArgs.reserve(NumTemplateArgs);
6214 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006215 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006216}
6217
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006218/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006219void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006220 const RecordData &Record, unsigned &Idx) {
6221 unsigned NumDecls = Record[Idx++];
6222 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006223 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006224 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6225 Set.addDecl(D, AS);
6226 }
6227}
6228
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006229CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006230ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006231 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006232 bool isVirtual = static_cast<bool>(Record[Idx++]);
6233 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6234 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006235 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006236 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6237 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006238 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006239 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006240 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006241 Result.setInheritConstructors(inheritConstructors);
6242 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006243}
6244
Sean Huntcbb67482011-01-08 20:30:50 +00006245std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006246ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006247 unsigned &Idx) {
6248 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006249 unsigned NumInitializers = Record[Idx++];
6250 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006251 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006252 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006253 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006254 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006255 bool IsBaseVirtual = false;
6256 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006257 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006258
Sean Hunt156b6402011-05-04 01:19:08 +00006259 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6260 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006261 case CTOR_INITIALIZER_BASE:
6262 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006263 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006264 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006265
6266 case CTOR_INITIALIZER_DELEGATING:
6267 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006268 break;
6269
6270 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006271 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006272 break;
6273
6274 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006275 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006276 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006277 }
Sean Hunt156b6402011-05-04 01:19:08 +00006278
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006279 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006280 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006281 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6282 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006283 bool IsWritten = Record[Idx++];
6284 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006285 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006286 if (IsWritten) {
6287 SourceOrderOrNumArrayIndices = Record[Idx++];
6288 } else {
6289 SourceOrderOrNumArrayIndices = Record[Idx++];
6290 Indices.reserve(SourceOrderOrNumArrayIndices);
6291 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006292 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006293 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006294
Sean Huntcbb67482011-01-08 20:30:50 +00006295 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006296 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006297 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006298 LParenLoc, Init, RParenLoc,
6299 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006300 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006301 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6302 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006303 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006304 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006305 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006306 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006307 else
Douglas Gregor35942772011-09-09 21:34:22 +00006308 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006309 MemberOrEllipsisLoc, LParenLoc,
6310 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006311 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006312 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006313 LParenLoc, Init, RParenLoc,
6314 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006315 }
6316
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006317 if (IsWritten)
6318 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006319 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006320 }
6321 }
6322
Sean Huntcbb67482011-01-08 20:30:50 +00006323 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006324}
6325
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006326NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006327ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006328 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006329 unsigned N = Record[Idx++];
6330 NestedNameSpecifier *NNS = 0, *Prev = 0;
6331 for (unsigned I = 0; I != N; ++I) {
6332 NestedNameSpecifier::SpecifierKind Kind
6333 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6334 switch (Kind) {
6335 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006336 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006337 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006338 break;
6339 }
6340
6341 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006342 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006343 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006344 break;
6345 }
6346
Douglas Gregor14aba762011-02-24 02:36:08 +00006347 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006348 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006349 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006350 break;
6351 }
6352
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006353 case NestedNameSpecifier::TypeSpec:
6354 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006355 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006356 if (!T)
6357 return 0;
6358
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006359 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006360 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006361 break;
6362 }
6363
6364 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006365 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006366 // No associated value, and there can't be a prefix.
6367 break;
6368 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006369 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006370 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006371 }
6372 return NNS;
6373}
6374
Douglas Gregordc355712011-02-25 00:36:19 +00006375NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006376ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006377 unsigned &Idx) {
6378 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006379 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006380 for (unsigned I = 0; I != N; ++I) {
6381 NestedNameSpecifier::SpecifierKind Kind
6382 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6383 switch (Kind) {
6384 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006385 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006386 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006387 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006388 break;
6389 }
6390
6391 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006392 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006393 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006394 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006395 break;
6396 }
6397
6398 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006399 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006400 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006401 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006402 break;
6403 }
6404
6405 case NestedNameSpecifier::TypeSpec:
6406 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006407 bool Template = Record[Idx++];
6408 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6409 if (!T)
6410 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006411 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006412
6413 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006414 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006415 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6416 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006417 break;
6418 }
6419
6420 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006421 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006422 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006423 break;
6424 }
6425 }
Douglas Gregordc355712011-02-25 00:36:19 +00006426 }
6427
Douglas Gregor35942772011-09-09 21:34:22 +00006428 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006429}
6430
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006431SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006432ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006433 unsigned &Idx) {
6434 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6435 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006436 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006437}
6438
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006439/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006440llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006441 unsigned BitWidth = Record[Idx++];
6442 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6443 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6444 Idx += NumWords;
6445 return Result;
6446}
6447
6448/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006449llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006450 bool isUnsigned = Record[Idx++];
6451 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6452}
6453
Douglas Gregor17fc2232009-04-14 21:55:33 +00006454/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006455llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006456 return llvm::APFloat(ReadAPInt(Record, Idx));
6457}
6458
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006459// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006460std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006461 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006462 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006463 Idx += Len;
6464 return Result;
6465}
6466
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006467VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6468 unsigned &Idx) {
6469 unsigned Major = Record[Idx++];
6470 unsigned Minor = Record[Idx++];
6471 unsigned Subminor = Record[Idx++];
6472 if (Minor == 0)
6473 return VersionTuple(Major);
6474 if (Subminor == 0)
6475 return VersionTuple(Major, Minor - 1);
6476 return VersionTuple(Major, Minor - 1, Subminor - 1);
6477}
6478
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006479CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006480 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006481 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006482 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006483 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006484}
6485
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006486DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006487 return Diag(SourceLocation(), DiagID);
6488}
6489
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006490DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006491 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006492}
Douglas Gregor025452f2009-04-17 00:04:06 +00006493
Douglas Gregor668c1a42009-04-21 22:25:48 +00006494/// \brief Retrieve the identifier table associated with the
6495/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006496IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006497 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006498}
6499
Douglas Gregor025452f2009-04-17 00:04:06 +00006500/// \brief Record that the given ID maps to the given switch-case
6501/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006502void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006503 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6504 "Already have a SwitchCase with this ID");
6505 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006506}
6507
6508/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006509SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006510 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6511 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006512}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006513
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006514void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006515 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006516}
6517
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006518void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006519 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006520 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6521 serialization::ModuleFile *> >::iterator
6522 I = CommentsCursors.begin(),
6523 E = CommentsCursors.end();
6524 I != E; ++I) {
6525 llvm::BitstreamCursor &Cursor = I->first;
6526 serialization::ModuleFile &F = *I->second;
6527 SavedStreamPosition SavedPosition(Cursor);
6528
6529 RecordData Record;
6530 while (true) {
6531 unsigned Code = Cursor.ReadCode();
6532 if (Code == llvm::bitc::END_BLOCK)
6533 break;
6534
6535 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6536 // No known subblocks, always skip them.
6537 Cursor.ReadSubBlockID();
6538 if (Cursor.SkipBlock()) {
6539 Error("malformed block record in AST file");
6540 return;
6541 }
6542 continue;
6543 }
6544
6545 if (Code == llvm::bitc::DEFINE_ABBREV) {
6546 Cursor.ReadAbbrevRecord();
6547 continue;
6548 }
6549
6550 // Read a record.
6551 Record.clear();
6552 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006553 case COMMENTS_RAW_COMMENT: {
6554 unsigned Idx = 0;
6555 SourceRange SR = ReadSourceRange(F, Record, Idx);
6556 RawComment::CommentKind Kind =
6557 (RawComment::CommentKind) Record[Idx++];
6558 bool IsTrailingComment = Record[Idx++];
6559 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006560 Comments.push_back(new (Context) RawComment(SR, Kind,
6561 IsTrailingComment,
6562 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006563 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006564 }
6565 }
6566 }
6567 }
6568 Context.Comments.addCommentsToFront(Comments);
6569}
6570
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006571void ASTReader::finishPendingActions() {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006572 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty() ||
6573 !PendingMacroIDs.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006574 // If any identifiers with corresponding top-level declarations have
6575 // been loaded, load those declarations now.
6576 while (!PendingIdentifierInfos.empty()) {
6577 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6578 PendingIdentifierInfos.front().DeclIDs, true);
6579 PendingIdentifierInfos.pop_front();
6580 }
6581
Douglas Gregora1be2782011-12-17 23:38:30 +00006582 // Load pending declaration chains.
6583 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6584 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006585 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006586 }
6587 PendingDeclChains.clear();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00006588
6589 // Load any pending macro definitions.
Douglas Gregore9652bf2012-10-11 17:31:34 +00006590 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
6591 // FIXME: std::move here
6592 SmallVector<MacroID, 2> GlobalIDs = PendingMacroIDs.begin()[I].second;
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006593 MacroInfo *Hint = 0;
Douglas Gregore9652bf2012-10-11 17:31:34 +00006594 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
6595 ++IDIdx) {
Douglas Gregor3ab50fe2012-10-11 17:41:54 +00006596 Hint = getMacro(GlobalIDs[IDIdx], Hint);
Douglas Gregore9652bf2012-10-11 17:31:34 +00006597 }
6598 }
6599 PendingMacroIDs.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006600 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006601
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006602 // If we deserialized any C++ or Objective-C class definitions, any
6603 // Objective-C protocol definitions, or any redeclarable templates, make sure
6604 // that all redeclarations point to the definitions. Note that this can only
6605 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006606 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6607 DEnd = PendingDefinitions.end();
6608 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006609 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6610 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6611 // Make sure that the TagType points at the definition.
6612 const_cast<TagType*>(TagT)->decl = TD;
6613 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006614
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006615 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6616 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6617 REnd = RD->redecls_end();
6618 R != REnd; ++R)
6619 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6620
6621 }
6622
Douglas Gregorfc529f72011-12-19 19:00:47 +00006623 continue;
6624 }
6625
Douglas Gregor1d784b22012-01-01 19:51:50 +00006626 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006627 // Make sure that the ObjCInterfaceType points at the definition.
6628 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6629 ->Decl = ID;
6630
Douglas Gregor1d784b22012-01-01 19:51:50 +00006631 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6632 REnd = ID->redecls_end();
6633 R != REnd; ++R)
6634 R->Data = ID->Data;
6635
6636 continue;
6637 }
6638
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006639 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6640 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6641 REnd = PD->redecls_end();
6642 R != REnd; ++R)
6643 R->Data = PD->Data;
6644
6645 continue;
6646 }
6647
6648 RedeclarableTemplateDecl *RTD
6649 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6650 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6651 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006652 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006653 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006654 }
6655 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006656
6657 // Load the bodies of any functions or methods we've encountered. We do
6658 // this now (delayed) so that we can be sure that the declaration chains
6659 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006660 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6661 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006662 PB != PBEnd; ++PB) {
6663 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6664 // FIXME: Check for =delete/=default?
6665 // FIXME: Complain about ODR violations here?
6666 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6667 FD->setLazyBody(PB->second);
6668 continue;
6669 }
6670
6671 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6672 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6673 MD->setLazyBody(PB->second);
6674 }
6675 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006676}
6677
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006678void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006679 assert(NumCurrentElementsDeserializing &&
6680 "FinishedDeserializing not paired with StartedDeserializing");
6681 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006682 // We decrease NumCurrentElementsDeserializing only after pending actions
6683 // are finished, to avoid recursively re-calling finishPendingActions().
6684 finishPendingActions();
6685 }
6686 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006687
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006688 if (NumCurrentElementsDeserializing == 0 &&
6689 Consumer && !PassingDeclsToConsumer) {
6690 // Guard variable to avoid recursively redoing the process of passing
6691 // decls to consumer.
6692 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6693 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006694
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006695 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006696 // We are not in recursive loading, so it's safe to pass the "interesting"
6697 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006698 Decl *D = InterestingDecls.front();
6699 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006700 PassInterestingDeclToConsumer(D);
6701 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006702 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006703}
Douglas Gregor501c1032010-08-19 00:28:17 +00006704
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006705ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006706 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006707 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006708 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6709 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006710 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006711 Consumer(0), ModuleMgr(PP.getFileManager()),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006712 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006713 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006714 DisableStatCache(DisableStatCache),
6715 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006716 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6717 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006718 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006719 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6720 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6721 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6722 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006723 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6724 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006725 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006726 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006727{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006728 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006729}
6730
Sebastian Redle1dde812010-08-24 00:50:04 +00006731ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006732 for (DeclContextVisibleUpdatesPending::iterator
6733 I = PendingVisibleUpdates.begin(),
6734 E = PendingVisibleUpdates.end();
6735 I != E; ++I) {
6736 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6737 F = I->second.end();
6738 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006739 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006740 }
6741}