blob: 4e6673bedd132d504e1cf246c3834867ac181801 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Chandler Carrutha2398d72011-12-09 00:02:23 +000017#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000018#include "ASTCommon.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000019#include "ASTReaderInternals.h"
Douglas Gregore737f502010-08-12 20:07:10 +000020#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000021#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000022#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000027#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000028#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000029#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000030#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000031#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000033#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000038#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000039#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000040#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000041#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000045#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000046#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000048#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000049#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000050#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000051#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000053#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000054
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000057using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
59//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000060// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000061//===----------------------------------------------------------------------===//
62
Sebastian Redl571db7f2010-08-18 23:56:56 +000063ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000064
65bool
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +000066PCHValidator::ReadLanguageOptions(const ModuleFile &M,
67 const LangOptions &LangOpts) {
David Blaikie4e4d0842012-03-11 07:00:24 +000068 const LangOptions &PPLangOpts = PP.getLangOpts();
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000069
70#define LANGOPT(Name, Bits, Default, Description) \
71 if (PPLangOpts.Name != LangOpts.Name) { \
72 Reader.Diag(diag::err_pch_langopt_mismatch) \
73 << Description << LangOpts.Name << PPLangOpts.Name; \
74 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000075 }
76
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000077#define VALUE_LANGOPT(Name, Bits, Default, Description) \
78 if (PPLangOpts.Name != LangOpts.Name) { \
79 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
80 << Description; \
81 return true; \
82}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000083
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000084#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
85 if (PPLangOpts.get##Name() != LangOpts.get##Name()) { \
86 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
87 << Description; \
88 return true; \
89 }
90
91#define BENIGN_LANGOPT(Name, Bits, Default, Description)
92#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
93#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +000094
95 if (PPLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
96 Reader.Diag(diag::err_pch_langopt_value_mismatch)
97 << "target Objective-C runtime";
98 return true;
99 }
Douglas Gregor7d5e81b2011-09-13 18:26:39 +0000100
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000101 return false;
102}
103
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000104bool PCHValidator::ReadTargetTriple(const ModuleFile &M, StringRef Triple) {
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000105 if (Triple == PP.getTargetInfo().getTriple().str())
106 return false;
107
108 Reader.Diag(diag::warn_pch_target_triple)
109 << Triple << PP.getTargetInfo().getTriple().str();
110 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000111}
112
Benjamin Kramer54353f42010-11-25 18:29:30 +0000113namespace {
114 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000115 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000116 };
117 struct EmptyBlock {
118 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
119 };
120}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000121
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000123 PCHPredefinesBlocks R) {
124 // First, sum up the lengths.
125 unsigned LL = 0, RL = 0;
126 for (unsigned I = 0, N = L.size(); I != N; ++I) {
127 LL += L[I].size();
128 }
129 for (unsigned I = 0, N = R.size(); I != N; ++I) {
130 RL += R[I].Data.size();
131 }
132 if (LL != RL)
133 return false;
134 if (LL == 0 && RL == 0)
135 return true;
136
137 // Kick out empty parts, they confuse the algorithm below.
138 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
139 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
140
141 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000143 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000144 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000145 for (;;) {
146 // Compare the current pieces.
147 if (LR.size() == RR.size()) {
148 // If they're the same length, it's pretty easy.
149 if (LR != RR)
150 return false;
151 // Both pieces are done, advance.
152 ++LI;
153 ++RI;
154 // If either string is done, they're both done, since they're the same
155 // length.
156 if (LI == LN) {
157 assert(RI == RN && "Strings not the same length after all?");
158 return true;
159 }
160 LR = L[LI];
161 RR = R[RI].Data;
162 } else if (LR.size() < RR.size()) {
163 // Right piece is longer.
164 if (!RR.startswith(LR))
165 return false;
166 ++LI;
167 assert(LI != LN && "Strings not the same length after all?");
168 RR = RR.substr(LR.size());
169 LR = L[LI];
170 } else {
171 // Left piece is longer.
172 if (!LR.startswith(RR))
173 return false;
174 ++RI;
175 assert(RI != RN && "Strings not the same length after all?");
176 LR = LR.substr(RR.size());
177 RR = R[RI].Data;
178 }
179 }
180}
181
Chris Lattner5f9e2722011-07-23 10:55:15 +0000182static std::pair<FileID, StringRef::size_type>
183FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
184 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000185 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
186 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000188 Res.first = Buffers[I].BufferID;
189 break;
190 }
191 }
192 return Res;
193}
194
195bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000196 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000197 std::string &SuggestedPredefines,
198 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000199 // We are in the context of an implicit include, so the predefines buffer will
200 // have a #include entry for the PCH file itself (as normalized by the
201 // preprocessor initialization). Find it and skip over it in the checking
202 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000203 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000204 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000205 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
206 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000207 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 std::pair<StringRef,StringRef> Split =
209 StringRef(PP.getPredefines()).split(PCHInclude.str());
210 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000211 if (Left == PP.getPredefines()) {
212 Error("Missing PCH include entry!");
213 return true;
214 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000215
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000216 // If the concatenation of all the PCH buffers is equal to the adjusted
217 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000218 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000219 CommandLine.push_back(Left);
220 CommandLine.push_back(Right);
221 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000222 return false;
223
224 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000226 // The predefines buffers are different. Determine what the differences are,
227 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000228 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000229 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
230 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000231
Chris Lattner5f9e2722011-07-23 10:55:15 +0000232 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000233 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000234
235 // Pick out implicit #includes after the PCH and don't consider them for
236 // validation; we will insert them into SuggestedPredefines so that the
237 // preprocessor includes them.
238 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000240 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
241 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
242 if (AfterPCHLines[i].startswith("#include ")) {
243 IncludesAfterPCH += AfterPCHLines[i];
244 IncludesAfterPCH += '\n';
245 } else {
246 CmdLineLines.push_back(AfterPCHLines[i]);
247 }
248 }
249
250 // Make sure we add the includes last into SuggestedPredefines before we
251 // exit this function.
252 struct AddIncludesRAII {
253 std::string &SuggestedPredefines;
254 std::string &IncludesAfterPCH;
255
256 AddIncludesRAII(std::string &SuggestedPredefines,
257 std::string &IncludesAfterPCH)
258 : SuggestedPredefines(SuggestedPredefines),
259 IncludesAfterPCH(IncludesAfterPCH) { }
260 ~AddIncludesRAII() {
261 SuggestedPredefines += IncludesAfterPCH;
262 }
263 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000264
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000265 // Sort both sets of predefined buffer lines, since we allow some extra
266 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000267 std::sort(CmdLineLines.begin(), CmdLineLines.end());
268 std::sort(PCHLines.begin(), PCHLines.end());
269
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000270 // Determine which predefines that were used to build the PCH file are missing
271 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000272 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000273 std::set_difference(PCHLines.begin(), PCHLines.end(),
274 CmdLineLines.begin(), CmdLineLines.end(),
275 std::back_inserter(MissingPredefines));
276
277 bool MissingDefines = false;
278 bool ConflictingDefines = false;
279 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000281 if (Missing.startswith("#include ")) {
282 // An -include was specified when generating the PCH; it is included in
283 // the PCH, just ignore it.
284 continue;
285 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000286 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000287 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
288 return true;
289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000291 // This is a macro definition. Determine the name of the macro we're
292 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000293 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000294 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000295 = Missing.find_first_of("( \n\r", StartOfMacroName);
296 assert(EndOfMacroName != std::string::npos &&
297 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000298 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000299
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000300 // Determine whether this macro was given a different definition on the
301 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000302 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000303 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000304 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000305 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
306 MacroDefStart);
307 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000308 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000309 // Different macro; we're done.
310 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000311 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
314 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000315 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000316 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000317 (*ConflictPos)[MacroDefLen] != '(')
318 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000320 // We found a conflicting macro definition.
321 break;
322 }
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000324 if (ConflictPos != CmdLineLines.end()) {
325 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
326 << MacroName;
327
328 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000329 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000330 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000331 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000332 SourceLocation PCHMissingLoc =
333 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000334 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000335 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000336
337 ConflictingDefines = true;
338 continue;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000341 // If the macro doesn't conflict, then we'll just pick up the macro
342 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000343 if (ConflictingDefines)
344 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000346 if (!MissingDefines) {
347 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
348 MissingDefines = true;
349 }
350
351 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000352 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000353 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000354 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000355 SourceLocation PCHMissingLoc =
356 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000357 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000358 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000361 if (ConflictingDefines)
362 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000364 // Determine what predefines were introduced based on command-line
365 // parameters that were not present when building the PCH
366 // file. Extra #defines are okay, so long as the identifiers being
367 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000368 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000369 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
370 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000371 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000372 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000373 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000374 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000375 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
376 return true;
377 }
378
379 // This is an extra macro definition. Determine the name of the
380 // macro we're defining.
381 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000382 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000383 = Extra.find_first_of("( \n\r", StartOfMacroName);
384 assert(EndOfMacroName != std::string::npos &&
385 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000386 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000387
388 // Check whether this name was used somewhere in the PCH file. If
389 // so, defining it as a macro could change behavior, so we reject
390 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000391 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000392 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000393 return true;
394 }
395
396 // Add this definition to the suggested predefines buffer.
397 SuggestedPredefines += Extra;
398 SuggestedPredefines += '\n';
399 }
400
401 // If we get here, it's because the predefines buffer had compatible
402 // contents. Accept the PCH file.
403 return false;
404}
405
Douglas Gregor12fab312010-03-16 16:35:32 +0000406void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
407 unsigned ID) {
408 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
409 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000410}
411
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +0000412void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000413 PP.setCounterValue(Value);
414}
415
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000416//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000417// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000418//===----------------------------------------------------------------------===//
419
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000420void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000421ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000422 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000423}
424
Chris Lattner4c6f9522009-04-27 05:14:47 +0000425
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000426
Douglas Gregor98339b92011-08-25 20:47:51 +0000427unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
428 return serialization::ComputeHash(Sel);
429}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000430
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Douglas Gregor98339b92011-08-25 20:47:51 +0000432std::pair<unsigned, unsigned>
433ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
434 using namespace clang::io;
435 unsigned KeyLen = ReadUnalignedLE16(d);
436 unsigned DataLen = ReadUnalignedLE16(d);
437 return std::make_pair(KeyLen, DataLen);
438}
439
440ASTSelectorLookupTrait::internal_key_type
441ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
442 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000443 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000444 unsigned N = ReadUnalignedLE16(d);
445 IdentifierInfo *FirstII
446 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
447 if (N == 0)
448 return SelTable.getNullarySelector(FirstII);
449 else if (N == 1)
450 return SelTable.getUnarySelector(FirstII);
451
452 SmallVector<IdentifierInfo *, 16> Args;
453 Args.push_back(FirstII);
454 for (unsigned I = 1; I != N; ++I)
455 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
456
457 return SelTable.getSelector(N, Args.data());
458}
459
460ASTSelectorLookupTrait::data_type
461ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
462 unsigned DataLen) {
463 using namespace clang::io;
464
465 data_type Result;
466
467 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
468 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
469 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
470
471 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000472 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
473 if (ObjCMethodDecl *Method
474 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
475 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Douglas Gregor98339b92011-08-25 20:47:51 +0000478 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000479 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
480 if (ObjCMethodDecl *Method
481 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
482 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000483 }
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregor98339b92011-08-25 20:47:51 +0000485 return Result;
486}
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor98339b92011-08-25 20:47:51 +0000488unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
489 return llvm::HashString(StringRef(a.first, a.second));
490}
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregor98339b92011-08-25 20:47:51 +0000492std::pair<unsigned, unsigned>
493ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
494 using namespace clang::io;
495 unsigned DataLen = ReadUnalignedLE16(d);
496 unsigned KeyLen = ReadUnalignedLE16(d);
497 return std::make_pair(KeyLen, DataLen);
498}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000499
Douglas Gregor98339b92011-08-25 20:47:51 +0000500std::pair<const char*, unsigned>
501ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
502 assert(n >= 2 && d[n-1] == '\0');
503 return std::make_pair((const char*) d, n-1);
504}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000505
Douglas Gregor98339b92011-08-25 20:47:51 +0000506IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
507 const unsigned char* d,
508 unsigned DataLen) {
509 using namespace clang::io;
510 unsigned RawID = ReadUnalignedLE32(d);
511 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Douglas Gregor98339b92011-08-25 20:47:51 +0000513 // Wipe out the "is interesting" bit.
514 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000515
Douglas Gregor98339b92011-08-25 20:47:51 +0000516 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
517 if (!IsInteresting) {
518 // For uninteresting identifiers, just build the IdentifierInfo
519 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000520 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000521 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000522 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000523 KnownII = II;
524 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000525 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000526 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000527 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000528 return II;
529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000531 unsigned ObjCOrBuiltinID = ReadUnalignedLE16(d);
Douglas Gregor98339b92011-08-25 20:47:51 +0000532 unsigned Bits = ReadUnalignedLE16(d);
533 bool CPlusPlusOperatorKeyword = Bits & 0x01;
534 Bits >>= 1;
535 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
536 Bits >>= 1;
537 bool Poisoned = Bits & 0x01;
538 Bits >>= 1;
539 bool ExtensionToken = Bits & 0x01;
540 Bits >>= 1;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000541 bool hadMacroDefinition = Bits & 0x01;
542 Bits >>= 1;
Douglas Gregor98339b92011-08-25 20:47:51 +0000543 bool hasMacroDefinition = Bits & 0x01;
544 Bits >>= 1;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000545
Douglas Gregor98339b92011-08-25 20:47:51 +0000546 assert(Bits == 0 && "Extra bits in the identifier?");
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000547 DataLen -= 8;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000548
Douglas Gregor98339b92011-08-25 20:47:51 +0000549 // Build the IdentifierInfo itself and link the identifier ID with
550 // the new IdentifierInfo.
551 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000552 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000553 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000554 KnownII = II;
555 }
Douglas Gregor057df202012-01-18 20:56:22 +0000556 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000557 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000558
Douglas Gregor98339b92011-08-25 20:47:51 +0000559 // Set or check the various bits in the IdentifierInfo structure.
560 // Token IDs are read-only.
561 if (HasRevertedTokenIDToIdentifier)
562 II->RevertTokenIDToIdentifier();
563 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
564 assert(II->isExtensionToken() == ExtensionToken &&
565 "Incorrect extension token flag");
566 (void)ExtensionToken;
567 if (Poisoned)
568 II->setIsPoisoned(true);
569 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
570 "Incorrect C++ operator keyword flag");
571 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000572
Douglas Gregor98339b92011-08-25 20:47:51 +0000573 // If this identifier is a macro, deserialize the macro
574 // definition.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000575 if (hadMacroDefinition) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000576 // FIXME: Check for conflicts?
Douglas Gregora8235d62012-10-09 23:05:51 +0000577 uint32_t LocalID = ReadUnalignedLE32(d);
Douglas Gregor13292642011-12-02 15:45:10 +0000578 unsigned LocalSubmoduleID = ReadUnalignedLE32(d);
579
580 // Determine whether this macro definition should be visible now, or
581 // whether it is in a hidden submodule.
582 bool Visible = true;
583 if (SubmoduleID GlobalSubmoduleID
584 = Reader.getGlobalSubmoduleID(F, LocalSubmoduleID)) {
585 if (Module *Owner = Reader.getSubmodule(GlobalSubmoduleID)) {
586 if (Owner->NameVisibility == Module::Hidden) {
587 // The owning module is not visible, and this macro definition should
588 // not be, either.
589 Visible = false;
590
591 // Note that this macro definition was hidden because its owning
592 // module is not yet visible.
593 Reader.HiddenNamesMap[Owner].push_back(II);
594 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000595 }
Douglas Gregor13292642011-12-02 15:45:10 +0000596 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +0000597
Douglas Gregora8235d62012-10-09 23:05:51 +0000598 Reader.setIdentifierIsMacro(II, Reader.getGlobalMacroID(F, LocalID),
599 Visible && hasMacroDefinition);
Douglas Gregor13292642011-12-02 15:45:10 +0000600 DataLen -= 8;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000601 }
602
Douglas Gregoreee242f2011-10-27 09:33:13 +0000603 Reader.SetIdentifierInfo(ID, II);
604
Douglas Gregor98339b92011-08-25 20:47:51 +0000605 // Read all of the declarations visible at global scope with this
606 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000607 if (DataLen > 0) {
608 SmallVector<uint32_t, 4> DeclIDs;
609 for (; DataLen > 0; DataLen -= 4)
610 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
611 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000612 }
613
Douglas Gregor98339b92011-08-25 20:47:51 +0000614 return II;
615}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000616
Douglas Gregor98339b92011-08-25 20:47:51 +0000617unsigned
618ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
619 llvm::FoldingSetNodeID ID;
620 ID.AddInteger(Key.Kind);
621
622 switch (Key.Kind) {
623 case DeclarationName::Identifier:
624 case DeclarationName::CXXLiteralOperatorName:
625 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
626 break;
627 case DeclarationName::ObjCZeroArgSelector:
628 case DeclarationName::ObjCOneArgSelector:
629 case DeclarationName::ObjCMultiArgSelector:
630 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
631 break;
632 case DeclarationName::CXXOperatorName:
633 ID.AddInteger((OverloadedOperatorKind)Key.Data);
634 break;
635 case DeclarationName::CXXConstructorName:
636 case DeclarationName::CXXDestructorName:
637 case DeclarationName::CXXConversionFunctionName:
638 case DeclarationName::CXXUsingDirective:
639 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000640 }
641
Douglas Gregor98339b92011-08-25 20:47:51 +0000642 return ID.ComputeHash();
643}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000644
Douglas Gregor98339b92011-08-25 20:47:51 +0000645ASTDeclContextNameLookupTrait::internal_key_type
646ASTDeclContextNameLookupTrait::GetInternalKey(
647 const external_key_type& Name) const {
648 DeclNameKey Key;
649 Key.Kind = Name.getNameKind();
650 switch (Name.getNameKind()) {
651 case DeclarationName::Identifier:
652 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
653 break;
654 case DeclarationName::ObjCZeroArgSelector:
655 case DeclarationName::ObjCOneArgSelector:
656 case DeclarationName::ObjCMultiArgSelector:
657 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
658 break;
659 case DeclarationName::CXXOperatorName:
660 Key.Data = Name.getCXXOverloadedOperator();
661 break;
662 case DeclarationName::CXXLiteralOperatorName:
663 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
664 break;
665 case DeclarationName::CXXConstructorName:
666 case DeclarationName::CXXDestructorName:
667 case DeclarationName::CXXConversionFunctionName:
668 case DeclarationName::CXXUsingDirective:
669 Key.Data = 0;
670 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000671 }
672
Douglas Gregor98339b92011-08-25 20:47:51 +0000673 return Key;
674}
675
Douglas Gregor98339b92011-08-25 20:47:51 +0000676std::pair<unsigned, unsigned>
677ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
678 using namespace clang::io;
679 unsigned KeyLen = ReadUnalignedLE16(d);
680 unsigned DataLen = ReadUnalignedLE16(d);
681 return std::make_pair(KeyLen, DataLen);
682}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000683
Douglas Gregor98339b92011-08-25 20:47:51 +0000684ASTDeclContextNameLookupTrait::internal_key_type
685ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
686 using namespace clang::io;
687
688 DeclNameKey Key;
689 Key.Kind = (DeclarationName::NameKind)*d++;
690 switch (Key.Kind) {
691 case DeclarationName::Identifier:
692 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
693 break;
694 case DeclarationName::ObjCZeroArgSelector:
695 case DeclarationName::ObjCOneArgSelector:
696 case DeclarationName::ObjCMultiArgSelector:
697 Key.Data =
698 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
699 .getAsOpaquePtr();
700 break;
701 case DeclarationName::CXXOperatorName:
702 Key.Data = *d++; // OverloadedOperatorKind
703 break;
704 case DeclarationName::CXXLiteralOperatorName:
705 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
706 break;
707 case DeclarationName::CXXConstructorName:
708 case DeclarationName::CXXDestructorName:
709 case DeclarationName::CXXConversionFunctionName:
710 case DeclarationName::CXXUsingDirective:
711 Key.Data = 0;
712 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000713 }
714
Douglas Gregor98339b92011-08-25 20:47:51 +0000715 return Key;
716}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000717
Douglas Gregor98339b92011-08-25 20:47:51 +0000718ASTDeclContextNameLookupTrait::data_type
719ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
720 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000721 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000722 using namespace clang::io;
723 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000724 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000725 return std::make_pair(Start, Start + NumDecls);
726}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000727
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000728bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000729 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000730 const std::pair<uint64_t, uint64_t> &Offsets,
731 DeclContextInfo &Info) {
732 SavedStreamPosition SavedPosition(Cursor);
733 // First the lexical decls.
734 if (Offsets.first != 0) {
735 Cursor.JumpToBit(Offsets.first);
736
737 RecordData Record;
738 const char *Blob;
739 unsigned BlobLen;
740 unsigned Code = Cursor.ReadCode();
741 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
742 if (RecCode != DECL_CONTEXT_LEXICAL) {
743 Error("Expected lexical block");
744 return true;
745 }
746
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000747 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
748 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000749 }
750
751 // Now the lookup table.
752 if (Offsets.second != 0) {
753 Cursor.JumpToBit(Offsets.second);
754
755 RecordData Record;
756 const char *Blob;
757 unsigned BlobLen;
758 unsigned Code = Cursor.ReadCode();
759 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
760 if (RecCode != DECL_CONTEXT_VISIBLE) {
761 Error("Expected visible lookup table block");
762 return true;
763 }
764 Info.NameLookupTableData
765 = ASTDeclContextNameLookupTable::Create(
766 (const unsigned char *)Blob + Record[0],
767 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000768 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000769 }
770
771 return false;
772}
773
Chris Lattner5f9e2722011-07-23 10:55:15 +0000774void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000775 Error(diag::err_fe_pch_malformed, Msg);
776}
777
778void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000779 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000780 if (Diags.isDiagnosticInFlight())
781 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
782 else
783 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000784}
785
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000786/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000787bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000788 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000789 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000790 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000791 SuggestedPredefines,
792 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000793 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000794}
795
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000796//===----------------------------------------------------------------------===//
797// Source Manager Deserialization
798//===----------------------------------------------------------------------===//
799
Douglas Gregorbd945002009-04-13 16:31:14 +0000800/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000801/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000802bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000803 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000804 unsigned Idx = 0;
805 LineTableInfo &LineTable = SourceMgr.getLineTable();
806
807 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000808 std::map<int, int> FileIDs;
809 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000810 // Extract the file name
811 unsigned FilenameLen = Record[Idx++];
812 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
813 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000814 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000815 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000816 }
817
818 // Parse the line entries
819 std::vector<LineEntry> Entries;
820 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000821 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000822 assert(FID >= 0 && "Serialized line entries for non-local file.");
823 // Remap FileID from 1-based old view.
824 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000825
826 // Extract the line entries
827 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000828 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000829 Entries.clear();
830 Entries.reserve(NumEntries);
831 for (unsigned I = 0; I != NumEntries; ++I) {
832 unsigned FileOffset = Record[Idx++];
833 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000834 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000835 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000836 = (SrcMgr::CharacteristicKind)Record[Idx++];
837 unsigned IncludeOffset = Record[Idx++];
838 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
839 FileKind, IncludeOffset));
840 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000841 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000842 }
843
844 return false;
845}
846
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847namespace {
848
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000849class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000850public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000851 const ino_t ino;
852 const dev_t dev;
853 const mode_t mode;
854 const time_t mtime;
855 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000857 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000858 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000859};
860
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000861class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000862 public:
863 typedef const char *external_key_type;
864 typedef const char *internal_key_type;
865
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000866 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000867
868 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000869 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000870 }
871
872 static internal_key_type GetInternalKey(const char *path) { return path; }
873
874 static bool EqualKey(internal_key_type a, internal_key_type b) {
875 return strcmp(a, b) == 0;
876 }
877
878 static std::pair<unsigned, unsigned>
879 ReadKeyDataLength(const unsigned char*& d) {
880 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
881 unsigned DataLen = (unsigned) *d++;
882 return std::make_pair(KeyLen + 1, DataLen);
883 }
884
885 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
886 return (const char *)d;
887 }
888
889 static data_type ReadData(const internal_key_type, const unsigned char *d,
890 unsigned /*DataLen*/) {
891 using namespace clang::io;
892
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000893 ino_t ino = (ino_t) ReadUnalignedLE32(d);
894 dev_t dev = (dev_t) ReadUnalignedLE32(d);
895 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000896 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000897 off_t size = (off_t) ReadUnalignedLE64(d);
898 return data_type(ino, dev, mode, mtime, size);
899 }
900};
901
902/// \brief stat() cache for precompiled headers.
903///
904/// This cache is very similar to the stat cache used by pretokenized
905/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000906class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000907 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000908 CacheTy *Cache;
909
910 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000911public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000912 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
913 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000914 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
915 Cache = CacheTy::Create(Buckets, Base);
916 }
917
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000918 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner898a0612010-11-23 21:17:56 +0000920 LookupResult getStat(const char *Path, struct stat &StatBuf,
921 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000922 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000923 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000924
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000925 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000926 if (I == Cache->end()) {
927 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000928 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000929 }
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000931 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000932 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner10e286a2010-11-23 19:19:34 +0000934 StatBuf.st_ino = Data.ino;
935 StatBuf.st_dev = Data.dev;
936 StatBuf.st_mtime = Data.mtime;
937 StatBuf.st_mode = Data.mode;
938 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000939 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000940 }
941};
942} // end anonymous namespace
943
944
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000945/// \brief Read a source manager block
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000946ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000947 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000948
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000949 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000950
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000951 // Set the source-location entry cursor to the current position in
952 // the stream. This cursor will be used to read the contents of the
953 // source manager block initially, and then lazily read
954 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000955 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000956
957 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000958 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000959 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000960 return Failure;
961 }
962
963 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000964 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000965 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000966 return Failure;
967 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000968
Douglas Gregor14f79002009-04-10 03:52:48 +0000969 RecordData Record;
970 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000971 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000972 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000973 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000974 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000975 return Failure;
976 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000977 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregor14f79002009-04-10 03:52:48 +0000980 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
981 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000982 SLocEntryCursor.ReadSubBlockID();
983 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000984 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000985 return Failure;
986 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000987 continue;
988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregor14f79002009-04-10 03:52:48 +0000990 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000991 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000992 continue;
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Douglas Gregor14f79002009-04-10 03:52:48 +0000995 // Read a record.
996 const char *BlobStart;
997 unsigned BlobLen;
998 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000999 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001000 default: // Default behavior: ignore.
1001 break;
1002
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001003 case SM_SLOC_FILE_ENTRY:
1004 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001005 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001006 // Once we hit one of the source location entries, we're done.
1007 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001008 }
1009 }
1010}
1011
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001012/// \brief If a header file is not found at the path that we expect it to be
1013/// and the PCH file was moved from its original location, try to resolve the
1014/// file by assuming that header+PCH were moved together and the header is in
1015/// the same place relative to the PCH.
1016static std::string
1017resolveFileRelativeToOriginalDir(const std::string &Filename,
1018 const std::string &OriginalDir,
1019 const std::string &CurrDir) {
1020 assert(OriginalDir != CurrDir &&
1021 "No point trying to resolve the file if the PCH dir didn't change");
1022 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001023 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001024 fs::make_absolute(filePath);
1025 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001026 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001027
1028 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1029 fileDirE = path::end(path::parent_path(filePath));
1030 path::const_iterator origDirI = path::begin(OriginalDir),
1031 origDirE = path::end(OriginalDir);
1032 // Skip the common path components from filePath and OriginalDir.
1033 while (fileDirI != fileDirE && origDirI != origDirE &&
1034 *fileDirI == *origDirI) {
1035 ++fileDirI;
1036 ++origDirI;
1037 }
1038 for (; origDirI != origDirE; ++origDirI)
1039 path::append(currPCHPath, "..");
1040 path::append(currPCHPath, fileDirI, fileDirE);
1041 path::append(currPCHPath, path::filename(Filename));
1042 return currPCHPath.str();
1043}
1044
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001045/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001046ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001047 if (ID == 0)
1048 return Success;
1049
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001050 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001051 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001052 return Failure;
1053 }
1054
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001055 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001056 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001057 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001058 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001059
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001061 unsigned Code = SLocEntryCursor.ReadCode();
1062 if (Code == llvm::bitc::END_BLOCK ||
1063 Code == llvm::bitc::ENTER_SUBBLOCK ||
1064 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001065 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001066 return Failure;
1067 }
1068
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001069 RecordData Record;
1070 const char *BlobStart;
1071 unsigned BlobLen;
1072 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1073 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001074 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001075 return Failure;
1076
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001077 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00001078 if (Record.size() < 7) {
1079 Error("source location entry is incorrect");
1080 return Failure;
1081 }
1082
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001083 // We will detect whether a file changed and return 'Failure' for it, but
1084 // we will also try to fail gracefully by setting up the SLocEntry.
1085 ASTReader::ASTReadResult Result = Success;
1086
Douglas Gregora081da52011-11-16 20:05:18 +00001087 bool OverriddenBuffer = Record[6];
1088
1089 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1090 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001091 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora4581a12011-11-17 19:08:51 +00001092 const FileEntry *File =
1093 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1094 (time_t)Record[5])
1095 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001096 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1097 OriginalDir != CurrentDir) {
1098 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1099 OriginalDir,
1100 CurrentDir);
1101 if (!resolved.empty())
1102 File = FileMgr.getFile(resolved);
1103 }
Axel Naumann04331162011-01-27 10:55:51 +00001104 if (File == 0)
1105 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1106 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001107 if (File == 0) {
1108 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001109 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001110 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001111 Error(ErrorStr.c_str());
1112 return Failure;
1113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +00001115 if (!DisableValidation && !OverriddenBuffer &&
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001116 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001117#if !defined(LLVM_ON_WIN32)
1118 // In our regression testing, the Windows file system seems to
1119 // have inconsistent modification times that sometimes
1120 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001121 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001122#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001123 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001124 Error(diag::err_fe_pch_file_modified, Filename);
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001125 Result = Failure;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001126 }
1127
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001128 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001129 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001130 // This is the module's main file.
1131 IncludeLoc = getImportLocation(F);
1132 }
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001133 SrcMgr::CharacteristicKind
1134 FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1135 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001136 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001137 SrcMgr::FileInfo &FileInfo =
1138 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001139 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001140 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001141 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001142
Douglas Gregora081da52011-11-16 20:05:18 +00001143 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1144 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001145 if (NumFileDecls) {
1146 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001147 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1148 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001149 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001150
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001151 const SrcMgr::ContentCache *ContentCache
Argyrios Kyrtzidisff398962012-07-11 20:59:04 +00001152 = SourceMgr.getOrCreateContentCache(File,
1153 /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001154 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1155 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001156 unsigned Code = SLocEntryCursor.ReadCode();
1157 Record.clear();
1158 unsigned RecCode
1159 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1160
1161 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1162 Error("AST record has invalid code");
1163 return Failure;
1164 }
1165
1166 llvm::MemoryBuffer *Buffer
1167 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1168 Filename);
1169 SourceMgr.overrideFileContents(File, Buffer);
1170 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001171
1172 if (Result == Failure)
1173 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001174 break;
1175 }
1176
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001177 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001178 const char *Name = BlobStart;
1179 unsigned Offset = Record[0];
1180 unsigned Code = SLocEntryCursor.ReadCode();
1181 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001182 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001183 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001184
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001185 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001186 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001187 return Failure;
1188 }
1189
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001190 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001191 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1192 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001193 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1194 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Douglas Gregor6236a292011-12-02 21:56:05 +00001196 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001197 PCHPredefinesBlock Block = {
1198 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001199 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001200 };
1201 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001202 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001203
1204 break;
1205 }
1206
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001207 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001208 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001209 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001210 ReadSourceLocation(*F, Record[2]),
1211 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001212 Record[4],
1213 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001214 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001215 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001216 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001217 }
1218
1219 return Success;
1220}
1221
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001222/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001223SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001224 if (F->ImportLoc.isValid())
1225 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001226
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001227 // Otherwise we have a PCH. It's considered to be "imported" at the first
1228 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001229 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001230 // Main file is the importer. We assume that it is the first entry in the
1231 // entry table. We can't ask the manager, because at the time of PCH loading
1232 // the main file entry doesn't exist yet.
1233 // The very first entry is the invalid instantiation loc, which takes up
1234 // offsets 0 and 1.
1235 return SourceLocation::getFromRawEncoding(2U);
1236 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001237 //return F->Loaders[0]->FirstLoc;
1238 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001239}
1240
Chris Lattner6367f6d2009-04-27 01:05:14 +00001241/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1242/// specified cursor. Read the abbreviations that are at the top of the block
1243/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001244bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001245 unsigned BlockID) {
1246 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001247 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001248 return Failure;
1249 }
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Chris Lattner6367f6d2009-04-27 01:05:14 +00001251 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001252 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001253 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001254
Chris Lattner6367f6d2009-04-27 01:05:14 +00001255 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001256 if (Code != llvm::bitc::DEFINE_ABBREV) {
1257 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001258 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001259 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001260 Cursor.ReadAbbrevRecord();
1261 }
1262}
1263
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001264void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001265 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Douglas Gregor37e26842009-04-21 23:56:24 +00001267 // Keep track of where we are in the stream, then jump back there
1268 // after reading this macro.
1269 SavedStreamPosition SavedPosition(Stream);
1270
1271 Stream.JumpToBit(Offset);
1272 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001273 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001274 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Douglas Gregor37e26842009-04-21 23:56:24 +00001276 while (true) {
1277 unsigned Code = Stream.ReadCode();
1278 switch (Code) {
1279 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001280 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001281
1282 case llvm::bitc::ENTER_SUBBLOCK:
1283 // No known subblocks, always skip them.
1284 Stream.ReadSubBlockID();
1285 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001286 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001287 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001288 }
1289 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Douglas Gregor37e26842009-04-21 23:56:24 +00001291 case llvm::bitc::DEFINE_ABBREV:
1292 Stream.ReadAbbrevRecord();
1293 continue;
1294 default: break;
1295 }
1296
1297 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001298 const char *BlobStart = 0;
1299 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001300 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001301 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001302 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001303 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001304 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001305 case PP_MACRO_OBJECT_LIKE:
1306 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001307 // If we already have a macro, that means that we've hit the end
1308 // of the definition of the macro we were looking for. We're
1309 // done.
1310 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001311 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001312
Douglas Gregor95eab172011-07-28 20:55:49 +00001313 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001314 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001315 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001316 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001317 }
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Douglas Gregora8235d62012-10-09 23:05:51 +00001319 unsigned GlobalID = getGlobalMacroID(F, Record[1]);
1320
1321 // If this macro has already been loaded, don't do so again.
1322 if (MacrosLoaded[GlobalID - NUM_PREDEF_MACRO_IDS])
1323 return;
1324
1325 unsigned NextIndex = 2;
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.
1363 MacroUpdatesMap::iterator Update = MacroUpdates.find(GlobalID);
1364 if (Update != MacroUpdates.end()) {
1365 if (MI->getUndefLoc().isInvalid()) {
1366 MI->setUndefLoc(Update->second.UndefLoc);
1367 if (PPMutationListener *Listener = PP.getPPMutationListener())
1368 Listener->UndefinedMacro(MI);
1369 }
1370 MacroUpdates.erase(Update);
1371 }
1372
Douglas Gregor37e26842009-04-21 23:56:24 +00001373 // Finally, install the macro.
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001374 PP.setMacroInfo(II, MI, /*LoadedFromAST=*/true);
Douglas Gregor37e26842009-04-21 23:56:24 +00001375
1376 // Remember that we saw this macro last so that we add the tokens that
1377 // form its body to it.
1378 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001379
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001380 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1381 Record[NextIndex]) {
1382 // We have a macro definition. Register the association
1383 PreprocessedEntityID
1384 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1385 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1386 PPRec.RegisterMacroDefinition(Macro,
1387 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001388 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001389
Douglas Gregor37e26842009-04-21 23:56:24 +00001390 ++NumMacrosRead;
1391 break;
1392 }
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001394 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001395 // If we see a TOKEN before a PP_MACRO_*, then the file is
1396 // erroneous, just pretend we didn't see this.
1397 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregor37e26842009-04-21 23:56:24 +00001399 Token Tok;
1400 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001401 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001402 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001403 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001404 Tok.setIdentifierInfo(II);
1405 Tok.setKind((tok::TokenKind)Record[3]);
1406 Tok.setFlag((Token::TokenFlags)Record[4]);
1407 Macro->AddTokenToBody(Tok);
1408 break;
1409 }
David Blaikie7530c032012-01-17 06:56:22 +00001410 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001411 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001412}
1413
Douglas Gregor86c67d82011-07-28 22:39:26 +00001414PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001415ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001416 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001417 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1418 assert(I != M.PreprocessedEntityRemap.end()
1419 && "Invalid index into preprocessed entity index remap");
1420
1421 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001422}
1423
Douglas Gregor98339b92011-08-25 20:47:51 +00001424unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1425 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001426}
Douglas Gregor98339b92011-08-25 20:47:51 +00001427
1428HeaderFileInfoTrait::internal_key_type
1429HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1430
1431bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1432 if (strcmp(a, b) == 0)
1433 return true;
1434
1435 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1436 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001437
1438 // Determine whether the actual files are equivalent.
1439 bool Result = false;
1440 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001441 return false;
1442
Douglas Gregor99a922b2011-12-09 16:22:07 +00001443 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001444}
1445
1446std::pair<unsigned, unsigned>
1447HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1448 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1449 unsigned DataLen = (unsigned) *d++;
1450 return std::make_pair(KeyLen + 1, DataLen);
1451}
1452
1453HeaderFileInfoTrait::data_type
1454HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1455 unsigned DataLen) {
1456 const unsigned char *End = d + DataLen;
1457 using namespace clang::io;
1458 HeaderFileInfo HFI;
1459 unsigned Flags = *d++;
1460 HFI.isImport = (Flags >> 5) & 0x01;
1461 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1462 HFI.DirInfo = (Flags >> 2) & 0x03;
1463 HFI.Resolved = (Flags >> 1) & 0x01;
1464 HFI.IndexHeaderMapHeader = Flags & 0x01;
1465 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001466 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1467 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001468 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1469 // The framework offset is 1 greater than the actual offset,
1470 // since 0 is used as an indicator for "no framework name".
1471 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1472 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1473 }
1474
1475 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1476 (void)End;
1477
1478 // This HeaderFileInfo was externally loaded.
1479 HFI.External = true;
1480 return HFI;
1481}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001482
Douglas Gregora8235d62012-10-09 23:05:51 +00001483void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, MacroID ID,
1484 bool Visible) {
Douglas Gregor13292642011-12-02 15:45:10 +00001485 if (Visible) {
1486 // Note that this identifier has a macro definition.
1487 II->setHasMacroDefinition(true);
Douglas Gregor3644d972012-10-09 16:01:50 +00001488 } else {
1489 II->setHadMacroDefinition(true);
Douglas Gregor13292642011-12-02 15:45:10 +00001490 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001491
1492 // FIXME: This could end up overwriting a previously recording macro
1493 // definition here, which is not cool at all.
1494 UnreadMacroIDs[II] = ID;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001495}
1496
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001497void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001498 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1499 E = ModuleMgr.rend(); I != E; ++I) {
1500 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001501
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001502 // If there was no preprocessor block, skip this file.
1503 if (!MacroCursor.getBitStreamReader())
1504 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001505
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001506 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001507 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001508
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001509 RecordData Record;
1510 while (true) {
1511 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001512 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001513 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001514
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001515 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1516 // No known subblocks, always skip them.
1517 Cursor.ReadSubBlockID();
1518 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001519 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001520 return;
1521 }
1522 continue;
1523 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001524
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001525 if (Code == llvm::bitc::DEFINE_ABBREV) {
1526 Cursor.ReadAbbrevRecord();
1527 continue;
1528 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001529
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001530 // Read a record.
1531 const char *BlobStart;
1532 unsigned BlobLen;
1533 Record.clear();
1534 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1535 default: // Default behavior: ignore.
1536 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001537
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001538 case PP_MACRO_OBJECT_LIKE:
1539 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001540 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001541 break;
1542
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001543 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001544 // Ignore tokens.
1545 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001546 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001547 }
1548 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001549
1550 // Drain the unread macro-record offsets map.
Douglas Gregora8235d62012-10-09 23:05:51 +00001551 while (!UnreadMacroIDs.empty())
1552 LoadMacroDefinition(UnreadMacroIDs.begin());
Douglas Gregor295a2a62010-10-30 00:23:06 +00001553}
1554
1555void ASTReader::LoadMacroDefinition(
Douglas Gregora8235d62012-10-09 23:05:51 +00001556 llvm::DenseMap<IdentifierInfo *, MacroID>::iterator Pos) {
1557 assert(Pos != UnreadMacroIDs.end() && "Unknown macro definition");
1558 uint64_t GlobalID = Pos->second;
1559 UnreadMacroIDs.erase(Pos);
1560 getMacro(GlobalID);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001561}
1562
1563void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
Douglas Gregora8235d62012-10-09 23:05:51 +00001564 llvm::DenseMap<IdentifierInfo *, MacroID>::iterator Pos
1565 = UnreadMacroIDs.find(II);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001566 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001567}
1568
Douglas Gregoreee242f2011-10-27 09:33:13 +00001569namespace {
1570 /// \brief Visitor class used to look up identifirs in an AST file.
1571 class IdentifierLookupVisitor {
1572 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001573 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001574 IdentifierInfo *Found;
1575 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001576 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1577 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001578
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001579 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001580 IdentifierLookupVisitor *This
1581 = static_cast<IdentifierLookupVisitor *>(UserData);
1582
Douglas Gregor057df202012-01-18 20:56:22 +00001583 // If we've already searched this module file, skip it now.
1584 if (M.Generation <= This->PriorGeneration)
1585 return true;
1586
Douglas Gregoreee242f2011-10-27 09:33:13 +00001587 ASTIdentifierLookupTable *IdTable
1588 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1589 if (!IdTable)
1590 return false;
1591
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001592 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1593 M, This->Found);
1594
Douglas Gregoreee242f2011-10-27 09:33:13 +00001595 std::pair<const char*, unsigned> Key(This->Name.begin(),
1596 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001597 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001598 if (Pos == IdTable->end())
1599 return false;
1600
1601 // Dereferencing the iterator has the effect of building the
1602 // IdentifierInfo node and populating it with the various
1603 // declarations it needs.
1604 This->Found = *Pos;
1605 return true;
1606 }
1607
1608 // \brief Retrieve the identifier info found within the module
1609 // files.
1610 IdentifierInfo *getIdentifierInfo() const { return Found; }
1611 };
1612}
1613
1614void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor057df202012-01-18 20:56:22 +00001615 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001616 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001617 PriorGeneration = IdentifierGeneration[&II];
1618
1619 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1620 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1621 markIdentifierUpToDate(&II);
1622}
1623
1624void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1625 if (!II)
1626 return;
1627
1628 II->setOutOfDate(false);
1629
1630 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001631 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001632 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001633}
1634
Chris Lattner5f9e2722011-07-23 10:55:15 +00001635const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001636 std::string Filename = filenameStrRef;
1637 MaybeAddSystemRootToFilename(Filename);
1638 const FileEntry *File = FileMgr.getFile(Filename);
1639 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1640 OriginalDir != CurrentDir) {
1641 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1642 OriginalDir,
1643 CurrentDir);
1644 if (!resolved.empty())
1645 File = FileMgr.getFile(resolved);
1646 }
1647
1648 return File;
1649}
1650
Douglas Gregore650c8c2009-07-07 00:12:59 +00001651/// \brief If we are loading a relocatable PCH file, and the filename is
1652/// not an absolute path, add the system root to the beginning of the file
1653/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001654void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001655 // If this is not a relocatable PCH file, there's nothing to do.
1656 if (!RelocatablePCH)
1657 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Michael J. Spencer256053b2010-12-17 21:22:22 +00001659 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001660 return;
1661
Douglas Gregor832d6202011-07-22 16:35:34 +00001662 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001663 // If no system root was given, default to '/'
1664 Filename.insert(Filename.begin(), '/');
1665 return;
1666 }
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Douglas Gregor832d6202011-07-22 16:35:34 +00001668 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001669 if (isysroot[Length - 1] != '/')
1670 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Douglas Gregor832d6202011-07-22 16:35:34 +00001672 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001673}
1674
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001675ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001676ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001677 llvm::BitstreamCursor &Stream = F.Stream;
1678
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001679 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001680 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001681 return Failure;
1682 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001683
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001684 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001685 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001686 while (!Stream.AtEndOfStream()) {
1687 unsigned Code = Stream.ReadCode();
1688 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001689 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001690 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001691 return Failure;
1692 }
Chris Lattner7356a312009-04-11 21:15:38 +00001693
Argyrios Kyrtzidis1f941242012-09-21 01:30:00 +00001694 DeclContext *DC = Context.getTranslationUnitDecl();
1695 if (!DC->hasExternalVisibleStorage() && DC->hasExternalLexicalStorage())
1696 DC->setMustBuildLookupTable();
1697
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001698 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001699 }
1700
1701 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1702 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001703 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001704 // We lazily load the decls block, but we want to set up the
1705 // DeclsCursor cursor to point into it. Clone our current bitcode
1706 // cursor to it, enter the block and read the abbrevs in that block.
1707 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001708 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001709 if (Stream.SkipBlock() || // Skip with the main cursor.
1710 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001711 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001712 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001713 return Failure;
1714 }
1715 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001717 case DECL_UPDATES_BLOCK_ID:
1718 if (Stream.SkipBlock()) {
1719 Error("malformed block record in AST file");
1720 return Failure;
1721 }
1722 break;
1723
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001724 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001725 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001726 if (!PP.getExternalSource())
1727 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001728
Douglas Gregorecdcb882010-10-20 22:00:55 +00001729 if (Stream.SkipBlock() ||
1730 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001731 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001732 return Failure;
1733 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001734 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001735 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001736
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001737 case PREPROCESSOR_DETAIL_BLOCK_ID:
1738 F.PreprocessorDetailCursor = Stream;
1739 if (Stream.SkipBlock() ||
1740 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1741 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1742 Error("malformed preprocessor detail record in AST file");
1743 return Failure;
1744 }
1745 F.PreprocessorDetailStartOffset
1746 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001747
1748 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001749 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001750 if (!PP.getPreprocessingRecord()->getExternalSource())
1751 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001752 break;
1753
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001754 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001755 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001756 case Success:
1757 break;
1758
1759 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001760 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001761 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001762
1763 case IgnorePCH:
1764 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001765 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001766 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001767
1768 case SUBMODULE_BLOCK_ID:
1769 switch (ReadSubmoduleBlock(F)) {
1770 case Success:
1771 break;
1772
1773 case Failure:
1774 Error("malformed submodule block in AST file");
1775 return Failure;
1776
1777 case IgnorePCH:
1778 return IgnorePCH;
1779 }
1780 break;
1781
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001782 case COMMENTS_BLOCK_ID: {
1783 llvm::BitstreamCursor C = Stream;
1784 if (Stream.SkipBlock() ||
1785 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1786 Error("malformed comments block in AST file");
1787 return Failure;
1788 }
1789 CommentsCursors.push_back(std::make_pair(C, &F));
1790 break;
1791 }
1792
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001793 default:
1794 if (!Stream.SkipBlock())
1795 break;
1796 Error("malformed block record in AST file");
1797 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001798 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001799 continue;
1800 }
1801
1802 if (Code == llvm::bitc::DEFINE_ABBREV) {
1803 Stream.ReadAbbrevRecord();
1804 continue;
1805 }
1806
1807 // Read and process a record.
1808 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001809 const char *BlobStart = 0;
1810 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001811 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001812 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001813 default: // Default behavior: ignore.
1814 break;
1815
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001816 case METADATA: {
1817 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1818 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001819 : diag::warn_pch_version_too_new);
1820 return IgnorePCH;
1821 }
1822
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001823 bool hasErrors = Record[5];
1824 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1825 Diag(diag::err_pch_with_compiler_errors);
1826 return IgnorePCH;
1827 }
1828
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001829 RelocatablePCH = Record[4];
1830 if (Listener) {
1831 std::string TargetTriple(BlobStart, BlobLen);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001832 if (Listener->ReadTargetTriple(F, TargetTriple))
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001833 return IgnorePCH;
1834 }
1835 break;
1836 }
1837
Douglas Gregore95b9192011-08-17 21:07:30 +00001838 case IMPORTS: {
1839 // Load each of the imported PCH files.
1840 unsigned Idx = 0, N = Record.size();
1841 while (Idx < N) {
1842 // Read information about the AST file.
1843 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1844 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001845 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001846 Record.begin() + Idx + Length);
1847 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001848
Douglas Gregore95b9192011-08-17 21:07:30 +00001849 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001850 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001851 case Failure: return Failure;
1852 // If we have to ignore the dependency, we'll have to ignore this too.
1853 case IgnorePCH: return IgnorePCH;
1854 case Success: break;
1855 }
1856 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001857 break;
1858 }
1859
Douglas Gregora119da02011-08-02 16:26:37 +00001860 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001861 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001862 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001863 return Failure;
1864 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001865 F.TypeOffsets = (const uint32_t *)BlobStart;
1866 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001867 unsigned LocalBaseTypeIndex = Record[1];
1868 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001869
Douglas Gregora119da02011-08-02 16:26:37 +00001870 if (F.LocalNumTypes > 0) {
1871 // Introduce the global -> local mapping for types within this module.
1872 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1873
1874 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001875 F.TypeRemap.insertOrReplace(
1876 std::make_pair(LocalBaseTypeIndex,
1877 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001878
1879 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1880 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001881 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001882 }
1883
Douglas Gregor496c7092011-08-03 15:48:04 +00001884 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001885 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001886 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001887 return Failure;
1888 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001889 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001890 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001891 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001892 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001893
Douglas Gregor496c7092011-08-03 15:48:04 +00001894 if (F.LocalNumDecls > 0) {
1895 // Introduce the global -> local mapping for declarations within this
1896 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001897 GlobalDeclMap.insert(
1898 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001899
1900 // Introduce the local -> global mapping for declarations within this
1901 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001902 F.DeclRemap.insertOrReplace(
1903 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001904
Douglas Gregora1be2782011-12-17 23:38:30 +00001905 // Introduce the global -> local mapping for declarations within this
1906 // module.
1907 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1908
Douglas Gregor496c7092011-08-03 15:48:04 +00001909 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1910 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001911 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001912 }
1913
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001914 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001915 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001916 DeclContextInfo &Info = F.DeclContextInfos[TU];
1917 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1918 Info.NumLexicalDecls
1919 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001920 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001921 break;
1922 }
1923
Sebastian Redle1dde812010-08-24 00:50:04 +00001924 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001925 unsigned Idx = 0;
1926 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001927 ASTDeclContextNameLookupTable *Table =
1928 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001929 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001930 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001931 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001932 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1933 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001934 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001935 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001936 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001937 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001938 break;
1939 }
1940
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001941 case LANGUAGE_OPTIONS:
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00001942 if (ParseLanguageOptions(F, Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001943 return IgnorePCH;
1944 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001945
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001946 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001947 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001948 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001949 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001950 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001951 (const unsigned char *)F.IdentifierTableData + Record[0],
1952 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001953 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001954
1955 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001956 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001957 break;
1958
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001959 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001960 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001961 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001962 return Failure;
1963 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001964 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1965 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001966 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001967 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001968
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001969 if (F.LocalNumIdentifiers > 0) {
1970 // Introduce the global -> local mapping for identifiers within this
1971 // module.
1972 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1973 &F));
1974
1975 // Introduce the local -> global mapping for identifiers within this
1976 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001977 F.IdentifierRemap.insertOrReplace(
1978 std::make_pair(LocalBaseIdentifierID,
1979 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001980
1981 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1982 + F.LocalNumIdentifiers);
1983 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001984 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001985 }
Douglas Gregora8235d62012-10-09 23:05:51 +00001986
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001987 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001988 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1989 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001990 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001991
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001992 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001993 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1994 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001995 break;
1996
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001997 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001998 TotalNumStatements += Record[0];
1999 TotalNumMacros += Record[1];
2000 TotalLexicalDeclContexts += Record[2];
2001 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00002002 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002003
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002004 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002005 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2006 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00002007 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002008
Sean Huntebcbe1d2011-05-04 23:29:54 +00002009 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002010 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2011 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002012 break;
2013
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002014 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00002015 if (Record.size() % 4 != 0) {
2016 Error("invalid weak identifiers record");
2017 return Failure;
2018 }
2019
2020 // FIXME: Ignore weak undeclared identifiers from non-original PCH
2021 // files. This isn't the way to do it :)
2022 WeakUndeclaredIdentifiers.clear();
2023
2024 // Translate the weak, undeclared identifiers into global IDs.
2025 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
2026 WeakUndeclaredIdentifiers.push_back(
2027 getGlobalIdentifierID(F, Record[I++]));
2028 WeakUndeclaredIdentifiers.push_back(
2029 getGlobalIdentifierID(F, Record[I++]));
2030 WeakUndeclaredIdentifiers.push_back(
2031 ReadSourceLocation(F, Record, I).getRawEncoding());
2032 WeakUndeclaredIdentifiers.push_back(Record[I++]);
2033 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002034 break;
2035
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002036 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002037 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2038 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002039 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002040
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002041 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002042 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002043 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002044 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002045 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002046
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002047 if (F.LocalNumSelectors > 0) {
2048 // Introduce the global -> local mapping for selectors within this
2049 // module.
2050 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2051
2052 // Introduce the local -> global mapping for selectors within this
2053 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002054 F.SelectorRemap.insertOrReplace(
2055 std::make_pair(LocalBaseSelectorID,
2056 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002057
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002058 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2059 }
2060 break;
2061 }
2062
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002063 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002064 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002065 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002066 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002067 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002068 F.SelectorLookupTableData + Record[0],
2069 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002070 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002071 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002072 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002073
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002074 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002075 if (!Record.empty()) {
2076 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2077 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2078 Record[Idx++]));
2079 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2080 getRawEncoding());
2081 }
2082 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002083 break;
2084
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002085 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002086 if (!Record.empty() && Listener)
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00002087 Listener->ReadCounter(F, Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002088 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002089
2090 case FILE_SORTED_DECLS:
2091 F.FileSortedDecls = (const DeclID *)BlobStart;
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002092 F.NumFileSortedDecls = Record[0];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002093 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002094
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002095 case SOURCE_LOCATION_OFFSETS: {
2096 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002097 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002098 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002099 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002100 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2101 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002102 // Make our entry in the range map. BaseID is negative and growing, so
2103 // we invert it. Because we invert it, though, we need the other end of
2104 // the range.
2105 unsigned RangeStart =
2106 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2107 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2108 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2109
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002110 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2111 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2112 GlobalSLocOffsetMap.insert(
2113 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2114 - SLocSpaceSize,&F));
2115
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002116 // Initialize the remapping table.
2117 // Invalid stays invalid.
2118 F.SLocRemap.insert(std::make_pair(0U, 0));
2119 // This module. Base was 2 when being compiled.
2120 F.SLocRemap.insert(std::make_pair(2U,
2121 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002122
2123 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002124 break;
2125 }
2126
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002127 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002128 // Additional remapping information.
2129 const unsigned char *Data = (const unsigned char*)BlobStart;
2130 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002131
2132 // Continuous range maps we may be updating in our module.
2133 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002134 ContinuousRangeMap<uint32_t, int, 2>::Builder
2135 IdentifierRemap(F.IdentifierRemap);
Douglas Gregora8235d62012-10-09 23:05:51 +00002136 ContinuousRangeMap<uint32_t, int, 2>::Builder
2137 MacroRemap(F.MacroRemap);
2138 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002139 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2140 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002141 SubmoduleRemap(F.SubmoduleRemap);
2142 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002143 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002144 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002145 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2146
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002147 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002148 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002149 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002150 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002151 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002152 if (!OM) {
2153 Error("SourceLocation remap refers to unknown module");
2154 return Failure;
2155 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002156
2157 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2158 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora8235d62012-10-09 23:05:51 +00002159 uint32_t MacroIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002160 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002161 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002162 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2163 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002164 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002165
2166 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2167 SLocRemap.insert(std::make_pair(SLocOffset,
2168 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002169 IdentifierRemap.insert(
2170 std::make_pair(IdentifierIDOffset,
2171 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregora8235d62012-10-09 23:05:51 +00002172 MacroRemap.insert(std::make_pair(MacroIDOffset,
2173 OM->BaseMacroID - MacroIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002174 PreprocessedEntityRemap.insert(
2175 std::make_pair(PreprocessedEntityIDOffset,
2176 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002177 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2178 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002179 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2180 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002181 DeclRemap.insert(std::make_pair(DeclIDOffset,
2182 OM->BaseDeclID - DeclIDOffset));
2183
Douglas Gregora119da02011-08-02 16:26:37 +00002184 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002185 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002186
2187 // Global -> local mappings.
2188 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002189 }
2190 break;
2191 }
2192
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002193 case SOURCE_MANAGER_LINE_TABLE:
2194 if (ParseLineTable(F, Record))
2195 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002196 break;
2197
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002198 case FILE_SOURCE_LOCATION_OFFSETS:
2199 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2200 F.LocalNumSLocFileEntries = Record[0];
2201 break;
2202
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002203 case SOURCE_LOCATION_PRELOADS: {
2204 // Need to transform from the local view (1-based IDs) to the global view,
2205 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002206 if (!F.PreloadSLocEntries.empty()) {
2207 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2208 return Failure;
2209 }
2210
2211 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002212 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002213 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002214
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002215 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002216 if (!DisableStatCache) {
2217 ASTStatCache *MyStatCache =
2218 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2219 (const unsigned char *)BlobStart,
2220 NumStatHits, NumStatMisses);
2221 FileMgr.addStatCache(MyStatCache);
2222 F.StatCache = MyStatCache;
2223 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002224 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002225 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002226
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002227 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002228 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2229 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002230 break;
2231
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002232 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002233 if (Record.size() % 3 != 0) {
2234 Error("Invalid VTABLE_USES record");
2235 return Failure;
2236 }
2237
Sebastian Redl40566802010-08-05 18:21:25 +00002238 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002239 // FIXME: Modules will have some trouble with this. This is clearly not
2240 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002241 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002242
2243 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2244 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2245 VTableUses.push_back(
2246 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2247 VTableUses.push_back(Record[Idx++]);
2248 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002249 break;
2250
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002251 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002252 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2253 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002254 break;
2255
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002257 if (PendingInstantiations.size() % 2 != 0) {
Axel Naumann39d26c32012-10-02 09:09:43 +00002258 Error("Invalid existing PendingInstantiations");
2259 return Failure;
2260 }
2261
2262 if (Record.size() % 2 != 0) {
Douglas Gregorf2abb522011-07-28 19:26:52 +00002263 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2264 return Failure;
2265 }
Axel Naumann39d26c32012-10-02 09:09:43 +00002266
Douglas Gregorf2abb522011-07-28 19:26:52 +00002267 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2268 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2269 PendingInstantiations.push_back(
2270 ReadSourceLocation(F, Record, I).getRawEncoding());
2271 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002272 break;
2273
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002274 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002275 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002276 // FIXME: Modules will have some trouble with this.
2277 SemaDeclRefs.clear();
2278 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2279 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002280 break;
2281
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002282 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002283 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002284 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002285 ActualOriginalFileName.assign(BlobStart, BlobLen);
2286 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002287 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002288 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Douglas Gregor31d375f2011-05-06 21:43:30 +00002290 case ORIGINAL_FILE_ID:
2291 OriginalFileID = FileID::get(Record[0]);
2292 break;
2293
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002294 case ORIGINAL_PCH_DIR:
2295 // The primary AST will be the last to get here, so it will be the one
2296 // that's used.
2297 OriginalDir.assign(BlobStart, BlobLen);
2298 break;
2299
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002300 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002301 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002302 StringRef ASTBranch(BlobStart, BlobLen);
2303 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002304 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002305 return IgnorePCH;
2306 }
2307 break;
2308 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002309
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002310 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002311 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2312 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2313 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002314
2315 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002316
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002317 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002318 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002319 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002320 if (!PP.getPreprocessingRecord()->getExternalSource())
2321 PP.getPreprocessingRecord()->SetExternalSource(*this);
2322 StartingID
2323 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002324 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002325 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002326
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002327 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002328 // Introduce the global -> local mapping for preprocessed entities in
2329 // this module.
2330 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2331
2332 // Introduce the local -> global mapping for preprocessed entities in
2333 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002334 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002335 std::make_pair(LocalBasePreprocessedEntityID,
2336 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2337 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002338
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002339 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002340 }
2341
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002342 case DECL_UPDATE_OFFSETS: {
2343 if (Record.size() % 2 != 0) {
2344 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2345 return Failure;
2346 }
2347 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002348 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2349 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002350 break;
2351 }
2352
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002353 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002354 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002355 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002356 return Failure;
2357 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002358 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002359 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002360 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002361 break;
2362 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002363
Douglas Gregorcff9f262012-01-27 01:47:08 +00002364 case OBJC_CATEGORIES_MAP: {
2365 if (F.LocalNumObjCCategoriesInMap != 0) {
2366 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002367 return Failure;
2368 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002369
2370 F.LocalNumObjCCategoriesInMap = Record[0];
2371 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002372 break;
2373 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002374
Douglas Gregorcff9f262012-01-27 01:47:08 +00002375 case OBJC_CATEGORIES:
2376 F.ObjCCategories.swap(Record);
2377 break;
2378
Douglas Gregor7c789c12010-10-29 22:39:52 +00002379 case CXX_BASE_SPECIFIER_OFFSETS: {
2380 if (F.LocalNumCXXBaseSpecifiers != 0) {
2381 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2382 return Failure;
2383 }
2384
2385 F.LocalNumCXXBaseSpecifiers = Record[0];
2386 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002387 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002388 break;
2389 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002390
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002391 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002392 if (Record.size() % 2 != 0) {
2393 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2394 return Failure;
2395 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002396
2397 if (F.PragmaDiagMappings.empty())
2398 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002399 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002400 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2401 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002402 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002403
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002404 case CUDA_SPECIAL_DECL_REFS:
2405 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002406 // FIXME: Modules will have trouble with this.
2407 CUDASpecialDeclRefs.clear();
2408 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2409 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002410 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002411
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002412 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002413 F.HeaderFileInfoTableData = BlobStart;
2414 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002415 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002416 if (Record[0]) {
2417 F.HeaderFileInfoTable
2418 = HeaderFileInfoLookupTable::Create(
2419 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002420 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002421 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002422 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002423 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002424
2425 PP.getHeaderSearchInfo().SetExternalSource(this);
2426 if (!PP.getHeaderSearchInfo().getExternalLookup())
2427 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002428 }
2429 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002430 }
2431
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002432 case FP_PRAGMA_OPTIONS:
2433 // Later tables overwrite earlier ones.
2434 FPPragmaOptions.swap(Record);
2435 break;
2436
2437 case OPENCL_EXTENSIONS:
2438 // Later tables overwrite earlier ones.
2439 OpenCLExtensions.swap(Record);
2440 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002441
2442 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002443 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2444 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002445 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002446
2447 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002448 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2449 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002450 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002451
2452 case IMPORTED_MODULES: {
2453 if (F.Kind != MK_Module) {
2454 // If we aren't loading a module (which has its own exports), make
2455 // all of the imported modules visible.
2456 // FIXME: Deal with macros-only imports.
2457 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2458 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2459 ImportedModules.push_back(GlobalID);
2460 }
2461 }
2462 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002463 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002464
Douglas Gregora1be2782011-12-17 23:38:30 +00002465 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002466 F.RedeclarationChains.swap(Record);
2467 break;
2468 }
2469
2470 case LOCAL_REDECLARATIONS_MAP: {
2471 if (F.LocalNumRedeclarationsInMap != 0) {
2472 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002473 return Failure;
2474 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002475
Douglas Gregor2171bf12012-01-15 16:58:34 +00002476 F.LocalNumRedeclarationsInMap = Record[0];
2477 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002478 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002479 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002480
2481 case MERGED_DECLARATIONS: {
2482 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2483 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2484 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2485 for (unsigned N = Record[Idx++]; N > 0; --N)
2486 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2487 }
2488 break;
2489 }
Douglas Gregora8235d62012-10-09 23:05:51 +00002490
2491 case MACRO_OFFSET: {
2492 if (F.LocalNumMacros != 0) {
2493 Error("duplicate MACRO_OFFSET record in AST file");
2494 return Failure;
2495 }
2496 F.MacroOffsets = (const uint32_t *)BlobStart;
2497 F.LocalNumMacros = Record[0];
2498 unsigned LocalBaseMacroID = Record[1];
2499 F.BaseMacroID = getTotalNumMacros();
2500
2501 if (F.LocalNumMacros > 0) {
2502 // Introduce the global -> local mapping for macros within this module.
2503 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
2504
2505 // Introduce the local -> global mapping for macros within this module.
2506 F.MacroRemap.insertOrReplace(
2507 std::make_pair(LocalBaseMacroID,
2508 F.BaseMacroID - LocalBaseMacroID));
2509
2510 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
2511 }
2512 break;
2513 }
2514
2515 case MACRO_UPDATES: {
2516 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2517 MacroID ID = getGlobalMacroID(F, Record[I++]);
2518 if (I == N)
2519 break;
2520
2521 MacroUpdates[ID].UndefLoc = ReadSourceLocation(F, Record, I);
2522 }
2523 break;
2524 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002525 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002526 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002527 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002528 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002529}
2530
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002531ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002532 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002533
Douglas Gregorc69a2922011-08-25 20:58:51 +00002534 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2535 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2536 unsigned Code = SLocEntryCursor.ReadCode();
2537 if (Code == llvm::bitc::END_BLOCK ||
2538 Code == llvm::bitc::ENTER_SUBBLOCK ||
2539 Code == llvm::bitc::DEFINE_ABBREV) {
2540 Error("incorrectly-formatted source location entry in AST file");
2541 return Failure;
2542 }
2543
2544 RecordData Record;
2545 const char *BlobStart;
2546 unsigned BlobLen;
2547 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2548 default:
2549 Error("incorrectly-formatted source location entry in AST file");
2550 return Failure;
2551
2552 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002553 // If the buffer was overridden, the file need not exist.
2554 if (Record[6])
2555 break;
2556
Douglas Gregorc69a2922011-08-25 20:58:51 +00002557 StringRef Filename(BlobStart, BlobLen);
2558 const FileEntry *File = getFileEntry(Filename);
2559
2560 if (File == 0) {
2561 std::string ErrorStr = "could not find file '";
2562 ErrorStr += Filename;
2563 ErrorStr += "' referenced by AST file";
2564 Error(ErrorStr.c_str());
2565 return IgnorePCH;
2566 }
2567
Douglas Gregora081da52011-11-16 20:05:18 +00002568 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002569 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002570 return Failure;
2571 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002572
2573 off_t StoredSize = (off_t)Record[4];
2574 time_t StoredTime = (time_t)Record[5];
2575
2576 // Check if there was a request to override the contents of the file
2577 // that was part of the precompiled header. Overridding such a file
2578 // can lead to problems when lexing using the source locations from the
2579 // PCH.
2580 SourceManager &SM = getSourceManager();
2581 if (SM.isFileOverridden(File)) {
2582 Error(diag::err_fe_pch_file_overridden, Filename);
2583 // After emitting the diagnostic, recover by disabling the override so
2584 // that the original file will be used.
2585 SM.disableFileContentsOverride(File);
2586 // The FileEntry is a virtual file entry with the size of the contents
2587 // that would override the original contents. Set it to the original's
2588 // size/time.
2589 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2590 StoredSize, StoredTime);
2591 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002592
Douglas Gregorc69a2922011-08-25 20:58:51 +00002593 // The stat info from the FileEntry came from the cached stat
2594 // info of the PCH, so we cannot trust it.
2595 struct stat StatBuf;
2596 if (::stat(File->getName(), &StatBuf) != 0) {
2597 StatBuf.st_size = File->getSize();
2598 StatBuf.st_mtime = File->getModificationTime();
2599 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002600
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002601 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002602#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002603 // In our regression testing, the Windows file system seems to
2604 // have inconsistent modification times that sometimes
2605 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002606 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002607#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002608 )) {
2609 Error(diag::err_fe_pch_file_modified, Filename);
2610 return IgnorePCH;
2611 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002612
Douglas Gregorc69a2922011-08-25 20:58:51 +00002613 break;
2614 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002615 }
2616 }
2617
2618 return Success;
2619}
2620
Douglas Gregorecc2c092011-12-01 22:20:10 +00002621void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002622 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2623 if (Decl *D = Names[I].dyn_cast<Decl *>())
Douglas Gregorf143ffc2012-01-06 16:22:39 +00002624 D->Hidden = false;
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002625 else {
2626 IdentifierInfo *II = Names[I].get<IdentifierInfo *>();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002627 // FIXME: Check if this works correctly with macro history.
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002628 if (!II->hasMacroDefinition()) {
Douglas Gregorc12906e2012-09-24 19:56:18 +00002629 // Make sure that this macro hasn't been #undef'd in the mean-time.
2630 llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator Known
2631 = PP.Macros.find(II);
2632 if (Known == PP.Macros.end() ||
2633 Known->second->getUndefLoc().isInvalid()) {
2634 II->setHasMacroDefinition(true);
2635 if (DeserializationListener)
2636 DeserializationListener->MacroVisible(II);
2637 }
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002638 }
2639 }
Douglas Gregor13292642011-12-02 15:45:10 +00002640 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002641}
2642
Douglas Gregor5e356932011-12-01 17:11:21 +00002643void ASTReader::makeModuleVisible(Module *Mod,
2644 Module::NameVisibilityKind NameVisibility) {
2645 llvm::SmallPtrSet<Module *, 4> Visited;
2646 llvm::SmallVector<Module *, 4> Stack;
2647 Stack.push_back(Mod);
2648 while (!Stack.empty()) {
2649 Mod = Stack.back();
2650 Stack.pop_back();
2651
2652 if (NameVisibility <= Mod->NameVisibility) {
2653 // This module already has this level of visibility (or greater), so
2654 // there is nothing more to do.
2655 continue;
2656 }
2657
Douglas Gregor51f564f2011-12-31 04:05:44 +00002658 if (!Mod->isAvailable()) {
2659 // Modules that aren't available cannot be made visible.
2660 continue;
2661 }
2662
Douglas Gregor5e356932011-12-01 17:11:21 +00002663 // Update the module's name visibility.
2664 Mod->NameVisibility = NameVisibility;
2665
Douglas Gregorecc2c092011-12-01 22:20:10 +00002666 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002667 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002668 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2669 if (Hidden != HiddenNamesMap.end()) {
2670 makeNamesVisible(Hidden->second);
2671 HiddenNamesMap.erase(Hidden);
2672 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002673
2674 // Push any non-explicit submodules onto the stack to be marked as
2675 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002676 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2677 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002678 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002679 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2680 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002681 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002682
2683 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002684 bool AnyWildcard = false;
2685 bool UnrestrictedWildcard = false;
2686 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002687 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2688 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002689 if (!Mod->Exports[I].getInt()) {
2690 // Export a named module directly; no wildcards involved.
2691 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002692 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002693
2694 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002695 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002696
2697 // Wildcard export: export all of the imported modules that match
2698 // the given pattern.
2699 AnyWildcard = true;
2700 if (UnrestrictedWildcard)
2701 continue;
2702
2703 if (Module *Restriction = Mod->Exports[I].getPointer())
2704 WildcardRestrictions.push_back(Restriction);
2705 else {
2706 WildcardRestrictions.clear();
2707 UnrestrictedWildcard = true;
2708 }
2709 }
2710
2711 // If there were any wildcards, push any imported modules that were
2712 // re-exported by the wildcard restriction.
2713 if (!AnyWildcard)
2714 continue;
2715
2716 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2717 Module *Imported = Mod->Imports[I];
Benjamin Kramerd48bcb22012-08-22 15:37:55 +00002718 if (!Visited.insert(Imported))
Douglas Gregor0adaa882011-12-05 17:28:06 +00002719 continue;
2720
2721 bool Acceptable = UnrestrictedWildcard;
2722 if (!Acceptable) {
2723 // Check whether this module meets one of the restrictions.
2724 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2725 Module *Restriction = WildcardRestrictions[R];
2726 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2727 Acceptable = true;
2728 break;
2729 }
2730 }
2731 }
2732
2733 if (!Acceptable)
2734 continue;
2735
Douglas Gregor0adaa882011-12-05 17:28:06 +00002736 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002737 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002738 }
2739}
2740
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002741ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002742 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002743 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002744 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002745
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002746 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002747 case Failure: return Failure;
2748 case IgnorePCH: return IgnorePCH;
2749 case Success: break;
2750 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002751
2752 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002753
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002754 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002755 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002756 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2757 // if DisableValidation is true, defines that were set on command-line
2758 // but not in the PCH file will not be added to SuggestedPredefines.
2759 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002760 return IgnorePCH;
2761
Douglas Gregoreee242f2011-10-27 09:33:13 +00002762 // Mark all of the identifiers in the identifier table as being out of date,
2763 // so that various accessors know to check the loaded modules when the
2764 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002765 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2766 IdEnd = PP.getIdentifierTable().end();
2767 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002768 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002769
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002770 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002771 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2772 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2773 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002774 Module *ResolvedMod = getSubmodule(GlobalID);
2775
2776 if (Unresolved.IsImport) {
2777 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002778 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002779 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002780 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002781
2782 if (ResolvedMod || Unresolved.IsWildcard)
2783 Unresolved.Mod->Exports.push_back(
2784 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002785 }
Douglas Gregor55988682011-12-05 16:33:54 +00002786 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002787
Douglas Gregor35942772011-09-09 21:34:22 +00002788 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002789
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002790 if (DeserializationListener)
2791 DeserializationListener->ReaderInitialized(this);
2792
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002793 if (!OriginalFileID.isInvalid()) {
2794 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2795 + OriginalFileID.getOpaqueValue() - 1);
2796
2797 // If this AST file is a precompiled preamble, then set the preamble file ID
2798 // of the source manager to the file source file from which the preamble was
2799 // built.
2800 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002801 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002802 } else if (Type == MK_MainFile) {
2803 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002804 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002805 }
2806
Douglas Gregorcff9f262012-01-27 01:47:08 +00002807 // For any Objective-C class definitions we have already loaded, make sure
2808 // that we load any additional categories.
2809 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2810 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2811 ObjCClassesLoaded[I],
2812 PreviousGeneration);
2813 }
2814
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002815 return Success;
2816}
2817
Chris Lattner5f9e2722011-07-23 10:55:15 +00002818ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002819 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002820 ModuleFile *ImportedBy) {
2821 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002822 bool NewModule;
2823 std::string ErrorStr;
2824 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002825 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002826
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002827 if (!M) {
2828 // We couldn't load the module.
2829 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2830 + ErrorStr;
2831 Error(Msg);
2832 return Failure;
2833 }
2834
2835 if (!NewModule) {
2836 // We've already loaded this module.
2837 return Success;
2838 }
2839
2840 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2841 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002842 if (FileName != "-") {
2843 CurrentDir = llvm::sys::path::parent_path(FileName);
2844 if (CurrentDir.empty()) CurrentDir = ".";
2845 }
2846
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002847 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002848 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002849 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002850 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002851
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002852 // Sniff for the signature.
2853 if (Stream.Read(8) != 'C' ||
2854 Stream.Read(8) != 'P' ||
2855 Stream.Read(8) != 'C' ||
2856 Stream.Read(8) != 'H') {
2857 Diag(diag::err_not_a_pch_file) << FileName;
2858 return Failure;
2859 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002860
Douglas Gregor2cf26342009-04-09 22:27:44 +00002861 while (!Stream.AtEndOfStream()) {
2862 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Douglas Gregore1d918e2009-04-10 23:10:45 +00002864 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002865 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002866 return Failure;
2867 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002868
2869 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002870
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002871 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002872 switch (BlockID) {
2873 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002874 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002875 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002876 return Failure;
2877 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002878 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002879 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002880 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002881 case Success:
2882 break;
2883
2884 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002885 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002886
2887 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002888 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002889 // AST block, skipping subblocks, to see if there are other
2890 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002891
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002892 // FIXME: We can't clear loaded slocentries anymore.
2893 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002894
2895 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002896 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002897 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002898
Douglas Gregore1d918e2009-04-10 23:10:45 +00002899 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002900 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002901 break;
2902 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002903 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002904 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002905 return Failure;
2906 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002907 break;
2908 }
Mike Stump1eb44332009-09-09 15:08:12 +00002909 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002910
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002911 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002912 // bits of all files we've seen.
2913 F.GlobalBitOffset = TotalModulesSizeInBits;
2914 TotalModulesSizeInBits += F.SizeInBits;
2915 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002916
2917 // Make sure that the files this module was built against are still available.
2918 if (!DisableValidation) {
2919 switch(validateFileEntries(*M)) {
2920 case Failure: return Failure;
2921 case IgnorePCH: return IgnorePCH;
2922 case Success: break;
2923 }
2924 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002925
2926 // Preload SLocEntries.
2927 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2928 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002929 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2930 // directly because the entry may have already been loaded in which case
2931 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2932 // SourceManager.
2933 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002934 }
2935
Douglas Gregorc69a2922011-08-25 20:58:51 +00002936
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002937 return Success;
2938}
2939
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002940void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002941 // If there's a listener, notify them that we "read" the translation unit.
2942 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002943 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2944 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002945
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002946 // Make sure we load the declaration update records for the translation unit,
2947 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002948 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2949 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002950
Douglas Gregor5f957282011-08-11 22:18:49 +00002951 // FIXME: Find a better way to deal with collisions between these
2952 // built-in types. Right now, we just ignore the problem.
2953
2954 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002955 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002956 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2957 if (!Context.CFConstantStringTypeDecl)
2958 Context.setCFConstantStringType(GetType(String));
2959 }
2960
2961 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2962 QualType FileType = GetType(File);
2963 if (FileType.isNull()) {
2964 Error("FILE type is NULL");
2965 return;
2966 }
2967
2968 if (!Context.FILEDecl) {
2969 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2970 Context.setFILEDecl(Typedef->getDecl());
2971 else {
2972 const TagType *Tag = FileType->getAs<TagType>();
2973 if (!Tag) {
2974 Error("Invalid FILE type in AST file");
2975 return;
2976 }
2977 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002978 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002979 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002980 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002981
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002982 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002983 QualType Jmp_bufType = GetType(Jmp_buf);
2984 if (Jmp_bufType.isNull()) {
2985 Error("jmp_buf type is NULL");
2986 return;
2987 }
2988
2989 if (!Context.jmp_bufDecl) {
2990 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2991 Context.setjmp_bufDecl(Typedef->getDecl());
2992 else {
2993 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2994 if (!Tag) {
2995 Error("Invalid jmp_buf type in AST file");
2996 return;
2997 }
2998 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002999 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00003000 }
Mike Stump782fa302009-07-28 02:25:19 +00003001 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00003002
Douglas Gregor72cd7a02011-11-11 19:13:12 +00003003 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00003004 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
3005 if (Sigjmp_bufType.isNull()) {
3006 Error("sigjmp_buf type is NULL");
3007 return;
3008 }
3009
3010 if (!Context.sigjmp_bufDecl) {
3011 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
3012 Context.setsigjmp_bufDecl(Typedef->getDecl());
3013 else {
3014 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
3015 assert(Tag && "Invalid sigjmp_buf type in AST file");
3016 Context.setsigjmp_bufDecl(Tag->getDecl());
3017 }
3018 }
3019 }
3020
3021 if (unsigned ObjCIdRedef
3022 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
3023 if (Context.ObjCIdRedefinitionType.isNull())
3024 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
3025 }
3026
3027 if (unsigned ObjCClassRedef
3028 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
3029 if (Context.ObjCClassRedefinitionType.isNull())
3030 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
3031 }
3032
3033 if (unsigned ObjCSelRedef
3034 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
3035 if (Context.ObjCSelRedefinitionType.isNull())
3036 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
3037 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003038
3039 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
3040 QualType Ucontext_tType = GetType(Ucontext_t);
3041 if (Ucontext_tType.isNull()) {
3042 Error("ucontext_t type is NULL");
3043 return;
3044 }
3045
3046 if (!Context.ucontext_tDecl) {
3047 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
3048 Context.setucontext_tDecl(Typedef->getDecl());
3049 else {
3050 const TagType *Tag = Ucontext_tType->getAs<TagType>();
3051 assert(Tag && "Invalid ucontext_t type in AST file");
3052 Context.setucontext_tDecl(Tag->getDecl());
3053 }
3054 }
3055 }
Douglas Gregor5f957282011-08-11 22:18:49 +00003056 }
3057
Douglas Gregor35942772011-09-09 21:34:22 +00003058 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003059
3060 // If there were any CUDA special declarations, deserialize them.
3061 if (!CUDASpecialDeclRefs.empty()) {
3062 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00003063 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003064 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
3065 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003066
3067 // Re-export any modules that were imported by a non-module AST file.
3068 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
3069 if (Module *Imported = getSubmodule(ImportedModules[I]))
3070 makeModuleVisible(Imported, Module::AllVisible);
3071 }
3072 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003073}
3074
Douglas Gregorecc2c092011-12-01 22:20:10 +00003075void ASTReader::finalizeForWriting() {
3076 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
3077 HiddenEnd = HiddenNamesMap.end();
3078 Hidden != HiddenEnd; ++Hidden) {
3079 makeNamesVisible(Hidden->second);
3080 }
3081 HiddenNamesMap.clear();
3082}
3083
Douglas Gregorb64c1932009-05-12 01:31:05 +00003084/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003085/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003086/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003087std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003088 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003089 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003090 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003091 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003092 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003093 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003094 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003095 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003096 return std::string();
3097 }
3098
3099 // Initialize the stream
3100 llvm::BitstreamReader StreamFile;
3101 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003102 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003103 (const unsigned char *)Buffer->getBufferEnd());
3104 Stream.init(StreamFile);
3105
3106 // Sniff for the signature.
3107 if (Stream.Read(8) != 'C' ||
3108 Stream.Read(8) != 'P' ||
3109 Stream.Read(8) != 'C' ||
3110 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003111 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003112 return std::string();
3113 }
3114
3115 RecordData Record;
3116 while (!Stream.AtEndOfStream()) {
3117 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003118
Douglas Gregorb64c1932009-05-12 01:31:05 +00003119 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3120 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003122 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003123 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003124 case AST_BLOCK_ID:
3125 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003126 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003127 return std::string();
3128 }
3129 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003130
Douglas Gregorb64c1932009-05-12 01:31:05 +00003131 default:
3132 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003133 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003134 return std::string();
3135 }
3136 break;
3137 }
3138 continue;
3139 }
3140
3141 if (Code == llvm::bitc::END_BLOCK) {
3142 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003143 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003144 return std::string();
3145 }
3146 continue;
3147 }
3148
3149 if (Code == llvm::bitc::DEFINE_ABBREV) {
3150 Stream.ReadAbbrevRecord();
3151 continue;
3152 }
3153
3154 Record.clear();
3155 const char *BlobStart = 0;
3156 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003157 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003158 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003159 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003160 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003161
3162 return std::string();
3163}
3164
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003165ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003166 // Enter the submodule block.
3167 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3168 Error("malformed submodule block record in AST file");
3169 return Failure;
3170 }
3171
3172 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003173 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003174 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003175 RecordData Record;
3176 while (true) {
3177 unsigned Code = F.Stream.ReadCode();
3178 if (Code == llvm::bitc::END_BLOCK) {
3179 if (F.Stream.ReadBlockEnd()) {
3180 Error("error at end of submodule block in AST file");
3181 return Failure;
3182 }
3183 return Success;
3184 }
3185
3186 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3187 // No known subblocks, always skip them.
3188 F.Stream.ReadSubBlockID();
3189 if (F.Stream.SkipBlock()) {
3190 Error("malformed block record in AST file");
3191 return Failure;
3192 }
3193 continue;
3194 }
3195
3196 if (Code == llvm::bitc::DEFINE_ABBREV) {
3197 F.Stream.ReadAbbrevRecord();
3198 continue;
3199 }
3200
3201 // Read a record.
3202 const char *BlobStart;
3203 unsigned BlobLen;
3204 Record.clear();
3205 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3206 default: // Default behavior: ignore.
3207 break;
3208
3209 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003210 if (First) {
3211 Error("missing submodule metadata record at beginning of block");
3212 return Failure;
3213 }
3214
Douglas Gregore209e502011-12-06 01:10:29 +00003215 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003216 Error("malformed module definition");
3217 return Failure;
3218 }
3219
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003220 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003221 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3222 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3223 bool IsFramework = Record[2];
3224 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003225 bool IsSystem = Record[4];
3226 bool InferSubmodules = Record[5];
3227 bool InferExplicitSubmodules = Record[6];
3228 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003229
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003230 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003231 if (Parent)
3232 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003233
3234 // Retrieve this (sub)module from the module map, creating it if
3235 // necessary.
3236 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3237 IsFramework,
3238 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003239 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3240 if (GlobalIndex >= SubmodulesLoaded.size() ||
3241 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003242 Error("too many submodules");
3243 return Failure;
3244 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003245
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00003246 CurrentModule->setASTFile(F.File);
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003247 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003248 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003249 CurrentModule->InferSubmodules = InferSubmodules;
3250 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3251 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003252 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003253 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003254
Douglas Gregore209e502011-12-06 01:10:29 +00003255 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003256 break;
3257 }
3258
Douglas Gregor77d029f2011-12-08 19:11:24 +00003259 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003260 if (First) {
3261 Error("missing submodule metadata record at beginning of block");
3262 return Failure;
3263 }
3264
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003265 if (!CurrentModule)
3266 break;
3267
3268 StringRef FileName(BlobStart, BlobLen);
3269 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003270 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003271 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003272 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003273 Error("mismatched umbrella headers in submodule");
3274 return Failure;
3275 }
3276 }
3277 break;
3278 }
3279
3280 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003281 if (First) {
3282 Error("missing submodule metadata record at beginning of block");
3283 return Failure;
3284 }
3285
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003286 if (!CurrentModule)
3287 break;
3288
3289 // FIXME: Be more lazy about this!
3290 StringRef FileName(BlobStart, BlobLen);
3291 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3292 if (std::find(CurrentModule->Headers.begin(),
3293 CurrentModule->Headers.end(),
3294 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003295 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003296 }
3297 break;
3298 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00003299
3300 case SUBMODULE_TOPHEADER: {
3301 if (First) {
3302 Error("missing submodule metadata record at beginning of block");
3303 return Failure;
3304 }
3305
3306 if (!CurrentModule)
3307 break;
3308
3309 // FIXME: Be more lazy about this!
3310 StringRef FileName(BlobStart, BlobLen);
3311 if (const FileEntry *File = PP.getFileManager().getFile(FileName))
3312 CurrentModule->TopHeaders.insert(File);
3313 break;
3314 }
3315
Douglas Gregor77d029f2011-12-08 19:11:24 +00003316 case SUBMODULE_UMBRELLA_DIR: {
3317 if (First) {
3318 Error("missing submodule metadata record at beginning of block");
3319 return Failure;
3320 }
3321
3322 if (!CurrentModule)
3323 break;
3324
3325 StringRef DirName(BlobStart, BlobLen);
3326 if (const DirectoryEntry *Umbrella
3327 = PP.getFileManager().getDirectory(DirName)) {
3328 if (!CurrentModule->getUmbrellaDir())
3329 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3330 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3331 Error("mismatched umbrella directories in submodule");
3332 return Failure;
3333 }
3334 }
3335 break;
3336 }
3337
Douglas Gregor26ced122011-12-01 00:59:36 +00003338 case SUBMODULE_METADATA: {
3339 if (!First) {
3340 Error("submodule metadata record not at beginning of block");
3341 return Failure;
3342 }
3343 First = false;
3344
3345 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003346 F.LocalNumSubmodules = Record[0];
3347 unsigned LocalBaseSubmoduleID = Record[1];
3348 if (F.LocalNumSubmodules > 0) {
3349 // Introduce the global -> local mapping for submodules within this
3350 // module.
3351 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3352
3353 // Introduce the local -> global mapping for submodules within this
3354 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003355 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003356 std::make_pair(LocalBaseSubmoduleID,
3357 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3358
3359 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3360 }
3361 break;
3362 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003363
Douglas Gregor55988682011-12-05 16:33:54 +00003364 case SUBMODULE_IMPORTS: {
3365 if (First) {
3366 Error("missing submodule metadata record at beginning of block");
3367 return Failure;
3368 }
3369
3370 if (!CurrentModule)
3371 break;
3372
3373 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3374 UnresolvedModuleImportExport Unresolved;
3375 Unresolved.File = &F;
3376 Unresolved.Mod = CurrentModule;
3377 Unresolved.ID = Record[Idx];
3378 Unresolved.IsImport = true;
3379 Unresolved.IsWildcard = false;
3380 UnresolvedModuleImportExports.push_back(Unresolved);
3381 }
3382 break;
3383 }
3384
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003385 case SUBMODULE_EXPORTS: {
3386 if (First) {
3387 Error("missing submodule metadata record at beginning of block");
3388 return Failure;
3389 }
3390
3391 if (!CurrentModule)
3392 break;
3393
3394 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003395 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003396 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003397 Unresolved.Mod = CurrentModule;
3398 Unresolved.ID = Record[Idx];
3399 Unresolved.IsImport = false;
3400 Unresolved.IsWildcard = Record[Idx + 1];
3401 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003402 }
3403
3404 // Once we've loaded the set of exports, there's no reason to keep
3405 // the parsed, unresolved exports around.
3406 CurrentModule->UnresolvedExports.clear();
3407 break;
3408 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003409 case SUBMODULE_REQUIRES: {
3410 if (First) {
3411 Error("missing submodule metadata record at beginning of block");
3412 return Failure;
3413 }
3414
3415 if (!CurrentModule)
3416 break;
3417
3418 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003419 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003420 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003421 break;
3422 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003423 }
3424 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003425}
3426
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003427/// \brief Parse the record that corresponds to a LangOptions data
3428/// structure.
3429///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003430/// This routine parses the language options from the AST file and then gives
3431/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003432///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003433/// \returns true if the listener deems the file unacceptable, false otherwise.
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003434bool ASTReader::ParseLanguageOptions(const ModuleFile &M,
3435 const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003436 if (Listener) {
3437 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003438 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003439#define LANGOPT(Name, Bits, Default, Description) \
3440 LangOpts.Name = Record[Idx++];
3441#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3442 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3443#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003444
3445 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3446 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3447 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003448
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003449 unsigned Length = Record[Idx++];
3450 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3451 Record.begin() + Idx + Length);
Argyrios Kyrtzidis62288ed2012-10-10 02:12:47 +00003452 return Listener->ReadLanguageOptions(M, LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003453 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003454
3455 return false;
3456}
3457
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003458std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003459ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003460 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003461 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003462 assert(I != GlobalPreprocessedEntityMap.end() &&
3463 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003464 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003465 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3466 return std::make_pair(M, LocalIndex);
3467}
3468
Argyrios Kyrtzidis632dcc92012-10-02 16:10:51 +00003469std::pair<PreprocessingRecord::iterator, PreprocessingRecord::iterator>
3470ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
3471 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
3472 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
3473 Mod.NumPreprocessedEntities);
3474
3475 return std::make_pair(PreprocessingRecord::iterator(),
3476 PreprocessingRecord::iterator());
3477}
3478
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00003479std::pair<ASTReader::ModuleDeclIterator, ASTReader::ModuleDeclIterator>
3480ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
3481 return std::make_pair(ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
3482 ModuleDeclIterator(this, &Mod,
3483 Mod.FileSortedDecls + Mod.NumFileSortedDecls));
3484}
3485
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003486PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3487 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003488 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3489 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003490 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003491 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003492
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003493 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003494 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003495
3496 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3497 switch (Code) {
3498 case llvm::bitc::END_BLOCK:
3499 return 0;
3500
3501 case llvm::bitc::ENTER_SUBBLOCK:
3502 Error("unexpected subblock record in preprocessor detail block");
3503 return 0;
3504
3505 case llvm::bitc::DEFINE_ABBREV:
3506 Error("unexpected abbrevation record in preprocessor detail block");
3507 return 0;
3508
3509 default:
3510 break;
3511 }
3512
3513 if (!PP.getPreprocessingRecord()) {
3514 Error("no preprocessing record");
3515 return 0;
3516 }
3517
3518 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003519 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3520 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003521 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3522 const char *BlobStart = 0;
3523 unsigned BlobLen = 0;
3524 RecordData Record;
3525 PreprocessorDetailRecordTypes RecType =
3526 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3527 Code, Record, BlobStart, BlobLen);
3528 switch (RecType) {
3529 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003530 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003531 IdentifierInfo *Name = 0;
3532 MacroDefinition *Def = 0;
3533 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003534 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003535 else {
3536 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003537 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003538 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3539 }
3540
3541 MacroExpansion *ME;
3542 if (isBuiltin)
3543 ME = new (PPRec) MacroExpansion(Name, Range);
3544 else
3545 ME = new (PPRec) MacroExpansion(Def, Range);
3546
3547 return ME;
3548 }
3549
3550 case PPD_MACRO_DEFINITION: {
3551 // Decode the identifier info and then check again; if the macro is
3552 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003553 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003554 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003555 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003556
3557 if (DeserializationListener)
3558 DeserializationListener->MacroDefinitionRead(PPID, MD);
3559
3560 return MD;
3561 }
3562
3563 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003564 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003565 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3566 const FileEntry *File = 0;
3567 if (!FullFileName.empty())
3568 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003569
3570 // FIXME: Stable encoding
3571 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003572 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003573 InclusionDirective *ID
3574 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003575 StringRef(BlobStart, Record[0]),
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00003576 Record[1], Record[3],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003577 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003578 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003579 return ID;
3580 }
3581 }
David Blaikie7530c032012-01-17 06:56:22 +00003582
3583 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003584}
3585
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003586/// \brief \arg SLocMapI points at a chunk of a module that contains no
3587/// preprocessed entities or the entities it contains are not the ones we are
3588/// looking for. Find the next module that contains entities and return the ID
3589/// of the first entry.
3590PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3591 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3592 ++SLocMapI;
3593 for (GlobalSLocOffsetMapType::const_iterator
3594 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003595 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003596 if (M.NumPreprocessedEntities)
3597 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3598 }
3599
3600 return getTotalNumPreprocessedEntities();
3601}
3602
3603namespace {
3604
3605template <unsigned PPEntityOffset::*PPLoc>
3606struct PPEntityComp {
3607 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003608 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003609
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003610 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003611
Benjamin Kramer88df1252011-09-21 06:42:26 +00003612 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3613 SourceLocation LHS = getLoc(L);
3614 SourceLocation RHS = getLoc(R);
3615 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3616 }
3617
3618 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003619 SourceLocation LHS = getLoc(L);
3620 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3621 }
3622
Benjamin Kramer88df1252011-09-21 06:42:26 +00003623 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003624 SourceLocation RHS = getLoc(R);
3625 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3626 }
3627
3628 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3629 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3630 }
3631};
3632
3633}
3634
3635/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3636PreprocessedEntityID
3637ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3638 if (SourceMgr.isLocalSourceLocation(BLoc))
3639 return getTotalNumPreprocessedEntities();
3640
3641 GlobalSLocOffsetMapType::const_iterator
3642 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3643 BLoc.getOffset());
3644 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3645 "Corrupted global sloc offset map");
3646
3647 if (SLocMapI->second->NumPreprocessedEntities == 0)
3648 return findNextPreprocessedEntity(SLocMapI);
3649
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003650 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003651 typedef const PPEntityOffset *pp_iterator;
3652 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3653 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003654
3655 size_t Count = M.NumPreprocessedEntities;
3656 size_t Half;
3657 pp_iterator First = pp_begin;
3658 pp_iterator PPI;
3659
3660 // Do a binary search manually instead of using std::lower_bound because
3661 // The end locations of entities may be unordered (when a macro expansion
3662 // is inside another macro argument), but for this case it is not important
3663 // whether we get the first macro expansion or its containing macro.
3664 while (Count > 0) {
3665 Half = Count/2;
3666 PPI = First;
3667 std::advance(PPI, Half);
3668 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3669 BLoc)){
3670 First = PPI;
3671 ++First;
3672 Count = Count - Half - 1;
3673 } else
3674 Count = Half;
3675 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003676
3677 if (PPI == pp_end)
3678 return findNextPreprocessedEntity(SLocMapI);
3679
3680 return getGlobalPreprocessedEntityID(M,
3681 M.BasePreprocessedEntityID + (PPI - pp_begin));
3682}
3683
3684/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3685PreprocessedEntityID
3686ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3687 if (SourceMgr.isLocalSourceLocation(ELoc))
3688 return getTotalNumPreprocessedEntities();
3689
3690 GlobalSLocOffsetMapType::const_iterator
3691 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3692 ELoc.getOffset());
3693 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3694 "Corrupted global sloc offset map");
3695
3696 if (SLocMapI->second->NumPreprocessedEntities == 0)
3697 return findNextPreprocessedEntity(SLocMapI);
3698
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003699 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003700 typedef const PPEntityOffset *pp_iterator;
3701 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3702 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3703 pp_iterator PPI =
3704 std::upper_bound(pp_begin, pp_end, ELoc,
3705 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3706
3707 if (PPI == pp_end)
3708 return findNextPreprocessedEntity(SLocMapI);
3709
3710 return getGlobalPreprocessedEntityID(M,
3711 M.BasePreprocessedEntityID + (PPI - pp_begin));
3712}
3713
3714/// \brief Returns a pair of [Begin, End) indices of preallocated
3715/// preprocessed entities that \arg Range encompasses.
3716std::pair<unsigned, unsigned>
3717 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3718 if (Range.isInvalid())
3719 return std::make_pair(0,0);
3720 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3721
3722 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3723 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3724 return std::make_pair(BeginID, EndID);
3725}
3726
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003727/// \brief Optionally returns true or false if the preallocated preprocessed
3728/// entity with index \arg Index came from file \arg FID.
3729llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3730 FileID FID) {
3731 if (FID.isInvalid())
3732 return false;
3733
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003734 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3735 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003736 unsigned LocalIndex = PPInfo.second;
3737 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3738
3739 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3740 if (Loc.isInvalid())
3741 return false;
3742
3743 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3744 return true;
3745 else
3746 return false;
3747}
3748
Douglas Gregord10a3812011-08-25 18:14:34 +00003749namespace {
3750 /// \brief Visitor used to search for information about a header file.
3751 class HeaderFileInfoVisitor {
3752 ASTReader &Reader;
3753 const FileEntry *FE;
3754
3755 llvm::Optional<HeaderFileInfo> HFI;
3756
3757 public:
3758 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3759 : Reader(Reader), FE(FE) { }
3760
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003761 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003762 HeaderFileInfoVisitor *This
3763 = static_cast<HeaderFileInfoVisitor *>(UserData);
3764
3765 HeaderFileInfoTrait Trait(This->Reader, M,
3766 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3767 M.HeaderFileFrameworkStrings,
3768 This->FE->getName());
3769
3770 HeaderFileInfoLookupTable *Table
3771 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3772 if (!Table)
3773 return false;
3774
3775 // Look in the on-disk hash table for an entry for this file name.
3776 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3777 &Trait);
3778 if (Pos == Table->end())
3779 return false;
3780
3781 This->HFI = *Pos;
3782 return true;
3783 }
3784
3785 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3786 };
3787}
3788
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003789HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003790 HeaderFileInfoVisitor Visitor(*this, FE);
3791 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3792 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003793 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003794 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3795 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003796 }
3797
3798 return HeaderFileInfo();
3799}
3800
David Blaikied6471f72011-09-25 23:23:43 +00003801void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003802 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003803 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003804 unsigned Idx = 0;
3805 while (Idx < F.PragmaDiagMappings.size()) {
3806 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003807 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3808 Diag.DiagStatePoints.push_back(
3809 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3810 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003811 while (1) {
3812 assert(Idx < F.PragmaDiagMappings.size() &&
3813 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3814 if (Idx >= F.PragmaDiagMappings.size()) {
3815 break; // Something is messed up but at least avoid infinite loop in
3816 // release build.
3817 }
3818 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3819 if (DiagID == (unsigned)-1) {
3820 break; // no more diag/map pairs for this location.
3821 }
3822 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003823 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3824 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003825 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003826 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003827 }
3828}
3829
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003830/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003831ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003832 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003833 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003834 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003835 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003836}
3837
3838/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003839///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003840/// The index is the type ID, shifted and minus the number of predefs. This
3841/// routine actually reads the record corresponding to the type at the given
3842/// location. It is a helper routine for GetType, which deals with reading type
3843/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003844QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003845 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003846 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003847
Douglas Gregor0b748912009-04-14 21:18:50 +00003848 // Keep track of where we are in the stream, then jump back there
3849 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003850 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003851
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003852 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003853
Douglas Gregord89275b2009-07-06 18:54:52 +00003854 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003855 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003856
Douglas Gregor393f2492011-07-22 00:38:23 +00003857 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003858 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003859 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003860 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003861 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3862 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003863 if (Record.size() != 2) {
3864 Error("Incorrect encoding of extended qualifier type");
3865 return QualType();
3866 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003867 QualType Base = readType(*Loc.F, Record, Idx);
3868 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003869 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003870 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003871
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003872 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003873 if (Record.size() != 1) {
3874 Error("Incorrect encoding of complex type");
3875 return QualType();
3876 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003877 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003878 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003879 }
3880
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003881 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003882 if (Record.size() != 1) {
3883 Error("Incorrect encoding of pointer type");
3884 return QualType();
3885 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003886 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003887 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003888 }
3889
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003890 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003891 if (Record.size() != 1) {
3892 Error("Incorrect encoding of block pointer type");
3893 return QualType();
3894 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003895 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003896 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003897 }
3898
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003899 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003900 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003901 Error("Incorrect encoding of lvalue reference type");
3902 return QualType();
3903 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003904 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003905 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003906 }
3907
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003908 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003909 if (Record.size() != 1) {
3910 Error("Incorrect encoding of rvalue reference type");
3911 return QualType();
3912 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003913 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003914 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003915 }
3916
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003917 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003918 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003919 Error("Incorrect encoding of member pointer type");
3920 return QualType();
3921 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003922 QualType PointeeType = readType(*Loc.F, Record, Idx);
3923 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003924 if (PointeeType.isNull() || ClassType.isNull())
3925 return QualType();
3926
Douglas Gregor35942772011-09-09 21:34:22 +00003927 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003928 }
3929
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003930 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003931 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003932 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3933 unsigned IndexTypeQuals = Record[2];
3934 unsigned Idx = 3;
3935 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003936 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003937 ASM, IndexTypeQuals);
3938 }
3939
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003940 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003941 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003942 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3943 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003944 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003945 }
3946
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003947 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003948 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003949 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3950 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003951 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3952 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003953 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003954 ASM, IndexTypeQuals,
3955 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003956 }
3957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003958 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003959 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003960 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003961 return QualType();
3962 }
3963
Douglas Gregor393f2492011-07-22 00:38:23 +00003964 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003965 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003966 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003967 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003968 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003969 }
3970
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003971 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003972 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003973 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003974 return QualType();
3975 }
3976
Douglas Gregor393f2492011-07-22 00:38:23 +00003977 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003978 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003979 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003980 }
3981
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003982 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003983 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003984 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003985 return QualType();
3986 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003987 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003988 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3989 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003990 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003991 }
3992
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003993 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003994 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003995
3996 FunctionProtoType::ExtProtoInfo EPI;
3997 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003998 /*hasregparm*/ Record[2],
3999 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00004000 static_cast<CallingConv>(Record[4]),
4001 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00004002
John McCallf85e1932011-06-15 23:02:42 +00004003 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004004 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004005 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004006 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004007 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00004008
4009 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00004010 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00004011 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00004012 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004013 ExceptionSpecificationType EST =
4014 static_cast<ExceptionSpecificationType>(Record[Idx++]);
4015 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00004016 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00004017 if (EST == EST_Dynamic) {
4018 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00004019 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00004020 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00004021 EPI.Exceptions = Exceptions.data();
4022 } else if (EST == EST_ComputedNoexcept) {
4023 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00004024 } else if (EST == EST_Uninstantiated) {
4025 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
4026 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Richard Smithb9d0b762012-07-27 04:22:15 +00004027 } else if (EST == EST_Unevaluated) {
4028 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00004029 }
Douglas Gregor35942772011-09-09 21:34:22 +00004030 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00004031 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004032 }
4033
Douglas Gregor409448c2011-07-21 22:35:25 +00004034 case TYPE_UNRESOLVED_USING: {
4035 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00004036 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00004037 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
4038 }
4039
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004040 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004041 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004042 Error("incorrect encoding of typedef type");
4043 return QualType();
4044 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004045 unsigned Idx = 0;
4046 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004047 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004048 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004049 Canonical = Context.getCanonicalType(Canonical);
4050 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004051 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004052
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004053 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00004054 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004055
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004056 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004057 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004058 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004059 return QualType();
4060 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004061 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004062 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004063 }
Mike Stump1eb44332009-09-09 15:08:12 +00004064
Douglas Gregorf8af9822012-02-12 18:42:33 +00004065 case TYPE_DECLTYPE: {
4066 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
4067 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
4068 }
Anders Carlsson395b4752009-06-24 19:06:50 +00004069
Sean Huntca63c202011-05-24 22:41:36 +00004070 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00004071 QualType BaseType = readType(*Loc.F, Record, Idx);
4072 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00004073 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00004074 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00004075 }
4076
Richard Smith34b41d92011-02-20 03:19:35 +00004077 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00004078 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00004079
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004080 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004081 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004082 Error("incorrect encoding of record type");
4083 return QualType();
4084 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004085 unsigned Idx = 0;
4086 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004087 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
4088 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
4089 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00004090 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004091 return T;
4092 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004093
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004094 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004095 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004096 Error("incorrect encoding of enum type");
4097 return QualType();
4098 }
Douglas Gregor409448c2011-07-21 22:35:25 +00004099 unsigned Idx = 0;
4100 bool IsDependent = Record[Idx++];
4101 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00004102 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00004103 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004104 return T;
4105 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004106
John McCall9d156a72011-01-06 01:58:22 +00004107 case TYPE_ATTRIBUTED: {
4108 if (Record.size() != 3) {
4109 Error("incorrect encoding of attributed type");
4110 return QualType();
4111 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004112 QualType modifiedType = readType(*Loc.F, Record, Idx);
4113 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00004114 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00004115 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00004116 }
4117
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004118 case TYPE_PAREN: {
4119 if (Record.size() != 1) {
4120 Error("incorrect encoding of paren type");
4121 return QualType();
4122 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004123 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004124 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004125 }
4126
Douglas Gregor7536dd52010-12-20 02:24:11 +00004127 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004128 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004129 Error("incorrect encoding of pack expansion type");
4130 return QualType();
4131 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004132 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004133 if (Pattern.isNull())
4134 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004135 llvm::Optional<unsigned> NumExpansions;
4136 if (Record[1])
4137 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004138 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004139 }
4140
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004141 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004142 unsigned Idx = 0;
4143 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004144 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004145 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004146 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004147 }
4148
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004149 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004150 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004151 ObjCInterfaceDecl *ItfD
4152 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004153 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004154 }
4155
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004156 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004157 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004158 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004159 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004160 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004161 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004162 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004163 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004164 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004165
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004166 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004167 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004168 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004169 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004170 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004172 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004173 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004174 QualType Parm = readType(*Loc.F, Record, Idx);
4175 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004176 return
Douglas Gregor35942772011-09-09 21:34:22 +00004177 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004178 Replacement);
4179 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004180
Douglas Gregorc3069d62011-01-14 02:55:32 +00004181 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4182 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004183 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004184 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004185 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004186 cast<TemplateTypeParmType>(Parm),
4187 ArgPack);
4188 }
4189
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004190 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004191 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004192 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004193 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004194 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004195 return
Douglas Gregor35942772011-09-09 21:34:22 +00004196 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004197 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004198
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004199 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004200 unsigned Idx = 0;
4201 unsigned Depth = Record[Idx++];
4202 unsigned Index = Record[Idx++];
4203 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004204 TemplateTypeParmDecl *D
4205 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004206 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004207 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004208
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004209 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004210 unsigned Idx = 0;
4211 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004212 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004213 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004214 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004215 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004216 Canon = Context.getCanonicalType(Canon);
4217 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004218 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004220 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004221 unsigned Idx = 0;
4222 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004223 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004224 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004225 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004226 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004227 Args.reserve(NumArgs);
4228 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004229 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004230 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004231 Args.size(), Args.data());
4232 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004233
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004234 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004235 unsigned Idx = 0;
4236
4237 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004238 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004239 ArrayType::ArraySizeModifier ASM
4240 = (ArrayType::ArraySizeModifier)Record[Idx++];
4241 unsigned IndexTypeQuals = Record[Idx++];
4242
4243 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004244 Expr *NumElts = ReadExpr(*Loc.F);
4245 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004246
Douglas Gregor35942772011-09-09 21:34:22 +00004247 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004248 IndexTypeQuals, Brackets);
4249 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004250
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004251 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004252 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004253 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004254 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004255 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004256 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004257 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004258 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004259 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004260 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004261 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004262 else
Douglas Gregor35942772011-09-09 21:34:22 +00004263 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004264 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004265 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004266 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004267 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004268
4269 case TYPE_ATOMIC: {
4270 if (Record.size() != 1) {
4271 Error("Incorrect encoding of atomic type");
4272 return QualType();
4273 }
4274 QualType ValueType = readType(*Loc.F, Record, Idx);
4275 return Context.getAtomicType(ValueType);
4276 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004277 }
David Blaikie7530c032012-01-17 06:56:22 +00004278 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004279}
4280
Sebastian Redlc3632732010-10-05 15:59:54 +00004281class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004282 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004283 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004284 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004285 unsigned &Idx;
4286
Sebastian Redlc3632732010-10-05 15:59:54 +00004287 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4288 unsigned &I) {
4289 return Reader.ReadSourceLocation(F, R, I);
4290 }
4291
Douglas Gregor409448c2011-07-21 22:35:25 +00004292 template<typename T>
4293 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4294 return Reader.ReadDeclAs<T>(F, Record, Idx);
4295 }
4296
John McCalla1ee0c52009-10-16 21:56:05 +00004297public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004298 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004299 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004300 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004301 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004302
John McCall51bd8032009-10-18 01:05:36 +00004303 // We want compile-time assurance that we've enumerated all of
4304 // these, so unfortunately we have to declare them first, then
4305 // define them out-of-line.
4306#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004307#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004308 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004309#include "clang/AST/TypeLocNodes.def"
4310
John McCall51bd8032009-10-18 01:05:36 +00004311 void VisitFunctionTypeLoc(FunctionTypeLoc);
4312 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004313};
4314
John McCall51bd8032009-10-18 01:05:36 +00004315void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004316 // nothing to do
4317}
John McCall51bd8032009-10-18 01:05:36 +00004318void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004319 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004320 if (TL.needsExtraLocalData()) {
4321 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4322 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4323 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4324 TL.setModeAttr(Record[Idx++]);
4325 }
John McCalla1ee0c52009-10-16 21:56:05 +00004326}
John McCall51bd8032009-10-18 01:05:36 +00004327void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004328 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004329}
John McCall51bd8032009-10-18 01:05:36 +00004330void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004331 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004332}
John McCall51bd8032009-10-18 01:05:36 +00004333void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004334 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004335}
John McCall51bd8032009-10-18 01:05:36 +00004336void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004337 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004338}
John McCall51bd8032009-10-18 01:05:36 +00004339void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004340 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004341}
John McCall51bd8032009-10-18 01:05:36 +00004342void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004343 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004344 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004345}
John McCall51bd8032009-10-18 01:05:36 +00004346void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004347 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4348 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004349 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004350 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004351 else
John McCall51bd8032009-10-18 01:05:36 +00004352 TL.setSizeExpr(0);
4353}
4354void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4355 VisitArrayTypeLoc(TL);
4356}
4357void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4358 VisitArrayTypeLoc(TL);
4359}
4360void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4361 VisitArrayTypeLoc(TL);
4362}
4363void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4364 DependentSizedArrayTypeLoc TL) {
4365 VisitArrayTypeLoc(TL);
4366}
4367void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4368 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004369 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004370}
4371void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004372 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004373}
4374void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004375 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004376}
4377void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004378 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004379 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4380 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara796aa442011-03-12 11:17:06 +00004381 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004382 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004383 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004384 }
4385}
4386void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4387 VisitFunctionTypeLoc(TL);
4388}
4389void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4390 VisitFunctionTypeLoc(TL);
4391}
John McCalled976492009-12-04 22:46:56 +00004392void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004393 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004394}
John McCall51bd8032009-10-18 01:05:36 +00004395void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004396 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004397}
4398void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004399 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4400 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4401 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004402}
4403void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004404 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4405 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4406 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4407 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004408}
4409void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004410 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004411}
Sean Huntca63c202011-05-24 22:41:36 +00004412void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4413 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4414 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4415 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4416 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4417}
Richard Smith34b41d92011-02-20 03:19:35 +00004418void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4419 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4420}
John McCall51bd8032009-10-18 01:05:36 +00004421void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004422 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004423}
4424void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004425 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004426}
John McCall9d156a72011-01-06 01:58:22 +00004427void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4428 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4429 if (TL.hasAttrOperand()) {
4430 SourceRange range;
4431 range.setBegin(ReadSourceLocation(Record, Idx));
4432 range.setEnd(ReadSourceLocation(Record, Idx));
4433 TL.setAttrOperandParensRange(range);
4434 }
4435 if (TL.hasAttrExprOperand()) {
4436 if (Record[Idx++])
4437 TL.setAttrExprOperand(Reader.ReadExpr(F));
4438 else
4439 TL.setAttrExprOperand(0);
4440 } else if (TL.hasAttrEnumOperand())
4441 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4442}
John McCall51bd8032009-10-18 01:05:36 +00004443void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004444 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004445}
John McCall49a832b2009-10-18 09:09:24 +00004446void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4447 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004448 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004449}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004450void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4451 SubstTemplateTypeParmPackTypeLoc TL) {
4452 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4453}
John McCall51bd8032009-10-18 01:05:36 +00004454void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4455 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004456 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004457 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4458 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4459 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004460 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4461 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004462 Reader.GetTemplateArgumentLocInfo(F,
4463 TL.getTypePtr()->getArg(i).getKind(),
4464 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004465}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004466void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4467 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4468 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4469}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004470void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004471 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004472 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004473}
John McCall3cb0ebd2010-03-10 03:28:59 +00004474void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004475 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004476}
Douglas Gregor4714c122010-03-31 17:34:00 +00004477void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004478 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004479 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004480 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004481}
John McCall33500952010-06-11 00:33:02 +00004482void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4483 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004484 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004485 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004486 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004487 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004488 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4489 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004490 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4491 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004492 Reader.GetTemplateArgumentLocInfo(F,
4493 TL.getTypePtr()->getArg(I).getKind(),
4494 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004495}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004496void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4497 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4498}
John McCall51bd8032009-10-18 01:05:36 +00004499void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004500 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004501}
4502void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4503 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004504 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4505 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004506 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004507 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004508}
John McCall54e14c42009-10-22 22:37:11 +00004509void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004510 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004511}
Eli Friedmanb001de72011-10-06 23:00:33 +00004512void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4513 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4514 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4515 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4516}
John McCalla1ee0c52009-10-16 21:56:05 +00004517
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004518TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004519 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004520 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004521 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004522 if (InfoTy.isNull())
4523 return 0;
4524
Douglas Gregor35942772011-09-09 21:34:22 +00004525 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004526 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004527 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004528 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004529 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004530}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004531
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004532QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004533 unsigned FastQuals = ID & Qualifiers::FastMask;
4534 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004535
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004536 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004537 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004538 switch ((PredefinedTypeIDs)Index) {
4539 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004540 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4541 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004542
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004543 case PREDEF_TYPE_CHAR_U_ID:
4544 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004545 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004546 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004547 break;
4548
Douglas Gregor35942772011-09-09 21:34:22 +00004549 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4550 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4551 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4552 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4553 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4554 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4555 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4556 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4557 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4558 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4559 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4560 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4561 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004562 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004563 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4564 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4565 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4566 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4567 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004568 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004569 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4570 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4571 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4572 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4573 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4574 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4575 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4576 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4577 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004578
4579 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004580 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004581 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004582
4583 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4584 T = Context.ARCUnbridgedCastTy;
4585 break;
4586
Meador Ingefb40e3f2012-07-01 15:57:25 +00004587 case PREDEF_TYPE_VA_LIST_TAG:
4588 T = Context.getVaListTagType();
4589 break;
Eli Friedmana6c66ce2012-08-31 00:14:07 +00004590
4591 case PREDEF_TYPE_BUILTIN_FN:
4592 T = Context.BuiltinFnTy;
4593 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004594 }
4595
4596 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004597 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004598 }
4599
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004600 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004601 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004602 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004603 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004604 if (TypesLoaded[Index].isNull())
4605 return QualType();
4606
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004607 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004608 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004609 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004610 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004611 }
Mike Stump1eb44332009-09-09 15:08:12 +00004612
John McCall0953e762009-09-24 19:53:00 +00004613 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004614}
4615
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004616QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004617 return GetType(getGlobalTypeID(F, LocalID));
4618}
4619
4620serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004621ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004622 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4623 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4624
4625 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4626 return LocalID;
4627
4628 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4629 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4630 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4631
4632 unsigned GlobalIndex = LocalIndex + I->second;
4633 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4634}
4635
John McCall833ca992009-10-29 08:12:44 +00004636TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004637ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004638 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004639 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004640 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004641 switch (Kind) {
4642 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004643 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004644 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004645 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004646 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004647 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4648 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004649 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004650 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004651 SourceLocation());
4652 }
4653 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004654 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4655 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004656 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004657 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004658 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004659 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004660 }
John McCall833ca992009-10-29 08:12:44 +00004661 case TemplateArgument::Null:
4662 case TemplateArgument::Integral:
4663 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004664 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004665 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004666 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004667 return TemplateArgumentLocInfo();
4668 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004669 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004670}
4671
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004672TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004673ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004674 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004675 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004676
4677 if (Arg.getKind() == TemplateArgument::Expression) {
4678 if (Record[Index++]) // bool InfoHasSameExpr.
4679 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4680 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004681 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004682 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004683}
4684
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004685Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004686 return GetDecl(ID);
4687}
4688
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004689uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004690 unsigned &Idx){
4691 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004692 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004693
Douglas Gregore92b8a12011-08-04 00:01:48 +00004694 unsigned LocalID = Record[Idx++];
4695 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004696}
4697
4698CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004699 RecordLocation Loc = getLocalBitOffset(Offset);
4700 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004701 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004702 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004703 ReadingKindTracker ReadingKind(Read_Decl, *this);
4704 RecordData Record;
4705 unsigned Code = Cursor.ReadCode();
4706 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4707 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4708 Error("Malformed AST file: missing C++ base specifiers");
4709 return 0;
4710 }
4711
4712 unsigned Idx = 0;
4713 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004714 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004715 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4716 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004717 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004718 return Bases;
4719}
4720
Douglas Gregor409448c2011-07-21 22:35:25 +00004721serialization::DeclID
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00004722ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004723 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004724 return LocalID;
4725
4726 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004727 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004728 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4729
4730 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004731}
4732
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004733bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004734 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004735 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4736 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4737 return &M == I->second;
4738}
4739
Douglas Gregorcff9f262012-01-27 01:47:08 +00004740ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4741 if (!D->isFromASTFile())
4742 return 0;
4743 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4744 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4745 return I->second;
4746}
4747
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004748SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4749 if (ID < NUM_PREDEF_DECL_IDS)
4750 return SourceLocation();
4751
4752 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4753
4754 if (Index > DeclsLoaded.size()) {
4755 Error("declaration ID out-of-range for AST file");
4756 return SourceLocation();
4757 }
4758
4759 if (Decl *D = DeclsLoaded[Index])
4760 return D->getLocation();
4761
4762 unsigned RawLocation = 0;
4763 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4764 return ReadSourceLocation(*Rec.F, RawLocation);
4765}
4766
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004767Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004768 if (ID < NUM_PREDEF_DECL_IDS) {
4769 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004770 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004771 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004772
4773 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004774 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004775
4776 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004777 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004778
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004779 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004780 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004781
Douglas Gregor79d67262011-08-12 05:59:41 +00004782 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004783 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004784
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004785 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4786 return Context.getObjCProtocolDecl();
4787
Douglas Gregor772eeae2011-08-12 06:49:56 +00004788 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004789 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004790
4791 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004792 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004793
4794 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004795 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004796
4797 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4798 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004799 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004800 }
4801
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004802 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4803
Richard Smith2fbf3732011-12-20 04:39:57 +00004804 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004805 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004806 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004807 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004808 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004809
Douglas Gregorfd002a72011-12-16 22:37:11 +00004810 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004811 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004812 if (DeserializationListener)
4813 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4814 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004815
4816 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004817}
4818
Douglas Gregora1be2782011-12-17 23:38:30 +00004819DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4820 DeclID GlobalID) {
4821 if (GlobalID < NUM_PREDEF_DECL_IDS)
4822 return GlobalID;
4823
4824 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4825 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4826 ModuleFile *Owner = I->second;
4827
4828 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4829 = M.GlobalToLocalDeclIDs.find(Owner);
4830 if (Pos == M.GlobalToLocalDeclIDs.end())
4831 return 0;
4832
4833 return GlobalID - Owner->BaseDeclID + Pos->second;
4834}
4835
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004836serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004837 const RecordData &Record,
4838 unsigned &Idx) {
4839 if (Idx >= Record.size()) {
4840 Error("Corrupted AST file");
4841 return 0;
4842 }
4843
4844 return getGlobalDeclID(F, Record[Idx++]);
4845}
4846
Chris Lattner887e2b32009-04-27 05:46:25 +00004847/// \brief Resolve the offset of a statement into a statement.
4848///
4849/// This operation will read a new statement from the external
4850/// source each time it is called, and is meant to be used via a
4851/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004852Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004853 // Switch case IDs are per Decl.
4854 ClearSwitchCaseIDs();
4855
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004856 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004857 RecordLocation Loc = getLocalBitOffset(Offset);
4858 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4859 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004860}
4861
Douglas Gregor851c75a2011-08-24 21:27:34 +00004862namespace {
4863 class FindExternalLexicalDeclsVisitor {
4864 ASTReader &Reader;
4865 const DeclContext *DC;
4866 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004867
Douglas Gregor851c75a2011-08-24 21:27:34 +00004868 SmallVectorImpl<Decl*> &Decls;
4869 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4870
4871 public:
4872 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4873 bool (*isKindWeWant)(Decl::Kind),
4874 SmallVectorImpl<Decl*> &Decls)
4875 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4876 {
4877 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4878 PredefsVisited[I] = false;
4879 }
4880
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004881 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004882 if (Preorder)
4883 return false;
4884
4885 FindExternalLexicalDeclsVisitor *This
4886 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4887
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004888 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004889 = M.DeclContextInfos.find(This->DC);
4890 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4891 return false;
4892
4893 // Load all of the declaration IDs
4894 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4895 *IDE = ID + Info->second.NumLexicalDecls;
4896 ID != IDE; ++ID) {
4897 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4898 continue;
4899
4900 // Don't add predefined declarations to the lexical context more
4901 // than once.
4902 if (ID->second < NUM_PREDEF_DECL_IDS) {
4903 if (This->PredefsVisited[ID->second])
4904 continue;
4905
4906 This->PredefsVisited[ID->second] = true;
4907 }
4908
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004909 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4910 if (!This->DC->isDeclInLexicalTraversal(D))
4911 This->Decls.push_back(D);
4912 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004913 }
4914
4915 return false;
4916 }
4917 };
4918}
4919
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004920ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004921 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004922 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004923 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004924 // least. Walk all of the modules in the order they were loaded.
4925 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4926 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004927 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004928 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004929}
4930
Douglas Gregor0d95f772011-08-24 19:03:07 +00004931namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004932
4933class DeclIDComp {
4934 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004935 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004936
4937public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004938 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004939
4940 bool operator()(LocalDeclID L, LocalDeclID R) const {
4941 SourceLocation LHS = getLocation(L);
4942 SourceLocation RHS = getLocation(R);
4943 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4944 }
4945
4946 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4947 SourceLocation RHS = getLocation(R);
4948 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4949 }
4950
4951 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4952 SourceLocation LHS = getLocation(L);
4953 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4954 }
4955
4956 SourceLocation getLocation(LocalDeclID ID) const {
4957 return Reader.getSourceManager().getFileLoc(
4958 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4959 }
4960};
4961
4962}
4963
4964void ASTReader::FindFileRegionDecls(FileID File,
4965 unsigned Offset, unsigned Length,
4966 SmallVectorImpl<Decl *> &Decls) {
4967 SourceManager &SM = getSourceManager();
4968
4969 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4970 if (I == FileDeclIDs.end())
4971 return;
4972
4973 FileDeclsInfo &DInfo = I->second;
4974 if (DInfo.Decls.empty())
4975 return;
4976
4977 SourceLocation
4978 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4979 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4980
4981 DeclIDComp DIDComp(*this, *DInfo.Mod);
4982 ArrayRef<serialization::LocalDeclID>::iterator
4983 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4984 BeginLoc, DIDComp);
4985 if (BeginIt != DInfo.Decls.begin())
4986 --BeginIt;
4987
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004988 // If we are pointing at a top-level decl inside an objc container, we need
4989 // to backtrack until we find it otherwise we will fail to report that the
4990 // region overlaps with an objc container.
4991 while (BeginIt != DInfo.Decls.begin() &&
4992 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4993 ->isTopLevelDeclInObjCContainer())
4994 --BeginIt;
4995
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004996 ArrayRef<serialization::LocalDeclID>::iterator
4997 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4998 EndLoc, DIDComp);
4999 if (EndIt != DInfo.Decls.end())
5000 ++EndIt;
5001
5002 for (ArrayRef<serialization::LocalDeclID>::iterator
5003 DIt = BeginIt; DIt != EndIt; ++DIt)
5004 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
5005}
5006
5007namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005008 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00005009 /// declaration context.
5010 class DeclContextNameLookupVisitor {
5011 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005012 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005013 DeclarationName Name;
5014 SmallVectorImpl<NamedDecl *> &Decls;
5015
5016 public:
5017 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005018 SmallVectorImpl<const DeclContext *> &Contexts,
5019 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00005020 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005021 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005022
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005023 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00005024 DeclContextNameLookupVisitor *This
5025 = static_cast<DeclContextNameLookupVisitor *>(UserData);
5026
5027 // Check whether we have any visible declaration information for
5028 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005029 ModuleFile::DeclContextInfosMap::iterator Info;
5030 bool FoundInfo = false;
5031 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5032 Info = M.DeclContextInfos.find(This->Contexts[I]);
5033 if (Info != M.DeclContextInfos.end() &&
5034 Info->second.NameLookupTableData) {
5035 FoundInfo = true;
5036 break;
5037 }
5038 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00005039
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005040 if (!FoundInfo)
5041 return false;
5042
Douglas Gregor0d95f772011-08-24 19:03:07 +00005043 // Look for this name within this module.
5044 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00005045 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00005046 ASTDeclContextNameLookupTable::iterator Pos
5047 = LookupTable->find(This->Name);
5048 if (Pos == LookupTable->end())
5049 return false;
5050
5051 bool FoundAnything = false;
5052 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
5053 for (; Data.first != Data.second; ++Data.first) {
5054 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
5055 if (!ND)
5056 continue;
5057
5058 if (ND->getDeclName() != This->Name) {
Axel Naumann3dd82f72012-10-01 09:51:27 +00005059 // A name might be null because the decl's redeclarable part is
5060 // currently read before reading its name. The lookup is triggered by
5061 // building that decl (likely indirectly), and so it is later in the
5062 // sense of "already existing" and can be ignored here.
Douglas Gregor0d95f772011-08-24 19:03:07 +00005063 continue;
5064 }
5065
5066 // Record this declaration.
5067 FoundAnything = true;
5068 This->Decls.push_back(ND);
5069 }
5070
5071 return FoundAnything;
5072 }
5073 };
5074}
5075
John McCall76bd1f32010-06-01 09:23:16 +00005076DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005077ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00005078 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00005079 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00005080 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005081 if (!Name)
5082 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
5083 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00005084
Chris Lattner5f9e2722011-07-23 10:55:15 +00005085 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00005086
5087 // Compute the declaration contexts we need to look into. Multiple such
5088 // declaration contexts occur when two declaration contexts from disjoint
5089 // modules get merged, e.g., when two namespaces with the same name are
5090 // independently defined in separate modules.
5091 SmallVector<const DeclContext *, 2> Contexts;
5092 Contexts.push_back(DC);
5093
5094 if (DC->isNamespace()) {
5095 MergedDeclsMap::iterator Merged
5096 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5097 if (Merged != MergedDecls.end()) {
5098 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5099 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5100 }
5101 }
5102
5103 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00005104 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00005105 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00005106 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00005107 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005108}
5109
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005110namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005111 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005112 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005113 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005114 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005115 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005116 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005117
5118 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005119 DeclContextAllNamesVisitor(ASTReader &Reader,
5120 SmallVectorImpl<const DeclContext *> &Contexts,
5121 llvm::DenseMap<DeclarationName,
5122 SmallVector<NamedDecl *, 8> > &Decls)
5123 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005124
5125 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005126 DeclContextAllNamesVisitor *This
5127 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005128
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005129 // Check whether we have any visible declaration information for
5130 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005131 ModuleFile::DeclContextInfosMap::iterator Info;
5132 bool FoundInfo = false;
5133 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5134 Info = M.DeclContextInfos.find(This->Contexts[I]);
5135 if (Info != M.DeclContextInfos.end() &&
5136 Info->second.NameLookupTableData) {
5137 FoundInfo = true;
5138 break;
5139 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005140 }
5141
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005142 if (!FoundInfo)
5143 return false;
5144
5145 ASTDeclContextNameLookupTable *LookupTable =
5146 Info->second.NameLookupTableData;
5147 bool FoundAnything = false;
5148 for (ASTDeclContextNameLookupTable::data_iterator
5149 I = LookupTable->data_begin(), E = LookupTable->data_end();
5150 I != E; ++I) {
5151 ASTDeclContextNameLookupTrait::data_type Data = *I;
5152 for (; Data.first != Data.second; ++Data.first) {
5153 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5154 *Data.first);
5155 if (!ND)
5156 continue;
5157
5158 // Record this declaration.
5159 FoundAnything = true;
5160 This->Decls[ND->getDeclName()].push_back(ND);
5161 }
5162 }
5163
5164 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005165 }
5166 };
5167}
5168
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005169void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005170 if (!DC->hasExternalVisibleStorage())
5171 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005172 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5173
5174 // Compute the declaration contexts we need to look into. Multiple such
5175 // declaration contexts occur when two declaration contexts from disjoint
5176 // modules get merged, e.g., when two namespaces with the same name are
5177 // independently defined in separate modules.
5178 SmallVector<const DeclContext *, 2> Contexts;
5179 Contexts.push_back(DC);
5180
5181 if (DC->isNamespace()) {
5182 MergedDeclsMap::iterator Merged
5183 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5184 if (Merged != MergedDecls.end()) {
5185 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5186 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5187 }
5188 }
5189
5190 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5191 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5192 ++NumVisibleDeclContextsRead;
5193
5194 for (llvm::DenseMap<DeclarationName,
5195 llvm::SmallVector<NamedDecl*, 8> >::iterator
5196 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5197 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5198 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005199 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005200}
5201
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005202/// \brief Under non-PCH compilation the consumer receives the objc methods
5203/// before receiving the implementation, and codegen depends on this.
5204/// We simulate this by deserializing and passing to consumer the methods of the
5205/// implementation before passing the deserialized implementation decl.
5206static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5207 ASTConsumer *Consumer) {
5208 assert(ImplD && Consumer);
5209
5210 for (ObjCImplDecl::method_iterator
5211 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005212 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005213
5214 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5215}
5216
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005217void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005218 assert(Consumer);
5219 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005220 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005221 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005222
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005223 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005224 }
5225}
5226
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005227void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5228 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5229 PassObjCImplDeclToConsumer(ImplD, Consumer);
5230 else
5231 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5232}
5233
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005234void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005235 this->Consumer = Consumer;
5236
Douglas Gregorfdd01722009-04-14 00:24:19 +00005237 if (!Consumer)
5238 return;
5239
5240 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005241 // Force deserialization of this decl, which will cause it to be queued for
5242 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005243 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005244 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005245 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005246
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005247 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005248}
5249
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005250void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005251 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005252
Mike Stump1eb44332009-09-09 15:08:12 +00005253 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005254 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005255 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005256 unsigned NumDeclsLoaded
5257 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5258 (Decl *)0);
5259 unsigned NumIdentifiersLoaded
5260 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5261 IdentifiersLoaded.end(),
5262 (IdentifierInfo *)0);
Douglas Gregora8235d62012-10-09 23:05:51 +00005263 unsigned NumMacrosLoaded
5264 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
5265 MacrosLoaded.end(),
5266 (MacroInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005267 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005268 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5269 SelectorsLoaded.end(),
5270 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005271
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005272 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5273 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005274 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005275 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5276 NumSLocEntriesRead, TotalNumSLocEntries,
5277 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005278 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005279 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005280 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5281 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5282 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005283 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005284 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5285 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005286 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005287 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005288 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5289 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Douglas Gregora8235d62012-10-09 23:05:51 +00005290 if (!MacrosLoaded.empty())
5291 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5292 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
5293 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005294 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005295 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005296 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5297 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005298 if (TotalNumStatements)
5299 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5300 NumStatementsRead, TotalNumStatements,
5301 ((float)NumStatementsRead/TotalNumStatements * 100));
5302 if (TotalNumMacros)
5303 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5304 NumMacrosRead, TotalNumMacros,
5305 ((float)NumMacrosRead/TotalNumMacros * 100));
5306 if (TotalLexicalDeclContexts)
5307 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5308 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5309 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5310 * 100));
5311 if (TotalVisibleDeclContexts)
5312 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5313 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5314 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5315 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005316 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005317 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005318 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5319 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005320 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005321 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005322 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005323 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005324 dump();
5325 std::fprintf(stderr, "\n");
5326}
5327
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005328template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005329static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005330dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005331 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005332 InitialCapacity> &Map) {
5333 if (Map.begin() == Map.end())
5334 return;
5335
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005336 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005337 llvm::errs() << Name << ":\n";
5338 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5339 I != IEnd; ++I) {
5340 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5341 << "\n";
5342 }
5343}
5344
Douglas Gregor23d7df52011-07-21 19:50:14 +00005345void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005346 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005347 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005348 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005349 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005350 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005351 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregora8235d62012-10-09 23:05:51 +00005352 dumpModuleIDMap("Global macro map", GlobalMacroMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005353 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005354 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005355 dumpModuleIDMap("Global preprocessed entity map",
5356 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005357
5358 llvm::errs() << "\n*** PCH/Modules Loaded:";
5359 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5360 MEnd = ModuleMgr.end();
5361 M != MEnd; ++M)
5362 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005363}
5364
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005365/// Return the amount of memory used by memory buffers, breaking down
5366/// by heap-backed versus mmap'ed memory.
5367void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005368 for (ModuleConstIterator I = ModuleMgr.begin(),
5369 E = ModuleMgr.end(); I != E; ++I) {
5370 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005371 size_t bytes = buf->getBufferSize();
5372 switch (buf->getBufferKind()) {
5373 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5374 sizes.malloc_bytes += bytes;
5375 break;
5376 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5377 sizes.mmap_bytes += bytes;
5378 break;
5379 }
5380 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005381 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005382}
5383
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005384void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005385 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005386 S.ExternalSource = this;
5387
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005388 // Makes sure any declarations that were deserialized "too early"
5389 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005390 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005391 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5392 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005393 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005394 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005395
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005396 // Load the offsets of the declarations that Sema references.
5397 // They will be lazily deserialized when needed.
5398 if (!SemaDeclRefs.empty()) {
5399 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005400 if (!SemaObj->StdNamespace)
5401 SemaObj->StdNamespace = SemaDeclRefs[0];
5402 if (!SemaObj->StdBadAlloc)
5403 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005404 }
5405
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005406 if (!FPPragmaOptions.empty()) {
5407 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5408 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5409 }
5410
5411 if (!OpenCLExtensions.empty()) {
5412 unsigned I = 0;
5413#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5414#include "clang/Basic/OpenCLExtensions.def"
5415
5416 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5417 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005418}
5419
Douglas Gregor211f6e82011-08-20 04:39:52 +00005420IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor057df202012-01-18 20:56:22 +00005421 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5422 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005423 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005424 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005425 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005426 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005427}
5428
Douglas Gregor95f42922010-10-14 22:11:03 +00005429namespace clang {
5430 /// \brief An identifier-lookup iterator that enumerates all of the
5431 /// identifiers stored within a set of AST files.
5432 class ASTIdentifierIterator : public IdentifierIterator {
5433 /// \brief The AST reader whose identifiers are being enumerated.
5434 const ASTReader &Reader;
5435
5436 /// \brief The current index into the chain of AST files stored in
5437 /// the AST reader.
5438 unsigned Index;
5439
5440 /// \brief The current position within the identifier lookup table
5441 /// of the current AST file.
5442 ASTIdentifierLookupTable::key_iterator Current;
5443
5444 /// \brief The end position within the identifier lookup table of
5445 /// the current AST file.
5446 ASTIdentifierLookupTable::key_iterator End;
5447
5448 public:
5449 explicit ASTIdentifierIterator(const ASTReader &Reader);
5450
Chris Lattner5f9e2722011-07-23 10:55:15 +00005451 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005452 };
5453}
5454
5455ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005456 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005457 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005458 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005459 Current = IdTable->key_begin();
5460 End = IdTable->key_end();
5461}
5462
Chris Lattner5f9e2722011-07-23 10:55:15 +00005463StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005464 while (Current == End) {
5465 // If we have exhausted all of our AST files, we're done.
5466 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005467 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005468
5469 --Index;
5470 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005471 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5472 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005473 Current = IdTable->key_begin();
5474 End = IdTable->key_end();
5475 }
5476
5477 // We have any identifiers remaining in the current AST file; return
5478 // the next one.
5479 std::pair<const char*, unsigned> Key = *Current;
5480 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005481 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005482}
5483
5484IdentifierIterator *ASTReader::getIdentifiers() const {
5485 return new ASTIdentifierIterator(*this);
5486}
5487
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005488namespace clang { namespace serialization {
5489 class ReadMethodPoolVisitor {
5490 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005491 Selector Sel;
5492 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005493 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5494 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005495
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005496 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005497 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5498 unsigned PriorGeneration)
5499 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005500
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005501 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005502 ReadMethodPoolVisitor *This
5503 = static_cast<ReadMethodPoolVisitor *>(UserData);
5504
5505 if (!M.SelectorLookupTable)
5506 return false;
5507
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005508 // If we've already searched this module file, skip it now.
5509 if (M.Generation <= This->PriorGeneration)
5510 return true;
5511
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005512 ASTSelectorLookupTable *PoolTable
5513 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5514 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5515 if (Pos == PoolTable->end())
5516 return false;
5517
5518 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005519 // FIXME: Not quite happy with the statistics here. We probably should
5520 // disable this tracking when called via LoadSelector.
5521 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005522 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005523 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005524 if (This->Reader.DeserializationListener)
5525 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5526 This->Sel);
5527
5528 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5529 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5530 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005531 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005532
5533 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005534 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5535 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005536 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005537
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005538 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005539 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5540 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005541 }
5542 };
5543} } // end namespace clang::serialization
5544
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005545/// \brief Add the given set of methods to the method list.
5546static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5547 ObjCMethodList &List) {
5548 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5549 S.addMethodToGlobalList(&List, Methods[I]);
5550 }
5551}
5552
5553void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005554 // Get the selector generation and update it to the current generation.
5555 unsigned &Generation = SelectorGeneration[Sel];
5556 unsigned PriorGeneration = Generation;
5557 Generation = CurrentGeneration;
5558
5559 // Search for methods defined with this selector.
5560 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005561 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005562
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005563 if (Visitor.getInstanceMethods().empty() &&
5564 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005565 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005566 return;
5567 }
5568
5569 if (!getSema())
5570 return;
5571
5572 Sema &S = *getSema();
5573 Sema::GlobalMethodPool::iterator Pos
5574 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5575
5576 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5577 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005578}
5579
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005580void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005581 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005582 Namespaces.clear();
5583
5584 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5585 if (NamespaceDecl *Namespace
5586 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5587 Namespaces.push_back(Namespace);
5588 }
5589}
5590
Douglas Gregora8623202011-07-27 20:58:46 +00005591void ASTReader::ReadTentativeDefinitions(
5592 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5593 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5594 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5595 if (Var)
5596 TentativeDefs.push_back(Var);
5597 }
5598 TentativeDefinitions.clear();
5599}
5600
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005601void ASTReader::ReadUnusedFileScopedDecls(
5602 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5603 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5604 DeclaratorDecl *D
5605 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5606 if (D)
5607 Decls.push_back(D);
5608 }
5609 UnusedFileScopedDecls.clear();
5610}
5611
Douglas Gregor0129b562011-07-27 21:57:17 +00005612void ASTReader::ReadDelegatingConstructors(
5613 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5614 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5615 CXXConstructorDecl *D
5616 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5617 if (D)
5618 Decls.push_back(D);
5619 }
5620 DelegatingCtorDecls.clear();
5621}
5622
Douglas Gregord58a0a52011-07-28 00:39:29 +00005623void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5624 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5625 TypedefNameDecl *D
5626 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5627 if (D)
5628 Decls.push_back(D);
5629 }
5630 ExtVectorDecls.clear();
5631}
5632
Douglas Gregora126f172011-07-28 00:53:40 +00005633void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5634 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5635 CXXRecordDecl *D
5636 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5637 if (D)
5638 Decls.push_back(D);
5639 }
5640 DynamicClasses.clear();
5641}
5642
Douglas Gregorec12ce22011-07-28 14:20:37 +00005643void
5644ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5645 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5646 NamedDecl *D
5647 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5648 if (D)
5649 Decls.push_back(D);
5650 }
5651 LocallyScopedExternalDecls.clear();
5652}
5653
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005654void ASTReader::ReadReferencedSelectors(
5655 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5656 if (ReferencedSelectorsData.empty())
5657 return;
5658
5659 // If there are @selector references added them to its pool. This is for
5660 // implementation of -Wselector.
5661 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5662 unsigned I = 0;
5663 while (I < DataSize) {
5664 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5665 SourceLocation SelLoc
5666 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5667 Sels.push_back(std::make_pair(Sel, SelLoc));
5668 }
5669 ReferencedSelectorsData.clear();
5670}
5671
Douglas Gregor31e37b22011-07-28 18:09:57 +00005672void ASTReader::ReadWeakUndeclaredIdentifiers(
5673 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5674 if (WeakUndeclaredIdentifiers.empty())
5675 return;
5676
5677 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5678 IdentifierInfo *WeakId
5679 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5680 IdentifierInfo *AliasId
5681 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5682 SourceLocation Loc
5683 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5684 bool Used = WeakUndeclaredIdentifiers[I++];
5685 WeakInfo WI(AliasId, Loc);
5686 WI.setUsed(Used);
5687 WeakIDs.push_back(std::make_pair(WeakId, WI));
5688 }
5689 WeakUndeclaredIdentifiers.clear();
5690}
5691
Douglas Gregordfe65432011-07-28 19:11:31 +00005692void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5693 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5694 ExternalVTableUse VT;
5695 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5696 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5697 VT.DefinitionRequired = VTableUses[Idx++];
5698 VTables.push_back(VT);
5699 }
5700
5701 VTableUses.clear();
5702}
5703
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005704void ASTReader::ReadPendingInstantiations(
5705 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5706 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5707 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5708 SourceLocation Loc
5709 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
Axel Naumann39d26c32012-10-02 09:09:43 +00005710
Douglas Gregore5fa3c22012-10-03 18:34:48 +00005711 Pending.push_back(std::make_pair(D, Loc));
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005712 }
5713 PendingInstantiations.clear();
5714}
5715
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005716void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005717 // It would be complicated to avoid reading the methods anyway. So don't.
5718 ReadMethodPool(Sel);
5719}
5720
Douglas Gregor95eab172011-07-28 20:55:49 +00005721void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005722 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005723 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005724 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005725 if (DeserializationListener)
5726 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005727}
5728
Douglas Gregord89275b2009-07-06 18:54:52 +00005729/// \brief Set the globally-visible declarations associated with the given
5730/// identifier.
5731///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005732/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005733/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005734/// them.
5735///
5736/// \param II an IdentifierInfo that refers to one or more globally-visible
5737/// declarations.
5738///
5739/// \param DeclIDs the set of declaration IDs with the name @p II that are
5740/// visible at global scope.
5741///
5742/// \param Nonrecursive should be true to indicate that the caller knows that
5743/// this call is non-recursive, and therefore the globally-visible declarations
5744/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005745void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005746ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005747 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005748 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005749 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005750 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5751 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5752 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005753 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005754 return;
5755 }
Mike Stump1eb44332009-09-09 15:08:12 +00005756
Douglas Gregord89275b2009-07-06 18:54:52 +00005757 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5758 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5759 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005760 // Introduce this declaration into the translation-unit scope
5761 // and add it to the declaration chain for this identifier, so
5762 // that (unqualified) name lookup will find it.
5763 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005764 } else {
5765 // Queue this declaration so that it will be added to the
5766 // translation unit scope and identifier's declaration chain
5767 // once a Sema object is known.
5768 PreloadedDecls.push_back(D);
5769 }
5770 }
5771}
5772
Douglas Gregor95eab172011-07-28 20:55:49 +00005773IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005774 if (ID == 0)
5775 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005776
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005777 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005778 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005779 return 0;
5780 }
Mike Stump1eb44332009-09-09 15:08:12 +00005781
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005782 ID -= 1;
5783 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005784 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5785 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005786 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005787 unsigned Index = ID - M->BaseIdentifierID;
5788 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005789
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005790 // All of the strings in the AST file are preceded by a 16-bit length.
5791 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005792 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5793 // unsigned integers. This is important to avoid integer overflow when
5794 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005795 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005796 unsigned StrLen = (((unsigned) StrLenPtr[0])
5797 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005798 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005799 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005800 if (DeserializationListener)
5801 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005802 }
Mike Stump1eb44332009-09-09 15:08:12 +00005803
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005804 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005805}
5806
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005807IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005808 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5809}
5810
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005811IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005812 if (LocalID < NUM_PREDEF_IDENT_IDS)
5813 return LocalID;
5814
5815 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5816 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5817 assert(I != M.IdentifierRemap.end()
5818 && "Invalid index into identifier index remap");
5819
5820 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005821}
5822
Douglas Gregora8235d62012-10-09 23:05:51 +00005823MacroInfo *ASTReader::getMacro(MacroID ID) {
5824 if (ID == 0)
5825 return 0;
5826
5827 if (MacrosLoaded.empty()) {
5828 Error("no macro table in AST file");
5829 return 0;
5830 }
5831
5832 ID -= NUM_PREDEF_MACRO_IDS;
5833 if (!MacrosLoaded[ID]) {
5834 GlobalMacroMapType::iterator I
5835 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
5836 assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
5837 ModuleFile *M = I->second;
5838 unsigned Index = ID - M->BaseMacroID;
5839 ReadMacroRecord(*M, M->MacroOffsets[Index]);
5840 }
5841
5842 return MacrosLoaded[ID];
5843}
5844
5845MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
5846 if (LocalID < NUM_PREDEF_MACRO_IDS)
5847 return LocalID;
5848
5849 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5850 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
5851 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
5852
5853 return LocalID + I->second;
5854}
5855
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005856bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005857 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005858}
5859
Douglas Gregor26ced122011-12-01 00:59:36 +00005860serialization::SubmoduleID
5861ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5862 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5863 return LocalID;
5864
5865 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5866 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5867 assert(I != M.SubmoduleRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005868 && "Invalid index into submodule index remap");
Douglas Gregor26ced122011-12-01 00:59:36 +00005869
5870 return LocalID + I->second;
5871}
5872
5873Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5874 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5875 assert(GlobalID == 0 && "Unhandled global submodule ID");
5876 return 0;
5877 }
5878
5879 if (GlobalID > SubmodulesLoaded.size()) {
5880 Error("submodule ID out of range in AST file");
5881 return 0;
5882 }
5883
5884 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5885}
5886
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005887Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005888 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5889}
5890
5891Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005892 if (ID == 0)
5893 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005894
Sebastian Redl725cd962010-08-04 20:40:17 +00005895 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005896 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005897 return Selector();
5898 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005899
Sebastian Redl725cd962010-08-04 20:40:17 +00005900 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005901 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005902 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5903 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005904 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005905 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005906 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005907 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005908 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005909 if (DeserializationListener)
5910 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005911 }
5912
Sebastian Redl725cd962010-08-04 20:40:17 +00005913 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005914}
5915
Douglas Gregor8451ec72011-07-28 14:41:43 +00005916Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005917 return DecodeSelector(ID);
5918}
5919
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005920uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005921 // ID 0 (the null selector) is considered an external selector.
5922 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005923}
5924
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005925serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005926ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005927 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5928 return LocalID;
5929
5930 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5931 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5932 assert(I != M.SelectorRemap.end()
Douglas Gregora8235d62012-10-09 23:05:51 +00005933 && "Invalid index into selector index remap");
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005934
5935 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005936}
5937
Mike Stump1eb44332009-09-09 15:08:12 +00005938DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005939ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005940 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005941 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5942 switch (Kind) {
5943 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005944 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005945
5946 case DeclarationName::ObjCZeroArgSelector:
5947 case DeclarationName::ObjCOneArgSelector:
5948 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005949 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005950
5951 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005952 return Context.DeclarationNames.getCXXConstructorName(
5953 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005954
5955 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005956 return Context.DeclarationNames.getCXXDestructorName(
5957 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005958
5959 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005960 return Context.DeclarationNames.getCXXConversionFunctionName(
5961 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005962
5963 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005964 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005965 (OverloadedOperatorKind)Record[Idx++]);
5966
Sean Hunt3e518bd2009-11-29 07:34:05 +00005967 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005968 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005969 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005970
Douglas Gregor2cf26342009-04-09 22:27:44 +00005971 case DeclarationName::CXXUsingDirective:
5972 return DeclarationName::getUsingDirectiveName();
5973 }
5974
David Blaikie7530c032012-01-17 06:56:22 +00005975 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005976}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005977
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005978void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005979 DeclarationNameLoc &DNLoc,
5980 DeclarationName Name,
5981 const RecordData &Record, unsigned &Idx) {
5982 switch (Name.getNameKind()) {
5983 case DeclarationName::CXXConstructorName:
5984 case DeclarationName::CXXDestructorName:
5985 case DeclarationName::CXXConversionFunctionName:
5986 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5987 break;
5988
5989 case DeclarationName::CXXOperatorName:
5990 DNLoc.CXXOperatorName.BeginOpNameLoc
5991 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5992 DNLoc.CXXOperatorName.EndOpNameLoc
5993 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5994 break;
5995
5996 case DeclarationName::CXXLiteralOperatorName:
5997 DNLoc.CXXLiteralOperatorName.OpNameLoc
5998 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5999 break;
6000
6001 case DeclarationName::Identifier:
6002 case DeclarationName::ObjCZeroArgSelector:
6003 case DeclarationName::ObjCOneArgSelector:
6004 case DeclarationName::ObjCMultiArgSelector:
6005 case DeclarationName::CXXUsingDirective:
6006 break;
6007 }
6008}
6009
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006010void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006011 DeclarationNameInfo &NameInfo,
6012 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00006013 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006014 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
6015 DeclarationNameLoc DNLoc;
6016 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
6017 NameInfo.setInfo(DNLoc);
6018}
6019
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006020void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006021 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00006022 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006023 unsigned NumTPLists = Record[Idx++];
6024 Info.NumTemplParamLists = NumTPLists;
6025 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00006026 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00006027 for (unsigned i=0; i != NumTPLists; ++i)
6028 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
6029 }
6030}
6031
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006032TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006033ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006034 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00006035 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006036 switch (Kind) {
6037 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00006038 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006039
6040 case TemplateName::OverloadedTemplate: {
6041 unsigned size = Record[Idx++];
6042 UnresolvedSet<8> Decls;
6043 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006044 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006045
Douglas Gregor35942772011-09-09 21:34:22 +00006046 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006047 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006048
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006049 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006050 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006051 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00006052 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006053 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006054 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006055
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006056 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006057 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006058 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00006059 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00006060 GetIdentifierInfo(F, Record,
6061 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00006062 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00006063 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006064 }
John McCall14606042011-06-30 08:33:18 +00006065
6066 case TemplateName::SubstTemplateTemplateParm: {
6067 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00006068 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00006069 if (!param) return TemplateName();
6070 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006071 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00006072 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006073
6074 case TemplateName::SubstTemplateTemplateParmPack: {
6075 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00006076 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006077 if (!Param)
6078 return TemplateName();
6079
6080 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
6081 if (ArgPack.getKind() != TemplateArgument::Pack)
6082 return TemplateName();
6083
Douglas Gregor35942772011-09-09 21:34:22 +00006084 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006085 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006086 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006087
David Blaikieb219cfc2011-09-23 05:06:16 +00006088 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006089}
6090
6091TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006092ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00006093 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00006094 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
6095 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006096 case TemplateArgument::Null:
6097 return TemplateArgument();
6098 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00006099 return TemplateArgument(readType(F, Record, Idx));
Eli Friedmand7a6b162012-09-26 02:36:12 +00006100 case TemplateArgument::Declaration: {
6101 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx);
6102 bool ForReferenceParam = Record[Idx++];
6103 return TemplateArgument(D, ForReferenceParam);
6104 }
6105 case TemplateArgument::NullPtr:
6106 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006107 case TemplateArgument::Integral: {
6108 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00006109 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00006110 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00006111 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00006112 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006113 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00006114 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00006115 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00006116 llvm::Optional<unsigned> NumTemplateExpansions;
6117 if (unsigned NumExpansions = Record[Idx++])
6118 NumTemplateExpansions = NumExpansions - 1;
6119 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00006120 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006121 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00006122 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006123 case TemplateArgument::Pack: {
6124 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006125 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00006126 for (unsigned I = 0; I != NumArgs; ++I)
6127 Args[I] = ReadTemplateArgument(F, Record, Idx);
6128 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006129 }
6130 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006131
David Blaikieb219cfc2011-09-23 05:06:16 +00006132 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00006133}
6134
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006135TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006136ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00006137 const RecordData &Record, unsigned &Idx) {
6138 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
6139 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
6140 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006141
6142 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00006143 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006144 Params.reserve(NumParams);
6145 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00006146 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00006147
6148 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00006149 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006150 Params.data(), Params.size(), RAngleLoc);
6151 return TemplateParams;
6152}
6153
6154void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006155ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00006156ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006157 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006158 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006159 unsigned NumTemplateArgs = Record[Idx++];
6160 TemplArgs.reserve(NumTemplateArgs);
6161 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00006162 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00006163}
6164
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006165/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006166void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006167 const RecordData &Record, unsigned &Idx) {
6168 unsigned NumDecls = Record[Idx++];
6169 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006170 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00006171 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
6172 Set.addDecl(D, AS);
6173 }
6174}
6175
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006176CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006177ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00006178 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006179 bool isVirtual = static_cast<bool>(Record[Idx++]);
6180 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6181 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006182 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006183 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6184 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006185 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006186 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006187 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006188 Result.setInheritConstructors(inheritConstructors);
6189 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006190}
6191
Sean Huntcbb67482011-01-08 20:30:50 +00006192std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006193ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006194 unsigned &Idx) {
6195 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006196 unsigned NumInitializers = Record[Idx++];
6197 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006198 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006199 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006200 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006201 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006202 bool IsBaseVirtual = false;
6203 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006204 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006205
Sean Hunt156b6402011-05-04 01:19:08 +00006206 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6207 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006208 case CTOR_INITIALIZER_BASE:
6209 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006210 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006211 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006212
6213 case CTOR_INITIALIZER_DELEGATING:
6214 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006215 break;
6216
6217 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006218 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006219 break;
6220
6221 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006222 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006223 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006224 }
Sean Hunt156b6402011-05-04 01:19:08 +00006225
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006226 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006227 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006228 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6229 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006230 bool IsWritten = Record[Idx++];
6231 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006232 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006233 if (IsWritten) {
6234 SourceOrderOrNumArrayIndices = Record[Idx++];
6235 } else {
6236 SourceOrderOrNumArrayIndices = Record[Idx++];
6237 Indices.reserve(SourceOrderOrNumArrayIndices);
6238 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006239 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006240 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006241
Sean Huntcbb67482011-01-08 20:30:50 +00006242 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006243 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006244 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006245 LParenLoc, Init, RParenLoc,
6246 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006247 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006248 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6249 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006250 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006251 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006252 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006253 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006254 else
Douglas Gregor35942772011-09-09 21:34:22 +00006255 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006256 MemberOrEllipsisLoc, LParenLoc,
6257 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006258 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006259 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006260 LParenLoc, Init, RParenLoc,
6261 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006262 }
6263
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006264 if (IsWritten)
6265 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006266 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006267 }
6268 }
6269
Sean Huntcbb67482011-01-08 20:30:50 +00006270 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006271}
6272
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006273NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006274ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006275 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006276 unsigned N = Record[Idx++];
6277 NestedNameSpecifier *NNS = 0, *Prev = 0;
6278 for (unsigned I = 0; I != N; ++I) {
6279 NestedNameSpecifier::SpecifierKind Kind
6280 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6281 switch (Kind) {
6282 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006283 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006284 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006285 break;
6286 }
6287
6288 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006289 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006290 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006291 break;
6292 }
6293
Douglas Gregor14aba762011-02-24 02:36:08 +00006294 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006295 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006296 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006297 break;
6298 }
6299
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006300 case NestedNameSpecifier::TypeSpec:
6301 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006302 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006303 if (!T)
6304 return 0;
6305
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006306 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006307 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006308 break;
6309 }
6310
6311 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006312 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006313 // No associated value, and there can't be a prefix.
6314 break;
6315 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006316 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006317 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006318 }
6319 return NNS;
6320}
6321
Douglas Gregordc355712011-02-25 00:36:19 +00006322NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006323ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006324 unsigned &Idx) {
6325 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006326 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006327 for (unsigned I = 0; I != N; ++I) {
6328 NestedNameSpecifier::SpecifierKind Kind
6329 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6330 switch (Kind) {
6331 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006332 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006333 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006334 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006335 break;
6336 }
6337
6338 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006339 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006340 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006341 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006342 break;
6343 }
6344
6345 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006346 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006347 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006348 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006349 break;
6350 }
6351
6352 case NestedNameSpecifier::TypeSpec:
6353 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006354 bool Template = Record[Idx++];
6355 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6356 if (!T)
6357 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006358 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006359
6360 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006361 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006362 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6363 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006364 break;
6365 }
6366
6367 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006368 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006369 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006370 break;
6371 }
6372 }
Douglas Gregordc355712011-02-25 00:36:19 +00006373 }
6374
Douglas Gregor35942772011-09-09 21:34:22 +00006375 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006376}
6377
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006378SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006379ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006380 unsigned &Idx) {
6381 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6382 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006383 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006384}
6385
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006386/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006387llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006388 unsigned BitWidth = Record[Idx++];
6389 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6390 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6391 Idx += NumWords;
6392 return Result;
6393}
6394
6395/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006396llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006397 bool isUnsigned = Record[Idx++];
6398 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6399}
6400
Douglas Gregor17fc2232009-04-14 21:55:33 +00006401/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006402llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006403 return llvm::APFloat(ReadAPInt(Record, Idx));
6404}
6405
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006406// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006407std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006408 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006409 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006410 Idx += Len;
6411 return Result;
6412}
6413
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006414VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6415 unsigned &Idx) {
6416 unsigned Major = Record[Idx++];
6417 unsigned Minor = Record[Idx++];
6418 unsigned Subminor = Record[Idx++];
6419 if (Minor == 0)
6420 return VersionTuple(Major);
6421 if (Subminor == 0)
6422 return VersionTuple(Major, Minor - 1);
6423 return VersionTuple(Major, Minor - 1, Subminor - 1);
6424}
6425
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006426CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006427 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006428 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006429 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006430 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006431}
6432
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006433DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006434 return Diag(SourceLocation(), DiagID);
6435}
6436
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006437DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006438 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006439}
Douglas Gregor025452f2009-04-17 00:04:06 +00006440
Douglas Gregor668c1a42009-04-21 22:25:48 +00006441/// \brief Retrieve the identifier table associated with the
6442/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006443IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006444 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006445}
6446
Douglas Gregor025452f2009-04-17 00:04:06 +00006447/// \brief Record that the given ID maps to the given switch-case
6448/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006449void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006450 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6451 "Already have a SwitchCase with this ID");
6452 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006453}
6454
6455/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006456SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006457 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6458 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006459}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006460
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006461void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006462 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006463}
6464
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006465void ASTReader::ReadComments() {
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006466 std::vector<RawComment *> Comments;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006467 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6468 serialization::ModuleFile *> >::iterator
6469 I = CommentsCursors.begin(),
6470 E = CommentsCursors.end();
6471 I != E; ++I) {
6472 llvm::BitstreamCursor &Cursor = I->first;
6473 serialization::ModuleFile &F = *I->second;
6474 SavedStreamPosition SavedPosition(Cursor);
6475
6476 RecordData Record;
6477 while (true) {
6478 unsigned Code = Cursor.ReadCode();
6479 if (Code == llvm::bitc::END_BLOCK)
6480 break;
6481
6482 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6483 // No known subblocks, always skip them.
6484 Cursor.ReadSubBlockID();
6485 if (Cursor.SkipBlock()) {
6486 Error("malformed block record in AST file");
6487 return;
6488 }
6489 continue;
6490 }
6491
6492 if (Code == llvm::bitc::DEFINE_ABBREV) {
6493 Cursor.ReadAbbrevRecord();
6494 continue;
6495 }
6496
6497 // Read a record.
6498 Record.clear();
6499 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006500 case COMMENTS_RAW_COMMENT: {
6501 unsigned Idx = 0;
6502 SourceRange SR = ReadSourceRange(F, Record, Idx);
6503 RawComment::CommentKind Kind =
6504 (RawComment::CommentKind) Record[Idx++];
6505 bool IsTrailingComment = Record[Idx++];
6506 bool IsAlmostTrailingComment = Record[Idx++];
Dmitri Gribenko811c8202012-07-06 18:19:34 +00006507 Comments.push_back(new (Context) RawComment(SR, Kind,
6508 IsTrailingComment,
6509 IsAlmostTrailingComment));
Chandler Carruth13691bb2012-06-20 06:47:54 +00006510 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006511 }
6512 }
6513 }
6514 }
6515 Context.Comments.addCommentsToFront(Comments);
6516}
6517
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006518void ASTReader::finishPendingActions() {
Douglas Gregorcff9f262012-01-27 01:47:08 +00006519 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006520 // If any identifiers with corresponding top-level declarations have
6521 // been loaded, load those declarations now.
6522 while (!PendingIdentifierInfos.empty()) {
6523 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6524 PendingIdentifierInfos.front().DeclIDs, true);
6525 PendingIdentifierInfos.pop_front();
6526 }
6527
Douglas Gregora1be2782011-12-17 23:38:30 +00006528 // Load pending declaration chains.
6529 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6530 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006531 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006532 }
6533 PendingDeclChains.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006534 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006535
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006536 // If we deserialized any C++ or Objective-C class definitions, any
6537 // Objective-C protocol definitions, or any redeclarable templates, make sure
6538 // that all redeclarations point to the definitions. Note that this can only
6539 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006540 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6541 DEnd = PendingDefinitions.end();
6542 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006543 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6544 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6545 // Make sure that the TagType points at the definition.
6546 const_cast<TagType*>(TagT)->decl = TD;
6547 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006548
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006549 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6550 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6551 REnd = RD->redecls_end();
6552 R != REnd; ++R)
6553 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6554
6555 }
6556
Douglas Gregorfc529f72011-12-19 19:00:47 +00006557 continue;
6558 }
6559
Douglas Gregor1d784b22012-01-01 19:51:50 +00006560 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006561 // Make sure that the ObjCInterfaceType points at the definition.
6562 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6563 ->Decl = ID;
6564
Douglas Gregor1d784b22012-01-01 19:51:50 +00006565 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6566 REnd = ID->redecls_end();
6567 R != REnd; ++R)
6568 R->Data = ID->Data;
6569
6570 continue;
6571 }
6572
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006573 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6574 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6575 REnd = PD->redecls_end();
6576 R != REnd; ++R)
6577 R->Data = PD->Data;
6578
6579 continue;
6580 }
6581
6582 RedeclarableTemplateDecl *RTD
6583 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6584 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6585 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006586 R != REnd; ++R)
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006587 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006588 }
6589 PendingDefinitions.clear();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006590
6591 // Load the bodies of any functions or methods we've encountered. We do
6592 // this now (delayed) so that we can be sure that the declaration chains
6593 // have been fully wired up.
Douglas Gregorce12d2f2012-10-09 17:50:23 +00006594 for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
6595 PBEnd = PendingBodies.end();
Douglas Gregor5456b0fe2012-10-09 17:21:28 +00006596 PB != PBEnd; ++PB) {
6597 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
6598 // FIXME: Check for =delete/=default?
6599 // FIXME: Complain about ODR violations here?
6600 if (!getContext().getLangOpts().Modules || !FD->hasBody())
6601 FD->setLazyBody(PB->second);
6602 continue;
6603 }
6604
6605 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
6606 if (!getContext().getLangOpts().Modules || !MD->hasBody())
6607 MD->setLazyBody(PB->second);
6608 }
6609 PendingBodies.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006610}
6611
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006612void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006613 assert(NumCurrentElementsDeserializing &&
6614 "FinishedDeserializing not paired with StartedDeserializing");
6615 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006616 // We decrease NumCurrentElementsDeserializing only after pending actions
6617 // are finished, to avoid recursively re-calling finishPendingActions().
6618 finishPendingActions();
6619 }
6620 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006621
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006622 if (NumCurrentElementsDeserializing == 0 &&
6623 Consumer && !PassingDeclsToConsumer) {
6624 // Guard variable to avoid recursively redoing the process of passing
6625 // decls to consumer.
6626 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6627 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006628
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006629 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006630 // We are not in recursive loading, so it's safe to pass the "interesting"
6631 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006632 Decl *D = InterestingDecls.front();
6633 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006634 PassInterestingDeclToConsumer(D);
6635 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006636 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006637}
Douglas Gregor501c1032010-08-19 00:28:17 +00006638
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006639ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006640 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006641 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006642 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6643 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006644 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +00006645 Consumer(0), ModuleMgr(PP.getFileManager()),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006646 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006647 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006648 DisableStatCache(DisableStatCache),
6649 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006650 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6651 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006652 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006653 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6654 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6655 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6656 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006657 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6658 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006659 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006660 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006661{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006662 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006663}
6664
Sebastian Redle1dde812010-08-24 00:50:04 +00006665ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006666 for (DeclContextVisibleUpdatesPending::iterator
6667 I = PendingVisibleUpdates.begin(),
6668 E = PendingVisibleUpdates.end();
6669 I != E; ++I) {
6670 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6671 F = I->second.end();
6672 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006673 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006674 }
6675}