blob: 3d643f6b43969d3ed495c046ac37988d40859ef5 [file] [log] [blame]
Sebastian Redl904c9c82010-08-18 23:57:11 +00001//===--- ASTReader.cpp - AST File Reader ------------------------*- C++ -*-===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redlc43b54c2010-08-18 23:56:43 +000010// This file defines the ASTReader class, which reads AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
Chris Lattner4c6f9522009-04-27 05:14:47 +000013
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000014#include "clang/Serialization/ASTReader.h"
15#include "clang/Serialization/ASTDeserializationListener.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000016#include "clang/Serialization/ModuleManager.h"
Chandler Carrutha2398d72011-12-09 00:02:23 +000017#include "clang/Serialization/SerializationDiagnostic.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000018#include "ASTCommon.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000019#include "ASTReaderInternals.h"
Douglas Gregore737f502010-08-12 20:07:10 +000020#include "clang/Sema/Sema.h"
John McCall5f1e0942010-08-24 08:50:51 +000021#include "clang/Sema/Scope.h"
Douglas Gregorfdd01722009-04-14 00:24:19 +000022#include "clang/AST/ASTConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/ASTContext.h"
John McCall2a7fb272010-08-25 05:32:35 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Douglas Gregor5f791bb2011-02-28 23:58:31 +000027#include "clang/AST/NestedNameSpecifier.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000028#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000029#include "clang/AST/TypeLocVisitor.h"
Chris Lattner42d42b52009-04-10 21:41:48 +000030#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000031#include "clang/Lex/PreprocessingRecord.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000033#include "clang/Lex/HeaderSearch.h"
Douglas Gregor668c1a42009-04-21 22:25:48 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000038#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000039#include "clang/Basic/TargetInfo.h"
Douglas Gregor445e23e2009-10-05 21:07:28 +000040#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000041#include "clang/Basic/VersionTuple.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamReader.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Support/MemoryBuffer.h"
John McCall833ca992009-10-29 08:12:44 +000045#include "llvm/Support/ErrorHandling.h"
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000046#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Nick Lewyckyb346d2f2012-04-16 02:51:46 +000048#include "llvm/Support/SaveAndRestore.h"
Michael J. Spencer3a321e22010-12-09 17:36:38 +000049#include "llvm/Support/system_error.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000050#include <algorithm>
Douglas Gregore721f952009-04-28 18:58:38 +000051#include <iterator>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052#include <cstdio>
Douglas Gregor4fed3f42009-04-27 18:38:38 +000053#include <sys/stat.h>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000054
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor98339b92011-08-25 20:47:51 +000057using namespace clang::serialization::reader;
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
59//===----------------------------------------------------------------------===//
Sebastian Redl3c7f4132010-08-18 23:57:06 +000060// PCH validator implementation
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000061//===----------------------------------------------------------------------===//
62
Sebastian Redl571db7f2010-08-18 23:56:56 +000063ASTReaderListener::~ASTReaderListener() {}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000064
65bool
66PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) {
David Blaikie4e4d0842012-03-11 07:00:24 +000067 const LangOptions &PPLangOpts = PP.getLangOpts();
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000068
69#define LANGOPT(Name, Bits, Default, Description) \
70 if (PPLangOpts.Name != LangOpts.Name) { \
71 Reader.Diag(diag::err_pch_langopt_mismatch) \
72 << Description << LangOpts.Name << PPLangOpts.Name; \
73 return true; \
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000074 }
75
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000076#define VALUE_LANGOPT(Name, Bits, Default, Description) \
77 if (PPLangOpts.Name != LangOpts.Name) { \
78 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
79 << Description; \
80 return true; \
81}
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +000082
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000083#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
84 if (PPLangOpts.get##Name() != LangOpts.get##Name()) { \
85 Reader.Diag(diag::err_pch_langopt_value_mismatch) \
86 << Description; \
87 return true; \
88 }
89
90#define BENIGN_LANGOPT(Name, Bits, Default, Description)
91#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
92#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +000093
94 if (PPLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
95 Reader.Diag(diag::err_pch_langopt_value_mismatch)
96 << "target Objective-C runtime";
97 return true;
98 }
Douglas Gregor7d5e81b2011-09-13 18:26:39 +000099
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000100 return false;
101}
102
Chris Lattner5f9e2722011-07-23 10:55:15 +0000103bool PCHValidator::ReadTargetTriple(StringRef Triple) {
Daniel Dunbardc3c0d22009-11-11 00:52:11 +0000104 if (Triple == PP.getTargetInfo().getTriple().str())
105 return false;
106
107 Reader.Diag(diag::warn_pch_target_triple)
108 << Triple << PP.getTargetInfo().getTriple().str();
109 return true;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000110}
111
Benjamin Kramer54353f42010-11-25 18:29:30 +0000112namespace {
113 struct EmptyStringRef {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000114 bool operator ()(StringRef r) const { return r.empty(); }
Benjamin Kramer54353f42010-11-25 18:29:30 +0000115 };
116 struct EmptyBlock {
117 bool operator ()(const PCHPredefinesBlock &r) const {return r.Data.empty();}
118 };
119}
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000120
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121static bool EqualConcatenations(SmallVector<StringRef, 2> L,
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000122 PCHPredefinesBlocks R) {
123 // First, sum up the lengths.
124 unsigned LL = 0, RL = 0;
125 for (unsigned I = 0, N = L.size(); I != N; ++I) {
126 LL += L[I].size();
127 }
128 for (unsigned I = 0, N = R.size(); I != N; ++I) {
129 RL += R[I].Data.size();
130 }
131 if (LL != RL)
132 return false;
133 if (LL == 0 && RL == 0)
134 return true;
135
136 // Kick out empty parts, they confuse the algorithm below.
137 L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end());
138 R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end());
139
140 // Do it the hard way. At this point, both vectors must be non-empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 StringRef LR = L[0], RR = R[0].Data;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000142 unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size();
Daniel Dunbarc76c9e02010-07-16 00:00:11 +0000143 (void) RN;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000144 for (;;) {
145 // Compare the current pieces.
146 if (LR.size() == RR.size()) {
147 // If they're the same length, it's pretty easy.
148 if (LR != RR)
149 return false;
150 // Both pieces are done, advance.
151 ++LI;
152 ++RI;
153 // If either string is done, they're both done, since they're the same
154 // length.
155 if (LI == LN) {
156 assert(RI == RN && "Strings not the same length after all?");
157 return true;
158 }
159 LR = L[LI];
160 RR = R[RI].Data;
161 } else if (LR.size() < RR.size()) {
162 // Right piece is longer.
163 if (!RR.startswith(LR))
164 return false;
165 ++LI;
166 assert(LI != LN && "Strings not the same length after all?");
167 RR = RR.substr(LR.size());
168 LR = L[LI];
169 } else {
170 // Left piece is longer.
171 if (!LR.startswith(RR))
172 return false;
173 ++RI;
174 assert(RI != RN && "Strings not the same length after all?");
175 LR = LR.substr(RR.size());
176 RR = R[RI].Data;
177 }
178 }
179}
180
Chris Lattner5f9e2722011-07-23 10:55:15 +0000181static std::pair<FileID, StringRef::size_type>
182FindMacro(const PCHPredefinesBlocks &Buffers, StringRef MacroDef) {
183 std::pair<FileID, StringRef::size_type> Res;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000184 for (unsigned I = 0, N = Buffers.size(); I != N; ++I) {
185 Res.second = Buffers[I].Data.find(MacroDef);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186 if (Res.second != StringRef::npos) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000187 Res.first = Buffers[I].BufferID;
188 break;
189 }
190 }
191 return Res;
192}
193
194bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000195 StringRef OriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000196 std::string &SuggestedPredefines,
197 FileManager &FileMgr) {
Daniel Dunbarc7162932009-11-11 23:58:53 +0000198 // We are in the context of an implicit include, so the predefines buffer will
199 // have a #include entry for the PCH file itself (as normalized by the
200 // preprocessor initialization). Find it and skip over it in the checking
201 // below.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000202 SmallString<256> PCHInclude;
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000203 PCHInclude += "#include \"";
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000204 PCHInclude += HeaderSearch::NormalizeDashIncludePath(OriginalFileName,
205 FileMgr);
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000206 PCHInclude += "\"\n";
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 std::pair<StringRef,StringRef> Split =
208 StringRef(PP.getPredefines()).split(PCHInclude.str());
209 StringRef Left = Split.first, Right = Split.second;
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +0000210 if (Left == PP.getPredefines()) {
211 Error("Missing PCH include entry!");
212 return true;
213 }
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000214
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000215 // If the concatenation of all the PCH buffers is equal to the adjusted
216 // command line, we're done.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 SmallVector<StringRef, 2> CommandLine;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000218 CommandLine.push_back(Left);
219 CommandLine.push_back(Right);
220 if (EqualConcatenations(CommandLine, Buffers))
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000221 return false;
222
223 SourceManager &SourceMgr = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000225 // The predefines buffers are different. Determine what the differences are,
226 // and whether they require us to reject the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000227 SmallVector<StringRef, 8> PCHLines;
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000228 for (unsigned I = 0, N = Buffers.size(); I != N; ++I)
229 Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Daniel Dunbare6750492009-11-13 16:46:11 +0000230
Chris Lattner5f9e2722011-07-23 10:55:15 +0000231 SmallVector<StringRef, 8> CmdLineLines;
Daniel Dunbare6750492009-11-13 16:46:11 +0000232 Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000233
234 // Pick out implicit #includes after the PCH and don't consider them for
235 // validation; we will insert them into SuggestedPredefines so that the
236 // preprocessor includes them.
237 std::string IncludesAfterPCH;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000238 SmallVector<StringRef, 8> AfterPCHLines;
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000239 Right.split(AfterPCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
240 for (unsigned i = 0, e = AfterPCHLines.size(); i != e; ++i) {
241 if (AfterPCHLines[i].startswith("#include ")) {
242 IncludesAfterPCH += AfterPCHLines[i];
243 IncludesAfterPCH += '\n';
244 } else {
245 CmdLineLines.push_back(AfterPCHLines[i]);
246 }
247 }
248
249 // Make sure we add the includes last into SuggestedPredefines before we
250 // exit this function.
251 struct AddIncludesRAII {
252 std::string &SuggestedPredefines;
253 std::string &IncludesAfterPCH;
254
255 AddIncludesRAII(std::string &SuggestedPredefines,
256 std::string &IncludesAfterPCH)
257 : SuggestedPredefines(SuggestedPredefines),
258 IncludesAfterPCH(IncludesAfterPCH) { }
259 ~AddIncludesRAII() {
260 SuggestedPredefines += IncludesAfterPCH;
261 }
262 } AddIncludes(SuggestedPredefines, IncludesAfterPCH);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000263
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000264 // Sort both sets of predefined buffer lines, since we allow some extra
265 // definitions and they may appear at any point in the output.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000266 std::sort(CmdLineLines.begin(), CmdLineLines.end());
267 std::sort(PCHLines.begin(), PCHLines.end());
268
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000269 // Determine which predefines that were used to build the PCH file are missing
270 // from the command line.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000271 std::vector<StringRef> MissingPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000272 std::set_difference(PCHLines.begin(), PCHLines.end(),
273 CmdLineLines.begin(), CmdLineLines.end(),
274 std::back_inserter(MissingPredefines));
275
276 bool MissingDefines = false;
277 bool ConflictingDefines = false;
278 for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000279 StringRef Missing = MissingPredefines[I];
Argyrios Kyrtzidis297c7062010-09-30 16:53:50 +0000280 if (Missing.startswith("#include ")) {
281 // An -include was specified when generating the PCH; it is included in
282 // the PCH, just ignore it.
283 continue;
284 }
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000285 if (!Missing.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000286 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
287 return true;
288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000290 // This is a macro definition. Determine the name of the macro we're
291 // defining.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000292 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000293 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000294 = Missing.find_first_of("( \n\r", StartOfMacroName);
295 assert(EndOfMacroName != std::string::npos &&
296 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000297 StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000298
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000299 // Determine whether this macro was given a different definition on the
300 // command line.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000301 std::string MacroDefStart = "#define " + MacroName.str();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000302 std::string::size_type MacroDefLen = MacroDefStart.size();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000303 SmallVector<StringRef, 8>::iterator ConflictPos
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000304 = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(),
305 MacroDefStart);
306 for (; ConflictPos != CmdLineLines.end(); ++ConflictPos) {
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000307 if (!ConflictPos->startswith(MacroDefStart)) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000308 // Different macro; we're done.
309 ConflictPos = CmdLineLines.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000310 break;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
313 assert(ConflictPos->size() > MacroDefLen &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000314 "Invalid #define in predefines buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +0000315 if ((*ConflictPos)[MacroDefLen] != ' ' &&
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000316 (*ConflictPos)[MacroDefLen] != '(')
317 continue; // Longer macro name; keep trying.
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000319 // We found a conflicting macro definition.
320 break;
321 }
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000323 if (ConflictPos != CmdLineLines.end()) {
324 Reader.Diag(diag::warn_cmdline_conflicting_macro_def)
325 << MacroName;
326
327 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000328 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000329 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000331 SourceLocation PCHMissingLoc =
332 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000333 .getLocWithOffset(MacroLoc.second);
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000334 Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000335
336 ConflictingDefines = true;
337 continue;
338 }
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Daniel Dunbar10014aa2009-11-11 03:45:59 +0000340 // If the macro doesn't conflict, then we'll just pick up the macro
341 // definition from the PCH file. Warn the user that they made a mistake.
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000342 if (ConflictingDefines)
343 continue; // Don't complain if there are already conflicting defs
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000345 if (!MissingDefines) {
346 Reader.Diag(diag::warn_cmdline_missing_macro_defs);
347 MissingDefines = true;
348 }
349
350 // Show the definition of this macro within the PCH file.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000351 std::pair<FileID, StringRef::size_type> MacroLoc =
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000352 FindMacro(Buffers, Missing);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000353 assert(MacroLoc.second!=StringRef::npos && "Unable to find macro!");
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000354 SourceLocation PCHMissingLoc =
355 SourceMgr.getLocForStartOfFile(MacroLoc.first)
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000356 .getLocWithOffset(MacroLoc.second);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000357 Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch);
358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000360 if (ConflictingDefines)
361 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000363 // Determine what predefines were introduced based on command-line
364 // parameters that were not present when building the PCH
365 // file. Extra #defines are okay, so long as the identifiers being
366 // defined were not used within the precompiled header.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000367 std::vector<StringRef> ExtraPredefines;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000368 std::set_difference(CmdLineLines.begin(), CmdLineLines.end(),
369 PCHLines.begin(), PCHLines.end(),
Mike Stump1eb44332009-09-09 15:08:12 +0000370 std::back_inserter(ExtraPredefines));
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000371 for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000372 StringRef &Extra = ExtraPredefines[I];
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000373 if (!Extra.startswith("#define ")) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000374 Reader.Diag(diag::warn_pch_compiler_options_mismatch);
375 return true;
376 }
377
378 // This is an extra macro definition. Determine the name of the
379 // macro we're defining.
380 std::string::size_type StartOfMacroName = strlen("#define ");
Mike Stump1eb44332009-09-09 15:08:12 +0000381 std::string::size_type EndOfMacroName
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000382 = Extra.find_first_of("( \n\r", StartOfMacroName);
383 assert(EndOfMacroName != std::string::npos &&
384 "Couldn't find the end of the macro name");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000385 StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000386
387 // Check whether this name was used somewhere in the PCH file. If
388 // so, defining it as a macro could change behavior, so we reject
389 // the PCH file.
Daniel Dunbar4d5936a2009-11-11 05:26:28 +0000390 if (IdentifierInfo *II = Reader.get(MacroName)) {
Daniel Dunbar4fda42e2009-11-11 00:52:00 +0000391 Reader.Diag(diag::warn_macro_name_used_in_pch) << II;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000392 return true;
393 }
394
395 // Add this definition to the suggested predefines buffer.
396 SuggestedPredefines += Extra;
397 SuggestedPredefines += '\n';
398 }
399
400 // If we get here, it's because the predefines buffer had compatible
401 // contents. Accept the PCH file.
402 return false;
403}
404
Douglas Gregor12fab312010-03-16 16:35:32 +0000405void PCHValidator::ReadHeaderFileInfo(const HeaderFileInfo &HFI,
406 unsigned ID) {
407 PP.getHeaderSearchInfo().setHeaderFileInfoForUID(HFI, ID);
408 ++NumHeaderInfos;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000409}
410
411void PCHValidator::ReadCounter(unsigned Value) {
412 PP.setCounterValue(Value);
413}
414
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000415//===----------------------------------------------------------------------===//
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000416// AST reader implementation
Douglas Gregor668c1a42009-04-21 22:25:48 +0000417//===----------------------------------------------------------------------===//
418
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000419void
Sebastian Redl571db7f2010-08-18 23:56:56 +0000420ASTReader::setDeserializationListener(ASTDeserializationListener *Listener) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000421 DeserializationListener = Listener;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000422}
423
Chris Lattner4c6f9522009-04-27 05:14:47 +0000424
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000425
Douglas Gregor98339b92011-08-25 20:47:51 +0000426unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
427 return serialization::ComputeHash(Sel);
428}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000429
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregor98339b92011-08-25 20:47:51 +0000431std::pair<unsigned, unsigned>
432ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
433 using namespace clang::io;
434 unsigned KeyLen = ReadUnalignedLE16(d);
435 unsigned DataLen = ReadUnalignedLE16(d);
436 return std::make_pair(KeyLen, DataLen);
437}
438
439ASTSelectorLookupTrait::internal_key_type
440ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
441 using namespace clang::io;
Douglas Gregor35942772011-09-09 21:34:22 +0000442 SelectorTable &SelTable = Reader.getContext().Selectors;
Douglas Gregor98339b92011-08-25 20:47:51 +0000443 unsigned N = ReadUnalignedLE16(d);
444 IdentifierInfo *FirstII
445 = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
446 if (N == 0)
447 return SelTable.getNullarySelector(FirstII);
448 else if (N == 1)
449 return SelTable.getUnarySelector(FirstII);
450
451 SmallVector<IdentifierInfo *, 16> Args;
452 Args.push_back(FirstII);
453 for (unsigned I = 1; I != N; ++I)
454 Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)));
455
456 return SelTable.getSelector(N, Args.data());
457}
458
459ASTSelectorLookupTrait::data_type
460ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
461 unsigned DataLen) {
462 using namespace clang::io;
463
464 data_type Result;
465
466 Result.ID = Reader.getGlobalSelectorID(F, ReadUnalignedLE32(d));
467 unsigned NumInstanceMethods = ReadUnalignedLE16(d);
468 unsigned NumFactoryMethods = ReadUnalignedLE16(d);
469
470 // Load instance methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000471 for (unsigned I = 0; I != NumInstanceMethods; ++I) {
472 if (ObjCMethodDecl *Method
473 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
474 Result.Instance.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Douglas Gregor98339b92011-08-25 20:47:51 +0000477 // Load factory methods
Douglas Gregor98339b92011-08-25 20:47:51 +0000478 for (unsigned I = 0; I != NumFactoryMethods; ++I) {
479 if (ObjCMethodDecl *Method
480 = Reader.GetLocalDeclAs<ObjCMethodDecl>(F, ReadUnalignedLE32(d)))
481 Result.Factory.push_back(Method);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Douglas Gregor98339b92011-08-25 20:47:51 +0000484 return Result;
485}
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregor98339b92011-08-25 20:47:51 +0000487unsigned ASTIdentifierLookupTrait::ComputeHash(const internal_key_type& a) {
488 return llvm::HashString(StringRef(a.first, a.second));
489}
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregor98339b92011-08-25 20:47:51 +0000491std::pair<unsigned, unsigned>
492ASTIdentifierLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
493 using namespace clang::io;
494 unsigned DataLen = ReadUnalignedLE16(d);
495 unsigned KeyLen = ReadUnalignedLE16(d);
496 return std::make_pair(KeyLen, DataLen);
497}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000498
Douglas Gregor98339b92011-08-25 20:47:51 +0000499std::pair<const char*, unsigned>
500ASTIdentifierLookupTrait::ReadKey(const unsigned char* d, unsigned n) {
501 assert(n >= 2 && d[n-1] == '\0');
502 return std::make_pair((const char*) d, n-1);
503}
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000504
Douglas Gregor98339b92011-08-25 20:47:51 +0000505IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
506 const unsigned char* d,
507 unsigned DataLen) {
508 using namespace clang::io;
509 unsigned RawID = ReadUnalignedLE32(d);
510 bool IsInteresting = RawID & 0x01;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor98339b92011-08-25 20:47:51 +0000512 // Wipe out the "is interesting" bit.
513 RawID = RawID >> 1;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000514
Douglas Gregor98339b92011-08-25 20:47:51 +0000515 IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
516 if (!IsInteresting) {
517 // For uninteresting identifiers, just build the IdentifierInfo
518 // and associate it with the persistent ID.
Douglas Gregor668c1a42009-04-21 22:25:48 +0000519 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000520 if (!II) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +0000521 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000522 KnownII = II;
523 }
Douglas Gregor668c1a42009-04-21 22:25:48 +0000524 Reader.SetIdentifierInfo(ID, II);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000525 II->setIsFromAST();
Douglas Gregor057df202012-01-18 20:56:22 +0000526 Reader.markIdentifierUpToDate(II);
Douglas Gregor668c1a42009-04-21 22:25:48 +0000527 return II;
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Douglas Gregor98339b92011-08-25 20:47:51 +0000530 unsigned Bits = ReadUnalignedLE16(d);
531 bool CPlusPlusOperatorKeyword = Bits & 0x01;
532 Bits >>= 1;
533 bool HasRevertedTokenIDToIdentifier = Bits & 0x01;
534 Bits >>= 1;
535 bool Poisoned = Bits & 0x01;
536 Bits >>= 1;
537 bool ExtensionToken = Bits & 0x01;
538 Bits >>= 1;
539 bool hasMacroDefinition = Bits & 0x01;
540 Bits >>= 1;
Craig Topper925be542011-12-19 05:04:33 +0000541 unsigned ObjCOrBuiltinID = Bits & 0x7FF;
542 Bits >>= 11;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000543
Douglas Gregor98339b92011-08-25 20:47:51 +0000544 assert(Bits == 0 && "Extra bits in the identifier?");
545 DataLen -= 6;
Douglas Gregor668c1a42009-04-21 22:25:48 +0000546
Douglas Gregor98339b92011-08-25 20:47:51 +0000547 // Build the IdentifierInfo itself and link the identifier ID with
548 // the new IdentifierInfo.
549 IdentifierInfo *II = KnownII;
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000550 if (!II) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000551 II = &Reader.getIdentifierTable().getOwn(StringRef(k.first, k.second));
Douglas Gregor5d5051f2012-01-24 15:24:38 +0000552 KnownII = II;
553 }
Douglas Gregor057df202012-01-18 20:56:22 +0000554 Reader.markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +0000555 II->setIsFromAST();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000556
Douglas Gregor98339b92011-08-25 20:47:51 +0000557 // Set or check the various bits in the IdentifierInfo structure.
558 // Token IDs are read-only.
559 if (HasRevertedTokenIDToIdentifier)
560 II->RevertTokenIDToIdentifier();
561 II->setObjCOrBuiltinID(ObjCOrBuiltinID);
562 assert(II->isExtensionToken() == ExtensionToken &&
563 "Incorrect extension token flag");
564 (void)ExtensionToken;
565 if (Poisoned)
566 II->setIsPoisoned(true);
567 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
568 "Incorrect C++ operator keyword flag");
569 (void)CPlusPlusOperatorKeyword;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000570
Douglas Gregor98339b92011-08-25 20:47:51 +0000571 // If this identifier is a macro, deserialize the macro
572 // definition.
573 if (hasMacroDefinition) {
574 // FIXME: Check for conflicts?
575 uint32_t Offset = ReadUnalignedLE32(d);
Douglas Gregor13292642011-12-02 15:45:10 +0000576 unsigned LocalSubmoduleID = ReadUnalignedLE32(d);
577
578 // Determine whether this macro definition should be visible now, or
579 // whether it is in a hidden submodule.
580 bool Visible = true;
581 if (SubmoduleID GlobalSubmoduleID
582 = Reader.getGlobalSubmoduleID(F, LocalSubmoduleID)) {
583 if (Module *Owner = Reader.getSubmodule(GlobalSubmoduleID)) {
584 if (Owner->NameVisibility == Module::Hidden) {
585 // The owning module is not visible, and this macro definition should
586 // not be, either.
587 Visible = false;
588
589 // Note that this macro definition was hidden because its owning
590 // module is not yet visible.
591 Reader.HiddenNamesMap[Owner].push_back(II);
592 }
593 }
594 }
595
596 Reader.setIdentifierIsMacro(II, F, Offset, Visible);
597 DataLen -= 8;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000598 }
599
Douglas Gregoreee242f2011-10-27 09:33:13 +0000600 Reader.SetIdentifierInfo(ID, II);
601
Douglas Gregor98339b92011-08-25 20:47:51 +0000602 // Read all of the declarations visible at global scope with this
603 // name.
Douglas Gregor98339b92011-08-25 20:47:51 +0000604 if (DataLen > 0) {
605 SmallVector<uint32_t, 4> DeclIDs;
606 for (; DataLen > 0; DataLen -= 4)
607 DeclIDs.push_back(Reader.getGlobalDeclID(F, ReadUnalignedLE32(d)));
608 Reader.SetGloballyVisibleDecls(II, DeclIDs);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000609 }
610
Douglas Gregor98339b92011-08-25 20:47:51 +0000611 return II;
612}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000613
Douglas Gregor98339b92011-08-25 20:47:51 +0000614unsigned
615ASTDeclContextNameLookupTrait::ComputeHash(const DeclNameKey &Key) const {
616 llvm::FoldingSetNodeID ID;
617 ID.AddInteger(Key.Kind);
618
619 switch (Key.Kind) {
620 case DeclarationName::Identifier:
621 case DeclarationName::CXXLiteralOperatorName:
622 ID.AddString(((IdentifierInfo*)Key.Data)->getName());
623 break;
624 case DeclarationName::ObjCZeroArgSelector:
625 case DeclarationName::ObjCOneArgSelector:
626 case DeclarationName::ObjCMultiArgSelector:
627 ID.AddInteger(serialization::ComputeHash(Selector(Key.Data)));
628 break;
629 case DeclarationName::CXXOperatorName:
630 ID.AddInteger((OverloadedOperatorKind)Key.Data);
631 break;
632 case DeclarationName::CXXConstructorName:
633 case DeclarationName::CXXDestructorName:
634 case DeclarationName::CXXConversionFunctionName:
635 case DeclarationName::CXXUsingDirective:
636 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000637 }
638
Douglas Gregor98339b92011-08-25 20:47:51 +0000639 return ID.ComputeHash();
640}
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000641
Douglas Gregor98339b92011-08-25 20:47:51 +0000642ASTDeclContextNameLookupTrait::internal_key_type
643ASTDeclContextNameLookupTrait::GetInternalKey(
644 const external_key_type& Name) const {
645 DeclNameKey Key;
646 Key.Kind = Name.getNameKind();
647 switch (Name.getNameKind()) {
648 case DeclarationName::Identifier:
649 Key.Data = (uint64_t)Name.getAsIdentifierInfo();
650 break;
651 case DeclarationName::ObjCZeroArgSelector:
652 case DeclarationName::ObjCOneArgSelector:
653 case DeclarationName::ObjCMultiArgSelector:
654 Key.Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
655 break;
656 case DeclarationName::CXXOperatorName:
657 Key.Data = Name.getCXXOverloadedOperator();
658 break;
659 case DeclarationName::CXXLiteralOperatorName:
660 Key.Data = (uint64_t)Name.getCXXLiteralIdentifier();
661 break;
662 case DeclarationName::CXXConstructorName:
663 case DeclarationName::CXXDestructorName:
664 case DeclarationName::CXXConversionFunctionName:
665 case DeclarationName::CXXUsingDirective:
666 Key.Data = 0;
667 break;
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +0000668 }
669
Douglas Gregor98339b92011-08-25 20:47:51 +0000670 return Key;
671}
672
Douglas Gregor98339b92011-08-25 20:47:51 +0000673std::pair<unsigned, unsigned>
674ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
675 using namespace clang::io;
676 unsigned KeyLen = ReadUnalignedLE16(d);
677 unsigned DataLen = ReadUnalignedLE16(d);
678 return std::make_pair(KeyLen, DataLen);
679}
Michael J. Spencer20249a12010-10-21 03:16:25 +0000680
Douglas Gregor98339b92011-08-25 20:47:51 +0000681ASTDeclContextNameLookupTrait::internal_key_type
682ASTDeclContextNameLookupTrait::ReadKey(const unsigned char* d, unsigned) {
683 using namespace clang::io;
684
685 DeclNameKey Key;
686 Key.Kind = (DeclarationName::NameKind)*d++;
687 switch (Key.Kind) {
688 case DeclarationName::Identifier:
689 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
690 break;
691 case DeclarationName::ObjCZeroArgSelector:
692 case DeclarationName::ObjCOneArgSelector:
693 case DeclarationName::ObjCMultiArgSelector:
694 Key.Data =
695 (uint64_t)Reader.getLocalSelector(F, ReadUnalignedLE32(d))
696 .getAsOpaquePtr();
697 break;
698 case DeclarationName::CXXOperatorName:
699 Key.Data = *d++; // OverloadedOperatorKind
700 break;
701 case DeclarationName::CXXLiteralOperatorName:
702 Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d));
703 break;
704 case DeclarationName::CXXConstructorName:
705 case DeclarationName::CXXDestructorName:
706 case DeclarationName::CXXConversionFunctionName:
707 case DeclarationName::CXXUsingDirective:
708 Key.Data = 0;
709 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000710 }
711
Douglas Gregor98339b92011-08-25 20:47:51 +0000712 return Key;
713}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000714
Douglas Gregor98339b92011-08-25 20:47:51 +0000715ASTDeclContextNameLookupTrait::data_type
716ASTDeclContextNameLookupTrait::ReadData(internal_key_type,
717 const unsigned char* d,
Nick Lewyckyb346d2f2012-04-16 02:51:46 +0000718 unsigned DataLen) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000719 using namespace clang::io;
720 unsigned NumDecls = ReadUnalignedLE16(d);
Douglas Gregor9b8b20f2012-01-06 16:09:53 +0000721 LE32DeclID *Start = (LE32DeclID *)d;
Douglas Gregor98339b92011-08-25 20:47:51 +0000722 return std::make_pair(Start, Start + NumDecls);
723}
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +0000724
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000725bool ASTReader::ReadDeclContextStorage(ModuleFile &M,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000726 llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000727 const std::pair<uint64_t, uint64_t> &Offsets,
728 DeclContextInfo &Info) {
729 SavedStreamPosition SavedPosition(Cursor);
730 // First the lexical decls.
731 if (Offsets.first != 0) {
732 Cursor.JumpToBit(Offsets.first);
733
734 RecordData Record;
735 const char *Blob;
736 unsigned BlobLen;
737 unsigned Code = Cursor.ReadCode();
738 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
739 if (RecCode != DECL_CONTEXT_LEXICAL) {
740 Error("Expected lexical block");
741 return true;
742 }
743
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +0000744 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair*>(Blob);
745 Info.NumLexicalDecls = BlobLen / sizeof(KindDeclIDPair);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000746 }
747
748 // Now the lookup table.
749 if (Offsets.second != 0) {
750 Cursor.JumpToBit(Offsets.second);
751
752 RecordData Record;
753 const char *Blob;
754 unsigned BlobLen;
755 unsigned Code = Cursor.ReadCode();
756 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
757 if (RecCode != DECL_CONTEXT_VISIBLE) {
758 Error("Expected visible lookup table block");
759 return true;
760 }
761 Info.NameLookupTableData
762 = ASTDeclContextNameLookupTable::Create(
763 (const unsigned char *)Blob + Record[0],
764 (const unsigned char *)Blob,
Douglas Gregor0d95f772011-08-24 19:03:07 +0000765 ASTDeclContextNameLookupTrait(*this, M));
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +0000766 }
767
768 return false;
769}
770
Chris Lattner5f9e2722011-07-23 10:55:15 +0000771void ASTReader::Error(StringRef Msg) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000772 Error(diag::err_fe_pch_malformed, Msg);
773}
774
775void ASTReader::Error(unsigned DiagID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000776 StringRef Arg1, StringRef Arg2) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +0000777 if (Diags.isDiagnosticInFlight())
778 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2);
779 else
780 Diag(DiagID) << Arg1 << Arg2;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000781}
782
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000783/// \brief Tell the AST listener about the predefines buffers in the chain.
Sebastian Redlc43b54c2010-08-18 23:56:43 +0000784bool ASTReader::CheckPredefinesBuffers() {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +0000785 if (Listener)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +0000786 return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers,
Daniel Dunbar7b5a1212009-11-11 05:29:04 +0000787 ActualOriginalFileName,
Nick Lewycky277a6e72011-02-23 21:16:44 +0000788 SuggestedPredefines,
789 FileMgr);
Douglas Gregore721f952009-04-28 18:58:38 +0000790 return false;
Douglas Gregore1d918e2009-04-10 23:10:45 +0000791}
792
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000793//===----------------------------------------------------------------------===//
794// Source Manager Deserialization
795//===----------------------------------------------------------------------===//
796
Douglas Gregorbd945002009-04-13 16:31:14 +0000797/// \brief Read the line table in the source manager block.
Sebastian Redlc3632732010-10-05 15:59:54 +0000798/// \returns true if there was an error.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000799bool ASTReader::ParseLineTable(ModuleFile &F,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000800 SmallVectorImpl<uint64_t> &Record) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000801 unsigned Idx = 0;
802 LineTableInfo &LineTable = SourceMgr.getLineTable();
803
804 // Parse the file names
Douglas Gregorff0a9872009-04-13 17:12:42 +0000805 std::map<int, int> FileIDs;
806 for (int I = 0, N = Record[Idx++]; I != N; ++I) {
Douglas Gregorbd945002009-04-13 16:31:14 +0000807 // Extract the file name
808 unsigned FilenameLen = Record[Idx++];
809 std::string Filename(&Record[Idx], &Record[Idx] + FilenameLen);
810 Idx += FilenameLen;
Douglas Gregore650c8c2009-07-07 00:12:59 +0000811 MaybeAddSystemRootToFilename(Filename);
Jay Foad65aa6882011-06-21 15:13:30 +0000812 FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
Douglas Gregorbd945002009-04-13 16:31:14 +0000813 }
814
815 // Parse the line entries
816 std::vector<LineEntry> Entries;
817 while (Idx < Record.size()) {
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000818 int FID = Record[Idx++];
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000819 assert(FID >= 0 && "Serialized line entries for non-local file.");
820 // Remap FileID from 1-based old view.
821 FID += F.SLocEntryBaseID - 1;
Douglas Gregorbd945002009-04-13 16:31:14 +0000822
823 // Extract the line entries
824 unsigned NumEntries = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000825 assert(NumEntries && "Numentries is 00000");
Douglas Gregorbd945002009-04-13 16:31:14 +0000826 Entries.clear();
827 Entries.reserve(NumEntries);
828 for (unsigned I = 0; I != NumEntries; ++I) {
829 unsigned FileOffset = Record[Idx++];
830 unsigned LineNo = Record[Idx++];
Argyrios Kyrtzidisf52a5d22010-07-02 11:55:05 +0000831 int FilenameID = FileIDs[Record[Idx++]];
Mike Stump1eb44332009-09-09 15:08:12 +0000832 SrcMgr::CharacteristicKind FileKind
Douglas Gregorbd945002009-04-13 16:31:14 +0000833 = (SrcMgr::CharacteristicKind)Record[Idx++];
834 unsigned IncludeOffset = Record[Idx++];
835 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
836 FileKind, IncludeOffset));
837 }
Douglas Gregor47d9de62012-06-08 16:40:28 +0000838 LineTable.AddEntry(FileID::get(FID), Entries);
Douglas Gregorbd945002009-04-13 16:31:14 +0000839 }
840
841 return false;
842}
843
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000844namespace {
845
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000846class ASTStatData {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000847public:
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000848 const ino_t ino;
849 const dev_t dev;
850 const mode_t mode;
851 const time_t mtime;
852 const off_t size;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000854 ASTStatData(ino_t i, dev_t d, mode_t mo, time_t m, off_t s)
Chris Lattner74e976b2010-11-23 19:28:12 +0000855 : ino(i), dev(d), mode(mo), mtime(m), size(s) {}
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000856};
857
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000858class ASTStatLookupTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000859 public:
860 typedef const char *external_key_type;
861 typedef const char *internal_key_type;
862
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000863 typedef ASTStatData data_type;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000864
865 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000866 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000867 }
868
869 static internal_key_type GetInternalKey(const char *path) { return path; }
870
871 static bool EqualKey(internal_key_type a, internal_key_type b) {
872 return strcmp(a, b) == 0;
873 }
874
875 static std::pair<unsigned, unsigned>
876 ReadKeyDataLength(const unsigned char*& d) {
877 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
878 unsigned DataLen = (unsigned) *d++;
879 return std::make_pair(KeyLen + 1, DataLen);
880 }
881
882 static internal_key_type ReadKey(const unsigned char *d, unsigned) {
883 return (const char *)d;
884 }
885
886 static data_type ReadData(const internal_key_type, const unsigned char *d,
887 unsigned /*DataLen*/) {
888 using namespace clang::io;
889
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000890 ino_t ino = (ino_t) ReadUnalignedLE32(d);
891 dev_t dev = (dev_t) ReadUnalignedLE32(d);
892 mode_t mode = (mode_t) ReadUnalignedLE16(d);
Mike Stump1eb44332009-09-09 15:08:12 +0000893 time_t mtime = (time_t) ReadUnalignedLE64(d);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000894 off_t size = (off_t) ReadUnalignedLE64(d);
895 return data_type(ino, dev, mode, mtime, size);
896 }
897};
898
899/// \brief stat() cache for precompiled headers.
900///
901/// This cache is very similar to the stat cache used by pretokenized
902/// headers.
Chris Lattner10e286a2010-11-23 19:19:34 +0000903class ASTStatCache : public FileSystemStatCache {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000904 typedef OnDiskChainedHashTable<ASTStatLookupTrait> CacheTy;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000905 CacheTy *Cache;
906
907 unsigned &NumStatHits, &NumStatMisses;
Mike Stump1eb44332009-09-09 15:08:12 +0000908public:
Chris Lattner74e976b2010-11-23 19:28:12 +0000909 ASTStatCache(const unsigned char *Buckets, const unsigned char *Base,
910 unsigned &NumStatHits, unsigned &NumStatMisses)
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911 : Cache(0), NumStatHits(NumStatHits), NumStatMisses(NumStatMisses) {
912 Cache = CacheTy::Create(Buckets, Base);
913 }
914
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000915 ~ASTStatCache() { delete Cache; }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattner898a0612010-11-23 21:17:56 +0000917 LookupResult getStat(const char *Path, struct stat &StatBuf,
918 int *FileDescriptor) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000919 // Do the lookup for the file's data in the AST file.
Chris Lattner10e286a2010-11-23 19:19:34 +0000920 CacheTy::iterator I = Cache->find(Path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000921
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000922 // If we don't get a hit in the AST file just forward to 'stat'.
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000923 if (I == Cache->end()) {
924 ++NumStatMisses;
Chris Lattner898a0612010-11-23 21:17:56 +0000925 return statChained(Path, StatBuf, FileDescriptor);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000928 ++NumStatHits;
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000929 ASTStatData Data = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Chris Lattner10e286a2010-11-23 19:19:34 +0000931 StatBuf.st_ino = Data.ino;
932 StatBuf.st_dev = Data.dev;
933 StatBuf.st_mtime = Data.mtime;
934 StatBuf.st_mode = Data.mode;
935 StatBuf.st_size = Data.size;
Chris Lattnerd6f61112010-11-23 20:05:15 +0000936 return CacheExists;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000937 }
938};
939} // end anonymous namespace
940
941
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000942/// \brief Read a source manager block
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000943ASTReader::ASTReadResult ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000944 using namespace SrcMgr;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000945
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000946 llvm::BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +0000947
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000948 // Set the source-location entry cursor to the current position in
949 // the stream. This cursor will be used to read the contents of the
950 // source manager block initially, and then lazily read
951 // source-location entries as needed.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000952 SLocEntryCursor = F.Stream;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000953
954 // The stream itself is going to skip over the source manager block.
Sebastian Redl93fb9ed2010-07-19 20:52:06 +0000955 if (F.Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000956 Error("malformed block record in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000957 return Failure;
958 }
959
960 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000961 if (SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000962 Error("malformed source manager block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000963 return Failure;
964 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000965
Douglas Gregor14f79002009-04-10 03:52:48 +0000966 RecordData Record;
967 while (true) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000968 unsigned Code = SLocEntryCursor.ReadCode();
Douglas Gregor14f79002009-04-10 03:52:48 +0000969 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000970 if (SLocEntryCursor.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000971 Error("error at end of Source Manager block in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000972 return Failure;
973 }
Douglas Gregore1d918e2009-04-10 23:10:45 +0000974 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor14f79002009-04-10 03:52:48 +0000977 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
978 // No known subblocks, always skip them.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000979 SLocEntryCursor.ReadSubBlockID();
980 if (SLocEntryCursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000981 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +0000982 return Failure;
983 }
Douglas Gregor14f79002009-04-10 03:52:48 +0000984 continue;
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Douglas Gregor14f79002009-04-10 03:52:48 +0000987 if (Code == llvm::bitc::DEFINE_ABBREV) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000988 SLocEntryCursor.ReadAbbrevRecord();
Douglas Gregor14f79002009-04-10 03:52:48 +0000989 continue;
990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregor14f79002009-04-10 03:52:48 +0000992 // Read a record.
993 const char *BlobStart;
994 unsigned BlobLen;
995 Record.clear();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000996 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000997 default: // Default behavior: ignore.
998 break;
999
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001000 case SM_SLOC_FILE_ENTRY:
1001 case SM_SLOC_BUFFER_ENTRY:
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001002 case SM_SLOC_EXPANSION_ENTRY:
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001003 // Once we hit one of the source location entries, we're done.
1004 return Success;
Douglas Gregor14f79002009-04-10 03:52:48 +00001005 }
1006 }
1007}
1008
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001009/// \brief If a header file is not found at the path that we expect it to be
1010/// and the PCH file was moved from its original location, try to resolve the
1011/// file by assuming that header+PCH were moved together and the header is in
1012/// the same place relative to the PCH.
1013static std::string
1014resolveFileRelativeToOriginalDir(const std::string &Filename,
1015 const std::string &OriginalDir,
1016 const std::string &CurrDir) {
1017 assert(OriginalDir != CurrDir &&
1018 "No point trying to resolve the file if the PCH dir didn't change");
1019 using namespace llvm::sys;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001020 SmallString<128> filePath(Filename);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001021 fs::make_absolute(filePath);
1022 assert(path::is_absolute(OriginalDir));
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001023 SmallString<128> currPCHPath(CurrDir);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001024
1025 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1026 fileDirE = path::end(path::parent_path(filePath));
1027 path::const_iterator origDirI = path::begin(OriginalDir),
1028 origDirE = path::end(OriginalDir);
1029 // Skip the common path components from filePath and OriginalDir.
1030 while (fileDirI != fileDirE && origDirI != origDirE &&
1031 *fileDirI == *origDirI) {
1032 ++fileDirI;
1033 ++origDirI;
1034 }
1035 for (; origDirI != origDirE; ++origDirI)
1036 path::append(currPCHPath, "..");
1037 path::append(currPCHPath, fileDirI, fileDirE);
1038 path::append(currPCHPath, path::filename(Filename));
1039 return currPCHPath.str();
1040}
1041
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001042/// \brief Read in the source location entry with the given ID.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001043ASTReader::ASTReadResult ASTReader::ReadSLocEntryRecord(int ID) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001044 if (ID == 0)
1045 return Success;
1046
Douglas Gregor0cdd7982011-07-21 18:46:38 +00001047 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001048 Error("source location entry ID out-of-range for AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001049 return Failure;
1050 }
1051
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001052 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001053 F->SLocEntryCursor.JumpToBit(F->SLocEntryOffsets[ID - F->SLocEntryBaseID]);
Sebastian Redlc3632732010-10-05 15:59:54 +00001054 llvm::BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001055 unsigned BaseOffset = F->SLocEntryBaseOffset;
Sebastian Redl9137a522010-07-16 17:50:48 +00001056
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001057 ++NumSLocEntriesRead;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001058 unsigned Code = SLocEntryCursor.ReadCode();
1059 if (Code == llvm::bitc::END_BLOCK ||
1060 Code == llvm::bitc::ENTER_SUBBLOCK ||
1061 Code == llvm::bitc::DEFINE_ABBREV) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001062 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001063 return Failure;
1064 }
1065
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001066 RecordData Record;
1067 const char *BlobStart;
1068 unsigned BlobLen;
1069 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1070 default:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001071 Error("incorrectly-formatted source location entry in AST file");
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001072 return Failure;
1073
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001074 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00001075 if (Record.size() < 7) {
1076 Error("source location entry is incorrect");
1077 return Failure;
1078 }
1079
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001080 // We will detect whether a file changed and return 'Failure' for it, but
1081 // we will also try to fail gracefully by setting up the SLocEntry.
1082 ASTReader::ASTReadResult Result = Success;
1083
Douglas Gregora081da52011-11-16 20:05:18 +00001084 bool OverriddenBuffer = Record[6];
1085
1086 std::string OrigFilename(BlobStart, BlobStart + BlobLen);
1087 std::string Filename = OrigFilename;
Douglas Gregore650c8c2009-07-07 00:12:59 +00001088 MaybeAddSystemRootToFilename(Filename);
Douglas Gregora4581a12011-11-17 19:08:51 +00001089 const FileEntry *File =
1090 OverriddenBuffer? FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1091 (time_t)Record[5])
1092 : FileMgr.getFile(Filename, /*OpenFile=*/false);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001093 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1094 OriginalDir != CurrentDir) {
1095 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1096 OriginalDir,
1097 CurrentDir);
1098 if (!resolved.empty())
1099 File = FileMgr.getFile(resolved);
1100 }
Axel Naumann04331162011-01-27 10:55:51 +00001101 if (File == 0)
1102 File = FileMgr.getVirtualFile(Filename, (off_t)Record[4],
1103 (time_t)Record[5]);
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001104 if (File == 0) {
1105 std::string ErrorStr = "could not find file '";
Douglas Gregore650c8c2009-07-07 00:12:59 +00001106 ErrorStr += Filename;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001107 ErrorStr += "' referenced by AST file";
Chris Lattnerd3555ae2009-06-15 04:35:16 +00001108 Error(ErrorStr.c_str());
1109 return Failure;
1110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001112 if (!DisableValidation &&
1113 ((off_t)Record[4] != File->getSize()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001114#if !defined(LLVM_ON_WIN32)
1115 // In our regression testing, the Windows file system seems to
1116 // have inconsistent modification times that sometimes
1117 // erroneously trigger this error-handling path.
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001118 || (time_t)Record[5] != File->getModificationTime()
Douglas Gregor9f692a02010-04-09 15:54:22 +00001119#endif
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001120 )) {
Argyrios Kyrtzidis8d8f2c22011-04-25 22:23:56 +00001121 Error(diag::err_fe_pch_file_modified, Filename);
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001122 Result = Failure;
Douglas Gregor2d52be52010-03-21 22:49:54 +00001123 }
1124
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001125 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
Douglas Gregor72a9ae12011-07-22 16:00:58 +00001126 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001127 // This is the module's main file.
1128 IncludeLoc = getImportLocation(F);
1129 }
1130 FileID FID = SourceMgr.createFileID(File, IncludeLoc,
Chris Lattner75dfb652010-11-23 09:19:42 +00001131 (SrcMgr::CharacteristicKind)Record[2],
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001132 ID, BaseOffset + Record[0]);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001133 SrcMgr::FileInfo &FileInfo =
1134 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
Douglas Gregora081da52011-11-16 20:05:18 +00001135 FileInfo.NumCreatedFIDs = Record[7];
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136 if (Record[3])
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001137 FileInfo.setHasLineDirectives();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001138
Douglas Gregora081da52011-11-16 20:05:18 +00001139 const DeclID *FirstDecl = F->FileSortedDecls + Record[8];
1140 unsigned NumFileDecls = Record[9];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001141 if (NumFileDecls) {
1142 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
Argyrios Kyrtzidis9d128d02011-10-31 07:20:08 +00001143 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1144 NumFileDecls));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001145 }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001146
Douglas Gregor35f9ae62011-11-17 01:44:33 +00001147 const SrcMgr::ContentCache *ContentCache
1148 = SourceMgr.getOrCreateContentCache(File);
1149 if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1150 ContentCache->ContentsEntry == ContentCache->OrigEntry) {
Douglas Gregora081da52011-11-16 20:05:18 +00001151 unsigned Code = SLocEntryCursor.ReadCode();
1152 Record.clear();
1153 unsigned RecCode
1154 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
1155
1156 if (RecCode != SM_SLOC_BUFFER_BLOB) {
1157 Error("AST record has invalid code");
1158 return Failure;
1159 }
1160
1161 llvm::MemoryBuffer *Buffer
1162 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1163 Filename);
1164 SourceMgr.overrideFileContents(File, Buffer);
1165 }
Argyrios Kyrtzidisa4c29b62012-02-20 23:58:07 +00001166
1167 if (Result == Failure)
1168 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001169 break;
1170 }
1171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001172 case SM_SLOC_BUFFER_ENTRY: {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001173 const char *Name = BlobStart;
1174 unsigned Offset = Record[0];
1175 unsigned Code = SLocEntryCursor.ReadCode();
1176 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001177 unsigned RecCode
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001178 = SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen);
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001179
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001180 if (RecCode != SM_SLOC_BUFFER_BLOB) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001181 Error("AST record has invalid code");
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00001182 return Failure;
1183 }
1184
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185 llvm::MemoryBuffer *Buffer
Douglas Gregora081da52011-11-16 20:05:18 +00001186 = llvm::MemoryBuffer::getMemBuffer(StringRef(BlobStart, BlobLen - 1),
1187 Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001188 FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID,
1189 BaseOffset + Offset);
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Douglas Gregor6236a292011-12-02 21:56:05 +00001191 if (strcmp(Name, "<built-in>") == 0 && F->Kind == MK_PCH) {
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001192 PCHPredefinesBlock Block = {
1193 BufferID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001194 StringRef(BlobStart, BlobLen - 1)
Sebastian Redl7e9ad8b2010-07-14 17:49:11 +00001195 };
1196 PCHPredefinesBuffers.push_back(Block);
Douglas Gregor92b059e2009-04-28 20:33:11 +00001197 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001198
1199 break;
1200 }
1201
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001202 case SM_SLOC_EXPANSION_ENTRY: {
Sebastian Redlc3632732010-10-05 15:59:54 +00001203 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
Chandler Carruthbf340e42011-07-26 03:03:05 +00001204 SourceMgr.createExpansionLoc(SpellingLoc,
Sebastian Redlc3632732010-10-05 15:59:54 +00001205 ReadSourceLocation(*F, Record[2]),
1206 ReadSourceLocation(*F, Record[3]),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 Record[4],
1208 ID,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001209 BaseOffset + Record[0]);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001211 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001212 }
1213
1214 return Success;
1215}
1216
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001217/// \brief Find the location where the module F is imported.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001218SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001219 if (F->ImportLoc.isValid())
1220 return F->ImportLoc;
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001221
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001222 // Otherwise we have a PCH. It's considered to be "imported" at the first
1223 // location of its includer.
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001224 if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001225 // Main file is the importer. We assume that it is the first entry in the
1226 // entry table. We can't ask the manager, because at the time of PCH loading
1227 // the main file entry doesn't exist yet.
1228 // The very first entry is the invalid instantiation loc, which takes up
1229 // offsets 0 and 1.
1230 return SourceLocation::getFromRawEncoding(2U);
1231 }
Jonathan D. Turner2e091632011-07-29 18:09:09 +00001232 //return F->Loaders[0]->FirstLoc;
1233 return F->ImportedBy[0]->FirstLoc;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001234}
1235
Chris Lattner6367f6d2009-04-27 01:05:14 +00001236/// ReadBlockAbbrevs - Enter a subblock of the specified BlockID with the
1237/// specified cursor. Read the abbreviations that are at the top of the block
1238/// and then leave the cursor pointing into the block.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001239bool ASTReader::ReadBlockAbbrevs(llvm::BitstreamCursor &Cursor,
Chris Lattner6367f6d2009-04-27 01:05:14 +00001240 unsigned BlockID) {
1241 if (Cursor.EnterSubBlock(BlockID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001242 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001243 return Failure;
1244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattner6367f6d2009-04-27 01:05:14 +00001246 while (true) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001247 uint64_t Offset = Cursor.GetCurrentBitNo();
Chris Lattner6367f6d2009-04-27 01:05:14 +00001248 unsigned Code = Cursor.ReadCode();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001249
Chris Lattner6367f6d2009-04-27 01:05:14 +00001250 // We expect all abbrevs to be at the start of the block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001251 if (Code != llvm::bitc::DEFINE_ABBREV) {
1252 Cursor.JumpToBit(Offset);
Chris Lattner6367f6d2009-04-27 01:05:14 +00001253 return false;
Douglas Gregorecdcb882010-10-20 22:00:55 +00001254 }
Chris Lattner6367f6d2009-04-27 01:05:14 +00001255 Cursor.ReadAbbrevRecord();
1256 }
1257}
1258
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001259void ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
Douglas Gregorecdcb882010-10-20 22:00:55 +00001260 llvm::BitstreamCursor &Stream = F.MacroCursor;
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Douglas Gregor37e26842009-04-21 23:56:24 +00001262 // Keep track of where we are in the stream, then jump back there
1263 // after reading this macro.
1264 SavedStreamPosition SavedPosition(Stream);
1265
1266 Stream.JumpToBit(Offset);
1267 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001268 SmallVector<IdentifierInfo*, 16> MacroArgs;
Douglas Gregor37e26842009-04-21 23:56:24 +00001269 MacroInfo *Macro = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Douglas Gregor37e26842009-04-21 23:56:24 +00001271 while (true) {
1272 unsigned Code = Stream.ReadCode();
1273 switch (Code) {
1274 case llvm::bitc::END_BLOCK:
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001275 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001276
1277 case llvm::bitc::ENTER_SUBBLOCK:
1278 // No known subblocks, always skip them.
1279 Stream.ReadSubBlockID();
1280 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001281 Error("malformed block record in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001282 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001283 }
1284 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Douglas Gregor37e26842009-04-21 23:56:24 +00001286 case llvm::bitc::DEFINE_ABBREV:
1287 Stream.ReadAbbrevRecord();
1288 continue;
1289 default: break;
1290 }
1291
1292 // Read a record.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001293 const char *BlobStart = 0;
1294 unsigned BlobLen = 0;
Douglas Gregor37e26842009-04-21 23:56:24 +00001295 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001296 PreprocessorRecordTypes RecType =
Michael J. Spencer20249a12010-10-21 03:16:25 +00001297 (PreprocessorRecordTypes)Stream.ReadRecord(Code, Record, BlobStart,
Douglas Gregorecdcb882010-10-20 22:00:55 +00001298 BlobLen);
Douglas Gregor37e26842009-04-21 23:56:24 +00001299 switch (RecType) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001300 case PP_MACRO_OBJECT_LIKE:
1301 case PP_MACRO_FUNCTION_LIKE: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001302 // If we already have a macro, that means that we've hit the end
1303 // of the definition of the macro we were looking for. We're
1304 // done.
1305 if (Macro)
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001306 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001307
Douglas Gregor95eab172011-07-28 20:55:49 +00001308 IdentifierInfo *II = getLocalIdentifier(F, Record[0]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001309 if (II == 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001310 Error("macro must have a name in AST file");
Douglas Gregor3b2257c2011-08-04 18:09:14 +00001311 return;
Douglas Gregor37e26842009-04-21 23:56:24 +00001312 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001313
Sebastian Redlc3632732010-10-05 15:59:54 +00001314 SourceLocation Loc = ReadSourceLocation(F, Record[1]);
Douglas Gregor37e26842009-04-21 23:56:24 +00001315 bool isUsed = Record[2];
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001317 MacroInfo *MI = PP.AllocateMacroInfo(Loc);
Douglas Gregor37e26842009-04-21 23:56:24 +00001318 MI->setIsUsed(isUsed);
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001319 MI->setIsFromAST();
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregoraa93a872011-10-17 15:32:29 +00001321 bool IsPublic = Record[3];
1322 unsigned NextIndex = 4;
1323 MI->setVisibility(IsPublic, ReadSourceLocation(F, Record, NextIndex));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001324
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001325 if (RecType == PP_MACRO_FUNCTION_LIKE) {
Douglas Gregor37e26842009-04-21 23:56:24 +00001326 // Decode function-like macro info.
Douglas Gregor7143aab2011-09-01 17:04:32 +00001327 bool isC99VarArgs = Record[NextIndex++];
1328 bool isGNUVarArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 MacroArgs.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +00001330 unsigned NumArgs = Record[NextIndex++];
Douglas Gregor37e26842009-04-21 23:56:24 +00001331 for (unsigned i = 0; i != NumArgs; ++i)
Douglas Gregor7143aab2011-09-01 17:04:32 +00001332 MacroArgs.push_back(getLocalIdentifier(F, Record[NextIndex++]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001333
1334 // Install function-like macro info.
1335 MI->setIsFunctionLike();
1336 if (isC99VarArgs) MI->setIsC99Varargs();
1337 if (isGNUVarArgs) MI->setIsGNUVarargs();
Douglas Gregor75fdb232009-05-22 22:45:36 +00001338 MI->setArgumentList(MacroArgs.data(), MacroArgs.size(),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001339 PP.getPreprocessorAllocator());
Douglas Gregor37e26842009-04-21 23:56:24 +00001340 }
1341
1342 // Finally, install the macro.
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001343 PP.setMacroInfo(II, MI, /*LoadedFromAST=*/true);
Douglas Gregor37e26842009-04-21 23:56:24 +00001344
1345 // Remember that we saw this macro last so that we add the tokens that
1346 // form its body to it.
1347 Macro = MI;
Michael J. Spencer20249a12010-10-21 03:16:25 +00001348
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001349 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1350 Record[NextIndex]) {
1351 // We have a macro definition. Register the association
1352 PreprocessedEntityID
1353 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1354 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1355 PPRec.RegisterMacroDefinition(Macro,
1356 PPRec.getPPEntityID(GlobalID-1, /*isLoaded=*/true));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001357 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001358
Douglas Gregor37e26842009-04-21 23:56:24 +00001359 ++NumMacrosRead;
1360 break;
1361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001363 case PP_TOKEN: {
Douglas Gregor37e26842009-04-21 23:56:24 +00001364 // If we see a TOKEN before a PP_MACRO_*, then the file is
1365 // erroneous, just pretend we didn't see this.
1366 if (Macro == 0) break;
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Douglas Gregor37e26842009-04-21 23:56:24 +00001368 Token Tok;
1369 Tok.startToken();
Sebastian Redlc3632732010-10-05 15:59:54 +00001370 Tok.setLocation(ReadSourceLocation(F, Record[0]));
Douglas Gregor37e26842009-04-21 23:56:24 +00001371 Tok.setLength(Record[1]);
Douglas Gregor95eab172011-07-28 20:55:49 +00001372 if (IdentifierInfo *II = getLocalIdentifier(F, Record[2]))
Douglas Gregor37e26842009-04-21 23:56:24 +00001373 Tok.setIdentifierInfo(II);
1374 Tok.setKind((tok::TokenKind)Record[3]);
1375 Tok.setFlag((Token::TokenFlags)Record[4]);
1376 Macro->AddTokenToBody(Tok);
1377 break;
1378 }
David Blaikie7530c032012-01-17 06:56:22 +00001379 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001380 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001381}
1382
Douglas Gregor86c67d82011-07-28 22:39:26 +00001383PreprocessedEntityID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001384ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const {
Argyrios Kyrtzidis1f6d2252011-09-19 20:40:02 +00001385 ContinuousRangeMap<uint32_t, int, 2>::const_iterator
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001386 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1387 assert(I != M.PreprocessedEntityRemap.end()
1388 && "Invalid index into preprocessed entity index remap");
1389
1390 return LocalID + I->second;
Douglas Gregor86c67d82011-07-28 22:39:26 +00001391}
1392
Douglas Gregor98339b92011-08-25 20:47:51 +00001393unsigned HeaderFileInfoTrait::ComputeHash(const char *path) {
1394 return llvm::HashString(llvm::sys::path::filename(path));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001395}
Douglas Gregor98339b92011-08-25 20:47:51 +00001396
1397HeaderFileInfoTrait::internal_key_type
1398HeaderFileInfoTrait::GetInternalKey(const char *path) { return path; }
1399
1400bool HeaderFileInfoTrait::EqualKey(internal_key_type a, internal_key_type b) {
1401 if (strcmp(a, b) == 0)
1402 return true;
1403
1404 if (llvm::sys::path::filename(a) != llvm::sys::path::filename(b))
1405 return false;
Douglas Gregor99a922b2011-12-09 16:22:07 +00001406
1407 // Determine whether the actual files are equivalent.
1408 bool Result = false;
1409 if (llvm::sys::fs::equivalent(a, b, Result))
Douglas Gregor98339b92011-08-25 20:47:51 +00001410 return false;
1411
Douglas Gregor99a922b2011-12-09 16:22:07 +00001412 return Result;
Douglas Gregor98339b92011-08-25 20:47:51 +00001413}
1414
1415std::pair<unsigned, unsigned>
1416HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1417 unsigned KeyLen = (unsigned) clang::io::ReadUnalignedLE16(d);
1418 unsigned DataLen = (unsigned) *d++;
1419 return std::make_pair(KeyLen + 1, DataLen);
1420}
1421
1422HeaderFileInfoTrait::data_type
1423HeaderFileInfoTrait::ReadData(const internal_key_type, const unsigned char *d,
1424 unsigned DataLen) {
1425 const unsigned char *End = d + DataLen;
1426 using namespace clang::io;
1427 HeaderFileInfo HFI;
1428 unsigned Flags = *d++;
1429 HFI.isImport = (Flags >> 5) & 0x01;
1430 HFI.isPragmaOnce = (Flags >> 4) & 0x01;
1431 HFI.DirInfo = (Flags >> 2) & 0x03;
1432 HFI.Resolved = (Flags >> 1) & 0x01;
1433 HFI.IndexHeaderMapHeader = Flags & 0x01;
1434 HFI.NumIncludes = ReadUnalignedLE16(d);
Douglas Gregor541ba162011-10-17 18:53:12 +00001435 HFI.ControllingMacroID = Reader.getGlobalIdentifierID(M,
1436 ReadUnalignedLE32(d));
Douglas Gregor98339b92011-08-25 20:47:51 +00001437 if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) {
1438 // The framework offset is 1 greater than the actual offset,
1439 // since 0 is used as an indicator for "no framework name".
1440 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1441 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1442 }
1443
1444 assert(End == d && "Wrong data length in HeaderFileInfo deserialization");
1445 (void)End;
1446
1447 // This HeaderFileInfo was externally loaded.
1448 HFI.External = true;
1449 return HFI;
1450}
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001451
Douglas Gregor13292642011-12-02 15:45:10 +00001452void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
1453 uint64_t LocalOffset, bool Visible) {
1454 if (Visible) {
1455 // Note that this identifier has a macro definition.
1456 II->setHasMacroDefinition(true);
1457 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001458
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001459 // Adjust the offset to a global offset.
Douglas Gregor2d2689a2011-07-28 21:16:51 +00001460 UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
Douglas Gregor295a2a62010-10-30 00:23:06 +00001461}
1462
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001463void ASTReader::ReadDefinedMacros() {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001464 for (ModuleReverseIterator I = ModuleMgr.rbegin(),
1465 E = ModuleMgr.rend(); I != E; ++I) {
1466 llvm::BitstreamCursor &MacroCursor = (*I)->MacroCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00001467
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001468 // If there was no preprocessor block, skip this file.
1469 if (!MacroCursor.getBitStreamReader())
1470 continue;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001471
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001472 llvm::BitstreamCursor Cursor = MacroCursor;
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00001473 Cursor.JumpToBit((*I)->MacroStartOffset);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001474
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001475 RecordData Record;
1476 while (true) {
1477 unsigned Code = Cursor.ReadCode();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001478 if (Code == llvm::bitc::END_BLOCK)
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001479 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001480
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001481 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1482 // No known subblocks, always skip them.
1483 Cursor.ReadSubBlockID();
1484 if (Cursor.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001485 Error("malformed block record in AST file");
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001486 return;
1487 }
1488 continue;
1489 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001490
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001491 if (Code == llvm::bitc::DEFINE_ABBREV) {
1492 Cursor.ReadAbbrevRecord();
1493 continue;
1494 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001495
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001496 // Read a record.
1497 const char *BlobStart;
1498 unsigned BlobLen;
1499 Record.clear();
1500 switch (Cursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
1501 default: // Default behavior: ignore.
1502 break;
Douglas Gregor88a35862010-01-04 19:18:44 +00001503
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001504 case PP_MACRO_OBJECT_LIKE:
1505 case PP_MACRO_FUNCTION_LIKE:
Douglas Gregor95eab172011-07-28 20:55:49 +00001506 getLocalIdentifier(**I, Record[0]);
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001507 break;
1508
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001509 case PP_TOKEN:
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001510 // Ignore tokens.
1511 break;
Sebastian Redld27d3fc2010-07-21 22:31:37 +00001512 }
Douglas Gregor88a35862010-01-04 19:18:44 +00001513 }
1514 }
Douglas Gregor295a2a62010-10-30 00:23:06 +00001515
1516 // Drain the unread macro-record offsets map.
1517 while (!UnreadMacroRecordOffsets.empty())
1518 LoadMacroDefinition(UnreadMacroRecordOffsets.begin());
1519}
1520
1521void ASTReader::LoadMacroDefinition(
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001522 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos) {
Douglas Gregor295a2a62010-10-30 00:23:06 +00001523 assert(Pos != UnreadMacroRecordOffsets.end() && "Unknown macro definition");
Douglas Gregor295a2a62010-10-30 00:23:06 +00001524 uint64_t Offset = Pos->second;
1525 UnreadMacroRecordOffsets.erase(Pos);
1526
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001527 RecordLocation Loc = getLocalBitOffset(Offset);
1528 ReadMacroRecord(*Loc.F, Loc.Offset);
Douglas Gregor295a2a62010-10-30 00:23:06 +00001529}
1530
1531void ASTReader::LoadMacroDefinition(IdentifierInfo *II) {
1532 llvm::DenseMap<IdentifierInfo *, uint64_t>::iterator Pos
1533 = UnreadMacroRecordOffsets.find(II);
1534 LoadMacroDefinition(Pos);
Douglas Gregor88a35862010-01-04 19:18:44 +00001535}
1536
Douglas Gregoreee242f2011-10-27 09:33:13 +00001537namespace {
1538 /// \brief Visitor class used to look up identifirs in an AST file.
1539 class IdentifierLookupVisitor {
1540 StringRef Name;
Douglas Gregor057df202012-01-18 20:56:22 +00001541 unsigned PriorGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001542 IdentifierInfo *Found;
1543 public:
Douglas Gregor057df202012-01-18 20:56:22 +00001544 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration)
1545 : Name(Name), PriorGeneration(PriorGeneration), Found() { }
Douglas Gregoreee242f2011-10-27 09:33:13 +00001546
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001547 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00001548 IdentifierLookupVisitor *This
1549 = static_cast<IdentifierLookupVisitor *>(UserData);
1550
Douglas Gregor057df202012-01-18 20:56:22 +00001551 // If we've already searched this module file, skip it now.
1552 if (M.Generation <= This->PriorGeneration)
1553 return true;
1554
Douglas Gregoreee242f2011-10-27 09:33:13 +00001555 ASTIdentifierLookupTable *IdTable
1556 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
1557 if (!IdTable)
1558 return false;
1559
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001560 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(),
1561 M, This->Found);
1562
Douglas Gregoreee242f2011-10-27 09:33:13 +00001563 std::pair<const char*, unsigned> Key(This->Name.begin(),
1564 This->Name.size());
Douglas Gregor5d5051f2012-01-24 15:24:38 +00001565 ASTIdentifierLookupTable::iterator Pos = IdTable->find(Key, &Trait);
Douglas Gregoreee242f2011-10-27 09:33:13 +00001566 if (Pos == IdTable->end())
1567 return false;
1568
1569 // Dereferencing the iterator has the effect of building the
1570 // IdentifierInfo node and populating it with the various
1571 // declarations it needs.
1572 This->Found = *Pos;
1573 return true;
1574 }
1575
1576 // \brief Retrieve the identifier info found within the module
1577 // files.
1578 IdentifierInfo *getIdentifierInfo() const { return Found; }
1579 };
1580}
1581
1582void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
Douglas Gregor057df202012-01-18 20:56:22 +00001583 unsigned PriorGeneration = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001584 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001585 PriorGeneration = IdentifierGeneration[&II];
1586
1587 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration);
1588 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
1589 markIdentifierUpToDate(&II);
1590}
1591
1592void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
1593 if (!II)
1594 return;
1595
1596 II->setOutOfDate(false);
1597
1598 // Update the generation for this identifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001599 if (getContext().getLangOpts().Modules)
Douglas Gregor057df202012-01-18 20:56:22 +00001600 IdentifierGeneration[II] = CurrentGeneration;
Douglas Gregoreee242f2011-10-27 09:33:13 +00001601}
1602
Chris Lattner5f9e2722011-07-23 10:55:15 +00001603const FileEntry *ASTReader::getFileEntry(StringRef filenameStrRef) {
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00001604 std::string Filename = filenameStrRef;
1605 MaybeAddSystemRootToFilename(Filename);
1606 const FileEntry *File = FileMgr.getFile(Filename);
1607 if (File == 0 && !OriginalDir.empty() && !CurrentDir.empty() &&
1608 OriginalDir != CurrentDir) {
1609 std::string resolved = resolveFileRelativeToOriginalDir(Filename,
1610 OriginalDir,
1611 CurrentDir);
1612 if (!resolved.empty())
1613 File = FileMgr.getFile(resolved);
1614 }
1615
1616 return File;
1617}
1618
Douglas Gregore650c8c2009-07-07 00:12:59 +00001619/// \brief If we are loading a relocatable PCH file, and the filename is
1620/// not an absolute path, add the system root to the beginning of the file
1621/// name.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001622void ASTReader::MaybeAddSystemRootToFilename(std::string &Filename) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001623 // If this is not a relocatable PCH file, there's nothing to do.
1624 if (!RelocatablePCH)
1625 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Michael J. Spencer256053b2010-12-17 21:22:22 +00001627 if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
Douglas Gregore650c8c2009-07-07 00:12:59 +00001628 return;
1629
Douglas Gregor832d6202011-07-22 16:35:34 +00001630 if (isysroot.empty()) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001631 // If no system root was given, default to '/'
1632 Filename.insert(Filename.begin(), '/');
1633 return;
1634 }
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Douglas Gregor832d6202011-07-22 16:35:34 +00001636 unsigned Length = isysroot.size();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001637 if (isysroot[Length - 1] != '/')
1638 Filename.insert(Filename.begin(), '/');
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Douglas Gregor832d6202011-07-22 16:35:34 +00001640 Filename.insert(Filename.begin(), isysroot.begin(), isysroot.end());
Douglas Gregore650c8c2009-07-07 00:12:59 +00001641}
1642
Sebastian Redlc43b54c2010-08-18 23:56:43 +00001643ASTReader::ASTReadResult
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001644ASTReader::ReadASTBlock(ModuleFile &F) {
Sebastian Redl9137a522010-07-16 17:50:48 +00001645 llvm::BitstreamCursor &Stream = F.Stream;
1646
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001647 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001648 Error("malformed block record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001649 return Failure;
1650 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001651
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001652 // Read all of the records and blocks for the ASt file.
Douglas Gregor8038d512009-04-10 17:25:41 +00001653 RecordData Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001654 while (!Stream.AtEndOfStream()) {
1655 unsigned Code = Stream.ReadCode();
1656 if (Code == llvm::bitc::END_BLOCK) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001657 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001658 Error("error at end of module block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001659 return Failure;
1660 }
Chris Lattner7356a312009-04-11 21:15:38 +00001661
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001662 return Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001663 }
1664
1665 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1666 switch (Stream.ReadSubBlockID()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001667 case DECLTYPES_BLOCK_ID:
Chris Lattner6367f6d2009-04-27 01:05:14 +00001668 // We lazily load the decls block, but we want to set up the
1669 // DeclsCursor cursor to point into it. Clone our current bitcode
1670 // cursor to it, enter the block and read the abbrevs in that block.
1671 // With the main cursor, we just skip over it.
Sebastian Redl9137a522010-07-16 17:50:48 +00001672 F.DeclsCursor = Stream;
Chris Lattner6367f6d2009-04-27 01:05:14 +00001673 if (Stream.SkipBlock() || // Skip with the main cursor.
1674 // Read the abbrevs.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001675 ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001676 Error("malformed block record in AST file");
Chris Lattner6367f6d2009-04-27 01:05:14 +00001677 return Failure;
1678 }
1679 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00001681 case DECL_UPDATES_BLOCK_ID:
1682 if (Stream.SkipBlock()) {
1683 Error("malformed block record in AST file");
1684 return Failure;
1685 }
1686 break;
1687
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001688 case PREPROCESSOR_BLOCK_ID:
Sebastian Redl9137a522010-07-16 17:50:48 +00001689 F.MacroCursor = Stream;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001690 if (!PP.getExternalSource())
1691 PP.setExternalSource(this);
Douglas Gregor88a35862010-01-04 19:18:44 +00001692
Douglas Gregorecdcb882010-10-20 22:00:55 +00001693 if (Stream.SkipBlock() ||
1694 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001695 Error("malformed block record in AST file");
Chris Lattner7356a312009-04-11 21:15:38 +00001696 return Failure;
1697 }
Douglas Gregorecdcb882010-10-20 22:00:55 +00001698 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
Chris Lattner7356a312009-04-11 21:15:38 +00001699 break;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00001700
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001701 case PREPROCESSOR_DETAIL_BLOCK_ID:
1702 F.PreprocessorDetailCursor = Stream;
1703 if (Stream.SkipBlock() ||
1704 ReadBlockAbbrevs(F.PreprocessorDetailCursor,
1705 PREPROCESSOR_DETAIL_BLOCK_ID)) {
1706 Error("malformed preprocessor detail record in AST file");
1707 return Failure;
1708 }
1709 F.PreprocessorDetailStartOffset
1710 = F.PreprocessorDetailCursor.GetCurrentBitNo();
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001711
1712 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00001713 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001714 if (!PP.getPreprocessingRecord()->getExternalSource())
1715 PP.getPreprocessingRecord()->SetExternalSource(*this);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001716 break;
1717
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001718 case SOURCE_MANAGER_BLOCK_ID:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001719 switch (ReadSourceManagerBlock(F)) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00001720 case Success:
1721 break;
1722
1723 case Failure:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001724 Error("malformed source manager block in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001725 return Failure;
Douglas Gregore1d918e2009-04-10 23:10:45 +00001726
1727 case IgnorePCH:
1728 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001729 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001730 break;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001731
1732 case SUBMODULE_BLOCK_ID:
1733 switch (ReadSubmoduleBlock(F)) {
1734 case Success:
1735 break;
1736
1737 case Failure:
1738 Error("malformed submodule block in AST file");
1739 return Failure;
1740
1741 case IgnorePCH:
1742 return IgnorePCH;
1743 }
1744 break;
1745
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00001746 case COMMENTS_BLOCK_ID: {
1747 llvm::BitstreamCursor C = Stream;
1748 if (Stream.SkipBlock() ||
1749 ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
1750 Error("malformed comments block in AST file");
1751 return Failure;
1752 }
1753 CommentsCursors.push_back(std::make_pair(C, &F));
1754 break;
1755 }
1756
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001757 default:
1758 if (!Stream.SkipBlock())
1759 break;
1760 Error("malformed block record in AST file");
1761 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001762 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001763 continue;
1764 }
1765
1766 if (Code == llvm::bitc::DEFINE_ABBREV) {
1767 Stream.ReadAbbrevRecord();
1768 continue;
1769 }
1770
1771 // Read and process a record.
1772 Record.clear();
Douglas Gregor2bec0412009-04-10 21:16:55 +00001773 const char *BlobStart = 0;
1774 unsigned BlobLen = 0;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001775 switch ((ASTRecordTypes)Stream.ReadRecord(Code, Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00001776 &BlobStart, &BlobLen)) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001777 default: // Default behavior: ignore.
1778 break;
1779
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001780 case METADATA: {
1781 if (Record[0] != VERSION_MAJOR && !DisableValidation) {
1782 Diag(Record[0] < VERSION_MAJOR? diag::warn_pch_version_too_old
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001783 : diag::warn_pch_version_too_new);
1784 return IgnorePCH;
1785 }
1786
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001787 bool hasErrors = Record[5];
1788 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
1789 Diag(diag::err_pch_with_compiler_errors);
1790 return IgnorePCH;
1791 }
1792
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001793 RelocatablePCH = Record[4];
1794 if (Listener) {
1795 std::string TargetTriple(BlobStart, BlobLen);
1796 if (Listener->ReadTargetTriple(TargetTriple))
1797 return IgnorePCH;
1798 }
1799 break;
1800 }
1801
Douglas Gregore95b9192011-08-17 21:07:30 +00001802 case IMPORTS: {
1803 // Load each of the imported PCH files.
1804 unsigned Idx = 0, N = Record.size();
1805 while (Idx < N) {
1806 // Read information about the AST file.
1807 ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
1808 unsigned Length = Record[Idx++];
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001809 SmallString<128> ImportedFile(Record.begin() + Idx,
Douglas Gregore95b9192011-08-17 21:07:30 +00001810 Record.begin() + Idx + Length);
1811 Idx += Length;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001812
Douglas Gregore95b9192011-08-17 21:07:30 +00001813 // Load the AST file.
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001814 switch(ReadASTCore(ImportedFile, ImportedKind, &F)) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001815 case Failure: return Failure;
1816 // If we have to ignore the dependency, we'll have to ignore this too.
1817 case IgnorePCH: return IgnorePCH;
1818 case Success: break;
1819 }
1820 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00001821 break;
1822 }
1823
Douglas Gregora119da02011-08-02 16:26:37 +00001824 case TYPE_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001825 if (F.LocalNumTypes != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001826 Error("duplicate TYPE_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001827 return Failure;
1828 }
Sebastian Redl12d6da02010-07-19 22:06:55 +00001829 F.TypeOffsets = (const uint32_t *)BlobStart;
1830 F.LocalNumTypes = Record[0];
Douglas Gregore3605012011-08-02 18:32:54 +00001831 unsigned LocalBaseTypeIndex = Record[1];
1832 F.BaseTypeIndex = getTotalNumTypes();
Douglas Gregor1e849b62011-07-29 00:21:44 +00001833
Douglas Gregora119da02011-08-02 16:26:37 +00001834 if (F.LocalNumTypes > 0) {
1835 // Introduce the global -> local mapping for types within this module.
1836 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
1837
1838 // Introduce the local -> global mapping for types within this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001839 F.TypeRemap.insertOrReplace(
1840 std::make_pair(LocalBaseTypeIndex,
1841 F.BaseTypeIndex - LocalBaseTypeIndex));
Douglas Gregora119da02011-08-02 16:26:37 +00001842
1843 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
1844 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001845 break;
Douglas Gregora119da02011-08-02 16:26:37 +00001846 }
1847
Douglas Gregor496c7092011-08-03 15:48:04 +00001848 case DECL_OFFSET: {
Sebastian Redl12d6da02010-07-19 22:06:55 +00001849 if (F.LocalNumDecls != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001850 Error("duplicate DECL_OFFSET record in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001851 return Failure;
1852 }
Argyrios Kyrtzidis9d31fa72011-10-27 18:47:35 +00001853 F.DeclOffsets = (const DeclOffset *)BlobStart;
Sebastian Redl12d6da02010-07-19 22:06:55 +00001854 F.LocalNumDecls = Record[0];
Douglas Gregor496c7092011-08-03 15:48:04 +00001855 unsigned LocalBaseDeclID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001856 F.BaseDeclID = getTotalNumDecls();
Douglas Gregor96e973f2011-07-20 00:27:43 +00001857
Douglas Gregor496c7092011-08-03 15:48:04 +00001858 if (F.LocalNumDecls > 0) {
1859 // Introduce the global -> local mapping for declarations within this
1860 // module.
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001861 GlobalDeclMap.insert(
1862 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
Douglas Gregor496c7092011-08-03 15:48:04 +00001863
1864 // Introduce the local -> global mapping for declarations within this
1865 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001866 F.DeclRemap.insertOrReplace(
1867 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
Douglas Gregor496c7092011-08-03 15:48:04 +00001868
Douglas Gregora1be2782011-12-17 23:38:30 +00001869 // Introduce the global -> local mapping for declarations within this
1870 // module.
1871 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
1872
Douglas Gregor496c7092011-08-03 15:48:04 +00001873 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
1874 }
Douglas Gregor8038d512009-04-10 17:25:41 +00001875 break;
Douglas Gregor496c7092011-08-03 15:48:04 +00001876 }
1877
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001878 case TU_UPDATE_LEXICAL: {
Douglas Gregor35942772011-09-09 21:34:22 +00001879 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001880 DeclContextInfo &Info = F.DeclContextInfos[TU];
1881 Info.LexicalDecls = reinterpret_cast<const KindDeclIDPair *>(BlobStart);
1882 Info.NumLexicalDecls
1883 = static_cast<unsigned int>(BlobLen / sizeof(KindDeclIDPair));
Douglas Gregor35942772011-09-09 21:34:22 +00001884 TU->setHasExternalLexicalStorage(true);
Sebastian Redld692af72010-07-27 18:24:41 +00001885 break;
1886 }
1887
Sebastian Redle1dde812010-08-24 00:50:04 +00001888 case UPDATE_VISIBLE: {
Douglas Gregor496c7092011-08-03 15:48:04 +00001889 unsigned Idx = 0;
1890 serialization::DeclID ID = ReadDeclID(F, Record, Idx);
Benjamin Kramerb1758c62012-04-15 12:36:49 +00001891 ASTDeclContextNameLookupTable *Table =
1892 ASTDeclContextNameLookupTable::Create(
Douglas Gregor496c7092011-08-03 15:48:04 +00001893 (const unsigned char *)BlobStart + Record[Idx++],
Sebastian Redle1dde812010-08-24 00:50:04 +00001894 (const unsigned char *)BlobStart,
Douglas Gregor393f2492011-07-22 00:38:23 +00001895 ASTDeclContextNameLookupTrait(*this, F));
Douglas Gregor35942772011-09-09 21:34:22 +00001896 if (ID == PREDEF_DECL_TRANSLATION_UNIT_ID) { // Is it the TU?
1897 DeclContext *TU = Context.getTranslationUnitDecl();
Douglas Gregor0d95f772011-08-24 19:03:07 +00001898 F.DeclContextInfos[TU].NameLookupTableData = Table;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00001899 TU->setHasExternalVisibleStorage(true);
Sebastian Redle1dde812010-08-24 00:50:04 +00001900 } else
Douglas Gregor496c7092011-08-03 15:48:04 +00001901 PendingVisibleUpdates[ID].push_back(std::make_pair(Table, &F));
Sebastian Redle1dde812010-08-24 00:50:04 +00001902 break;
1903 }
1904
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001905 case LANGUAGE_OPTIONS:
Douglas Gregorfae3b2f2010-07-27 00:27:13 +00001906 if (ParseLanguageOptions(Record) && !DisableValidation)
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001907 return IgnorePCH;
1908 break;
Douglas Gregor2bec0412009-04-10 21:16:55 +00001909
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001910 case IDENTIFIER_TABLE:
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001911 F.IdentifierTableData = BlobStart;
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001912 if (Record[0]) {
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001913 F.IdentifierLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001914 = ASTIdentifierLookupTable::Create(
Sebastian Redl93fb9ed2010-07-19 20:52:06 +00001915 (const unsigned char *)F.IdentifierTableData + Record[0],
1916 (const unsigned char *)F.IdentifierTableData,
Sebastian Redlc3632732010-10-05 15:59:54 +00001917 ASTIdentifierLookupTrait(*this, F));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00001918
1919 PP.getIdentifierTable().setExternalIdentifierLookup(this);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001920 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001921 break;
1922
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001923 case IDENTIFIER_OFFSET: {
Sebastian Redl2da08f92010-07-19 22:28:42 +00001924 if (F.LocalNumIdentifiers != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001925 Error("duplicate IDENTIFIER_OFFSET record in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00001926 return Failure;
1927 }
Sebastian Redl2da08f92010-07-19 22:28:42 +00001928 F.IdentifierOffsets = (const uint32_t *)BlobStart;
1929 F.LocalNumIdentifiers = Record[0];
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001930 unsigned LocalBaseIdentifierID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00001931 F.BaseIdentifierID = getTotalNumIdentifiers();
Douglas Gregor67268d02011-07-20 00:59:32 +00001932
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001933 if (F.LocalNumIdentifiers > 0) {
1934 // Introduce the global -> local mapping for identifiers within this
1935 // module.
1936 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
1937 &F));
1938
1939 // Introduce the local -> global mapping for identifiers within this
1940 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00001941 F.IdentifierRemap.insertOrReplace(
1942 std::make_pair(LocalBaseIdentifierID,
1943 F.BaseIdentifierID - LocalBaseIdentifierID));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001944
1945 IdentifiersLoaded.resize(IdentifiersLoaded.size()
1946 + F.LocalNumIdentifiers);
1947 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001948 break;
Douglas Gregor6ec60e02011-08-03 21:49:18 +00001949 }
1950
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001951 case EXTERNAL_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001952 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1953 ExternalDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorfdd01722009-04-14 00:24:19 +00001954 break;
Douglas Gregor3e1af842009-04-17 22:13:46 +00001955
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001956 case SPECIAL_TYPES:
Douglas Gregor393f2492011-07-22 00:38:23 +00001957 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1958 SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
Douglas Gregorad1de002009-04-18 05:55:16 +00001959 break;
1960
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 case STATISTICS:
Sebastian Redl518d8cb2010-07-20 21:20:32 +00001962 TotalNumStatements += Record[0];
1963 TotalNumMacros += Record[1];
1964 TotalLexicalDeclContexts += Record[2];
1965 TotalVisibleDeclContexts += Record[3];
Douglas Gregor3e1af842009-04-17 22:13:46 +00001966 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001967
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001968 case UNUSED_FILESCOPED_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001969 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1970 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
Tanya Lattnere6bbc012010-02-12 00:07:30 +00001971 break;
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00001972
Sean Huntebcbe1d2011-05-04 23:29:54 +00001973 case DELEGATING_CTORS:
Douglas Gregor409448c2011-07-21 22:35:25 +00001974 for (unsigned I = 0, N = Record.size(); I != N; ++I)
1975 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00001976 break;
1977
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001978 case WEAK_UNDECLARED_IDENTIFIERS:
Douglas Gregor31e37b22011-07-28 18:09:57 +00001979 if (Record.size() % 4 != 0) {
1980 Error("invalid weak identifiers record");
1981 return Failure;
1982 }
1983
1984 // FIXME: Ignore weak undeclared identifiers from non-original PCH
1985 // files. This isn't the way to do it :)
1986 WeakUndeclaredIdentifiers.clear();
1987
1988 // Translate the weak, undeclared identifiers into global IDs.
1989 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
1990 WeakUndeclaredIdentifiers.push_back(
1991 getGlobalIdentifierID(F, Record[I++]));
1992 WeakUndeclaredIdentifiers.push_back(
1993 getGlobalIdentifierID(F, Record[I++]));
1994 WeakUndeclaredIdentifiers.push_back(
1995 ReadSourceLocation(F, Record, I).getRawEncoding());
1996 WeakUndeclaredIdentifiers.push_back(Record[I++]);
1997 }
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00001998 break;
1999
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002000 case LOCALLY_SCOPED_EXTERNAL_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002001 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2002 LocallyScopedExternalDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregor14c22f22009-04-22 22:18:58 +00002003 break;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002004
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002005 case SELECTOR_OFFSETS: {
Sebastian Redl059612d2010-08-03 21:58:15 +00002006 F.SelectorOffsets = (const uint32_t *)BlobStart;
Sebastian Redl725cd962010-08-04 20:40:17 +00002007 F.LocalNumSelectors = Record[0];
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002008 unsigned LocalBaseSelectorID = Record[1];
Douglas Gregor9827a802011-07-29 00:56:45 +00002009 F.BaseSelectorID = getTotalNumSelectors();
Douglas Gregor96958cb2011-07-20 01:10:58 +00002010
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002011 if (F.LocalNumSelectors > 0) {
2012 // Introduce the global -> local mapping for selectors within this
2013 // module.
2014 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
2015
2016 // Introduce the local -> global mapping for selectors within this
2017 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002018 F.SelectorRemap.insertOrReplace(
2019 std::make_pair(LocalBaseSelectorID,
2020 F.BaseSelectorID - LocalBaseSelectorID));
Douglas Gregor83941df2009-04-25 17:48:32 +00002021
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002022 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
2023 }
2024 break;
2025 }
2026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002027 case METHOD_POOL:
Sebastian Redl725cd962010-08-04 20:40:17 +00002028 F.SelectorLookupTableData = (const unsigned char *)BlobStart;
Douglas Gregor83941df2009-04-25 17:48:32 +00002029 if (Record[0])
Sebastian Redl725cd962010-08-04 20:40:17 +00002030 F.SelectorLookupTable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002031 = ASTSelectorLookupTable::Create(
Sebastian Redl725cd962010-08-04 20:40:17 +00002032 F.SelectorLookupTableData + Record[0],
2033 F.SelectorLookupTableData,
Douglas Gregor409448c2011-07-21 22:35:25 +00002034 ASTSelectorLookupTrait(*this, F));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002035 TotalNumMethodPoolEntries += Record[1];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002036 break;
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002037
Sebastian Redl4ee5a6f2010-09-22 00:42:30 +00002038 case REFERENCED_SELECTOR_POOL:
Douglas Gregor8451ec72011-07-28 14:41:43 +00002039 if (!Record.empty()) {
2040 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
2041 ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
2042 Record[Idx++]));
2043 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
2044 getRawEncoding());
2045 }
2046 }
Fariborz Jahanian32019832010-07-23 19:11:11 +00002047 break;
2048
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002049 case PP_COUNTER_VALUE:
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002050 if (!Record.empty() && Listener)
2051 Listener->ReadCounter(Record[0]);
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002052 break;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002053
2054 case FILE_SORTED_DECLS:
2055 F.FileSortedDecls = (const DeclID *)BlobStart;
2056 break;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002057
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002058 case SOURCE_LOCATION_OFFSETS: {
2059 F.SLocEntryOffsets = (const uint32_t *)BlobStart;
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002060 F.LocalNumSLocEntries = Record[0];
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002061 unsigned SLocSpaceSize = Record[1];
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002062 llvm::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002063 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
2064 SLocSpaceSize);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002065 // Make our entry in the range map. BaseID is negative and growing, so
2066 // we invert it. Because we invert it, though, we need the other end of
2067 // the range.
2068 unsigned RangeStart =
2069 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
2070 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
2071 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
2072
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002073 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
2074 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
2075 GlobalSLocOffsetMap.insert(
2076 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
2077 - SLocSpaceSize,&F));
2078
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002079 // Initialize the remapping table.
2080 // Invalid stays invalid.
2081 F.SLocRemap.insert(std::make_pair(0U, 0));
2082 // This module. Base was 2 when being compiled.
2083 F.SLocRemap.insert(std::make_pair(2U,
2084 static_cast<int>(F.SLocEntryBaseOffset - 2)));
Douglas Gregor0cdd7982011-07-21 18:46:38 +00002085
2086 TotalNumSLocEntries += F.LocalNumSLocEntries;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002087 break;
2088 }
2089
Douglas Gregor5d51a1d2011-08-01 16:01:55 +00002090 case MODULE_OFFSET_MAP: {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002091 // Additional remapping information.
2092 const unsigned char *Data = (const unsigned char*)BlobStart;
2093 const unsigned char *DataEnd = Data + BlobLen;
Douglas Gregorf33740e2011-08-02 10:56:51 +00002094
2095 // Continuous range maps we may be updating in our module.
2096 ContinuousRangeMap<uint32_t, int, 2>::Builder SLocRemap(F.SLocRemap);
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002097 ContinuousRangeMap<uint32_t, int, 2>::Builder
2098 IdentifierRemap(F.IdentifierRemap);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002099 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002100 PreprocessedEntityRemap(F.PreprocessedEntityRemap);
2101 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregor26ced122011-12-01 00:59:36 +00002102 SubmoduleRemap(F.SubmoduleRemap);
2103 ContinuousRangeMap<uint32_t, int, 2>::Builder
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002104 SelectorRemap(F.SelectorRemap);
Douglas Gregor496c7092011-08-03 15:48:04 +00002105 ContinuousRangeMap<uint32_t, int, 2>::Builder DeclRemap(F.DeclRemap);
Douglas Gregora119da02011-08-02 16:26:37 +00002106 ContinuousRangeMap<uint32_t, int, 2>::Builder TypeRemap(F.TypeRemap);
2107
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002108 while(Data < DataEnd) {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002109 uint16_t Len = io::ReadUnalignedLE16(Data);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002110 StringRef Name = StringRef((const char*)Data, Len);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002111 Data += Len;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002112 ModuleFile *OM = ModuleMgr.lookup(Name);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002113 if (!OM) {
2114 Error("SourceLocation remap refers to unknown module");
2115 return Failure;
2116 }
Douglas Gregorf33740e2011-08-02 10:56:51 +00002117
2118 uint32_t SLocOffset = io::ReadUnalignedLE32(Data);
2119 uint32_t IdentifierIDOffset = io::ReadUnalignedLE32(Data);
2120 uint32_t PreprocessedEntityIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregor26ced122011-12-01 00:59:36 +00002121 uint32_t SubmoduleIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002122 uint32_t SelectorIDOffset = io::ReadUnalignedLE32(Data);
2123 uint32_t DeclIDOffset = io::ReadUnalignedLE32(Data);
Douglas Gregora119da02011-08-02 16:26:37 +00002124 uint32_t TypeIndexOffset = io::ReadUnalignedLE32(Data);
Douglas Gregorf33740e2011-08-02 10:56:51 +00002125
2126 // Source location offset is mapped to OM->SLocEntryBaseOffset.
2127 SLocRemap.insert(std::make_pair(SLocOffset,
2128 static_cast<int>(OM->SLocEntryBaseOffset - SLocOffset)));
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002129 IdentifierRemap.insert(
2130 std::make_pair(IdentifierIDOffset,
2131 OM->BaseIdentifierID - IdentifierIDOffset));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002132 PreprocessedEntityRemap.insert(
2133 std::make_pair(PreprocessedEntityIDOffset,
2134 OM->BasePreprocessedEntityID - PreprocessedEntityIDOffset));
Douglas Gregor26ced122011-12-01 00:59:36 +00002135 SubmoduleRemap.insert(std::make_pair(SubmoduleIDOffset,
2136 OM->BaseSubmoduleID - SubmoduleIDOffset));
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002137 SelectorRemap.insert(std::make_pair(SelectorIDOffset,
2138 OM->BaseSelectorID - SelectorIDOffset));
Douglas Gregor496c7092011-08-03 15:48:04 +00002139 DeclRemap.insert(std::make_pair(DeclIDOffset,
2140 OM->BaseDeclID - DeclIDOffset));
2141
Douglas Gregora119da02011-08-02 16:26:37 +00002142 TypeRemap.insert(std::make_pair(TypeIndexOffset,
Douglas Gregore3605012011-08-02 18:32:54 +00002143 OM->BaseTypeIndex - TypeIndexOffset));
Douglas Gregora1be2782011-12-17 23:38:30 +00002144
2145 // Global -> local mappings.
2146 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002147 }
2148 break;
2149 }
2150
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002151 case SOURCE_MANAGER_LINE_TABLE:
2152 if (ParseLineTable(F, Record))
2153 return Failure;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002154 break;
2155
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00002156 case FILE_SOURCE_LOCATION_OFFSETS:
2157 F.SLocFileOffsets = (const uint32_t *)BlobStart;
2158 F.LocalNumSLocFileEntries = Record[0];
2159 break;
2160
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002161 case SOURCE_LOCATION_PRELOADS: {
2162 // Need to transform from the local view (1-based IDs) to the global view,
2163 // which is based off F.SLocEntryBaseID.
Douglas Gregorf249bf32011-08-25 21:09:44 +00002164 if (!F.PreloadSLocEntries.empty()) {
2165 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
2166 return Failure;
2167 }
2168
2169 F.PreloadSLocEntries.swap(Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00002170 break;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002171 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002172
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002173 case STAT_CACHE: {
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00002174 if (!DisableStatCache) {
2175 ASTStatCache *MyStatCache =
2176 new ASTStatCache((const unsigned char *)BlobStart + Record[0],
2177 (const unsigned char *)BlobStart,
2178 NumStatHits, NumStatMisses);
2179 FileMgr.addStatCache(MyStatCache);
2180 F.StatCache = MyStatCache;
2181 }
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002182 break;
Douglas Gregor52e71082009-10-16 18:18:30 +00002183 }
Kovarththanan Rajaratnam6b82f642010-03-07 19:10:13 +00002184
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002185 case EXT_VECTOR_DECLS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002186 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2187 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregorb81c1702009-04-27 20:06:05 +00002188 break;
2189
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002190 case VTABLE_USES:
Douglas Gregordfe65432011-07-28 19:11:31 +00002191 if (Record.size() % 3 != 0) {
2192 Error("Invalid VTABLE_USES record");
2193 return Failure;
2194 }
2195
Sebastian Redl40566802010-08-05 18:21:25 +00002196 // Later tables overwrite earlier ones.
Douglas Gregordfe65432011-07-28 19:11:31 +00002197 // FIXME: Modules will have some trouble with this. This is clearly not
2198 // the right way to do this.
Douglas Gregor409448c2011-07-21 22:35:25 +00002199 VTableUses.clear();
Douglas Gregordfe65432011-07-28 19:11:31 +00002200
2201 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
2202 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
2203 VTableUses.push_back(
2204 ReadSourceLocation(F, Record, Idx).getRawEncoding());
2205 VTableUses.push_back(Record[Idx++]);
2206 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002207 break;
2208
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002209 case DYNAMIC_CLASSES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002210 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2211 DynamicClasses.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002212 break;
2213
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002214 case PENDING_IMPLICIT_INSTANTIATIONS:
Douglas Gregorf2abb522011-07-28 19:26:52 +00002215 if (PendingInstantiations.size() % 2 != 0) {
2216 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
2217 return Failure;
2218 }
2219
2220 // Later lists of pending instantiations overwrite earlier ones.
2221 // FIXME: This is most certainly wrong for modules.
2222 PendingInstantiations.clear();
2223 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
2224 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
2225 PendingInstantiations.push_back(
2226 ReadSourceLocation(F, Record, I).getRawEncoding());
2227 }
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002228 break;
2229
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002230 case SEMA_DECL_REFS:
Sebastian Redl40566802010-08-05 18:21:25 +00002231 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002232 // FIXME: Modules will have some trouble with this.
2233 SemaDeclRefs.clear();
2234 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2235 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002236 break;
2237
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002238 case ORIGINAL_FILE_NAME:
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002239 // The primary AST will be the last to get here, so it will be the one
Sebastian Redl518d8cb2010-07-20 21:20:32 +00002240 // that's used.
Daniel Dunbar7b5a1212009-11-11 05:29:04 +00002241 ActualOriginalFileName.assign(BlobStart, BlobLen);
2242 OriginalFileName = ActualOriginalFileName;
Douglas Gregore650c8c2009-07-07 00:12:59 +00002243 MaybeAddSystemRootToFilename(OriginalFileName);
Douglas Gregorb64c1932009-05-12 01:31:05 +00002244 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Douglas Gregor31d375f2011-05-06 21:43:30 +00002246 case ORIGINAL_FILE_ID:
2247 OriginalFileID = FileID::get(Record[0]);
2248 break;
2249
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002250 case ORIGINAL_PCH_DIR:
2251 // The primary AST will be the last to get here, so it will be the one
2252 // that's used.
2253 OriginalDir.assign(BlobStart, BlobLen);
2254 break;
2255
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 case VERSION_CONTROL_BRANCH_REVISION: {
Ted Kremenek974be4d2010-02-12 23:31:14 +00002257 const std::string &CurBranch = getClangFullRepositoryVersion();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002258 StringRef ASTBranch(BlobStart, BlobLen);
2259 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002260 Diag(diag::warn_pch_different_branch) << ASTBranch << CurBranch;
Douglas Gregor445e23e2009-10-05 21:07:28 +00002261 return IgnorePCH;
2262 }
2263 break;
2264 }
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002265
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002266 case PPD_ENTITIES_OFFSETS: {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002267 F.PreprocessedEntityOffsets = (const PPEntityOffset *)BlobStart;
2268 assert(BlobLen % sizeof(PPEntityOffset) == 0);
2269 F.NumPreprocessedEntities = BlobLen / sizeof(PPEntityOffset);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002270
2271 unsigned LocalBasePreprocessedEntityID = Record[0];
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002272
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002273 unsigned StartingID;
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002274 if (!PP.getPreprocessingRecord())
Argyrios Kyrtzidisc6c54522012-03-05 05:48:17 +00002275 PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002276 if (!PP.getPreprocessingRecord()->getExternalSource())
2277 PP.getPreprocessingRecord()->SetExternalSource(*this);
2278 StartingID
2279 = PP.getPreprocessingRecord()
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002280 ->allocateLoadedEntities(F.NumPreprocessedEntities);
Douglas Gregor9827a802011-07-29 00:56:45 +00002281 F.BasePreprocessedEntityID = StartingID;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002282
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002283 if (F.NumPreprocessedEntities > 0) {
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002284 // Introduce the global -> local mapping for preprocessed entities in
2285 // this module.
2286 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
2287
2288 // Introduce the local -> global mapping for preprocessed entities in
2289 // this module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00002290 F.PreprocessedEntityRemap.insertOrReplace(
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002291 std::make_pair(LocalBasePreprocessedEntityID,
2292 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
2293 }
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002294
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002295 break;
Douglas Gregor4c30bb12011-07-21 00:47:40 +00002296 }
2297
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002298 case DECL_UPDATE_OFFSETS: {
2299 if (Record.size() % 2 != 0) {
2300 Error("invalid DECL_UPDATE_OFFSETS block in AST file");
2301 return Failure;
2302 }
2303 for (unsigned I = 0, N = Record.size(); I != N; I += 2)
Douglas Gregor496c7092011-08-03 15:48:04 +00002304 DeclUpdateOffsets[getGlobalDeclID(F, Record[I])]
2305 .push_back(std::make_pair(&F, Record[I+1]));
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002306 break;
2307 }
2308
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002309 case DECL_REPLACEMENTS: {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002310 if (Record.size() % 3 != 0) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002311 Error("invalid DECL_REPLACEMENTS block in AST file");
Sebastian Redl0b17c612010-08-13 00:28:03 +00002312 return Failure;
2313 }
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002314 for (unsigned I = 0, N = Record.size(); I != N; I += 3)
Douglas Gregor496c7092011-08-03 15:48:04 +00002315 ReplacedDecls[getGlobalDeclID(F, Record[I])]
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00002316 = ReplacedDeclInfo(&F, Record[I+1], Record[I+2]);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002317 break;
2318 }
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002319
Douglas Gregorcff9f262012-01-27 01:47:08 +00002320 case OBJC_CATEGORIES_MAP: {
2321 if (F.LocalNumObjCCategoriesInMap != 0) {
2322 Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002323 return Failure;
2324 }
Douglas Gregorcff9f262012-01-27 01:47:08 +00002325
2326 F.LocalNumObjCCategoriesInMap = Record[0];
2327 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)BlobStart;
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00002328 break;
2329 }
Douglas Gregor7c789c12010-10-29 22:39:52 +00002330
Douglas Gregorcff9f262012-01-27 01:47:08 +00002331 case OBJC_CATEGORIES:
2332 F.ObjCCategories.swap(Record);
2333 break;
2334
Douglas Gregor7c789c12010-10-29 22:39:52 +00002335 case CXX_BASE_SPECIFIER_OFFSETS: {
2336 if (F.LocalNumCXXBaseSpecifiers != 0) {
2337 Error("duplicate CXX_BASE_SPECIFIER_OFFSETS record in AST file");
2338 return Failure;
2339 }
2340
2341 F.LocalNumCXXBaseSpecifiers = Record[0];
2342 F.CXXBaseSpecifiersOffsets = (const uint32_t *)BlobStart;
Jonathan D. Turner1da90142011-07-21 21:15:19 +00002343 NumCXXBaseSpecifiersLoaded += F.LocalNumCXXBaseSpecifiers;
Douglas Gregor7c789c12010-10-29 22:39:52 +00002344 break;
2345 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002346
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002347 case DIAG_PRAGMA_MAPPINGS:
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002348 if (Record.size() % 2 != 0) {
2349 Error("invalid DIAG_USER_MAPPINGS block in AST file");
2350 return Failure;
2351 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002352
2353 if (F.PragmaDiagMappings.empty())
2354 F.PragmaDiagMappings.swap(Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002355 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002356 F.PragmaDiagMappings.insert(F.PragmaDiagMappings.end(),
2357 Record.begin(), Record.end());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002358 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002359
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002360 case CUDA_SPECIAL_DECL_REFS:
2361 // Later tables overwrite earlier ones.
Douglas Gregor409448c2011-07-21 22:35:25 +00002362 // FIXME: Modules will have trouble with this.
2363 CUDASpecialDeclRefs.clear();
2364 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2365 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002366 break;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002367
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002368 case HEADER_SEARCH_TABLE: {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002369 F.HeaderFileInfoTableData = BlobStart;
2370 F.LocalNumHeaderFileInfos = Record[1];
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002371 F.HeaderFileFrameworkStrings = BlobStart + Record[2];
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002372 if (Record[0]) {
2373 F.HeaderFileInfoTable
2374 = HeaderFileInfoLookupTable::Create(
2375 (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002376 (const unsigned char *)F.HeaderFileInfoTableData,
Douglas Gregor95eab172011-07-28 20:55:49 +00002377 HeaderFileInfoTrait(*this, F,
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002378 &PP.getHeaderSearchInfo(),
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002379 BlobStart + Record[2]));
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002380
2381 PP.getHeaderSearchInfo().SetExternalSource(this);
2382 if (!PP.getHeaderSearchInfo().getExternalLookup())
2383 PP.getHeaderSearchInfo().SetExternalLookup(this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002384 }
2385 break;
Douglas Gregorb4dc4852011-07-28 04:50:02 +00002386 }
2387
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002388 case FP_PRAGMA_OPTIONS:
2389 // Later tables overwrite earlier ones.
2390 FPPragmaOptions.swap(Record);
2391 break;
2392
2393 case OPENCL_EXTENSIONS:
2394 // Later tables overwrite earlier ones.
2395 OpenCLExtensions.swap(Record);
2396 break;
Sean Huntebcbe1d2011-05-04 23:29:54 +00002397
2398 case TENTATIVE_DEFINITIONS:
Douglas Gregor409448c2011-07-21 22:35:25 +00002399 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2400 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
Sean Huntebcbe1d2011-05-04 23:29:54 +00002401 break;
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002402
2403 case KNOWN_NAMESPACES:
Douglas Gregor409448c2011-07-21 22:35:25 +00002404 for (unsigned I = 0, N = Record.size(); I != N; ++I)
2405 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002406 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002407
2408 case IMPORTED_MODULES: {
2409 if (F.Kind != MK_Module) {
2410 // If we aren't loading a module (which has its own exports), make
2411 // all of the imported modules visible.
2412 // FIXME: Deal with macros-only imports.
2413 for (unsigned I = 0, N = Record.size(); I != N; ++I) {
2414 if (unsigned GlobalID = getGlobalSubmoduleID(F, Record[I]))
2415 ImportedModules.push_back(GlobalID);
2416 }
2417 }
2418 break;
Douglas Gregora1be2782011-12-17 23:38:30 +00002419 }
Douglas Gregor2171bf12012-01-15 16:58:34 +00002420
Douglas Gregora1be2782011-12-17 23:38:30 +00002421 case LOCAL_REDECLARATIONS: {
Douglas Gregor2171bf12012-01-15 16:58:34 +00002422 F.RedeclarationChains.swap(Record);
2423 break;
2424 }
2425
2426 case LOCAL_REDECLARATIONS_MAP: {
2427 if (F.LocalNumRedeclarationsInMap != 0) {
2428 Error("duplicate LOCAL_REDECLARATIONS_MAP record in AST file");
Douglas Gregora1be2782011-12-17 23:38:30 +00002429 return Failure;
2430 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002431
Douglas Gregor2171bf12012-01-15 16:58:34 +00002432 F.LocalNumRedeclarationsInMap = Record[0];
2433 F.RedeclarationsMap = (const LocalRedeclarationsInfo *)BlobStart;
Douglas Gregora1be2782011-12-17 23:38:30 +00002434 break;
Douglas Gregorf6137e42011-12-03 00:59:55 +00002435 }
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00002436
2437 case MERGED_DECLARATIONS: {
2438 for (unsigned Idx = 0; Idx < Record.size(); /* increment in loop */) {
2439 GlobalDeclID CanonID = getGlobalDeclID(F, Record[Idx++]);
2440 SmallVectorImpl<GlobalDeclID> &Decls = StoredMergedDecls[CanonID];
2441 for (unsigned N = Record[Idx++]; N > 0; --N)
2442 Decls.push_back(getGlobalDeclID(F, Record[Idx++]));
2443 }
2444 break;
2445 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002446 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002447 }
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002448 Error("premature end of bitstream in AST file");
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002449 return Failure;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002450}
2451
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002452ASTReader::ASTReadResult ASTReader::validateFileEntries(ModuleFile &M) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002453 llvm::BitstreamCursor &SLocEntryCursor = M.SLocEntryCursor;
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002454
Douglas Gregorc69a2922011-08-25 20:58:51 +00002455 for (unsigned i = 0, e = M.LocalNumSLocFileEntries; i != e; ++i) {
2456 SLocEntryCursor.JumpToBit(M.SLocFileOffsets[i]);
2457 unsigned Code = SLocEntryCursor.ReadCode();
2458 if (Code == llvm::bitc::END_BLOCK ||
2459 Code == llvm::bitc::ENTER_SUBBLOCK ||
2460 Code == llvm::bitc::DEFINE_ABBREV) {
2461 Error("incorrectly-formatted source location entry in AST file");
2462 return Failure;
2463 }
2464
2465 RecordData Record;
2466 const char *BlobStart;
2467 unsigned BlobLen;
2468 switch (SLocEntryCursor.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
2469 default:
2470 Error("incorrectly-formatted source location entry in AST file");
2471 return Failure;
2472
2473 case SM_SLOC_FILE_ENTRY: {
Douglas Gregora081da52011-11-16 20:05:18 +00002474 // If the buffer was overridden, the file need not exist.
2475 if (Record[6])
2476 break;
2477
Douglas Gregorc69a2922011-08-25 20:58:51 +00002478 StringRef Filename(BlobStart, BlobLen);
2479 const FileEntry *File = getFileEntry(Filename);
2480
2481 if (File == 0) {
2482 std::string ErrorStr = "could not find file '";
2483 ErrorStr += Filename;
2484 ErrorStr += "' referenced by AST file";
2485 Error(ErrorStr.c_str());
2486 return IgnorePCH;
2487 }
2488
Douglas Gregora081da52011-11-16 20:05:18 +00002489 if (Record.size() < 7) {
Douglas Gregorc69a2922011-08-25 20:58:51 +00002490 Error("source location entry is incorrect");
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002491 return Failure;
2492 }
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002493
2494 off_t StoredSize = (off_t)Record[4];
2495 time_t StoredTime = (time_t)Record[5];
2496
2497 // Check if there was a request to override the contents of the file
2498 // that was part of the precompiled header. Overridding such a file
2499 // can lead to problems when lexing using the source locations from the
2500 // PCH.
2501 SourceManager &SM = getSourceManager();
2502 if (SM.isFileOverridden(File)) {
2503 Error(diag::err_fe_pch_file_overridden, Filename);
2504 // After emitting the diagnostic, recover by disabling the override so
2505 // that the original file will be used.
2506 SM.disableFileContentsOverride(File);
2507 // The FileEntry is a virtual file entry with the size of the contents
2508 // that would override the original contents. Set it to the original's
2509 // size/time.
2510 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File),
2511 StoredSize, StoredTime);
2512 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002513
Douglas Gregorc69a2922011-08-25 20:58:51 +00002514 // The stat info from the FileEntry came from the cached stat
2515 // info of the PCH, so we cannot trust it.
2516 struct stat StatBuf;
2517 if (::stat(File->getName(), &StatBuf) != 0) {
2518 StatBuf.st_size = File->getSize();
2519 StatBuf.st_mtime = File->getModificationTime();
2520 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002521
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002522 if ((StoredSize != StatBuf.st_size
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002523#if !defined(LLVM_ON_WIN32)
Douglas Gregorc69a2922011-08-25 20:58:51 +00002524 // In our regression testing, the Windows file system seems to
2525 // have inconsistent modification times that sometimes
2526 // erroneously trigger this error-handling path.
Argyrios Kyrtzidisd54dff02012-05-03 21:50:39 +00002527 || StoredTime != StatBuf.st_mtime
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002528#endif
Douglas Gregorc69a2922011-08-25 20:58:51 +00002529 )) {
2530 Error(diag::err_fe_pch_file_modified, Filename);
2531 return IgnorePCH;
2532 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002533
Douglas Gregorc69a2922011-08-25 20:58:51 +00002534 break;
2535 }
Argyrios Kyrtzidisb68ffb12011-06-01 05:43:53 +00002536 }
2537 }
2538
2539 return Success;
2540}
2541
Douglas Gregorecc2c092011-12-01 22:20:10 +00002542void ASTReader::makeNamesVisible(const HiddenNames &Names) {
Douglas Gregor13292642011-12-02 15:45:10 +00002543 for (unsigned I = 0, N = Names.size(); I != N; ++I) {
2544 if (Decl *D = Names[I].dyn_cast<Decl *>())
Douglas Gregorf143ffc2012-01-06 16:22:39 +00002545 D->Hidden = false;
Douglas Gregor1d4c1132011-12-20 22:06:13 +00002546 else {
2547 IdentifierInfo *II = Names[I].get<IdentifierInfo *>();
2548 if (!II->hasMacroDefinition()) {
2549 II->setHasMacroDefinition(true);
2550 if (DeserializationListener)
2551 DeserializationListener->MacroVisible(II);
2552 }
2553 }
Douglas Gregor13292642011-12-02 15:45:10 +00002554 }
Douglas Gregorecc2c092011-12-01 22:20:10 +00002555}
2556
Douglas Gregor5e356932011-12-01 17:11:21 +00002557void ASTReader::makeModuleVisible(Module *Mod,
2558 Module::NameVisibilityKind NameVisibility) {
2559 llvm::SmallPtrSet<Module *, 4> Visited;
2560 llvm::SmallVector<Module *, 4> Stack;
2561 Stack.push_back(Mod);
2562 while (!Stack.empty()) {
2563 Mod = Stack.back();
2564 Stack.pop_back();
2565
2566 if (NameVisibility <= Mod->NameVisibility) {
2567 // This module already has this level of visibility (or greater), so
2568 // there is nothing more to do.
2569 continue;
2570 }
2571
Douglas Gregor51f564f2011-12-31 04:05:44 +00002572 if (!Mod->isAvailable()) {
2573 // Modules that aren't available cannot be made visible.
2574 continue;
2575 }
2576
Douglas Gregor5e356932011-12-01 17:11:21 +00002577 // Update the module's name visibility.
2578 Mod->NameVisibility = NameVisibility;
2579
Douglas Gregorecc2c092011-12-01 22:20:10 +00002580 // If we've already deserialized any names from this module,
Douglas Gregor5e356932011-12-01 17:11:21 +00002581 // mark them as visible.
Douglas Gregorecc2c092011-12-01 22:20:10 +00002582 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
2583 if (Hidden != HiddenNamesMap.end()) {
2584 makeNamesVisible(Hidden->second);
2585 HiddenNamesMap.erase(Hidden);
2586 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002587
2588 // Push any non-explicit submodules onto the stack to be marked as
2589 // visible.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002590 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2591 SubEnd = Mod->submodule_end();
Douglas Gregor5e356932011-12-01 17:11:21 +00002592 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +00002593 if (!(*Sub)->IsExplicit && Visited.insert(*Sub))
2594 Stack.push_back(*Sub);
Douglas Gregor5e356932011-12-01 17:11:21 +00002595 }
Douglas Gregor07165b92011-12-02 19:11:09 +00002596
2597 // Push any exported modules onto the stack to be marked as visible.
Douglas Gregor0adaa882011-12-05 17:28:06 +00002598 bool AnyWildcard = false;
2599 bool UnrestrictedWildcard = false;
2600 llvm::SmallVector<Module *, 4> WildcardRestrictions;
Douglas Gregor07165b92011-12-02 19:11:09 +00002601 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
2602 Module *Exported = Mod->Exports[I].getPointer();
Douglas Gregor0adaa882011-12-05 17:28:06 +00002603 if (!Mod->Exports[I].getInt()) {
2604 // Export a named module directly; no wildcards involved.
2605 if (Visited.insert(Exported))
Douglas Gregor07165b92011-12-02 19:11:09 +00002606 Stack.push_back(Exported);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002607
2608 continue;
Douglas Gregor07165b92011-12-02 19:11:09 +00002609 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002610
2611 // Wildcard export: export all of the imported modules that match
2612 // the given pattern.
2613 AnyWildcard = true;
2614 if (UnrestrictedWildcard)
2615 continue;
2616
2617 if (Module *Restriction = Mod->Exports[I].getPointer())
2618 WildcardRestrictions.push_back(Restriction);
2619 else {
2620 WildcardRestrictions.clear();
2621 UnrestrictedWildcard = true;
2622 }
2623 }
2624
2625 // If there were any wildcards, push any imported modules that were
2626 // re-exported by the wildcard restriction.
2627 if (!AnyWildcard)
2628 continue;
2629
2630 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
2631 Module *Imported = Mod->Imports[I];
2632 if (Visited.count(Imported))
2633 continue;
2634
2635 bool Acceptable = UnrestrictedWildcard;
2636 if (!Acceptable) {
2637 // Check whether this module meets one of the restrictions.
2638 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2639 Module *Restriction = WildcardRestrictions[R];
2640 if (Imported == Restriction || Imported->isSubModuleOf(Restriction)) {
2641 Acceptable = true;
2642 break;
2643 }
2644 }
2645 }
2646
2647 if (!Acceptable)
2648 continue;
2649
2650 Visited.insert(Imported);
2651 Stack.push_back(Imported);
Douglas Gregor07165b92011-12-02 19:11:09 +00002652 }
Douglas Gregor5e356932011-12-01 17:11:21 +00002653 }
2654}
2655
Sebastian Redl1d9f1fe2010-10-05 16:15:19 +00002656ASTReader::ASTReadResult ASTReader::ReadAST(const std::string &FileName,
Douglas Gregor72a9ae12011-07-22 16:00:58 +00002657 ModuleKind Type) {
Douglas Gregor057df202012-01-18 20:56:22 +00002658 // Bump the generation number.
Douglas Gregorcff9f262012-01-27 01:47:08 +00002659 unsigned PreviousGeneration = CurrentGeneration++;
Douglas Gregor057df202012-01-18 20:56:22 +00002660
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002661 switch(ReadASTCore(FileName, Type, /*ImportedBy=*/0)) {
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002662 case Failure: return Failure;
2663 case IgnorePCH: return IgnorePCH;
2664 case Success: break;
2665 }
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002666
2667 // Here comes stuff that we only do once the entire chain is loaded.
Douglas Gregor057df202012-01-18 20:56:22 +00002668
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002669 // Check the predefines buffers.
Douglas Gregor6236a292011-12-02 21:56:05 +00002670 if (!DisableValidation && Type == MK_PCH &&
Argyrios Kyrtzidis26d43cd2011-09-12 18:09:38 +00002671 // FIXME: CheckPredefinesBuffers also sets the SuggestedPredefines;
2672 // if DisableValidation is true, defines that were set on command-line
2673 // but not in the PCH file will not be added to SuggestedPredefines.
2674 CheckPredefinesBuffers())
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002675 return IgnorePCH;
2676
Douglas Gregoreee242f2011-10-27 09:33:13 +00002677 // Mark all of the identifiers in the identifier table as being out of date,
2678 // so that various accessors know to check the loaded modules when the
2679 // identifier is used.
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002680 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
2681 IdEnd = PP.getIdentifierTable().end();
2682 Id != IdEnd; ++Id)
Douglas Gregoreee242f2011-10-27 09:33:13 +00002683 Id->second->setOutOfDate(true);
Douglas Gregor057df202012-01-18 20:56:22 +00002684
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002685 // Resolve any unresolved module exports.
Douglas Gregor55988682011-12-05 16:33:54 +00002686 for (unsigned I = 0, N = UnresolvedModuleImportExports.size(); I != N; ++I) {
2687 UnresolvedModuleImportExport &Unresolved = UnresolvedModuleImportExports[I];
2688 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002689 Module *ResolvedMod = getSubmodule(GlobalID);
2690
2691 if (Unresolved.IsImport) {
2692 if (ResolvedMod)
Douglas Gregor55988682011-12-05 16:33:54 +00002693 Unresolved.Mod->Imports.push_back(ResolvedMod);
Douglas Gregor0adaa882011-12-05 17:28:06 +00002694 continue;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002695 }
Douglas Gregor0adaa882011-12-05 17:28:06 +00002696
2697 if (ResolvedMod || Unresolved.IsWildcard)
2698 Unresolved.Mod->Exports.push_back(
2699 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002700 }
Douglas Gregor55988682011-12-05 16:33:54 +00002701 UnresolvedModuleImportExports.clear();
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002702
Douglas Gregor35942772011-09-09 21:34:22 +00002703 InitializeContext();
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002704
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002705 if (DeserializationListener)
2706 DeserializationListener->ReaderInitialized(this);
2707
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002708 if (!OriginalFileID.isInvalid()) {
2709 OriginalFileID = FileID::get(ModuleMgr.getPrimaryModule().SLocEntryBaseID
2710 + OriginalFileID.getOpaqueValue() - 1);
2711
2712 // If this AST file is a precompiled preamble, then set the preamble file ID
2713 // of the source manager to the file source file from which the preamble was
2714 // built.
2715 if (Type == MK_Preamble) {
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00002716 SourceMgr.setPreambleFileID(OriginalFileID);
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +00002717 } else if (Type == MK_MainFile) {
2718 SourceMgr.setMainFileID(OriginalFileID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002719 }
Douglas Gregor414cb642010-11-30 05:23:00 +00002720 }
2721
Douglas Gregorcff9f262012-01-27 01:47:08 +00002722 // For any Objective-C class definitions we have already loaded, make sure
2723 // that we load any additional categories.
2724 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
2725 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
2726 ObjCClassesLoaded[I],
2727 PreviousGeneration);
2728 }
2729
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002730 return Success;
2731}
2732
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733ASTReader::ASTReadResult ASTReader::ReadASTCore(StringRef FileName,
Douglas Gregor10bc00f2011-08-18 04:12:04 +00002734 ModuleKind Type,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002735 ModuleFile *ImportedBy) {
2736 ModuleFile *M;
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002737 bool NewModule;
2738 std::string ErrorStr;
2739 llvm::tie(M, NewModule) = ModuleMgr.addModule(FileName, Type, ImportedBy,
Douglas Gregor057df202012-01-18 20:56:22 +00002740 CurrentGeneration, ErrorStr);
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002741
Douglas Gregorfac4ece2011-08-19 02:29:29 +00002742 if (!M) {
2743 // We couldn't load the module.
2744 std::string Msg = "Unable to load module \"" + FileName.str() + "\": "
2745 + ErrorStr;
2746 Error(Msg);
2747 return Failure;
2748 }
2749
2750 if (!NewModule) {
2751 // We've already loaded this module.
2752 return Success;
2753 }
2754
2755 // FIXME: This seems rather a hack. Should CurrentDir be part of the
2756 // module?
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002757 if (FileName != "-") {
2758 CurrentDir = llvm::sys::path::parent_path(FileName);
2759 if (CurrentDir.empty()) CurrentDir = ".";
2760 }
2761
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002762 ModuleFile &F = *M;
Sebastian Redl9137a522010-07-16 17:50:48 +00002763 llvm::BitstreamCursor &Stream = F.Stream;
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002764 Stream.init(F.StreamFile);
Sebastian Redl04e6fd42010-07-21 20:07:32 +00002765 F.SizeInBits = F.Buffer->getBufferSize() * 8;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002766
Sebastian Redlfbd4bf12010-07-17 00:12:06 +00002767 // Sniff for the signature.
2768 if (Stream.Read(8) != 'C' ||
2769 Stream.Read(8) != 'P' ||
2770 Stream.Read(8) != 'C' ||
2771 Stream.Read(8) != 'H') {
2772 Diag(diag::err_not_a_pch_file) << FileName;
2773 return Failure;
2774 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002775
Douglas Gregor2cf26342009-04-09 22:27:44 +00002776 while (!Stream.AtEndOfStream()) {
2777 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00002778
Douglas Gregore1d918e2009-04-10 23:10:45 +00002779 if (Code != llvm::bitc::ENTER_SUBBLOCK) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002780 Error("invalid record at top-level of AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002781 return Failure;
2782 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002783
2784 unsigned BlockID = Stream.ReadSubBlockID();
Douglas Gregor668c1a42009-04-21 22:25:48 +00002785
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002786 // We only know the AST subblock ID.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002787 switch (BlockID) {
2788 case llvm::bitc::BLOCKINFO_BLOCK_ID:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002789 if (Stream.ReadBlockInfoBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002790 Error("malformed BlockInfoBlock in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002791 return Failure;
2792 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002793 break;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002794 case AST_BLOCK_ID:
Sebastian Redl571db7f2010-08-18 23:56:56 +00002795 switch (ReadASTBlock(F)) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002796 case Success:
2797 break;
2798
2799 case Failure:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002800 return Failure;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002801
2802 case IgnorePCH:
Douglas Gregor2bec0412009-04-10 21:16:55 +00002803 // FIXME: We could consider reading through to the end of this
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002804 // AST block, skipping subblocks, to see if there are other
2805 // AST blocks elsewhere.
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002806
Douglas Gregorf62d43d2011-07-19 16:10:42 +00002807 // FIXME: We can't clear loaded slocentries anymore.
2808 //SourceMgr.ClearPreallocatedSLocEntries();
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002809
2810 // Remove the stat cache.
Sebastian Redl9137a522010-07-16 17:50:48 +00002811 if (F.StatCache)
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002812 FileMgr.removeStatCache((ASTStatCache*)F.StatCache);
Douglas Gregor2bf1eb02009-04-27 21:28:04 +00002813
Douglas Gregore1d918e2009-04-10 23:10:45 +00002814 return IgnorePCH;
Douglas Gregor0a0428e2009-04-10 20:39:37 +00002815 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002816 break;
2817 default:
Douglas Gregore1d918e2009-04-10 23:10:45 +00002818 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002819 Error("malformed block record in AST file");
Douglas Gregore1d918e2009-04-10 23:10:45 +00002820 return Failure;
2821 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002822 break;
2823 }
Mike Stump1eb44332009-09-09 15:08:12 +00002824 }
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002825
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002826 // Once read, set the ModuleFile bit base offset and update the size in
Douglas Gregor8f1231b2011-07-22 06:10:01 +00002827 // bits of all files we've seen.
2828 F.GlobalBitOffset = TotalModulesSizeInBits;
2829 TotalModulesSizeInBits += F.SizeInBits;
2830 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
Douglas Gregorc69a2922011-08-25 20:58:51 +00002831
2832 // Make sure that the files this module was built against are still available.
2833 if (!DisableValidation) {
2834 switch(validateFileEntries(*M)) {
2835 case Failure: return Failure;
2836 case IgnorePCH: return IgnorePCH;
2837 case Success: break;
2838 }
2839 }
Douglas Gregorf249bf32011-08-25 21:09:44 +00002840
2841 // Preload SLocEntries.
2842 for (unsigned I = 0, N = M->PreloadSLocEntries.size(); I != N; ++I) {
2843 int Index = int(M->PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00002844 // Load it through the SourceManager and don't call ReadSLocEntryRecord()
2845 // directly because the entry may have already been loaded in which case
2846 // calling ReadSLocEntryRecord() directly would trigger an assertion in
2847 // SourceManager.
2848 SourceMgr.getLoadedSLocEntryByID(Index);
Douglas Gregorf249bf32011-08-25 21:09:44 +00002849 }
2850
Douglas Gregorc69a2922011-08-25 20:58:51 +00002851
Sebastian Redlcdf3b832010-07-16 20:41:52 +00002852 return Success;
2853}
2854
Douglas Gregor712f2fc2011-09-09 22:02:16 +00002855void ASTReader::InitializeContext() {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002856 // If there's a listener, notify them that we "read" the translation unit.
2857 if (DeserializationListener)
Douglas Gregor35942772011-09-09 21:34:22 +00002858 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
2859 Context.getTranslationUnitDecl());
Douglas Gregor3747ee72010-10-01 01:18:02 +00002860
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002861 // Make sure we load the declaration update records for the translation unit,
2862 // if there are any.
Douglas Gregor35942772011-09-09 21:34:22 +00002863 loadDeclUpdateRecords(PREDEF_DECL_TRANSLATION_UNIT_ID,
2864 Context.getTranslationUnitDecl());
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00002865
Douglas Gregor5f957282011-08-11 22:18:49 +00002866 // FIXME: Find a better way to deal with collisions between these
2867 // built-in types. Right now, we just ignore the problem.
2868
2869 // Load the special types.
Douglas Gregora6ea10e2012-01-17 18:09:05 +00002870 if (SpecialTypes.size() >= NumSpecialTypeIDs) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002871 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
2872 if (!Context.CFConstantStringTypeDecl)
2873 Context.setCFConstantStringType(GetType(String));
2874 }
2875
2876 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
2877 QualType FileType = GetType(File);
2878 if (FileType.isNull()) {
2879 Error("FILE type is NULL");
2880 return;
2881 }
2882
2883 if (!Context.FILEDecl) {
2884 if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
2885 Context.setFILEDecl(Typedef->getDecl());
2886 else {
2887 const TagType *Tag = FileType->getAs<TagType>();
2888 if (!Tag) {
2889 Error("Invalid FILE type in AST file");
2890 return;
2891 }
2892 Context.setFILEDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002893 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002894 }
Douglas Gregorc29f77b2009-07-07 16:35:42 +00002895 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002896
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002897 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002898 QualType Jmp_bufType = GetType(Jmp_buf);
2899 if (Jmp_bufType.isNull()) {
2900 Error("jmp_buf type is NULL");
2901 return;
2902 }
2903
2904 if (!Context.jmp_bufDecl) {
2905 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
2906 Context.setjmp_bufDecl(Typedef->getDecl());
2907 else {
2908 const TagType *Tag = Jmp_bufType->getAs<TagType>();
2909 if (!Tag) {
2910 Error("Invalid jmp_buf type in AST file");
2911 return;
2912 }
2913 Context.setjmp_bufDecl(Tag->getDecl());
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002914 }
Jonathan D. Turnerde91db52011-08-05 23:07:10 +00002915 }
Mike Stump782fa302009-07-28 02:25:19 +00002916 }
Douglas Gregor02a5e872011-09-10 00:30:18 +00002917
Douglas Gregor72cd7a02011-11-11 19:13:12 +00002918 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
Douglas Gregor02a5e872011-09-10 00:30:18 +00002919 QualType Sigjmp_bufType = GetType(Sigjmp_buf);
2920 if (Sigjmp_bufType.isNull()) {
2921 Error("sigjmp_buf type is NULL");
2922 return;
2923 }
2924
2925 if (!Context.sigjmp_bufDecl) {
2926 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
2927 Context.setsigjmp_bufDecl(Typedef->getDecl());
2928 else {
2929 const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
2930 assert(Tag && "Invalid sigjmp_buf type in AST file");
2931 Context.setsigjmp_bufDecl(Tag->getDecl());
2932 }
2933 }
2934 }
2935
2936 if (unsigned ObjCIdRedef
2937 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
2938 if (Context.ObjCIdRedefinitionType.isNull())
2939 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
2940 }
2941
2942 if (unsigned ObjCClassRedef
2943 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
2944 if (Context.ObjCClassRedefinitionType.isNull())
2945 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
2946 }
2947
2948 if (unsigned ObjCSelRedef
2949 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
2950 if (Context.ObjCSelRedefinitionType.isNull())
2951 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
2952 }
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00002953
2954 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
2955 QualType Ucontext_tType = GetType(Ucontext_t);
2956 if (Ucontext_tType.isNull()) {
2957 Error("ucontext_t type is NULL");
2958 return;
2959 }
2960
2961 if (!Context.ucontext_tDecl) {
2962 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
2963 Context.setucontext_tDecl(Typedef->getDecl());
2964 else {
2965 const TagType *Tag = Ucontext_tType->getAs<TagType>();
2966 assert(Tag && "Invalid ucontext_t type in AST file");
2967 Context.setucontext_tDecl(Tag->getDecl());
2968 }
2969 }
2970 }
Douglas Gregor5f957282011-08-11 22:18:49 +00002971 }
2972
Douglas Gregor35942772011-09-09 21:34:22 +00002973 ReadPragmaDiagnosticMappings(Context.getDiagnostics());
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002974
2975 // If there were any CUDA special declarations, deserialize them.
2976 if (!CUDASpecialDeclRefs.empty()) {
2977 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
Douglas Gregor35942772011-09-09 21:34:22 +00002978 Context.setcudaConfigureCallDecl(
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002979 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
2980 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00002981
2982 // Re-export any modules that were imported by a non-module AST file.
2983 for (unsigned I = 0, N = ImportedModules.size(); I != N; ++I) {
2984 if (Module *Imported = getSubmodule(ImportedModules[I]))
2985 makeModuleVisible(Imported, Module::AllVisible);
2986 }
2987 ImportedModules.clear();
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00002988}
2989
Douglas Gregorecc2c092011-12-01 22:20:10 +00002990void ASTReader::finalizeForWriting() {
2991 for (HiddenNamesMapType::iterator Hidden = HiddenNamesMap.begin(),
2992 HiddenEnd = HiddenNamesMap.end();
2993 Hidden != HiddenEnd; ++Hidden) {
2994 makeNamesVisible(Hidden->second);
2995 }
2996 HiddenNamesMap.clear();
2997}
2998
Douglas Gregorb64c1932009-05-12 01:31:05 +00002999/// \brief Retrieve the name of the original source file name
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003000/// directly from the AST file, without actually loading the AST
Douglas Gregorb64c1932009-05-12 01:31:05 +00003001/// file.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003002std::string ASTReader::getOriginalSourceFile(const std::string &ASTFileName,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +00003003 FileManager &FileMgr,
David Blaikied6471f72011-09-25 23:23:43 +00003004 DiagnosticsEngine &Diags) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003005 // Open the AST file.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003006 std::string ErrStr;
Dylan Noblesmith6f42b622012-02-05 02:12:40 +00003007 OwningPtr<llvm::MemoryBuffer> Buffer;
Chris Lattner39b49bc2010-11-23 08:35:12 +00003008 Buffer.reset(FileMgr.getBufferForFile(ASTFileName, &ErrStr));
Douglas Gregorb64c1932009-05-12 01:31:05 +00003009 if (!Buffer) {
Kaelyn Uhrainda01f622012-06-20 00:36:03 +00003010 Diags.Report(diag::err_fe_unable_to_read_pch_file) << ASTFileName << ErrStr;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003011 return std::string();
3012 }
3013
3014 // Initialize the stream
3015 llvm::BitstreamReader StreamFile;
3016 llvm::BitstreamCursor Stream;
Mike Stump1eb44332009-09-09 15:08:12 +00003017 StreamFile.init((const unsigned char *)Buffer->getBufferStart(),
Douglas Gregorb64c1932009-05-12 01:31:05 +00003018 (const unsigned char *)Buffer->getBufferEnd());
3019 Stream.init(StreamFile);
3020
3021 // Sniff for the signature.
3022 if (Stream.Read(8) != 'C' ||
3023 Stream.Read(8) != 'P' ||
3024 Stream.Read(8) != 'C' ||
3025 Stream.Read(8) != 'H') {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003026 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003027 return std::string();
3028 }
3029
3030 RecordData Record;
3031 while (!Stream.AtEndOfStream()) {
3032 unsigned Code = Stream.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Douglas Gregorb64c1932009-05-12 01:31:05 +00003034 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3035 unsigned BlockID = Stream.ReadSubBlockID();
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003037 // We only know the AST subblock ID.
Douglas Gregorb64c1932009-05-12 01:31:05 +00003038 switch (BlockID) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003039 case AST_BLOCK_ID:
3040 if (Stream.EnterSubBlock(AST_BLOCK_ID)) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003041 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003042 return std::string();
3043 }
3044 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003045
Douglas Gregorb64c1932009-05-12 01:31:05 +00003046 default:
3047 if (Stream.SkipBlock()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003048 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003049 return std::string();
3050 }
3051 break;
3052 }
3053 continue;
3054 }
3055
3056 if (Code == llvm::bitc::END_BLOCK) {
3057 if (Stream.ReadBlockEnd()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003058 Diags.Report(diag::err_fe_pch_error_at_end_block) << ASTFileName;
Douglas Gregorb64c1932009-05-12 01:31:05 +00003059 return std::string();
3060 }
3061 continue;
3062 }
3063
3064 if (Code == llvm::bitc::DEFINE_ABBREV) {
3065 Stream.ReadAbbrevRecord();
3066 continue;
3067 }
3068
3069 Record.clear();
3070 const char *BlobStart = 0;
3071 unsigned BlobLen = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003072 if (Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003073 == ORIGINAL_FILE_NAME)
Douglas Gregorb64c1932009-05-12 01:31:05 +00003074 return std::string(BlobStart, BlobLen);
Mike Stump1eb44332009-09-09 15:08:12 +00003075 }
Douglas Gregorb64c1932009-05-12 01:31:05 +00003076
3077 return std::string();
3078}
3079
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003080ASTReader::ASTReadResult ASTReader::ReadSubmoduleBlock(ModuleFile &F) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003081 // Enter the submodule block.
3082 if (F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
3083 Error("malformed submodule block record in AST file");
3084 return Failure;
3085 }
3086
3087 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
Douglas Gregor26ced122011-12-01 00:59:36 +00003088 bool First = true;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003089 Module *CurrentModule = 0;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003090 RecordData Record;
3091 while (true) {
3092 unsigned Code = F.Stream.ReadCode();
3093 if (Code == llvm::bitc::END_BLOCK) {
3094 if (F.Stream.ReadBlockEnd()) {
3095 Error("error at end of submodule block in AST file");
3096 return Failure;
3097 }
3098 return Success;
3099 }
3100
3101 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
3102 // No known subblocks, always skip them.
3103 F.Stream.ReadSubBlockID();
3104 if (F.Stream.SkipBlock()) {
3105 Error("malformed block record in AST file");
3106 return Failure;
3107 }
3108 continue;
3109 }
3110
3111 if (Code == llvm::bitc::DEFINE_ABBREV) {
3112 F.Stream.ReadAbbrevRecord();
3113 continue;
3114 }
3115
3116 // Read a record.
3117 const char *BlobStart;
3118 unsigned BlobLen;
3119 Record.clear();
3120 switch (F.Stream.ReadRecord(Code, Record, &BlobStart, &BlobLen)) {
3121 default: // Default behavior: ignore.
3122 break;
3123
3124 case SUBMODULE_DEFINITION: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003125 if (First) {
3126 Error("missing submodule metadata record at beginning of block");
3127 return Failure;
3128 }
3129
Douglas Gregore209e502011-12-06 01:10:29 +00003130 if (Record.size() < 7) {
Douglas Gregor1e123682011-12-05 22:27:44 +00003131 Error("malformed module definition");
3132 return Failure;
3133 }
3134
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003135 StringRef Name(BlobStart, BlobLen);
Douglas Gregore209e502011-12-06 01:10:29 +00003136 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[0]);
3137 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
3138 bool IsFramework = Record[2];
3139 bool IsExplicit = Record[3];
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003140 bool IsSystem = Record[4];
3141 bool InferSubmodules = Record[5];
3142 bool InferExplicitSubmodules = Record[6];
3143 bool InferExportWildcard = Record[7];
Douglas Gregor1e123682011-12-05 22:27:44 +00003144
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003145 Module *ParentModule = 0;
Douglas Gregor26ced122011-12-01 00:59:36 +00003146 if (Parent)
3147 ParentModule = getSubmodule(Parent);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003148
3149 // Retrieve this (sub)module from the module map, creating it if
3150 // necessary.
3151 CurrentModule = ModMap.findOrCreateModule(Name, ParentModule,
3152 IsFramework,
3153 IsExplicit).first;
Douglas Gregore209e502011-12-06 01:10:29 +00003154 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
3155 if (GlobalIndex >= SubmodulesLoaded.size() ||
3156 SubmodulesLoaded[GlobalIndex]) {
Douglas Gregor26ced122011-12-01 00:59:36 +00003157 Error("too many submodules");
3158 return Failure;
3159 }
Douglas Gregora015cab2011-12-02 17:30:13 +00003160
Douglas Gregor305dc3e2011-12-20 00:28:52 +00003161 CurrentModule->IsFromModuleFile = true;
Douglas Gregora1f1fad2012-01-27 19:52:33 +00003162 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
Douglas Gregor1e123682011-12-05 22:27:44 +00003163 CurrentModule->InferSubmodules = InferSubmodules;
3164 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
3165 CurrentModule->InferExportWildcard = InferExportWildcard;
Douglas Gregora015cab2011-12-02 17:30:13 +00003166 if (DeserializationListener)
Douglas Gregore209e502011-12-06 01:10:29 +00003167 DeserializationListener->ModuleRead(GlobalID, CurrentModule);
Douglas Gregora015cab2011-12-02 17:30:13 +00003168
Douglas Gregore209e502011-12-06 01:10:29 +00003169 SubmodulesLoaded[GlobalIndex] = CurrentModule;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003170 break;
3171 }
3172
Douglas Gregor77d029f2011-12-08 19:11:24 +00003173 case SUBMODULE_UMBRELLA_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003174 if (First) {
3175 Error("missing submodule metadata record at beginning of block");
3176 return Failure;
3177 }
3178
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003179 if (!CurrentModule)
3180 break;
3181
3182 StringRef FileName(BlobStart, BlobLen);
3183 if (const FileEntry *Umbrella = PP.getFileManager().getFile(FileName)) {
Douglas Gregor10694ce2011-12-08 17:39:04 +00003184 if (!CurrentModule->getUmbrellaHeader())
Douglas Gregore209e502011-12-06 01:10:29 +00003185 ModMap.setUmbrellaHeader(CurrentModule, Umbrella);
Douglas Gregor10694ce2011-12-08 17:39:04 +00003186 else if (CurrentModule->getUmbrellaHeader() != Umbrella) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003187 Error("mismatched umbrella headers in submodule");
3188 return Failure;
3189 }
3190 }
3191 break;
3192 }
3193
3194 case SUBMODULE_HEADER: {
Douglas Gregor26ced122011-12-01 00:59:36 +00003195 if (First) {
3196 Error("missing submodule metadata record at beginning of block");
3197 return Failure;
3198 }
3199
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003200 if (!CurrentModule)
3201 break;
3202
3203 // FIXME: Be more lazy about this!
3204 StringRef FileName(BlobStart, BlobLen);
3205 if (const FileEntry *File = PP.getFileManager().getFile(FileName)) {
3206 if (std::find(CurrentModule->Headers.begin(),
3207 CurrentModule->Headers.end(),
3208 File) == CurrentModule->Headers.end())
Douglas Gregore209e502011-12-06 01:10:29 +00003209 ModMap.addHeader(CurrentModule, File);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003210 }
3211 break;
3212 }
Douglas Gregor26ced122011-12-01 00:59:36 +00003213
Douglas Gregor77d029f2011-12-08 19:11:24 +00003214 case SUBMODULE_UMBRELLA_DIR: {
3215 if (First) {
3216 Error("missing submodule metadata record at beginning of block");
3217 return Failure;
3218 }
3219
3220 if (!CurrentModule)
3221 break;
3222
3223 StringRef DirName(BlobStart, BlobLen);
3224 if (const DirectoryEntry *Umbrella
3225 = PP.getFileManager().getDirectory(DirName)) {
3226 if (!CurrentModule->getUmbrellaDir())
3227 ModMap.setUmbrellaDir(CurrentModule, Umbrella);
3228 else if (CurrentModule->getUmbrellaDir() != Umbrella) {
3229 Error("mismatched umbrella directories in submodule");
3230 return Failure;
3231 }
3232 }
3233 break;
3234 }
3235
Douglas Gregor26ced122011-12-01 00:59:36 +00003236 case SUBMODULE_METADATA: {
3237 if (!First) {
3238 Error("submodule metadata record not at beginning of block");
3239 return Failure;
3240 }
3241 First = false;
3242
3243 F.BaseSubmoduleID = getTotalNumSubmodules();
Douglas Gregor26ced122011-12-01 00:59:36 +00003244 F.LocalNumSubmodules = Record[0];
3245 unsigned LocalBaseSubmoduleID = Record[1];
3246 if (F.LocalNumSubmodules > 0) {
3247 // Introduce the global -> local mapping for submodules within this
3248 // module.
3249 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
3250
3251 // Introduce the local -> global mapping for submodules within this
3252 // module.
Douglas Gregoradafc2e2011-12-19 16:14:14 +00003253 F.SubmoduleRemap.insertOrReplace(
Douglas Gregor26ced122011-12-01 00:59:36 +00003254 std::make_pair(LocalBaseSubmoduleID,
3255 F.BaseSubmoduleID - LocalBaseSubmoduleID));
3256
3257 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
3258 }
3259 break;
3260 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003261
Douglas Gregor55988682011-12-05 16:33:54 +00003262 case SUBMODULE_IMPORTS: {
3263 if (First) {
3264 Error("missing submodule metadata record at beginning of block");
3265 return Failure;
3266 }
3267
3268 if (!CurrentModule)
3269 break;
3270
3271 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
3272 UnresolvedModuleImportExport Unresolved;
3273 Unresolved.File = &F;
3274 Unresolved.Mod = CurrentModule;
3275 Unresolved.ID = Record[Idx];
3276 Unresolved.IsImport = true;
3277 Unresolved.IsWildcard = false;
3278 UnresolvedModuleImportExports.push_back(Unresolved);
3279 }
3280 break;
3281 }
3282
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003283 case SUBMODULE_EXPORTS: {
3284 if (First) {
3285 Error("missing submodule metadata record at beginning of block");
3286 return Failure;
3287 }
3288
3289 if (!CurrentModule)
3290 break;
3291
3292 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
Douglas Gregor55988682011-12-05 16:33:54 +00003293 UnresolvedModuleImportExport Unresolved;
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003294 Unresolved.File = &F;
Douglas Gregor55988682011-12-05 16:33:54 +00003295 Unresolved.Mod = CurrentModule;
3296 Unresolved.ID = Record[Idx];
3297 Unresolved.IsImport = false;
3298 Unresolved.IsWildcard = Record[Idx + 1];
3299 UnresolvedModuleImportExports.push_back(Unresolved);
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003300 }
3301
3302 // Once we've loaded the set of exports, there's no reason to keep
3303 // the parsed, unresolved exports around.
3304 CurrentModule->UnresolvedExports.clear();
3305 break;
3306 }
Douglas Gregor51f564f2011-12-31 04:05:44 +00003307 case SUBMODULE_REQUIRES: {
3308 if (First) {
3309 Error("missing submodule metadata record at beginning of block");
3310 return Failure;
3311 }
3312
3313 if (!CurrentModule)
3314 break;
3315
3316 CurrentModule->addRequirement(StringRef(BlobStart, BlobLen),
David Blaikie4e4d0842012-03-11 07:00:24 +00003317 Context.getLangOpts(),
Douglas Gregordc58aa72012-01-30 06:01:29 +00003318 Context.getTargetInfo());
Douglas Gregor51f564f2011-12-31 04:05:44 +00003319 break;
3320 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003321 }
3322 }
Douglas Gregor392ed2b2011-11-30 17:33:56 +00003323}
3324
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003325/// \brief Parse the record that corresponds to a LangOptions data
3326/// structure.
3327///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003328/// This routine parses the language options from the AST file and then gives
3329/// them to the AST listener if one is set.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003330///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003331/// \returns true if the listener deems the file unacceptable, false otherwise.
John McCall260611a2012-06-20 06:18:46 +00003332bool ASTReader::ParseLanguageOptions(const RecordData &Record) {
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003333 if (Listener) {
3334 LangOptions LangOpts;
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003335 unsigned Idx = 0;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003336#define LANGOPT(Name, Bits, Default, Description) \
3337 LangOpts.Name = Record[Idx++];
3338#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
3339 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
3340#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00003341
3342 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
3343 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
3344 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00003345
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00003346 unsigned Length = Record[Idx++];
3347 LangOpts.CurrentModule.assign(Record.begin() + Idx,
3348 Record.begin() + Idx + Length);
Argyrios Kyrtzidis11e51102009-06-19 00:03:23 +00003349 return Listener->ReadLanguageOptions(LangOpts);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003350 }
Douglas Gregor0a0428e2009-04-10 20:39:37 +00003351
3352 return false;
3353}
3354
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003355std::pair<ModuleFile *, unsigned>
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003356ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003357 GlobalPreprocessedEntityMapType::iterator
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003358 I = GlobalPreprocessedEntityMap.find(GlobalIndex);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003359 assert(I != GlobalPreprocessedEntityMap.end() &&
3360 "Corrupted global preprocessed entity map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003361 ModuleFile *M = I->second;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003362 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
3363 return std::make_pair(M, LocalIndex);
3364}
3365
3366PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
3367 PreprocessedEntityID PPID = Index+1;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003368 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3369 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003370 unsigned LocalIndex = PPInfo.second;
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003371 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
Douglas Gregor4800a5c2011-02-08 21:58:10 +00003372
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00003373 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003374 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003375
3376 unsigned Code = M.PreprocessorDetailCursor.ReadCode();
3377 switch (Code) {
3378 case llvm::bitc::END_BLOCK:
3379 return 0;
3380
3381 case llvm::bitc::ENTER_SUBBLOCK:
3382 Error("unexpected subblock record in preprocessor detail block");
3383 return 0;
3384
3385 case llvm::bitc::DEFINE_ABBREV:
3386 Error("unexpected abbrevation record in preprocessor detail block");
3387 return 0;
3388
3389 default:
3390 break;
3391 }
3392
3393 if (!PP.getPreprocessingRecord()) {
3394 Error("no preprocessing record");
3395 return 0;
3396 }
3397
3398 // Read the record.
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003399 SourceRange Range(ReadSourceLocation(M, PPOffs.Begin),
3400 ReadSourceLocation(M, PPOffs.End));
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003401 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
3402 const char *BlobStart = 0;
3403 unsigned BlobLen = 0;
3404 RecordData Record;
3405 PreprocessorDetailRecordTypes RecType =
3406 (PreprocessorDetailRecordTypes)M.PreprocessorDetailCursor.ReadRecord(
3407 Code, Record, BlobStart, BlobLen);
3408 switch (RecType) {
3409 case PPD_MACRO_EXPANSION: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003410 bool isBuiltin = Record[0];
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003411 IdentifierInfo *Name = 0;
3412 MacroDefinition *Def = 0;
3413 if (isBuiltin)
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003414 Name = getLocalIdentifier(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003415 else {
3416 PreprocessedEntityID
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003417 GlobalID = getGlobalPreprocessedEntityID(M, Record[1]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003418 Def =cast<MacroDefinition>(PPRec.getLoadedPreprocessedEntity(GlobalID-1));
3419 }
3420
3421 MacroExpansion *ME;
3422 if (isBuiltin)
3423 ME = new (PPRec) MacroExpansion(Name, Range);
3424 else
3425 ME = new (PPRec) MacroExpansion(Def, Range);
3426
3427 return ME;
3428 }
3429
3430 case PPD_MACRO_DEFINITION: {
3431 // Decode the identifier info and then check again; if the macro is
3432 // still defined and associated with the identifier,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003433 IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003434 MacroDefinition *MD
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003435 = new (PPRec) MacroDefinition(II, Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003436
3437 if (DeserializationListener)
3438 DeserializationListener->MacroDefinitionRead(PPID, MD);
3439
3440 return MD;
3441 }
3442
3443 case PPD_INCLUSION_DIRECTIVE: {
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003444 const char *FullFileNameStart = BlobStart + Record[0];
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00003445 StringRef FullFileName(FullFileNameStart, BlobLen - Record[0]);
3446 const FileEntry *File = 0;
3447 if (!FullFileName.empty())
3448 File = PP.getFileManager().getFile(FullFileName);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003449
3450 // FIXME: Stable encoding
3451 InclusionDirective::InclusionKind Kind
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003452 = static_cast<InclusionDirective::InclusionKind>(Record[2]);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003453 InclusionDirective *ID
3454 = new (PPRec) InclusionDirective(PPRec, Kind,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003455 StringRef(BlobStart, Record[0]),
3456 Record[1],
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003457 File,
Argyrios Kyrtzidis8f958f12011-09-20 23:27:41 +00003458 Range);
Argyrios Kyrtzidis290ad8c2011-09-20 23:27:38 +00003459 return ID;
3460 }
3461 }
David Blaikie7530c032012-01-17 06:56:22 +00003462
3463 llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003464}
3465
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003466/// \brief \arg SLocMapI points at a chunk of a module that contains no
3467/// preprocessed entities or the entities it contains are not the ones we are
3468/// looking for. Find the next module that contains entities and return the ID
3469/// of the first entry.
3470PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
3471 GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
3472 ++SLocMapI;
3473 for (GlobalSLocOffsetMapType::const_iterator
3474 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003475 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003476 if (M.NumPreprocessedEntities)
3477 return getGlobalPreprocessedEntityID(M, M.BasePreprocessedEntityID);
3478 }
3479
3480 return getTotalNumPreprocessedEntities();
3481}
3482
3483namespace {
3484
3485template <unsigned PPEntityOffset::*PPLoc>
3486struct PPEntityComp {
3487 const ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003488 ModuleFile &M;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003489
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003490 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) { }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003491
Benjamin Kramer88df1252011-09-21 06:42:26 +00003492 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
3493 SourceLocation LHS = getLoc(L);
3494 SourceLocation RHS = getLoc(R);
3495 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3496 }
3497
3498 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003499 SourceLocation LHS = getLoc(L);
3500 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3501 }
3502
Benjamin Kramer88df1252011-09-21 06:42:26 +00003503 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003504 SourceLocation RHS = getLoc(R);
3505 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
3506 }
3507
3508 SourceLocation getLoc(const PPEntityOffset &PPE) const {
3509 return Reader.ReadSourceLocation(M, PPE.*PPLoc);
3510 }
3511};
3512
3513}
3514
3515/// \brief Returns the first preprocessed entity ID that ends after \arg BLoc.
3516PreprocessedEntityID
3517ASTReader::findBeginPreprocessedEntity(SourceLocation BLoc) const {
3518 if (SourceMgr.isLocalSourceLocation(BLoc))
3519 return getTotalNumPreprocessedEntities();
3520
3521 GlobalSLocOffsetMapType::const_iterator
3522 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3523 BLoc.getOffset());
3524 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3525 "Corrupted global sloc offset map");
3526
3527 if (SLocMapI->second->NumPreprocessedEntities == 0)
3528 return findNextPreprocessedEntity(SLocMapI);
3529
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003530 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003531 typedef const PPEntityOffset *pp_iterator;
3532 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3533 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
Argyrios Kyrtzidis4cd06342011-09-22 21:17:02 +00003534
3535 size_t Count = M.NumPreprocessedEntities;
3536 size_t Half;
3537 pp_iterator First = pp_begin;
3538 pp_iterator PPI;
3539
3540 // Do a binary search manually instead of using std::lower_bound because
3541 // The end locations of entities may be unordered (when a macro expansion
3542 // is inside another macro argument), but for this case it is not important
3543 // whether we get the first macro expansion or its containing macro.
3544 while (Count > 0) {
3545 Half = Count/2;
3546 PPI = First;
3547 std::advance(PPI, Half);
3548 if (SourceMgr.isBeforeInTranslationUnit(ReadSourceLocation(M, PPI->End),
3549 BLoc)){
3550 First = PPI;
3551 ++First;
3552 Count = Count - Half - 1;
3553 } else
3554 Count = Half;
3555 }
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003556
3557 if (PPI == pp_end)
3558 return findNextPreprocessedEntity(SLocMapI);
3559
3560 return getGlobalPreprocessedEntityID(M,
3561 M.BasePreprocessedEntityID + (PPI - pp_begin));
3562}
3563
3564/// \brief Returns the first preprocessed entity ID that begins after \arg ELoc.
3565PreprocessedEntityID
3566ASTReader::findEndPreprocessedEntity(SourceLocation ELoc) const {
3567 if (SourceMgr.isLocalSourceLocation(ELoc))
3568 return getTotalNumPreprocessedEntities();
3569
3570 GlobalSLocOffsetMapType::const_iterator
3571 SLocMapI = GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset -
3572 ELoc.getOffset());
3573 assert(SLocMapI != GlobalSLocOffsetMap.end() &&
3574 "Corrupted global sloc offset map");
3575
3576 if (SLocMapI->second->NumPreprocessedEntities == 0)
3577 return findNextPreprocessedEntity(SLocMapI);
3578
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003579 ModuleFile &M = *SLocMapI->second;
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00003580 typedef const PPEntityOffset *pp_iterator;
3581 pp_iterator pp_begin = M.PreprocessedEntityOffsets;
3582 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
3583 pp_iterator PPI =
3584 std::upper_bound(pp_begin, pp_end, ELoc,
3585 PPEntityComp<&PPEntityOffset::Begin>(*this, M));
3586
3587 if (PPI == pp_end)
3588 return findNextPreprocessedEntity(SLocMapI);
3589
3590 return getGlobalPreprocessedEntityID(M,
3591 M.BasePreprocessedEntityID + (PPI - pp_begin));
3592}
3593
3594/// \brief Returns a pair of [Begin, End) indices of preallocated
3595/// preprocessed entities that \arg Range encompasses.
3596std::pair<unsigned, unsigned>
3597 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
3598 if (Range.isInvalid())
3599 return std::make_pair(0,0);
3600 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
3601
3602 PreprocessedEntityID BeginID = findBeginPreprocessedEntity(Range.getBegin());
3603 PreprocessedEntityID EndID = findEndPreprocessedEntity(Range.getEnd());
3604 return std::make_pair(BeginID, EndID);
3605}
3606
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003607/// \brief Optionally returns true or false if the preallocated preprocessed
3608/// entity with index \arg Index came from file \arg FID.
3609llvm::Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
3610 FileID FID) {
3611 if (FID.isInvalid())
3612 return false;
3613
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003614 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
3615 ModuleFile &M = *PPInfo.first;
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +00003616 unsigned LocalIndex = PPInfo.second;
3617 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
3618
3619 SourceLocation Loc = ReadSourceLocation(M, PPOffs.Begin);
3620 if (Loc.isInvalid())
3621 return false;
3622
3623 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
3624 return true;
3625 else
3626 return false;
3627}
3628
Douglas Gregord10a3812011-08-25 18:14:34 +00003629namespace {
3630 /// \brief Visitor used to search for information about a header file.
3631 class HeaderFileInfoVisitor {
3632 ASTReader &Reader;
3633 const FileEntry *FE;
3634
3635 llvm::Optional<HeaderFileInfo> HFI;
3636
3637 public:
3638 HeaderFileInfoVisitor(ASTReader &Reader, const FileEntry *FE)
3639 : Reader(Reader), FE(FE) { }
3640
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003641 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003642 HeaderFileInfoVisitor *This
3643 = static_cast<HeaderFileInfoVisitor *>(UserData);
3644
3645 HeaderFileInfoTrait Trait(This->Reader, M,
3646 &This->Reader.getPreprocessor().getHeaderSearchInfo(),
3647 M.HeaderFileFrameworkStrings,
3648 This->FE->getName());
3649
3650 HeaderFileInfoLookupTable *Table
3651 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
3652 if (!Table)
3653 return false;
3654
3655 // Look in the on-disk hash table for an entry for this file name.
3656 HeaderFileInfoLookupTable::iterator Pos = Table->find(This->FE->getName(),
3657 &Trait);
3658 if (Pos == Table->end())
3659 return false;
3660
3661 This->HFI = *Pos;
3662 return true;
3663 }
3664
3665 llvm::Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
3666 };
3667}
3668
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003669HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
Douglas Gregord10a3812011-08-25 18:14:34 +00003670 HeaderFileInfoVisitor Visitor(*this, FE);
3671 ModuleMgr.visit(&HeaderFileInfoVisitor::visit, &Visitor);
3672 if (llvm::Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003673 if (Listener)
Douglas Gregord10a3812011-08-25 18:14:34 +00003674 Listener->ReadHeaderFileInfo(*HFI, FE->getUID());
3675 return *HFI;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003676 }
3677
3678 return HeaderFileInfo();
3679}
3680
David Blaikied6471f72011-09-25 23:23:43 +00003681void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00003682 for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003683 ModuleFile &F = *(*I);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003684 unsigned Idx = 0;
3685 while (Idx < F.PragmaDiagMappings.size()) {
3686 SourceLocation Loc = ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003687 Diag.DiagStates.push_back(*Diag.GetCurDiagState());
3688 Diag.DiagStatePoints.push_back(
3689 DiagnosticsEngine::DiagStatePoint(&Diag.DiagStates.back(),
3690 FullSourceLoc(Loc, SourceMgr)));
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003691 while (1) {
3692 assert(Idx < F.PragmaDiagMappings.size() &&
3693 "Invalid data, didn't find '-1' marking end of diag/map pairs");
3694 if (Idx >= F.PragmaDiagMappings.size()) {
3695 break; // Something is messed up but at least avoid infinite loop in
3696 // release build.
3697 }
3698 unsigned DiagID = F.PragmaDiagMappings[Idx++];
3699 if (DiagID == (unsigned)-1) {
3700 break; // no more diag/map pairs for this location.
3701 }
3702 diag::Mapping Map = (diag::Mapping)F.PragmaDiagMappings[Idx++];
Argyrios Kyrtzidis87429a02011-11-09 01:24:17 +00003703 DiagnosticMappingInfo MappingInfo = Diag.makeMappingInfo(Map, Loc);
3704 Diag.GetCurDiagState()->setMappingInfo(DiagID, MappingInfo);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00003705 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003706 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003707 }
3708}
3709
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003710/// \brief Get the correct cursor and offset for loading a type.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003711ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
Douglas Gregora119da02011-08-02 16:26:37 +00003712 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
Jonathan D. Turnere9b76c12011-07-20 21:31:32 +00003713 assert(I != GlobalTypeMap.end() && "Corrupted global type map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003714 ModuleFile *M = I->second;
Douglas Gregore3605012011-08-02 18:32:54 +00003715 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]);
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003716}
3717
3718/// \brief Read and return the type with the given index..
Douglas Gregor2cf26342009-04-09 22:27:44 +00003719///
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003720/// The index is the type ID, shifted and minus the number of predefs. This
3721/// routine actually reads the record corresponding to the type at the given
3722/// location. It is a helper routine for GetType, which deals with reading type
3723/// IDs.
Douglas Gregor393f2492011-07-22 00:38:23 +00003724QualType ASTReader::readTypeRecord(unsigned Index) {
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00003725 RecordLocation Loc = TypeCursorForIndex(Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00003726 llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
Sebastian Redl9137a522010-07-16 17:50:48 +00003727
Douglas Gregor0b748912009-04-14 21:18:50 +00003728 // Keep track of where we are in the stream, then jump back there
3729 // after reading this type.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003730 SavedStreamPosition SavedPosition(DeclsCursor);
Douglas Gregor0b748912009-04-14 21:18:50 +00003731
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00003732 ReadingKindTracker ReadingKind(Read_Type, *this);
Sebastian Redl27372b42010-08-11 18:52:41 +00003733
Douglas Gregord89275b2009-07-06 18:54:52 +00003734 // Note that we are loading a type record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00003735 Deserializing AType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00003736
Douglas Gregor393f2492011-07-22 00:38:23 +00003737 unsigned Idx = 0;
Sebastian Redlc3632732010-10-05 15:59:54 +00003738 DeclsCursor.JumpToBit(Loc.Offset);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003739 RecordData Record;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003740 unsigned Code = DeclsCursor.ReadCode();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003741 switch ((TypeCode)DeclsCursor.ReadRecord(Code, Record)) {
3742 case TYPE_EXT_QUAL: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003743 if (Record.size() != 2) {
3744 Error("Incorrect encoding of extended qualifier type");
3745 return QualType();
3746 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003747 QualType Base = readType(*Loc.F, Record, Idx);
3748 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]);
Douglas Gregor35942772011-09-09 21:34:22 +00003749 return Context.getQualifiedType(Base, Quals);
Douglas Gregor6d473962009-04-15 22:00:08 +00003750 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003751
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003752 case TYPE_COMPLEX: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003753 if (Record.size() != 1) {
3754 Error("Incorrect encoding of complex type");
3755 return QualType();
3756 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003757 QualType ElemType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003758 return Context.getComplexType(ElemType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003759 }
3760
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003761 case TYPE_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003762 if (Record.size() != 1) {
3763 Error("Incorrect encoding of pointer type");
3764 return QualType();
3765 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003766 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003767 return Context.getPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003768 }
3769
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003770 case TYPE_BLOCK_POINTER: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003771 if (Record.size() != 1) {
3772 Error("Incorrect encoding of block pointer type");
3773 return QualType();
3774 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003775 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003776 return Context.getBlockPointerType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003777 }
3778
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003779 case TYPE_LVALUE_REFERENCE: {
Richard Smithdf1550f2011-04-12 10:38:03 +00003780 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003781 Error("Incorrect encoding of lvalue reference type");
3782 return QualType();
3783 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003784 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003785 return Context.getLValueReferenceType(PointeeType, Record[1]);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003786 }
3787
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003788 case TYPE_RVALUE_REFERENCE: {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003789 if (Record.size() != 1) {
3790 Error("Incorrect encoding of rvalue reference type");
3791 return QualType();
3792 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003793 QualType PointeeType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003794 return Context.getRValueReferenceType(PointeeType);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003795 }
3796
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003797 case TYPE_MEMBER_POINTER: {
Argyrios Kyrtzidis240437b2010-07-02 11:55:15 +00003798 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003799 Error("Incorrect encoding of member pointer type");
3800 return QualType();
3801 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003802 QualType PointeeType = readType(*Loc.F, Record, Idx);
3803 QualType ClassType = readType(*Loc.F, Record, Idx);
Douglas Gregor1ab55e92010-12-10 17:03:06 +00003804 if (PointeeType.isNull() || ClassType.isNull())
3805 return QualType();
3806
Douglas Gregor35942772011-09-09 21:34:22 +00003807 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003808 }
3809
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003810 case TYPE_CONSTANT_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003811 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003812 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3813 unsigned IndexTypeQuals = Record[2];
3814 unsigned Idx = 3;
3815 llvm::APInt Size = ReadAPInt(Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003816 return Context.getConstantArrayType(ElementType, Size,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003817 ASM, IndexTypeQuals);
3818 }
3819
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003820 case TYPE_INCOMPLETE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003821 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003822 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3823 unsigned IndexTypeQuals = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003824 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003825 }
3826
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003827 case TYPE_VARIABLE_ARRAY: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003828 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregor0b748912009-04-14 21:18:50 +00003829 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1];
3830 unsigned IndexTypeQuals = Record[2];
Sebastian Redlc3632732010-10-05 15:59:54 +00003831 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]);
3832 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]);
Douglas Gregor35942772011-09-09 21:34:22 +00003833 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003834 ASM, IndexTypeQuals,
3835 SourceRange(LBLoc, RBLoc));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003836 }
3837
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003838 case TYPE_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003839 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003840 Error("incorrect encoding of vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003841 return QualType();
3842 }
3843
Douglas Gregor393f2492011-07-22 00:38:23 +00003844 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003845 unsigned NumElements = Record[1];
Bob Wilsone86d78c2010-11-10 21:56:12 +00003846 unsigned VecKind = Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003847 return Context.getVectorType(ElementType, NumElements,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003848 (VectorType::VectorKind)VecKind);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003849 }
3850
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003851 case TYPE_EXT_VECTOR: {
Chris Lattner788b0fd2010-06-23 06:00:24 +00003852 if (Record.size() != 3) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003853 Error("incorrect encoding of extended vector type in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003854 return QualType();
3855 }
3856
Douglas Gregor393f2492011-07-22 00:38:23 +00003857 QualType ElementType = readType(*Loc.F, Record, Idx);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003858 unsigned NumElements = Record[1];
Douglas Gregor35942772011-09-09 21:34:22 +00003859 return Context.getExtVectorType(ElementType, NumElements);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003860 }
3861
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003862 case TYPE_FUNCTION_NO_PROTO: {
John McCallf85e1932011-06-15 23:02:42 +00003863 if (Record.size() != 6) {
Douglas Gregora02b1472009-04-28 21:53:25 +00003864 Error("incorrect encoding of no-proto function type");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003865 return QualType();
3866 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003867 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCallf85e1932011-06-15 23:02:42 +00003868 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3],
3869 (CallingConv)Record[4], Record[5]);
Douglas Gregor35942772011-09-09 21:34:22 +00003870 return Context.getFunctionNoProtoType(ResultType, Info);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003871 }
3872
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003873 case TYPE_FUNCTION_PROTO: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003874 QualType ResultType = readType(*Loc.F, Record, Idx);
John McCalle23cf432010-12-14 08:05:40 +00003875
3876 FunctionProtoType::ExtProtoInfo EPI;
3877 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1],
Eli Friedmana49218e2011-04-09 08:18:08 +00003878 /*hasregparm*/ Record[2],
3879 /*regparm*/ Record[3],
John McCallf85e1932011-06-15 23:02:42 +00003880 static_cast<CallingConv>(Record[4]),
3881 /*produces*/ Record[5]);
John McCalle23cf432010-12-14 08:05:40 +00003882
John McCallf85e1932011-06-15 23:02:42 +00003883 unsigned Idx = 6;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003884 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00003885 SmallVector<QualType, 16> ParamTypes;
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003886 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003887 ParamTypes.push_back(readType(*Loc.F, Record, Idx));
John McCalle23cf432010-12-14 08:05:40 +00003888
3889 EPI.Variadic = Record[Idx++];
Richard Smitheefb3d52012-02-10 09:58:53 +00003890 EPI.HasTrailingReturn = Record[Idx++];
John McCalle23cf432010-12-14 08:05:40 +00003891 EPI.TypeQuals = Record[Idx++];
Douglas Gregorc938c162011-01-26 05:01:58 +00003892 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003893 ExceptionSpecificationType EST =
3894 static_cast<ExceptionSpecificationType>(Record[Idx++]);
3895 EPI.ExceptionSpecType = EST;
Douglas Gregorb0d06e22012-04-04 00:34:49 +00003896 SmallVector<QualType, 2> Exceptions;
Sebastian Redl60618fa2011-03-12 11:50:43 +00003897 if (EST == EST_Dynamic) {
3898 EPI.NumExceptions = Record[Idx++];
Sebastian Redl60618fa2011-03-12 11:50:43 +00003899 for (unsigned I = 0; I != EPI.NumExceptions; ++I)
Douglas Gregor393f2492011-07-22 00:38:23 +00003900 Exceptions.push_back(readType(*Loc.F, Record, Idx));
Sebastian Redl60618fa2011-03-12 11:50:43 +00003901 EPI.Exceptions = Exceptions.data();
3902 } else if (EST == EST_ComputedNoexcept) {
3903 EPI.NoexceptExpr = ReadExpr(*Loc.F);
Richard Smith7bb698a2012-04-21 17:47:47 +00003904 } else if (EST == EST_Uninstantiated) {
3905 EPI.ExceptionSpecDecl = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
3906 EPI.ExceptionSpecTemplate = ReadDeclAs<FunctionDecl>(*Loc.F, Record, Idx);
Sebastian Redl60618fa2011-03-12 11:50:43 +00003907 }
Douglas Gregor35942772011-09-09 21:34:22 +00003908 return Context.getFunctionType(ResultType, ParamTypes.data(), NumParams,
John McCalle23cf432010-12-14 08:05:40 +00003909 EPI);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003910 }
3911
Douglas Gregor409448c2011-07-21 22:35:25 +00003912 case TYPE_UNRESOLVED_USING: {
3913 unsigned Idx = 0;
Douglas Gregor35942772011-09-09 21:34:22 +00003914 return Context.getTypeDeclType(
Douglas Gregor409448c2011-07-21 22:35:25 +00003915 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx));
3916 }
3917
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003918 case TYPE_TYPEDEF: {
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003919 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003920 Error("incorrect encoding of typedef type");
3921 return QualType();
3922 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003923 unsigned Idx = 0;
3924 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00003925 QualType Canonical = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00003926 if (!Canonical.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00003927 Canonical = Context.getCanonicalType(Canonical);
3928 return Context.getTypedefType(Decl, Canonical);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00003929 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003930
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003931 case TYPE_TYPEOF_EXPR:
Douglas Gregor35942772011-09-09 21:34:22 +00003932 return Context.getTypeOfExprType(ReadExpr(*Loc.F));
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003933
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003934 case TYPE_TYPEOF: {
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003935 if (Record.size() != 1) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003936 Error("incorrect encoding of typeof(type) in AST file");
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003937 return QualType();
3938 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003939 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00003940 return Context.getTypeOfType(UnderlyingType);
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003941 }
Mike Stump1eb44332009-09-09 15:08:12 +00003942
Douglas Gregorf8af9822012-02-12 18:42:33 +00003943 case TYPE_DECLTYPE: {
3944 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
3945 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType);
3946 }
Anders Carlsson395b4752009-06-24 19:06:50 +00003947
Sean Huntca63c202011-05-24 22:41:36 +00003948 case TYPE_UNARY_TRANSFORM: {
Douglas Gregor393f2492011-07-22 00:38:23 +00003949 QualType BaseType = readType(*Loc.F, Record, Idx);
3950 QualType UnderlyingType = readType(*Loc.F, Record, Idx);
Sean Huntca63c202011-05-24 22:41:36 +00003951 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2];
Douglas Gregor35942772011-09-09 21:34:22 +00003952 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind);
Sean Huntca63c202011-05-24 22:41:36 +00003953 }
3954
Richard Smith34b41d92011-02-20 03:19:35 +00003955 case TYPE_AUTO:
Douglas Gregor35942772011-09-09 21:34:22 +00003956 return Context.getAutoType(readType(*Loc.F, Record, Idx));
Richard Smith34b41d92011-02-20 03:19:35 +00003957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003958 case TYPE_RECORD: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003959 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003960 Error("incorrect encoding of record type");
3961 return QualType();
3962 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003963 unsigned Idx = 0;
3964 bool IsDependent = Record[Idx++];
Douglas Gregor56ca8a92012-01-17 19:21:53 +00003965 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx);
3966 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl());
3967 QualType T = Context.getRecordType(RD);
John McCallf4c73712011-01-19 06:33:43 +00003968 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003969 return T;
3970 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00003971
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003972 case TYPE_ENUM: {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003973 if (Record.size() != 2) {
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00003974 Error("incorrect encoding of enum type");
3975 return QualType();
3976 }
Douglas Gregor409448c2011-07-21 22:35:25 +00003977 unsigned Idx = 0;
3978 bool IsDependent = Record[Idx++];
3979 QualType T
Douglas Gregor35942772011-09-09 21:34:22 +00003980 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx));
John McCallf4c73712011-01-19 06:33:43 +00003981 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00003982 return T;
3983 }
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003984
John McCall9d156a72011-01-06 01:58:22 +00003985 case TYPE_ATTRIBUTED: {
3986 if (Record.size() != 3) {
3987 Error("incorrect encoding of attributed type");
3988 return QualType();
3989 }
Douglas Gregor393f2492011-07-22 00:38:23 +00003990 QualType modifiedType = readType(*Loc.F, Record, Idx);
3991 QualType equivalentType = readType(*Loc.F, Record, Idx);
John McCall9d156a72011-01-06 01:58:22 +00003992 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]);
Douglas Gregor35942772011-09-09 21:34:22 +00003993 return Context.getAttributedType(kind, modifiedType, equivalentType);
John McCall9d156a72011-01-06 01:58:22 +00003994 }
3995
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003996 case TYPE_PAREN: {
3997 if (Record.size() != 1) {
3998 Error("incorrect encoding of paren type");
3999 return QualType();
4000 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004001 QualType InnerType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004002 return Context.getParenType(InnerType);
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004003 }
4004
Douglas Gregor7536dd52010-12-20 02:24:11 +00004005 case TYPE_PACK_EXPANSION: {
Douglas Gregorf9997a02011-02-01 15:24:58 +00004006 if (Record.size() != 2) {
Douglas Gregor7536dd52010-12-20 02:24:11 +00004007 Error("incorrect encoding of pack expansion type");
4008 return QualType();
4009 }
Douglas Gregor393f2492011-07-22 00:38:23 +00004010 QualType Pattern = readType(*Loc.F, Record, Idx);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004011 if (Pattern.isNull())
4012 return QualType();
Douglas Gregorcded4f62011-01-14 17:04:44 +00004013 llvm::Optional<unsigned> NumExpansions;
4014 if (Record[1])
4015 NumExpansions = Record[1] - 1;
Douglas Gregor35942772011-09-09 21:34:22 +00004016 return Context.getPackExpansionType(Pattern, NumExpansions);
Douglas Gregor7536dd52010-12-20 02:24:11 +00004017 }
4018
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004019 case TYPE_ELABORATED: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004020 unsigned Idx = 0;
4021 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004022 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004023 QualType NamedType = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004024 return Context.getElaboratedType(Keyword, NNS, NamedType);
John McCall7da24312009-09-05 00:15:47 +00004025 }
4026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004027 case TYPE_OBJC_INTERFACE: {
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004028 unsigned Idx = 0;
Douglas Gregor409448c2011-07-21 22:35:25 +00004029 ObjCInterfaceDecl *ItfD
4030 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00004031 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl());
John McCallc12c5bb2010-05-15 11:32:37 +00004032 }
4033
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004034 case TYPE_OBJC_OBJECT: {
John McCallc12c5bb2010-05-15 11:32:37 +00004035 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004036 QualType Base = readType(*Loc.F, Record, Idx);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004037 unsigned NumProtos = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004038 SmallVector<ObjCProtocolDecl*, 4> Protos;
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004039 for (unsigned I = 0; I != NumProtos; ++I)
Douglas Gregor409448c2011-07-21 22:35:25 +00004040 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004041 return Context.getObjCObjectType(Base, Protos.data(), NumProtos);
Chris Lattnerc6fa4452009-04-22 06:45:28 +00004042 }
Douglas Gregorb4e715b2009-04-13 20:46:52 +00004043
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004044 case TYPE_OBJC_OBJECT_POINTER: {
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004045 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004046 QualType Pointee = readType(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004047 return Context.getObjCObjectPointerType(Pointee);
Chris Lattnerd7a3fcd2009-04-22 06:40:03 +00004048 }
Argyrios Kyrtzidis24fab412009-09-29 19:42:55 +00004049
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004050 case TYPE_SUBST_TEMPLATE_TYPE_PARM: {
John McCall49a832b2009-10-18 09:09:24 +00004051 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004052 QualType Parm = readType(*Loc.F, Record, Idx);
4053 QualType Replacement = readType(*Loc.F, Record, Idx);
John McCall49a832b2009-10-18 09:09:24 +00004054 return
Douglas Gregor35942772011-09-09 21:34:22 +00004055 Context.getSubstTemplateTypeParmType(cast<TemplateTypeParmType>(Parm),
John McCall49a832b2009-10-18 09:09:24 +00004056 Replacement);
4057 }
John McCall3cb0ebd2010-03-10 03:28:59 +00004058
Douglas Gregorc3069d62011-01-14 02:55:32 +00004059 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: {
4060 unsigned Idx = 0;
Douglas Gregor393f2492011-07-22 00:38:23 +00004061 QualType Parm = readType(*Loc.F, Record, Idx);
Douglas Gregorc3069d62011-01-14 02:55:32 +00004062 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004063 return Context.getSubstTemplateTypeParmPackType(
Douglas Gregorc3069d62011-01-14 02:55:32 +00004064 cast<TemplateTypeParmType>(Parm),
4065 ArgPack);
4066 }
4067
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004068 case TYPE_INJECTED_CLASS_NAME: {
Douglas Gregor409448c2011-07-21 22:35:25 +00004069 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004070 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004071 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004072 // for AST reading, too much interdependencies.
Argyrios Kyrtzidis43921b52010-07-02 11:55:20 +00004073 return
Douglas Gregor35942772011-09-09 21:34:22 +00004074 QualType(new (Context, TypeAlignment) InjectedClassNameType(D, TST), 0);
John McCall3cb0ebd2010-03-10 03:28:59 +00004075 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004076
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004077 case TYPE_TEMPLATE_TYPE_PARM: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004078 unsigned Idx = 0;
4079 unsigned Depth = Record[Idx++];
4080 unsigned Index = Record[Idx++];
4081 bool Pack = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004082 TemplateTypeParmDecl *D
4083 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00004084 return Context.getTemplateTypeParmType(Depth, Index, Pack, D);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004085 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004086
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004087 case TYPE_DEPENDENT_NAME: {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004088 unsigned Idx = 0;
4089 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004090 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004091 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004092 QualType Canon = readType(*Loc.F, Record, Idx);
Douglas Gregor32adc8b2010-10-26 00:51:02 +00004093 if (!Canon.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004094 Canon = Context.getCanonicalType(Canon);
4095 return Context.getDependentNameType(Keyword, NNS, Name, Canon);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00004096 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004097
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004098 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004099 unsigned Idx = 0;
4100 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00004101 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx);
Douglas Gregor95eab172011-07-28 20:55:49 +00004102 const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx);
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004103 unsigned NumArgs = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00004104 SmallVector<TemplateArgument, 8> Args;
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004105 Args.reserve(NumArgs);
4106 while (NumArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00004107 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00004108 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name,
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +00004109 Args.size(), Args.data());
4110 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004111
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004112 case TYPE_DEPENDENT_SIZED_ARRAY: {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004113 unsigned Idx = 0;
4114
4115 // ArrayType
Douglas Gregor393f2492011-07-22 00:38:23 +00004116 QualType ElementType = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004117 ArrayType::ArraySizeModifier ASM
4118 = (ArrayType::ArraySizeModifier)Record[Idx++];
4119 unsigned IndexTypeQuals = Record[Idx++];
4120
4121 // DependentSizedArrayType
Sebastian Redlc3632732010-10-05 15:59:54 +00004122 Expr *NumElts = ReadExpr(*Loc.F);
4123 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx);
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004124
Douglas Gregor35942772011-09-09 21:34:22 +00004125 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM,
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +00004126 IndexTypeQuals, Brackets);
4127 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004128
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004129 case TYPE_TEMPLATE_SPECIALIZATION: {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004130 unsigned Idx = 0;
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004131 bool IsDependent = Record[Idx++];
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004132 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx);
Chris Lattner5f9e2722011-07-23 10:55:15 +00004133 SmallVector<TemplateArgument, 8> Args;
Sebastian Redlc3632732010-10-05 15:59:54 +00004134 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00004135 QualType Underlying = readType(*Loc.F, Record, Idx);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004136 QualType T;
Richard Smith3e4c6c42011-05-05 21:57:07 +00004137 if (Underlying.isNull())
Douglas Gregor35942772011-09-09 21:34:22 +00004138 T = Context.getCanonicalTemplateSpecializationType(Name, Args.data(),
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004139 Args.size());
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +00004140 else
Douglas Gregor35942772011-09-09 21:34:22 +00004141 T = Context.getTemplateSpecializationType(Name, Args.data(),
Richard Smith3e4c6c42011-05-05 21:57:07 +00004142 Args.size(), Underlying);
John McCallf4c73712011-01-19 06:33:43 +00004143 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent);
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +00004144 return T;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00004145 }
Eli Friedmanb001de72011-10-06 23:00:33 +00004146
4147 case TYPE_ATOMIC: {
4148 if (Record.size() != 1) {
4149 Error("Incorrect encoding of atomic type");
4150 return QualType();
4151 }
4152 QualType ValueType = readType(*Loc.F, Record, Idx);
4153 return Context.getAtomicType(ValueType);
4154 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00004155 }
David Blaikie7530c032012-01-17 06:56:22 +00004156 llvm_unreachable("Invalid TypeCode!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00004157}
4158
Sebastian Redlc3632732010-10-05 15:59:54 +00004159class clang::TypeLocReader : public TypeLocVisitor<TypeLocReader> {
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004160 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004161 ModuleFile &F;
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004162 const ASTReader::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +00004163 unsigned &Idx;
4164
Sebastian Redlc3632732010-10-05 15:59:54 +00004165 SourceLocation ReadSourceLocation(const ASTReader::RecordData &R,
4166 unsigned &I) {
4167 return Reader.ReadSourceLocation(F, R, I);
4168 }
4169
Douglas Gregor409448c2011-07-21 22:35:25 +00004170 template<typename T>
4171 T *ReadDeclAs(const ASTReader::RecordData &Record, unsigned &Idx) {
4172 return Reader.ReadDeclAs<T>(F, Record, Idx);
4173 }
4174
John McCalla1ee0c52009-10-16 21:56:05 +00004175public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004176 TypeLocReader(ASTReader &Reader, ModuleFile &F,
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004177 const ASTReader::RecordData &Record, unsigned &Idx)
Benjamin Kramerfacde172012-06-06 17:32:50 +00004178 : Reader(Reader), F(F), Record(Record), Idx(Idx)
Sebastian Redlc3632732010-10-05 15:59:54 +00004179 { }
John McCalla1ee0c52009-10-16 21:56:05 +00004180
John McCall51bd8032009-10-18 01:05:36 +00004181 // We want compile-time assurance that we've enumerated all of
4182 // these, so unfortunately we have to declare them first, then
4183 // define them out-of-line.
4184#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +00004185#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +00004186 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004187#include "clang/AST/TypeLocNodes.def"
4188
John McCall51bd8032009-10-18 01:05:36 +00004189 void VisitFunctionTypeLoc(FunctionTypeLoc);
4190 void VisitArrayTypeLoc(ArrayTypeLoc);
John McCalla1ee0c52009-10-16 21:56:05 +00004191};
4192
John McCall51bd8032009-10-18 01:05:36 +00004193void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
John McCalla1ee0c52009-10-16 21:56:05 +00004194 // nothing to do
4195}
John McCall51bd8032009-10-18 01:05:36 +00004196void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004197 TL.setBuiltinLoc(ReadSourceLocation(Record, Idx));
Douglas Gregorddf889a2010-01-18 18:04:31 +00004198 if (TL.needsExtraLocalData()) {
4199 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++]));
4200 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++]));
4201 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++]));
4202 TL.setModeAttr(Record[Idx++]);
4203 }
John McCalla1ee0c52009-10-16 21:56:05 +00004204}
John McCall51bd8032009-10-18 01:05:36 +00004205void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004206 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004207}
John McCall51bd8032009-10-18 01:05:36 +00004208void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004209 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004210}
John McCall51bd8032009-10-18 01:05:36 +00004211void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004212 TL.setCaretLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004213}
John McCall51bd8032009-10-18 01:05:36 +00004214void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004215 TL.setAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004216}
John McCall51bd8032009-10-18 01:05:36 +00004217void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004218 TL.setAmpAmpLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004219}
John McCall51bd8032009-10-18 01:05:36 +00004220void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004221 TL.setStarLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00004222 TL.setClassTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004223}
John McCall51bd8032009-10-18 01:05:36 +00004224void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004225 TL.setLBracketLoc(ReadSourceLocation(Record, Idx));
4226 TL.setRBracketLoc(ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004227 if (Record[Idx++])
Sebastian Redlc3632732010-10-05 15:59:54 +00004228 TL.setSizeExpr(Reader.ReadExpr(F));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004229 else
John McCall51bd8032009-10-18 01:05:36 +00004230 TL.setSizeExpr(0);
4231}
4232void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
4233 VisitArrayTypeLoc(TL);
4234}
4235void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
4236 VisitArrayTypeLoc(TL);
4237}
4238void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
4239 VisitArrayTypeLoc(TL);
4240}
4241void TypeLocReader::VisitDependentSizedArrayTypeLoc(
4242 DependentSizedArrayTypeLoc TL) {
4243 VisitArrayTypeLoc(TL);
4244}
4245void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
4246 DependentSizedExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004247 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004248}
4249void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004250 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004251}
4252void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004253 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004254}
4255void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +00004256 TL.setLocalRangeBegin(ReadSourceLocation(Record, Idx));
4257 TL.setLocalRangeEnd(ReadSourceLocation(Record, Idx));
Douglas Gregordab60ad2010-10-01 18:44:50 +00004258 TL.setTrailingReturn(Record[Idx++]);
John McCall51bd8032009-10-18 01:05:36 +00004259 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) {
Douglas Gregor409448c2011-07-21 22:35:25 +00004260 TL.setArg(i, ReadDeclAs<ParmVarDecl>(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004261 }
4262}
4263void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
4264 VisitFunctionTypeLoc(TL);
4265}
4266void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
4267 VisitFunctionTypeLoc(TL);
4268}
John McCalled976492009-12-04 22:46:56 +00004269void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004270 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCalled976492009-12-04 22:46:56 +00004271}
John McCall51bd8032009-10-18 01:05:36 +00004272void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004273 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004274}
4275void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004276 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4277 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4278 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004279}
4280void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004281 TL.setTypeofLoc(ReadSourceLocation(Record, Idx));
4282 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4283 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4284 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004285}
4286void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004287 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004288}
Sean Huntca63c202011-05-24 22:41:36 +00004289void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
4290 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4291 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4292 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4293 TL.setUnderlyingTInfo(Reader.GetTypeSourceInfo(F, Record, Idx));
4294}
Richard Smith34b41d92011-02-20 03:19:35 +00004295void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
4296 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4297}
John McCall51bd8032009-10-18 01:05:36 +00004298void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004299 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004300}
4301void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004302 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004303}
John McCall9d156a72011-01-06 01:58:22 +00004304void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
4305 TL.setAttrNameLoc(ReadSourceLocation(Record, Idx));
4306 if (TL.hasAttrOperand()) {
4307 SourceRange range;
4308 range.setBegin(ReadSourceLocation(Record, Idx));
4309 range.setEnd(ReadSourceLocation(Record, Idx));
4310 TL.setAttrOperandParensRange(range);
4311 }
4312 if (TL.hasAttrExprOperand()) {
4313 if (Record[Idx++])
4314 TL.setAttrExprOperand(Reader.ReadExpr(F));
4315 else
4316 TL.setAttrExprOperand(0);
4317 } else if (TL.hasAttrEnumOperand())
4318 TL.setAttrEnumOperandLoc(ReadSourceLocation(Record, Idx));
4319}
John McCall51bd8032009-10-18 01:05:36 +00004320void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004321 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004322}
John McCall49a832b2009-10-18 09:09:24 +00004323void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
4324 SubstTemplateTypeParmTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004325 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall49a832b2009-10-18 09:09:24 +00004326}
Douglas Gregorc3069d62011-01-14 02:55:32 +00004327void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
4328 SubstTemplateTypeParmPackTypeLoc TL) {
4329 TL.setNameLoc(ReadSourceLocation(Record, Idx));
4330}
John McCall51bd8032009-10-18 01:05:36 +00004331void TypeLocReader::VisitTemplateSpecializationTypeLoc(
4332 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004333 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004334 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
4335 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4336 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall833ca992009-10-29 08:12:44 +00004337 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
4338 TL.setArgLocInfo(i,
Sebastian Redlc3632732010-10-05 15:59:54 +00004339 Reader.GetTemplateArgumentLocInfo(F,
4340 TL.getTypePtr()->getArg(i).getKind(),
4341 Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004342}
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004343void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
4344 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4345 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4346}
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004347void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004348 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor9e876872011-03-01 18:12:44 +00004349 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004350}
John McCall3cb0ebd2010-03-10 03:28:59 +00004351void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004352 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall3cb0ebd2010-03-10 03:28:59 +00004353}
Douglas Gregor4714c122010-03-31 17:34:00 +00004354void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +00004355 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor2494dd02011-03-01 01:34:45 +00004356 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004357 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004358}
John McCall33500952010-06-11 00:33:02 +00004359void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
4360 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004361 TL.setElaboratedKeywordLoc(ReadSourceLocation(Record, Idx));
Douglas Gregor94fdffa2011-03-01 20:11:18 +00004362 TL.setQualifierLoc(Reader.ReadNestedNameSpecifierLoc(F, Record, Idx));
Abramo Bagnara66581d42012-02-06 22:45:07 +00004363 TL.setTemplateKeywordLoc(ReadSourceLocation(Record, Idx));
Abramo Bagnara55d23c92012-02-06 14:41:24 +00004364 TL.setTemplateNameLoc(ReadSourceLocation(Record, Idx));
Sebastian Redlc3632732010-10-05 15:59:54 +00004365 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4366 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004367 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
4368 TL.setArgLocInfo(I,
Sebastian Redlc3632732010-10-05 15:59:54 +00004369 Reader.GetTemplateArgumentLocInfo(F,
4370 TL.getTypePtr()->getArg(I).getKind(),
4371 Record, Idx));
John McCall33500952010-06-11 00:33:02 +00004372}
Douglas Gregor7536dd52010-12-20 02:24:11 +00004373void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
4374 TL.setEllipsisLoc(ReadSourceLocation(Record, Idx));
4375}
John McCall51bd8032009-10-18 01:05:36 +00004376void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004377 TL.setNameLoc(ReadSourceLocation(Record, Idx));
John McCallc12c5bb2010-05-15 11:32:37 +00004378}
4379void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
4380 TL.setHasBaseTypeAsWritten(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00004381 TL.setLAngleLoc(ReadSourceLocation(Record, Idx));
4382 TL.setRAngleLoc(ReadSourceLocation(Record, Idx));
John McCall51bd8032009-10-18 01:05:36 +00004383 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Sebastian Redlc3632732010-10-05 15:59:54 +00004384 TL.setProtocolLoc(i, ReadSourceLocation(Record, Idx));
John McCalla1ee0c52009-10-16 21:56:05 +00004385}
John McCall54e14c42009-10-22 22:37:11 +00004386void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004387 TL.setStarLoc(ReadSourceLocation(Record, Idx));
John McCall54e14c42009-10-22 22:37:11 +00004388}
Eli Friedmanb001de72011-10-06 23:00:33 +00004389void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
4390 TL.setKWLoc(ReadSourceLocation(Record, Idx));
4391 TL.setLParenLoc(ReadSourceLocation(Record, Idx));
4392 TL.setRParenLoc(ReadSourceLocation(Record, Idx));
4393}
John McCalla1ee0c52009-10-16 21:56:05 +00004394
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004395TypeSourceInfo *ASTReader::GetTypeSourceInfo(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004396 const RecordData &Record,
John McCalla1ee0c52009-10-16 21:56:05 +00004397 unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004398 QualType InfoTy = readType(F, Record, Idx);
John McCalla1ee0c52009-10-16 21:56:05 +00004399 if (InfoTy.isNull())
4400 return 0;
4401
Douglas Gregor35942772011-09-09 21:34:22 +00004402 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
Sebastian Redlc3632732010-10-05 15:59:54 +00004403 TypeLocReader TLR(*this, F, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +00004404 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
John McCalla1ee0c52009-10-16 21:56:05 +00004405 TLR.Visit(TL);
John McCalla93c9342009-12-07 02:54:59 +00004406 return TInfo;
John McCalla1ee0c52009-10-16 21:56:05 +00004407}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004408
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004409QualType ASTReader::GetType(TypeID ID) {
John McCall0953e762009-09-24 19:53:00 +00004410 unsigned FastQuals = ID & Qualifiers::FastMask;
4411 unsigned Index = ID >> Qualifiers::FastWidth;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004412
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004413 if (Index < NUM_PREDEF_TYPE_IDS) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004414 QualType T;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004415 switch ((PredefinedTypeIDs)Index) {
4416 case PREDEF_TYPE_NULL_ID: return QualType();
Douglas Gregor35942772011-09-09 21:34:22 +00004417 case PREDEF_TYPE_VOID_ID: T = Context.VoidTy; break;
4418 case PREDEF_TYPE_BOOL_ID: T = Context.BoolTy; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004419
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004420 case PREDEF_TYPE_CHAR_U_ID:
4421 case PREDEF_TYPE_CHAR_S_ID:
Douglas Gregor2cf26342009-04-09 22:27:44 +00004422 // FIXME: Check that the signedness of CharTy is correct!
Douglas Gregor35942772011-09-09 21:34:22 +00004423 T = Context.CharTy;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004424 break;
4425
Douglas Gregor35942772011-09-09 21:34:22 +00004426 case PREDEF_TYPE_UCHAR_ID: T = Context.UnsignedCharTy; break;
4427 case PREDEF_TYPE_USHORT_ID: T = Context.UnsignedShortTy; break;
4428 case PREDEF_TYPE_UINT_ID: T = Context.UnsignedIntTy; break;
4429 case PREDEF_TYPE_ULONG_ID: T = Context.UnsignedLongTy; break;
4430 case PREDEF_TYPE_ULONGLONG_ID: T = Context.UnsignedLongLongTy; break;
4431 case PREDEF_TYPE_UINT128_ID: T = Context.UnsignedInt128Ty; break;
4432 case PREDEF_TYPE_SCHAR_ID: T = Context.SignedCharTy; break;
4433 case PREDEF_TYPE_WCHAR_ID: T = Context.WCharTy; break;
4434 case PREDEF_TYPE_SHORT_ID: T = Context.ShortTy; break;
4435 case PREDEF_TYPE_INT_ID: T = Context.IntTy; break;
4436 case PREDEF_TYPE_LONG_ID: T = Context.LongTy; break;
4437 case PREDEF_TYPE_LONGLONG_ID: T = Context.LongLongTy; break;
4438 case PREDEF_TYPE_INT128_ID: T = Context.Int128Ty; break;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00004439 case PREDEF_TYPE_HALF_ID: T = Context.HalfTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004440 case PREDEF_TYPE_FLOAT_ID: T = Context.FloatTy; break;
4441 case PREDEF_TYPE_DOUBLE_ID: T = Context.DoubleTy; break;
4442 case PREDEF_TYPE_LONGDOUBLE_ID: T = Context.LongDoubleTy; break;
4443 case PREDEF_TYPE_OVERLOAD_ID: T = Context.OverloadTy; break;
4444 case PREDEF_TYPE_BOUND_MEMBER: T = Context.BoundMemberTy; break;
John McCall3c3b7f92011-10-25 17:37:35 +00004445 case PREDEF_TYPE_PSEUDO_OBJECT: T = Context.PseudoObjectTy; break;
Douglas Gregor35942772011-09-09 21:34:22 +00004446 case PREDEF_TYPE_DEPENDENT_ID: T = Context.DependentTy; break;
4447 case PREDEF_TYPE_UNKNOWN_ANY: T = Context.UnknownAnyTy; break;
4448 case PREDEF_TYPE_NULLPTR_ID: T = Context.NullPtrTy; break;
4449 case PREDEF_TYPE_CHAR16_ID: T = Context.Char16Ty; break;
4450 case PREDEF_TYPE_CHAR32_ID: T = Context.Char32Ty; break;
4451 case PREDEF_TYPE_OBJC_ID: T = Context.ObjCBuiltinIdTy; break;
4452 case PREDEF_TYPE_OBJC_CLASS: T = Context.ObjCBuiltinClassTy; break;
4453 case PREDEF_TYPE_OBJC_SEL: T = Context.ObjCBuiltinSelTy; break;
4454 case PREDEF_TYPE_AUTO_DEDUCT: T = Context.getAutoDeductType(); break;
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004455
4456 case PREDEF_TYPE_AUTO_RREF_DEDUCT:
Douglas Gregor35942772011-09-09 21:34:22 +00004457 T = Context.getAutoRRefDeductType();
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004458 break;
John McCall0ddaeb92011-10-17 18:09:15 +00004459
4460 case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
4461 T = Context.ARCUnbridgedCastTy;
4462 break;
4463
Meador Ingefb40e3f2012-07-01 15:57:25 +00004464 case PREDEF_TYPE_VA_LIST_TAG:
4465 T = Context.getVaListTagType();
4466 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004467 }
4468
4469 assert(!T.isNull() && "Unknown predefined type");
John McCall0953e762009-09-24 19:53:00 +00004470 return T.withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004471 }
4472
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004473 Index -= NUM_PREDEF_TYPE_IDS;
Sebastian Redlaaec0aa2010-07-20 22:37:49 +00004474 assert(Index < TypesLoaded.size() && "Type index out-of-range");
Sebastian Redl07a353c2010-07-14 20:26:45 +00004475 if (TypesLoaded[Index].isNull()) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004476 TypesLoaded[Index] = readTypeRecord(Index);
Douglas Gregor97475832010-10-05 18:37:06 +00004477 if (TypesLoaded[Index].isNull())
4478 return QualType();
4479
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004480 TypesLoaded[Index]->setFromAST();
Sebastian Redl30c514c2010-07-14 23:45:08 +00004481 if (DeserializationListener)
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004482 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
Sebastian Redl1476ed42010-07-16 16:36:56 +00004483 TypesLoaded[Index]);
Sebastian Redl07a353c2010-07-14 20:26:45 +00004484 }
Mike Stump1eb44332009-09-09 15:08:12 +00004485
John McCall0953e762009-09-24 19:53:00 +00004486 return TypesLoaded[Index].withFastQualifiers(FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004487}
4488
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004489QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
Douglas Gregor393f2492011-07-22 00:38:23 +00004490 return GetType(getGlobalTypeID(F, LocalID));
4491}
4492
4493serialization::TypeID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004494ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregora119da02011-08-02 16:26:37 +00004495 unsigned FastQuals = LocalID & Qualifiers::FastMask;
4496 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
4497
4498 if (LocalIndex < NUM_PREDEF_TYPE_IDS)
4499 return LocalID;
4500
4501 ContinuousRangeMap<uint32_t, int, 2>::iterator I
4502 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
4503 assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
4504
4505 unsigned GlobalIndex = LocalIndex + I->second;
4506 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
4507}
4508
John McCall833ca992009-10-29 08:12:44 +00004509TemplateArgumentLocInfo
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004510ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00004511 TemplateArgument::ArgKind Kind,
John McCall833ca992009-10-29 08:12:44 +00004512 const RecordData &Record,
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004513 unsigned &Index) {
John McCall833ca992009-10-29 08:12:44 +00004514 switch (Kind) {
4515 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00004516 return ReadExpr(F);
John McCall833ca992009-10-29 08:12:44 +00004517 case TemplateArgument::Type:
Sebastian Redlc3632732010-10-05 15:59:54 +00004518 return GetTypeSourceInfo(F, Record, Index);
Douglas Gregor788cd062009-11-11 01:00:40 +00004519 case TemplateArgument::Template: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004520 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4521 Index);
Sebastian Redlc3632732010-10-05 15:59:54 +00004522 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004523 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregora7fc9012011-01-05 18:58:31 +00004524 SourceLocation());
4525 }
4526 case TemplateArgument::TemplateExpansion: {
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004527 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record,
4528 Index);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004529 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004530 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index);
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004531 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
Douglas Gregorba68eca2011-01-05 17:40:24 +00004532 EllipsisLoc);
Douglas Gregor788cd062009-11-11 01:00:40 +00004533 }
John McCall833ca992009-10-29 08:12:44 +00004534 case TemplateArgument::Null:
4535 case TemplateArgument::Integral:
4536 case TemplateArgument::Declaration:
4537 case TemplateArgument::Pack:
4538 return TemplateArgumentLocInfo();
4539 }
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00004540 llvm_unreachable("unexpected template argument loc");
John McCall833ca992009-10-29 08:12:44 +00004541}
4542
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004543TemplateArgumentLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004544ASTReader::ReadTemplateArgumentLoc(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00004545 const RecordData &Record, unsigned &Index) {
Sebastian Redlc3632732010-10-05 15:59:54 +00004546 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004547
4548 if (Arg.getKind() == TemplateArgument::Expression) {
4549 if (Record[Index++]) // bool InfoHasSameExpr.
4550 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
4551 }
Sebastian Redlc3632732010-10-05 15:59:54 +00004552 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(),
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00004553 Record, Index));
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004554}
4555
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004556Decl *ASTReader::GetExternalDecl(uint32_t ID) {
John McCall76bd1f32010-06-01 09:23:16 +00004557 return GetDecl(ID);
4558}
4559
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004560uint64_t ASTReader::readCXXBaseSpecifiers(ModuleFile &M, const RecordData &Record,
Douglas Gregore92b8a12011-08-04 00:01:48 +00004561 unsigned &Idx){
4562 if (Idx >= Record.size())
Douglas Gregor7c789c12010-10-29 22:39:52 +00004563 return 0;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004564
Douglas Gregore92b8a12011-08-04 00:01:48 +00004565 unsigned LocalID = Record[Idx++];
4566 return getGlobalBitOffset(M, M.CXXBaseSpecifiersOffsets[LocalID - 1]);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004567}
4568
4569CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004570 RecordLocation Loc = getLocalBitOffset(Offset);
4571 llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004572 SavedStreamPosition SavedPosition(Cursor);
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004573 Cursor.JumpToBit(Loc.Offset);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004574 ReadingKindTracker ReadingKind(Read_Decl, *this);
4575 RecordData Record;
4576 unsigned Code = Cursor.ReadCode();
4577 unsigned RecCode = Cursor.ReadRecord(Code, Record);
4578 if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
4579 Error("Malformed AST file: missing C++ base specifiers");
4580 return 0;
4581 }
4582
4583 unsigned Idx = 0;
4584 unsigned NumBases = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00004585 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004586 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
4587 for (unsigned I = 0; I != NumBases; ++I)
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004588 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004589 return Bases;
4590}
4591
Douglas Gregor409448c2011-07-21 22:35:25 +00004592serialization::DeclID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004593ASTReader::getGlobalDeclID(ModuleFile &F, unsigned LocalID) const {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004594 if (LocalID < NUM_PREDEF_DECL_IDS)
Douglas Gregor496c7092011-08-03 15:48:04 +00004595 return LocalID;
4596
4597 ContinuousRangeMap<uint32_t, int, 2>::iterator I
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004598 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
Douglas Gregor496c7092011-08-03 15:48:04 +00004599 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
4600
4601 return LocalID + I->second;
Douglas Gregor409448c2011-07-21 22:35:25 +00004602}
4603
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004604bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004605 ModuleFile &M) const {
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004606 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(ID);
4607 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4608 return &M == I->second;
4609}
4610
Douglas Gregorcff9f262012-01-27 01:47:08 +00004611ModuleFile *ASTReader::getOwningModuleFile(Decl *D) {
4612 if (!D->isFromASTFile())
4613 return 0;
4614 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
4615 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4616 return I->second;
4617}
4618
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004619SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
4620 if (ID < NUM_PREDEF_DECL_IDS)
4621 return SourceLocation();
4622
4623 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4624
4625 if (Index > DeclsLoaded.size()) {
4626 Error("declaration ID out-of-range for AST file");
4627 return SourceLocation();
4628 }
4629
4630 if (Decl *D = DeclsLoaded[Index])
4631 return D->getLocation();
4632
4633 unsigned RawLocation = 0;
4634 RecordLocation Rec = DeclCursorForID(ID, RawLocation);
4635 return ReadSourceLocation(*Rec.F, RawLocation);
4636}
4637
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004638Decl *ASTReader::GetDecl(DeclID ID) {
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004639 if (ID < NUM_PREDEF_DECL_IDS) {
4640 switch ((PredefinedDeclIDs)ID) {
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004641 case PREDEF_DECL_NULL_ID:
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004642 return 0;
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004643
4644 case PREDEF_DECL_TRANSLATION_UNIT_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004645 return Context.getTranslationUnitDecl();
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004646
4647 case PREDEF_DECL_OBJC_ID_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004648 return Context.getObjCIdDecl();
Douglas Gregor79d67262011-08-12 05:59:41 +00004649
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004650 case PREDEF_DECL_OBJC_SEL_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004651 return Context.getObjCSelDecl();
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004652
Douglas Gregor79d67262011-08-12 05:59:41 +00004653 case PREDEF_DECL_OBJC_CLASS_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004654 return Context.getObjCClassDecl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004655
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004656 case PREDEF_DECL_OBJC_PROTOCOL_ID:
4657 return Context.getObjCProtocolDecl();
4658
Douglas Gregor772eeae2011-08-12 06:49:56 +00004659 case PREDEF_DECL_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004660 return Context.getInt128Decl();
Douglas Gregor772eeae2011-08-12 06:49:56 +00004661
4662 case PREDEF_DECL_UNSIGNED_INT_128_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004663 return Context.getUInt128Decl();
Douglas Gregore97179c2011-09-08 01:46:34 +00004664
4665 case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
Douglas Gregor35942772011-09-09 21:34:22 +00004666 return Context.getObjCInstanceTypeDecl();
Meador Ingec5613b22012-06-16 03:34:49 +00004667
4668 case PREDEF_DECL_BUILTIN_VA_LIST_ID:
4669 return Context.getBuiltinVaListDecl();
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004670 }
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00004671 }
4672
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004673 unsigned Index = ID - NUM_PREDEF_DECL_IDS;
4674
Richard Smith2fbf3732011-12-20 04:39:57 +00004675 if (Index >= DeclsLoaded.size()) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004676 assert(0 && "declaration ID out-of-range for AST file");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00004677 Error("declaration ID out-of-range for AST file");
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004678 return 0;
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004679 }
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004680
Douglas Gregorfd002a72011-12-16 22:37:11 +00004681 if (!DeclsLoaded[Index]) {
Douglas Gregor496c7092011-08-03 15:48:04 +00004682 ReadDeclRecord(ID);
Sebastian Redl30c514c2010-07-14 23:45:08 +00004683 if (DeserializationListener)
4684 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
4685 }
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004686
4687 return DeclsLoaded[Index];
Douglas Gregor2cf26342009-04-09 22:27:44 +00004688}
4689
Douglas Gregora1be2782011-12-17 23:38:30 +00004690DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
4691 DeclID GlobalID) {
4692 if (GlobalID < NUM_PREDEF_DECL_IDS)
4693 return GlobalID;
4694
4695 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
4696 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
4697 ModuleFile *Owner = I->second;
4698
4699 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
4700 = M.GlobalToLocalDeclIDs.find(Owner);
4701 if (Pos == M.GlobalToLocalDeclIDs.end())
4702 return 0;
4703
4704 return GlobalID - Owner->BaseDeclID + Pos->second;
4705}
4706
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004707serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00004708 const RecordData &Record,
4709 unsigned &Idx) {
4710 if (Idx >= Record.size()) {
4711 Error("Corrupted AST file");
4712 return 0;
4713 }
4714
4715 return getGlobalDeclID(F, Record[Idx++]);
4716}
4717
Chris Lattner887e2b32009-04-27 05:46:25 +00004718/// \brief Resolve the offset of a statement into a statement.
4719///
4720/// This operation will read a new statement from the external
4721/// source each time it is called, and is meant to be used via a
4722/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004723Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00004724 // Switch case IDs are per Decl.
4725 ClearSwitchCaseIDs();
4726
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00004727 // Offset here is a global offset across the entire chain.
Douglas Gregor8f1231b2011-07-22 06:10:01 +00004728 RecordLocation Loc = getLocalBitOffset(Offset);
4729 Loc.F->DeclsCursor.JumpToBit(Loc.Offset);
4730 return ReadStmtFromStream(*Loc.F);
Douglas Gregor250fc9c2009-04-18 00:07:54 +00004731}
4732
Douglas Gregor851c75a2011-08-24 21:27:34 +00004733namespace {
4734 class FindExternalLexicalDeclsVisitor {
4735 ASTReader &Reader;
4736 const DeclContext *DC;
4737 bool (*isKindWeWant)(Decl::Kind);
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004738
Douglas Gregor851c75a2011-08-24 21:27:34 +00004739 SmallVectorImpl<Decl*> &Decls;
4740 bool PredefsVisited[NUM_PREDEF_DECL_IDS];
4741
4742 public:
4743 FindExternalLexicalDeclsVisitor(ASTReader &Reader, const DeclContext *DC,
4744 bool (*isKindWeWant)(Decl::Kind),
4745 SmallVectorImpl<Decl*> &Decls)
4746 : Reader(Reader), DC(DC), isKindWeWant(isKindWeWant), Decls(Decls)
4747 {
4748 for (unsigned I = 0; I != NUM_PREDEF_DECL_IDS; ++I)
4749 PredefsVisited[I] = false;
4750 }
4751
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004752 static bool visit(ModuleFile &M, bool Preorder, void *UserData) {
Douglas Gregor851c75a2011-08-24 21:27:34 +00004753 if (Preorder)
4754 return false;
4755
4756 FindExternalLexicalDeclsVisitor *This
4757 = static_cast<FindExternalLexicalDeclsVisitor *>(UserData);
4758
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004759 ModuleFile::DeclContextInfosMap::iterator Info
Douglas Gregor851c75a2011-08-24 21:27:34 +00004760 = M.DeclContextInfos.find(This->DC);
4761 if (Info == M.DeclContextInfos.end() || !Info->second.LexicalDecls)
4762 return false;
4763
4764 // Load all of the declaration IDs
4765 for (const KindDeclIDPair *ID = Info->second.LexicalDecls,
4766 *IDE = ID + Info->second.NumLexicalDecls;
4767 ID != IDE; ++ID) {
4768 if (This->isKindWeWant && !This->isKindWeWant((Decl::Kind)ID->first))
4769 continue;
4770
4771 // Don't add predefined declarations to the lexical context more
4772 // than once.
4773 if (ID->second < NUM_PREDEF_DECL_IDS) {
4774 if (This->PredefsVisited[ID->second])
4775 continue;
4776
4777 This->PredefsVisited[ID->second] = true;
4778 }
4779
Douglas Gregor2ea054f2011-08-26 22:04:51 +00004780 if (Decl *D = This->Reader.GetLocalDecl(M, ID->second)) {
4781 if (!This->DC->isDeclInLexicalTraversal(D))
4782 This->Decls.push_back(D);
4783 }
Douglas Gregor851c75a2011-08-24 21:27:34 +00004784 }
4785
4786 return false;
4787 }
4788 };
4789}
4790
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004791ExternalLoadResult ASTReader::FindExternalLexicalDecls(const DeclContext *DC,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00004792 bool (*isKindWeWant)(Decl::Kind),
Chris Lattner5f9e2722011-07-23 10:55:15 +00004793 SmallVectorImpl<Decl*> &Decls) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004794 // There might be lexical decls in multiple modules, for the TU at
Douglas Gregor851c75a2011-08-24 21:27:34 +00004795 // least. Walk all of the modules in the order they were loaded.
4796 FindExternalLexicalDeclsVisitor Visitor(*this, DC, isKindWeWant, Decls);
4797 ModuleMgr.visitDepthFirst(&FindExternalLexicalDeclsVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004798 ++NumLexicalDeclContextsRead;
Douglas Gregorba6ffaf2011-07-15 21:46:17 +00004799 return ELR_Success;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004800}
4801
Douglas Gregor0d95f772011-08-24 19:03:07 +00004802namespace {
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004803
4804class DeclIDComp {
4805 ASTReader &Reader;
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004806 ModuleFile &Mod;
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004807
4808public:
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004809 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004810
4811 bool operator()(LocalDeclID L, LocalDeclID R) const {
4812 SourceLocation LHS = getLocation(L);
4813 SourceLocation RHS = getLocation(R);
4814 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4815 }
4816
4817 bool operator()(SourceLocation LHS, LocalDeclID R) const {
4818 SourceLocation RHS = getLocation(R);
4819 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4820 }
4821
4822 bool operator()(LocalDeclID L, SourceLocation RHS) const {
4823 SourceLocation LHS = getLocation(L);
4824 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
4825 }
4826
4827 SourceLocation getLocation(LocalDeclID ID) const {
4828 return Reader.getSourceManager().getFileLoc(
4829 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
4830 }
4831};
4832
4833}
4834
4835void ASTReader::FindFileRegionDecls(FileID File,
4836 unsigned Offset, unsigned Length,
4837 SmallVectorImpl<Decl *> &Decls) {
4838 SourceManager &SM = getSourceManager();
4839
4840 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
4841 if (I == FileDeclIDs.end())
4842 return;
4843
4844 FileDeclsInfo &DInfo = I->second;
4845 if (DInfo.Decls.empty())
4846 return;
4847
4848 SourceLocation
4849 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
4850 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
4851
4852 DeclIDComp DIDComp(*this, *DInfo.Mod);
4853 ArrayRef<serialization::LocalDeclID>::iterator
4854 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4855 BeginLoc, DIDComp);
4856 if (BeginIt != DInfo.Decls.begin())
4857 --BeginIt;
4858
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00004859 // If we are pointing at a top-level decl inside an objc container, we need
4860 // to backtrack until we find it otherwise we will fail to report that the
4861 // region overlaps with an objc container.
4862 while (BeginIt != DInfo.Decls.begin() &&
4863 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
4864 ->isTopLevelDeclInObjCContainer())
4865 --BeginIt;
4866
Argyrios Kyrtzidisdfb332d2011-11-03 02:20:32 +00004867 ArrayRef<serialization::LocalDeclID>::iterator
4868 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(),
4869 EndLoc, DIDComp);
4870 if (EndIt != DInfo.Decls.end())
4871 ++EndIt;
4872
4873 for (ArrayRef<serialization::LocalDeclID>::iterator
4874 DIt = BeginIt; DIt != EndIt; ++DIt)
4875 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
4876}
4877
4878namespace {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004879 /// \brief ModuleFile visitor used to perform name lookup into a
Douglas Gregor0d95f772011-08-24 19:03:07 +00004880 /// declaration context.
4881 class DeclContextNameLookupVisitor {
4882 ASTReader &Reader;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004883 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004884 DeclarationName Name;
4885 SmallVectorImpl<NamedDecl *> &Decls;
4886
4887 public:
4888 DeclContextNameLookupVisitor(ASTReader &Reader,
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004889 SmallVectorImpl<const DeclContext *> &Contexts,
4890 DeclarationName Name,
Douglas Gregor0d95f772011-08-24 19:03:07 +00004891 SmallVectorImpl<NamedDecl *> &Decls)
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004892 : Reader(Reader), Contexts(Contexts), Name(Name), Decls(Decls) { }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004893
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004894 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor0d95f772011-08-24 19:03:07 +00004895 DeclContextNameLookupVisitor *This
4896 = static_cast<DeclContextNameLookupVisitor *>(UserData);
4897
4898 // Check whether we have any visible declaration information for
4899 // this context in this module.
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004900 ModuleFile::DeclContextInfosMap::iterator Info;
4901 bool FoundInfo = false;
4902 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
4903 Info = M.DeclContextInfos.find(This->Contexts[I]);
4904 if (Info != M.DeclContextInfos.end() &&
4905 Info->second.NameLookupTableData) {
4906 FoundInfo = true;
4907 break;
4908 }
4909 }
Douglas Gregor0d95f772011-08-24 19:03:07 +00004910
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004911 if (!FoundInfo)
4912 return false;
4913
Douglas Gregor0d95f772011-08-24 19:03:07 +00004914 // Look for this name within this module.
4915 ASTDeclContextNameLookupTable *LookupTable =
Benjamin Kramerb1758c62012-04-15 12:36:49 +00004916 Info->second.NameLookupTableData;
Douglas Gregor0d95f772011-08-24 19:03:07 +00004917 ASTDeclContextNameLookupTable::iterator Pos
4918 = LookupTable->find(This->Name);
4919 if (Pos == LookupTable->end())
4920 return false;
4921
4922 bool FoundAnything = false;
4923 ASTDeclContextNameLookupTrait::data_type Data = *Pos;
4924 for (; Data.first != Data.second; ++Data.first) {
4925 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M, *Data.first);
4926 if (!ND)
4927 continue;
4928
4929 if (ND->getDeclName() != This->Name) {
4930 assert(!This->Name.getCXXNameType().isNull() &&
4931 "Name mismatch without a type");
4932 continue;
4933 }
4934
4935 // Record this declaration.
4936 FoundAnything = true;
4937 This->Decls.push_back(ND);
4938 }
4939
4940 return FoundAnything;
4941 }
4942 };
4943}
4944
John McCall76bd1f32010-06-01 09:23:16 +00004945DeclContext::lookup_result
Sebastian Redlc43b54c2010-08-18 23:56:43 +00004946ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
John McCall76bd1f32010-06-01 09:23:16 +00004947 DeclarationName Name) {
Mike Stump1eb44332009-09-09 15:08:12 +00004948 assert(DC->hasExternalVisibleStorage() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +00004949 "DeclContext has no visible decls in storage");
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004950 if (!Name)
4951 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
4952 DeclContext::lookup_iterator(0));
Ted Kremenekd5d7b3f2010-03-18 00:56:54 +00004953
Chris Lattner5f9e2722011-07-23 10:55:15 +00004954 SmallVector<NamedDecl *, 64> Decls;
Douglas Gregorc6c8e0e2012-01-09 17:30:44 +00004955
4956 // Compute the declaration contexts we need to look into. Multiple such
4957 // declaration contexts occur when two declaration contexts from disjoint
4958 // modules get merged, e.g., when two namespaces with the same name are
4959 // independently defined in separate modules.
4960 SmallVector<const DeclContext *, 2> Contexts;
4961 Contexts.push_back(DC);
4962
4963 if (DC->isNamespace()) {
4964 MergedDeclsMap::iterator Merged
4965 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
4966 if (Merged != MergedDecls.end()) {
4967 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
4968 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
4969 }
4970 }
4971
4972 DeclContextNameLookupVisitor Visitor(*this, Contexts, Name, Decls);
Douglas Gregor0d95f772011-08-24 19:03:07 +00004973 ModuleMgr.visit(&DeclContextNameLookupVisitor::visit, &Visitor);
Douglas Gregor25123082009-04-22 22:34:57 +00004974 ++NumVisibleDeclContextsRead;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00004975 SetExternalVisibleDeclsForName(DC, Name, Decls);
John McCall76bd1f32010-06-01 09:23:16 +00004976 return const_cast<DeclContext*>(DC)->lookup(Name);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004977}
4978
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004979namespace {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004980 /// \brief ModuleFile visitor used to retrieve all visible names in a
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004981 /// declaration context.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004982 class DeclContextAllNamesVisitor {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004983 ASTReader &Reader;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004984 llvm::SmallVectorImpl<const DeclContext *> &Contexts;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004985 llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8> > &Decls;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004986
4987 public:
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004988 DeclContextAllNamesVisitor(ASTReader &Reader,
4989 SmallVectorImpl<const DeclContext *> &Contexts,
4990 llvm::DenseMap<DeclarationName,
4991 SmallVector<NamedDecl *, 8> > &Decls)
4992 : Reader(Reader), Contexts(Contexts), Decls(Decls) { }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004993
4994 static bool visit(ModuleFile &M, void *UserData) {
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00004995 DeclContextAllNamesVisitor *This
4996 = static_cast<DeclContextAllNamesVisitor *>(UserData);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004997
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00004998 // Check whether we have any visible declaration information for
4999 // this context in this module.
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005000 ModuleFile::DeclContextInfosMap::iterator Info;
5001 bool FoundInfo = false;
5002 for (unsigned I = 0, N = This->Contexts.size(); I != N; ++I) {
5003 Info = M.DeclContextInfos.find(This->Contexts[I]);
5004 if (Info != M.DeclContextInfos.end() &&
5005 Info->second.NameLookupTableData) {
5006 FoundInfo = true;
5007 break;
5008 }
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005009 }
5010
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005011 if (!FoundInfo)
5012 return false;
5013
5014 ASTDeclContextNameLookupTable *LookupTable =
5015 Info->second.NameLookupTableData;
5016 bool FoundAnything = false;
5017 for (ASTDeclContextNameLookupTable::data_iterator
5018 I = LookupTable->data_begin(), E = LookupTable->data_end();
5019 I != E; ++I) {
5020 ASTDeclContextNameLookupTrait::data_type Data = *I;
5021 for (; Data.first != Data.second; ++Data.first) {
5022 NamedDecl *ND = This->Reader.GetLocalDeclAs<NamedDecl>(M,
5023 *Data.first);
5024 if (!ND)
5025 continue;
5026
5027 // Record this declaration.
5028 FoundAnything = true;
5029 This->Decls[ND->getDeclName()].push_back(ND);
5030 }
5031 }
5032
5033 return FoundAnything;
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005034 }
5035 };
5036}
5037
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005038void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005039 if (!DC->hasExternalVisibleStorage())
5040 return;
Nick Lewyckyb346d2f2012-04-16 02:51:46 +00005041 llvm::DenseMap<DeclarationName, llvm::SmallVector<NamedDecl*, 8> > Decls;
5042
5043 // Compute the declaration contexts we need to look into. Multiple such
5044 // declaration contexts occur when two declaration contexts from disjoint
5045 // modules get merged, e.g., when two namespaces with the same name are
5046 // independently defined in separate modules.
5047 SmallVector<const DeclContext *, 2> Contexts;
5048 Contexts.push_back(DC);
5049
5050 if (DC->isNamespace()) {
5051 MergedDeclsMap::iterator Merged
5052 = MergedDecls.find(const_cast<Decl *>(cast<Decl>(DC)));
5053 if (Merged != MergedDecls.end()) {
5054 for (unsigned I = 0, N = Merged->second.size(); I != N; ++I)
5055 Contexts.push_back(cast<DeclContext>(GetDecl(Merged->second[I])));
5056 }
5057 }
5058
5059 DeclContextAllNamesVisitor Visitor(*this, Contexts, Decls);
5060 ModuleMgr.visit(&DeclContextAllNamesVisitor::visit, &Visitor);
5061 ++NumVisibleDeclContextsRead;
5062
5063 for (llvm::DenseMap<DeclarationName,
5064 llvm::SmallVector<NamedDecl*, 8> >::iterator
5065 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
5066 SetExternalVisibleDeclsForName(DC, I->first, I->second);
5067 }
Argyrios Kyrtzidis394e5392012-04-26 18:34:14 +00005068 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
Argyrios Kyrtzidis643586f2012-03-22 16:08:04 +00005069}
5070
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005071/// \brief Under non-PCH compilation the consumer receives the objc methods
5072/// before receiving the implementation, and codegen depends on this.
5073/// We simulate this by deserializing and passing to consumer the methods of the
5074/// implementation before passing the deserialized implementation decl.
5075static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
5076 ASTConsumer *Consumer) {
5077 assert(ImplD && Consumer);
5078
5079 for (ObjCImplDecl::method_iterator
5080 I = ImplD->meth_begin(), E = ImplD->meth_end(); I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00005081 Consumer->HandleInterestingDecl(DeclGroupRef(*I));
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005082
5083 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
5084}
5085
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005086void ASTReader::PassInterestingDeclsToConsumer() {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005087 assert(Consumer);
5088 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005089 Decl *D = InterestingDecls.front();
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005090 InterestingDecls.pop_front();
Argyrios Kyrtzidis144b38a2011-09-13 21:35:00 +00005091
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005092 PassInterestingDeclToConsumer(D);
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005093 }
5094}
5095
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00005096void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
5097 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
5098 PassObjCImplDeclToConsumer(ImplD, Consumer);
5099 else
5100 Consumer->HandleInterestingDecl(DeclGroupRef(D));
5101}
5102
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005103void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
Douglas Gregor0af2ca42009-04-22 19:09:20 +00005104 this->Consumer = Consumer;
5105
Douglas Gregorfdd01722009-04-14 00:24:19 +00005106 if (!Consumer)
5107 return;
5108
5109 for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) {
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005110 // Force deserialization of this decl, which will cause it to be queued for
5111 // passing to the consumer.
Daniel Dunbar04a0b502009-09-17 03:06:44 +00005112 GetDecl(ExternalDefinitions[I]);
Douglas Gregorfdd01722009-04-14 00:24:19 +00005113 }
Douglas Gregor1a995dd2011-09-15 18:47:32 +00005114 ExternalDefinitions.clear();
Douglas Gregorc62a2fe2009-04-25 00:41:30 +00005115
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00005116 PassInterestingDeclsToConsumer();
Douglas Gregorfdd01722009-04-14 00:24:19 +00005117}
5118
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005119void ASTReader::PrintStats() {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005120 std::fprintf(stderr, "*** AST File Statistics:\n");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005121
Mike Stump1eb44332009-09-09 15:08:12 +00005122 unsigned NumTypesLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005123 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
John McCall0953e762009-09-24 19:53:00 +00005124 QualType());
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005125 unsigned NumDeclsLoaded
5126 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
5127 (Decl *)0);
5128 unsigned NumIdentifiersLoaded
5129 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
5130 IdentifiersLoaded.end(),
5131 (IdentifierInfo *)0);
Mike Stump1eb44332009-09-09 15:08:12 +00005132 unsigned NumSelectorsLoaded
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005133 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
5134 SelectorsLoaded.end(),
5135 Selector());
Douglas Gregor2d41cc12009-04-13 20:50:16 +00005136
Douglas Gregor4fed3f42009-04-27 18:38:38 +00005137 std::fprintf(stderr, " %u stat cache hits\n", NumStatHits);
5138 std::fprintf(stderr, " %u stat cache misses\n", NumStatMisses);
Douglas Gregor0cdd7982011-07-21 18:46:38 +00005139 if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005140 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n",
5141 NumSLocEntriesRead, TotalNumSLocEntries,
5142 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005143 if (!TypesLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005144 std::fprintf(stderr, " %u/%u types read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005145 NumTypesLoaded, (unsigned)TypesLoaded.size(),
5146 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
5147 if (!DeclsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005148 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n",
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00005149 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
5150 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005151 if (!IdentifiersLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005152 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n",
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005153 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
5154 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
Sebastian Redl725cd962010-08-04 20:40:17 +00005155 if (!SelectorsLoaded.empty())
Douglas Gregor83941df2009-04-25 17:48:32 +00005156 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n",
Sebastian Redl725cd962010-08-04 20:40:17 +00005157 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
5158 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
Douglas Gregor83941df2009-04-25 17:48:32 +00005159 if (TotalNumStatements)
5160 std::fprintf(stderr, " %u/%u statements read (%f%%)\n",
5161 NumStatementsRead, TotalNumStatements,
5162 ((float)NumStatementsRead/TotalNumStatements * 100));
5163 if (TotalNumMacros)
5164 std::fprintf(stderr, " %u/%u macros read (%f%%)\n",
5165 NumMacrosRead, TotalNumMacros,
5166 ((float)NumMacrosRead/TotalNumMacros * 100));
5167 if (TotalLexicalDeclContexts)
5168 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n",
5169 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
5170 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
5171 * 100));
5172 if (TotalVisibleDeclContexts)
5173 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n",
5174 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
5175 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
5176 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005177 if (TotalNumMethodPoolEntries) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005178 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n",
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005179 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
5180 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
Douglas Gregor83941df2009-04-25 17:48:32 +00005181 * 100));
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005182 std::fprintf(stderr, " %u method pool misses\n", NumMethodPoolMisses);
Douglas Gregor83941df2009-04-25 17:48:32 +00005183 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00005184 std::fprintf(stderr, "\n");
Douglas Gregor23d7df52011-07-21 19:50:14 +00005185 dump();
5186 std::fprintf(stderr, "\n");
5187}
5188
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005189template<typename Key, typename ModuleFile, unsigned InitialCapacity>
Douglas Gregor23d7df52011-07-21 19:50:14 +00005190static void
Chris Lattner5f9e2722011-07-23 10:55:15 +00005191dumpModuleIDMap(StringRef Name,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005192 const ContinuousRangeMap<Key, ModuleFile *,
Douglas Gregor23d7df52011-07-21 19:50:14 +00005193 InitialCapacity> &Map) {
5194 if (Map.begin() == Map.end())
5195 return;
5196
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005197 typedef ContinuousRangeMap<Key, ModuleFile *, InitialCapacity> MapType;
Douglas Gregor23d7df52011-07-21 19:50:14 +00005198 llvm::errs() << Name << ":\n";
5199 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
5200 I != IEnd; ++I) {
5201 llvm::errs() << " " << I->first << " -> " << I->second->FileName
5202 << "\n";
5203 }
5204}
5205
Douglas Gregor23d7df52011-07-21 19:50:14 +00005206void ASTReader::dump() {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005207 llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
Douglas Gregor8f1231b2011-07-22 06:10:01 +00005208 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
Douglas Gregor23d7df52011-07-21 19:50:14 +00005209 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
Douglas Gregor1e849b62011-07-29 00:21:44 +00005210 dumpModuleIDMap("Global type map", GlobalTypeMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005211 dumpModuleIDMap("Global declaration map", GlobalDeclMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005212 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
Douglas Gregor26ced122011-12-01 00:59:36 +00005213 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005214 dumpModuleIDMap("Global selector map", GlobalSelectorMap);
Douglas Gregor9827a802011-07-29 00:56:45 +00005215 dumpModuleIDMap("Global preprocessed entity map",
5216 GlobalPreprocessedEntityMap);
Douglas Gregor8df5c9b2011-08-02 11:12:41 +00005217
5218 llvm::errs() << "\n*** PCH/Modules Loaded:";
5219 for (ModuleManager::ModuleConstIterator M = ModuleMgr.begin(),
5220 MEnd = ModuleMgr.end();
5221 M != MEnd; ++M)
5222 (*M)->dump();
Douglas Gregor2cf26342009-04-09 22:27:44 +00005223}
5224
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005225/// Return the amount of memory used by memory buffers, breaking down
5226/// by heap-backed versus mmap'ed memory.
5227void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005228 for (ModuleConstIterator I = ModuleMgr.begin(),
5229 E = ModuleMgr.end(); I != E; ++I) {
5230 if (llvm::MemoryBuffer *buf = (*I)->Buffer.get()) {
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005231 size_t bytes = buf->getBufferSize();
5232 switch (buf->getBufferKind()) {
5233 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
5234 sizes.malloc_bytes += bytes;
5235 break;
5236 case llvm::MemoryBuffer::MemoryBuffer_MMap:
5237 sizes.mmap_bytes += bytes;
5238 break;
5239 }
5240 }
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005241 }
Ted Kremeneke9b5f3d2011-04-28 23:46:20 +00005242}
5243
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005244void ASTReader::InitializeSema(Sema &S) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005245 SemaObj = &S;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005246 S.ExternalSource = this;
5247
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005248 // Makes sure any declarations that were deserialized "too early"
5249 // still get added to the identifier's declaration chains.
Douglas Gregor76dc8892010-09-24 23:29:12 +00005250 for (unsigned I = 0, N = PreloadedDecls.size(); I != N; ++I) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005251 SemaObj->pushExternalDeclIntoScope(PreloadedDecls[I],
5252 PreloadedDecls[I]->getDeclName());
Douglas Gregor668c1a42009-04-21 22:25:48 +00005253 }
Douglas Gregor6cfc1a82009-04-22 21:15:06 +00005254 PreloadedDecls.clear();
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00005255
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005256 // Load the offsets of the declarations that Sema references.
5257 // They will be lazily deserialized when needed.
5258 if (!SemaDeclRefs.empty()) {
5259 assert(SemaDeclRefs.size() == 2 && "More decl refs than expected!");
Douglas Gregor1e5b6f62011-07-28 00:57:24 +00005260 if (!SemaObj->StdNamespace)
5261 SemaObj->StdNamespace = SemaDeclRefs[0];
5262 if (!SemaObj->StdBadAlloc)
5263 SemaObj->StdBadAlloc = SemaDeclRefs[1];
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00005264 }
5265
Peter Collingbourne84bccea2011-02-15 19:46:30 +00005266 if (!FPPragmaOptions.empty()) {
5267 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
5268 SemaObj->FPFeatures.fp_contract = FPPragmaOptions[0];
5269 }
5270
5271 if (!OpenCLExtensions.empty()) {
5272 unsigned I = 0;
5273#define OPENCLEXT(nm) SemaObj->OpenCLFeatures.nm = OpenCLExtensions[I++];
5274#include "clang/Basic/OpenCLExtensions.def"
5275
5276 assert(OpenCLExtensions.size() == I && "Wrong number of OPENCL_EXTENSIONS");
5277 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00005278}
5279
Douglas Gregor211f6e82011-08-20 04:39:52 +00005280IdentifierInfo* ASTReader::get(const char *NameStart, const char *NameEnd) {
Douglas Gregor057df202012-01-18 20:56:22 +00005281 IdentifierLookupVisitor Visitor(StringRef(NameStart, NameEnd - NameStart),
5282 /*PriorGeneration=*/0);
Douglas Gregor211f6e82011-08-20 04:39:52 +00005283 ModuleMgr.visit(IdentifierLookupVisitor::visit, &Visitor);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005284 IdentifierInfo *II = Visitor.getIdentifierInfo();
Douglas Gregor057df202012-01-18 20:56:22 +00005285 markIdentifierUpToDate(II);
Douglas Gregoreee242f2011-10-27 09:33:13 +00005286 return II;
Douglas Gregor668c1a42009-04-21 22:25:48 +00005287}
5288
Douglas Gregor95f42922010-10-14 22:11:03 +00005289namespace clang {
5290 /// \brief An identifier-lookup iterator that enumerates all of the
5291 /// identifiers stored within a set of AST files.
5292 class ASTIdentifierIterator : public IdentifierIterator {
5293 /// \brief The AST reader whose identifiers are being enumerated.
5294 const ASTReader &Reader;
5295
5296 /// \brief The current index into the chain of AST files stored in
5297 /// the AST reader.
5298 unsigned Index;
5299
5300 /// \brief The current position within the identifier lookup table
5301 /// of the current AST file.
5302 ASTIdentifierLookupTable::key_iterator Current;
5303
5304 /// \brief The end position within the identifier lookup table of
5305 /// the current AST file.
5306 ASTIdentifierLookupTable::key_iterator End;
5307
5308 public:
5309 explicit ASTIdentifierIterator(const ASTReader &Reader);
5310
Chris Lattner5f9e2722011-07-23 10:55:15 +00005311 virtual StringRef Next();
Douglas Gregor95f42922010-10-14 22:11:03 +00005312 };
5313}
5314
5315ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader)
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005316 : Reader(Reader), Index(Reader.ModuleMgr.size() - 1) {
Douglas Gregor95f42922010-10-14 22:11:03 +00005317 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005318 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005319 Current = IdTable->key_begin();
5320 End = IdTable->key_end();
5321}
5322
Chris Lattner5f9e2722011-07-23 10:55:15 +00005323StringRef ASTIdentifierIterator::Next() {
Douglas Gregor95f42922010-10-14 22:11:03 +00005324 while (Current == End) {
5325 // If we have exhausted all of our AST files, we're done.
5326 if (Index == 0)
Chris Lattner5f9e2722011-07-23 10:55:15 +00005327 return StringRef();
Douglas Gregor95f42922010-10-14 22:11:03 +00005328
5329 --Index;
5330 ASTIdentifierLookupTable *IdTable
Jonathan D. Turner5d6d89f2011-07-25 20:32:21 +00005331 = (ASTIdentifierLookupTable *)Reader.ModuleMgr[Index].
5332 IdentifierLookupTable;
Douglas Gregor95f42922010-10-14 22:11:03 +00005333 Current = IdTable->key_begin();
5334 End = IdTable->key_end();
5335 }
5336
5337 // We have any identifiers remaining in the current AST file; return
5338 // the next one.
5339 std::pair<const char*, unsigned> Key = *Current;
5340 ++Current;
Chris Lattner5f9e2722011-07-23 10:55:15 +00005341 return StringRef(Key.first, Key.second);
Douglas Gregor95f42922010-10-14 22:11:03 +00005342}
5343
5344IdentifierIterator *ASTReader::getIdentifiers() const {
5345 return new ASTIdentifierIterator(*this);
5346}
5347
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005348namespace clang { namespace serialization {
5349 class ReadMethodPoolVisitor {
5350 ASTReader &Reader;
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005351 Selector Sel;
5352 unsigned PriorGeneration;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005353 llvm::SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
5354 llvm::SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005355
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005356 public:
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005357 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
5358 unsigned PriorGeneration)
5359 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) { }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005360
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005361 static bool visit(ModuleFile &M, void *UserData) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005362 ReadMethodPoolVisitor *This
5363 = static_cast<ReadMethodPoolVisitor *>(UserData);
5364
5365 if (!M.SelectorLookupTable)
5366 return false;
5367
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005368 // If we've already searched this module file, skip it now.
5369 if (M.Generation <= This->PriorGeneration)
5370 return true;
5371
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005372 ASTSelectorLookupTable *PoolTable
5373 = (ASTSelectorLookupTable*)M.SelectorLookupTable;
5374 ASTSelectorLookupTable::iterator Pos = PoolTable->find(This->Sel);
5375 if (Pos == PoolTable->end())
5376 return false;
5377
5378 ++This->Reader.NumSelectorsRead;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00005379 // FIXME: Not quite happy with the statistics here. We probably should
5380 // disable this tracking when called via LoadSelector.
5381 // Also, should entries without methods count as misses?
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005382 ++This->Reader.NumMethodPoolEntriesRead;
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005383 ASTSelectorLookupTrait::data_type Data = *Pos;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005384 if (This->Reader.DeserializationListener)
5385 This->Reader.DeserializationListener->SelectorRead(Data.ID,
5386 This->Sel);
5387
5388 This->InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
5389 This->FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
5390 return true;
Sebastian Redl725cd962010-08-04 20:40:17 +00005391 }
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005392
5393 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005394 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
5395 return InstanceMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005396 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005397
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005398 /// \brief Retrieve the instance methods found by this visitor.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005399 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
5400 return FactoryMethods;
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005401 }
5402 };
5403} } // end namespace clang::serialization
5404
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005405/// \brief Add the given set of methods to the method list.
5406static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
5407 ObjCMethodList &List) {
5408 for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
5409 S.addMethodToGlobalList(&List, Methods[I]);
5410 }
5411}
5412
5413void ASTReader::ReadMethodPool(Selector Sel) {
Douglas Gregor8efca6b2012-01-25 01:14:32 +00005414 // Get the selector generation and update it to the current generation.
5415 unsigned &Generation = SelectorGeneration[Sel];
5416 unsigned PriorGeneration = Generation;
5417 Generation = CurrentGeneration;
5418
5419 // Search for methods defined with this selector.
5420 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005421 ModuleMgr.visit(&ReadMethodPoolVisitor::visit, &Visitor);
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005422
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005423 if (Visitor.getInstanceMethods().empty() &&
5424 Visitor.getFactoryMethods().empty()) {
Douglas Gregor3d15ab82011-08-25 14:51:20 +00005425 ++NumMethodPoolMisses;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00005426 return;
5427 }
5428
5429 if (!getSema())
5430 return;
5431
5432 Sema &S = *getSema();
5433 Sema::GlobalMethodPool::iterator Pos
5434 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
5435
5436 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
5437 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00005438}
5439
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005440void ASTReader::ReadKnownNamespaces(
Chris Lattner5f9e2722011-07-23 10:55:15 +00005441 SmallVectorImpl<NamespaceDecl *> &Namespaces) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +00005442 Namespaces.clear();
5443
5444 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
5445 if (NamespaceDecl *Namespace
5446 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
5447 Namespaces.push_back(Namespace);
5448 }
5449}
5450
Douglas Gregora8623202011-07-27 20:58:46 +00005451void ASTReader::ReadTentativeDefinitions(
5452 SmallVectorImpl<VarDecl *> &TentativeDefs) {
5453 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
5454 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
5455 if (Var)
5456 TentativeDefs.push_back(Var);
5457 }
5458 TentativeDefinitions.clear();
5459}
5460
Douglas Gregora2ee20a2011-07-27 21:45:57 +00005461void ASTReader::ReadUnusedFileScopedDecls(
5462 SmallVectorImpl<const DeclaratorDecl *> &Decls) {
5463 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
5464 DeclaratorDecl *D
5465 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
5466 if (D)
5467 Decls.push_back(D);
5468 }
5469 UnusedFileScopedDecls.clear();
5470}
5471
Douglas Gregor0129b562011-07-27 21:57:17 +00005472void ASTReader::ReadDelegatingConstructors(
5473 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
5474 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
5475 CXXConstructorDecl *D
5476 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
5477 if (D)
5478 Decls.push_back(D);
5479 }
5480 DelegatingCtorDecls.clear();
5481}
5482
Douglas Gregord58a0a52011-07-28 00:39:29 +00005483void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
5484 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
5485 TypedefNameDecl *D
5486 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
5487 if (D)
5488 Decls.push_back(D);
5489 }
5490 ExtVectorDecls.clear();
5491}
5492
Douglas Gregora126f172011-07-28 00:53:40 +00005493void ASTReader::ReadDynamicClasses(SmallVectorImpl<CXXRecordDecl *> &Decls) {
5494 for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) {
5495 CXXRecordDecl *D
5496 = dyn_cast_or_null<CXXRecordDecl>(GetDecl(DynamicClasses[I]));
5497 if (D)
5498 Decls.push_back(D);
5499 }
5500 DynamicClasses.clear();
5501}
5502
Douglas Gregorec12ce22011-07-28 14:20:37 +00005503void
5504ASTReader::ReadLocallyScopedExternalDecls(SmallVectorImpl<NamedDecl *> &Decls) {
5505 for (unsigned I = 0, N = LocallyScopedExternalDecls.size(); I != N; ++I) {
5506 NamedDecl *D
5507 = dyn_cast_or_null<NamedDecl>(GetDecl(LocallyScopedExternalDecls[I]));
5508 if (D)
5509 Decls.push_back(D);
5510 }
5511 LocallyScopedExternalDecls.clear();
5512}
5513
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00005514void ASTReader::ReadReferencedSelectors(
5515 SmallVectorImpl<std::pair<Selector, SourceLocation> > &Sels) {
5516 if (ReferencedSelectorsData.empty())
5517 return;
5518
5519 // If there are @selector references added them to its pool. This is for
5520 // implementation of -Wselector.
5521 unsigned int DataSize = ReferencedSelectorsData.size()-1;
5522 unsigned I = 0;
5523 while (I < DataSize) {
5524 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
5525 SourceLocation SelLoc
5526 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
5527 Sels.push_back(std::make_pair(Sel, SelLoc));
5528 }
5529 ReferencedSelectorsData.clear();
5530}
5531
Douglas Gregor31e37b22011-07-28 18:09:57 +00005532void ASTReader::ReadWeakUndeclaredIdentifiers(
5533 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo> > &WeakIDs) {
5534 if (WeakUndeclaredIdentifiers.empty())
5535 return;
5536
5537 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
5538 IdentifierInfo *WeakId
5539 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5540 IdentifierInfo *AliasId
5541 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
5542 SourceLocation Loc
5543 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
5544 bool Used = WeakUndeclaredIdentifiers[I++];
5545 WeakInfo WI(AliasId, Loc);
5546 WI.setUsed(Used);
5547 WeakIDs.push_back(std::make_pair(WeakId, WI));
5548 }
5549 WeakUndeclaredIdentifiers.clear();
5550}
5551
Douglas Gregordfe65432011-07-28 19:11:31 +00005552void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
5553 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
5554 ExternalVTableUse VT;
5555 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
5556 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
5557 VT.DefinitionRequired = VTableUses[Idx++];
5558 VTables.push_back(VT);
5559 }
5560
5561 VTableUses.clear();
5562}
5563
Douglas Gregor6e4a3f52011-07-28 19:49:54 +00005564void ASTReader::ReadPendingInstantiations(
5565 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation> > &Pending) {
5566 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
5567 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
5568 SourceLocation Loc
5569 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
5570 Pending.push_back(std::make_pair(D, Loc));
5571 }
5572 PendingInstantiations.clear();
5573}
5574
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005575void ASTReader::LoadSelector(Selector Sel) {
Sebastian Redle58aa892010-08-04 18:21:41 +00005576 // It would be complicated to avoid reading the methods anyway. So don't.
5577 ReadMethodPool(Sel);
5578}
5579
Douglas Gregor95eab172011-07-28 20:55:49 +00005580void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
Douglas Gregor668c1a42009-04-21 22:25:48 +00005581 assert(ID && "Non-zero identifier ID required");
Douglas Gregora02b1472009-04-28 21:53:25 +00005582 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00005583 IdentifiersLoaded[ID - 1] = II;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005584 if (DeserializationListener)
5585 DeserializationListener->IdentifierRead(ID, II);
Douglas Gregor668c1a42009-04-21 22:25:48 +00005586}
5587
Douglas Gregord89275b2009-07-06 18:54:52 +00005588/// \brief Set the globally-visible declarations associated with the given
5589/// identifier.
5590///
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005591/// If the AST reader is currently in a state where the given declaration IDs
Mike Stump1eb44332009-09-09 15:08:12 +00005592/// cannot safely be resolved, they are queued until it is safe to resolve
Douglas Gregord89275b2009-07-06 18:54:52 +00005593/// them.
5594///
5595/// \param II an IdentifierInfo that refers to one or more globally-visible
5596/// declarations.
5597///
5598/// \param DeclIDs the set of declaration IDs with the name @p II that are
5599/// visible at global scope.
5600///
5601/// \param Nonrecursive should be true to indicate that the caller knows that
5602/// this call is non-recursive, and therefore the globally-visible declarations
5603/// will not be placed onto the pending queue.
Mike Stump1eb44332009-09-09 15:08:12 +00005604void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005605ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
Chris Lattner5f9e2722011-07-23 10:55:15 +00005606 const SmallVectorImpl<uint32_t> &DeclIDs,
Douglas Gregord89275b2009-07-06 18:54:52 +00005607 bool Nonrecursive) {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00005608 if (NumCurrentElementsDeserializing && !Nonrecursive) {
Douglas Gregord89275b2009-07-06 18:54:52 +00005609 PendingIdentifierInfos.push_back(PendingIdentifierInfo());
5610 PendingIdentifierInfo &PII = PendingIdentifierInfos.back();
5611 PII.II = II;
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00005612 PII.DeclIDs.append(DeclIDs.begin(), DeclIDs.end());
Douglas Gregord89275b2009-07-06 18:54:52 +00005613 return;
5614 }
Mike Stump1eb44332009-09-09 15:08:12 +00005615
Douglas Gregord89275b2009-07-06 18:54:52 +00005616 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
5617 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
5618 if (SemaObj) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00005619 // Introduce this declaration into the translation-unit scope
5620 // and add it to the declaration chain for this identifier, so
5621 // that (unqualified) name lookup will find it.
5622 SemaObj->pushExternalDeclIntoScope(D, II);
Douglas Gregord89275b2009-07-06 18:54:52 +00005623 } else {
5624 // Queue this declaration so that it will be added to the
5625 // translation unit scope and identifier's declaration chain
5626 // once a Sema object is known.
5627 PreloadedDecls.push_back(D);
5628 }
5629 }
5630}
5631
Douglas Gregor95eab172011-07-28 20:55:49 +00005632IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00005633 if (ID == 0)
5634 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00005635
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005636 if (IdentifiersLoaded.empty()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005637 Error("no identifier table in AST file");
Douglas Gregorafaf3082009-04-11 00:14:32 +00005638 return 0;
5639 }
Mike Stump1eb44332009-09-09 15:08:12 +00005640
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005641 ID -= 1;
5642 if (!IdentifiersLoaded[ID]) {
Douglas Gregor67268d02011-07-20 00:59:32 +00005643 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
5644 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005645 ModuleFile *M = I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005646 unsigned Index = ID - M->BaseIdentifierID;
5647 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
Douglas Gregord6595a42009-04-25 21:04:17 +00005648
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005649 // All of the strings in the AST file are preceded by a 16-bit length.
5650 // Extract that 16-bit length to avoid having to execute strlen().
Ted Kremenek231bc0b2009-10-23 04:45:31 +00005651 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
5652 // unsigned integers. This is important to avoid integer overflow when
5653 // we cast them to 'unsigned'.
Ted Kremenekff1ea462009-10-23 03:57:22 +00005654 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
Douglas Gregor02fc7512009-04-28 20:01:51 +00005655 unsigned StrLen = (((unsigned) StrLenPtr[0])
5656 | (((unsigned) StrLenPtr[1]) << 8)) - 1;
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005657 IdentifiersLoaded[ID]
Douglas Gregor712f2fc2011-09-09 22:02:16 +00005658 = &PP.getIdentifierTable().get(StringRef(Str, StrLen));
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005659 if (DeserializationListener)
5660 DeserializationListener->IdentifierRead(ID + 1, IdentifiersLoaded[ID]);
Douglas Gregorafaf3082009-04-11 00:14:32 +00005661 }
Mike Stump1eb44332009-09-09 15:08:12 +00005662
Sebastian Redl11f5ccf2010-07-21 00:46:22 +00005663 return IdentifiersLoaded[ID];
Douglas Gregor2cf26342009-04-09 22:27:44 +00005664}
5665
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005666IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
Douglas Gregor95eab172011-07-28 20:55:49 +00005667 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
5668}
5669
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005670IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
Douglas Gregor6ec60e02011-08-03 21:49:18 +00005671 if (LocalID < NUM_PREDEF_IDENT_IDS)
5672 return LocalID;
5673
5674 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5675 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
5676 assert(I != M.IdentifierRemap.end()
5677 && "Invalid index into identifier index remap");
5678
5679 return LocalID + I->second;
Douglas Gregor95eab172011-07-28 20:55:49 +00005680}
5681
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005682bool ASTReader::ReadSLocEntry(int ID) {
Douglas Gregore23ac652011-04-20 00:21:03 +00005683 return ReadSLocEntryRecord(ID) != Success;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00005684}
5685
Douglas Gregor26ced122011-12-01 00:59:36 +00005686serialization::SubmoduleID
5687ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
5688 if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
5689 return LocalID;
5690
5691 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5692 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
5693 assert(I != M.SubmoduleRemap.end()
5694 && "Invalid index into identifier index remap");
5695
5696 return LocalID + I->second;
5697}
5698
5699Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
5700 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
5701 assert(GlobalID == 0 && "Unhandled global submodule ID");
5702 return 0;
5703 }
5704
5705 if (GlobalID > SubmodulesLoaded.size()) {
5706 Error("submodule ID out of range in AST file");
5707 return 0;
5708 }
5709
5710 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
5711}
5712
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005713Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005714 return DecodeSelector(getGlobalSelectorID(M, LocalID));
5715}
5716
5717Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005718 if (ID == 0)
5719 return Selector();
Mike Stump1eb44332009-09-09 15:08:12 +00005720
Sebastian Redl725cd962010-08-04 20:40:17 +00005721 if (ID > SelectorsLoaded.size()) {
Sebastian Redl3c7f4132010-08-18 23:57:06 +00005722 Error("selector ID out of range in AST file");
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005723 return Selector();
5724 }
Douglas Gregor83941df2009-04-25 17:48:32 +00005725
Sebastian Redl725cd962010-08-04 20:40:17 +00005726 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == 0) {
Douglas Gregor83941df2009-04-25 17:48:32 +00005727 // Load this selector from the selector table.
Douglas Gregor96958cb2011-07-20 01:10:58 +00005728 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
5729 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005730 ModuleFile &M = *I->second;
Douglas Gregor9827a802011-07-29 00:56:45 +00005731 ASTSelectorLookupTrait Trait(*this, M);
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005732 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
Douglas Gregor96958cb2011-07-20 01:10:58 +00005733 SelectorsLoaded[ID - 1] =
Douglas Gregor9827a802011-07-29 00:56:45 +00005734 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
Douglas Gregor96958cb2011-07-20 01:10:58 +00005735 if (DeserializationListener)
5736 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
Douglas Gregor83941df2009-04-25 17:48:32 +00005737 }
5738
Sebastian Redl725cd962010-08-04 20:40:17 +00005739 return SelectorsLoaded[ID - 1];
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005740}
5741
Douglas Gregor8451ec72011-07-28 14:41:43 +00005742Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
Douglas Gregor719770d2010-04-06 17:30:22 +00005743 return DecodeSelector(ID);
5744}
5745
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005746uint32_t ASTReader::GetNumExternalSelectors() {
Sebastian Redl725cd962010-08-04 20:40:17 +00005747 // ID 0 (the null selector) is considered an external selector.
5748 return getTotalNumSelectors() + 1;
Douglas Gregor719770d2010-04-06 17:30:22 +00005749}
5750
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005751serialization::SelectorID
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005752ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00005753 if (LocalID < NUM_PREDEF_SELECTOR_IDS)
5754 return LocalID;
5755
5756 ContinuousRangeMap<uint32_t, int, 2>::iterator I
5757 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
5758 assert(I != M.SelectorRemap.end()
5759 && "Invalid index into identifier index remap");
5760
5761 return LocalID + I->second;
Douglas Gregor8451ec72011-07-28 14:41:43 +00005762}
5763
Mike Stump1eb44332009-09-09 15:08:12 +00005764DeclarationName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005765ASTReader::ReadDeclarationName(ModuleFile &F,
Douglas Gregor393f2492011-07-22 00:38:23 +00005766 const RecordData &Record, unsigned &Idx) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00005767 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++];
5768 switch (Kind) {
5769 case DeclarationName::Identifier:
Douglas Gregor95eab172011-07-28 20:55:49 +00005770 return DeclarationName(GetIdentifierInfo(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005771
5772 case DeclarationName::ObjCZeroArgSelector:
5773 case DeclarationName::ObjCOneArgSelector:
5774 case DeclarationName::ObjCMultiArgSelector:
Douglas Gregor2d2689a2011-07-28 21:16:51 +00005775 return DeclarationName(ReadSelector(F, Record, Idx));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005776
5777 case DeclarationName::CXXConstructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005778 return Context.DeclarationNames.getCXXConstructorName(
5779 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005780
5781 case DeclarationName::CXXDestructorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005782 return Context.DeclarationNames.getCXXDestructorName(
5783 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005784
5785 case DeclarationName::CXXConversionFunctionName:
Douglas Gregor35942772011-09-09 21:34:22 +00005786 return Context.DeclarationNames.getCXXConversionFunctionName(
5787 Context.getCanonicalType(readType(F, Record, Idx)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005788
5789 case DeclarationName::CXXOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005790 return Context.DeclarationNames.getCXXOperatorName(
Douglas Gregor2cf26342009-04-09 22:27:44 +00005791 (OverloadedOperatorKind)Record[Idx++]);
5792
Sean Hunt3e518bd2009-11-29 07:34:05 +00005793 case DeclarationName::CXXLiteralOperatorName:
Douglas Gregor35942772011-09-09 21:34:22 +00005794 return Context.DeclarationNames.getCXXLiteralOperatorName(
Douglas Gregor95eab172011-07-28 20:55:49 +00005795 GetIdentifierInfo(F, Record, Idx));
Sean Hunt3e518bd2009-11-29 07:34:05 +00005796
Douglas Gregor2cf26342009-04-09 22:27:44 +00005797 case DeclarationName::CXXUsingDirective:
5798 return DeclarationName::getUsingDirectiveName();
5799 }
5800
David Blaikie7530c032012-01-17 06:56:22 +00005801 llvm_unreachable("Invalid NameKind!");
Douglas Gregor2cf26342009-04-09 22:27:44 +00005802}
Douglas Gregor0a0428e2009-04-10 20:39:37 +00005803
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005804void ASTReader::ReadDeclarationNameLoc(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005805 DeclarationNameLoc &DNLoc,
5806 DeclarationName Name,
5807 const RecordData &Record, unsigned &Idx) {
5808 switch (Name.getNameKind()) {
5809 case DeclarationName::CXXConstructorName:
5810 case DeclarationName::CXXDestructorName:
5811 case DeclarationName::CXXConversionFunctionName:
5812 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx);
5813 break;
5814
5815 case DeclarationName::CXXOperatorName:
5816 DNLoc.CXXOperatorName.BeginOpNameLoc
5817 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5818 DNLoc.CXXOperatorName.EndOpNameLoc
5819 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5820 break;
5821
5822 case DeclarationName::CXXLiteralOperatorName:
5823 DNLoc.CXXLiteralOperatorName.OpNameLoc
5824 = ReadSourceLocation(F, Record, Idx).getRawEncoding();
5825 break;
5826
5827 case DeclarationName::Identifier:
5828 case DeclarationName::ObjCZeroArgSelector:
5829 case DeclarationName::ObjCOneArgSelector:
5830 case DeclarationName::ObjCMultiArgSelector:
5831 case DeclarationName::CXXUsingDirective:
5832 break;
5833 }
5834}
5835
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005836void ASTReader::ReadDeclarationNameInfo(ModuleFile &F,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005837 DeclarationNameInfo &NameInfo,
5838 const RecordData &Record, unsigned &Idx) {
Douglas Gregor393f2492011-07-22 00:38:23 +00005839 NameInfo.setName(ReadDeclarationName(F, Record, Idx));
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005840 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx));
5841 DeclarationNameLoc DNLoc;
5842 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx);
5843 NameInfo.setInfo(DNLoc);
5844}
5845
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005846void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info,
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005847 const RecordData &Record, unsigned &Idx) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005848 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005849 unsigned NumTPLists = Record[Idx++];
5850 Info.NumTemplParamLists = NumTPLists;
5851 if (NumTPLists) {
Douglas Gregor35942772011-09-09 21:34:22 +00005852 Info.TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005853 for (unsigned i=0; i != NumTPLists; ++i)
5854 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx);
5855 }
5856}
5857
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005858TemplateName
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005859ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record,
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005860 unsigned &Idx) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005861 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++];
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005862 switch (Kind) {
5863 case TemplateName::Template:
Douglas Gregor409448c2011-07-21 22:35:25 +00005864 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005865
5866 case TemplateName::OverloadedTemplate: {
5867 unsigned size = Record[Idx++];
5868 UnresolvedSet<8> Decls;
5869 while (size--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005870 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005871
Douglas Gregor35942772011-09-09 21:34:22 +00005872 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end());
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005873 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005874
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005875 case TemplateName::QualifiedTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005876 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005877 bool hasTemplKeyword = Record[Idx++];
Douglas Gregor409448c2011-07-21 22:35:25 +00005878 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005879 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005880 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005881
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005882 case TemplateName::DependentTemplate: {
Douglas Gregor409448c2011-07-21 22:35:25 +00005883 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005884 if (Record[Idx++]) // isIdentifier
Douglas Gregor35942772011-09-09 21:34:22 +00005885 return Context.getDependentTemplateName(NNS,
Douglas Gregor95eab172011-07-28 20:55:49 +00005886 GetIdentifierInfo(F, Record,
5887 Idx));
Douglas Gregor35942772011-09-09 21:34:22 +00005888 return Context.getDependentTemplateName(NNS,
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005889 (OverloadedOperatorKind)Record[Idx++]);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005890 }
John McCall14606042011-06-30 08:33:18 +00005891
5892 case TemplateName::SubstTemplateTemplateParm: {
5893 TemplateTemplateParmDecl *param
Douglas Gregor409448c2011-07-21 22:35:25 +00005894 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
John McCall14606042011-06-30 08:33:18 +00005895 if (!param) return TemplateName();
5896 TemplateName replacement = ReadTemplateName(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00005897 return Context.getSubstTemplateTemplateParm(param, replacement);
John McCall14606042011-06-30 08:33:18 +00005898 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005899
5900 case TemplateName::SubstTemplateTemplateParmPack: {
5901 TemplateTemplateParmDecl *Param
Douglas Gregor409448c2011-07-21 22:35:25 +00005902 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005903 if (!Param)
5904 return TemplateName();
5905
5906 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx);
5907 if (ArgPack.getKind() != TemplateArgument::Pack)
5908 return TemplateName();
5909
Douglas Gregor35942772011-09-09 21:34:22 +00005910 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack);
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005911 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005912 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005913
David Blaikieb219cfc2011-09-23 05:06:16 +00005914 llvm_unreachable("Unhandled template name kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005915}
5916
5917TemplateArgument
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005918ASTReader::ReadTemplateArgument(ModuleFile &F,
Sebastian Redl577d4792010-07-22 22:43:28 +00005919 const RecordData &Record, unsigned &Idx) {
Douglas Gregora7fc9012011-01-05 18:58:31 +00005920 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++];
5921 switch (Kind) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005922 case TemplateArgument::Null:
5923 return TemplateArgument();
5924 case TemplateArgument::Type:
Douglas Gregor393f2492011-07-22 00:38:23 +00005925 return TemplateArgument(readType(F, Record, Idx));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005926 case TemplateArgument::Declaration:
Douglas Gregor409448c2011-07-21 22:35:25 +00005927 return TemplateArgument(ReadDecl(F, Record, Idx));
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005928 case TemplateArgument::Integral: {
5929 llvm::APSInt Value = ReadAPSInt(Record, Idx);
Douglas Gregor393f2492011-07-22 00:38:23 +00005930 QualType T = readType(F, Record, Idx);
Benjamin Kramer85524372012-06-07 15:09:51 +00005931 return TemplateArgument(Context, Value, T);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +00005932 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00005933 case TemplateArgument::Template:
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005934 return TemplateArgument(ReadTemplateName(F, Record, Idx));
Douglas Gregora7fc9012011-01-05 18:58:31 +00005935 case TemplateArgument::TemplateExpansion: {
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005936 TemplateName Name = ReadTemplateName(F, Record, Idx);
Douglas Gregor2be29f42011-01-14 23:41:42 +00005937 llvm::Optional<unsigned> NumTemplateExpansions;
5938 if (unsigned NumExpansions = Record[Idx++])
5939 NumTemplateExpansions = NumExpansions - 1;
5940 return TemplateArgument(Name, NumTemplateExpansions);
Douglas Gregorba68eca2011-01-05 17:40:24 +00005941 }
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005942 case TemplateArgument::Expression:
Sebastian Redlc3632732010-10-05 15:59:54 +00005943 return TemplateArgument(ReadExpr(F));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005944 case TemplateArgument::Pack: {
5945 unsigned NumArgs = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00005946 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs];
Douglas Gregor910f8002010-11-07 23:05:16 +00005947 for (unsigned I = 0; I != NumArgs; ++I)
5948 Args[I] = ReadTemplateArgument(F, Record, Idx);
5949 return TemplateArgument(Args, NumArgs);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005950 }
5951 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005952
David Blaikieb219cfc2011-09-23 05:06:16 +00005953 llvm_unreachable("Unhandled template argument kind!");
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00005954}
5955
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005956TemplateParameterList *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005957ASTReader::ReadTemplateParameterList(ModuleFile &F,
Sebastian Redlc3632732010-10-05 15:59:54 +00005958 const RecordData &Record, unsigned &Idx) {
5959 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx);
5960 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx);
5961 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005962
5963 unsigned NumParams = Record[Idx++];
Chris Lattner5f9e2722011-07-23 10:55:15 +00005964 SmallVector<NamedDecl *, 16> Params;
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005965 Params.reserve(NumParams);
5966 while (NumParams--)
Douglas Gregor409448c2011-07-21 22:35:25 +00005967 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx));
Michael J. Spencer20249a12010-10-21 03:16:25 +00005968
5969 TemplateParameterList* TemplateParams =
Douglas Gregor35942772011-09-09 21:34:22 +00005970 TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005971 Params.data(), Params.size(), RAngleLoc);
5972 return TemplateParams;
5973}
5974
5975void
Sebastian Redlc43b54c2010-08-18 23:56:43 +00005976ASTReader::
Chris Lattner5f9e2722011-07-23 10:55:15 +00005977ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005978 ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00005979 unsigned &Idx) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005980 unsigned NumTemplateArgs = Record[Idx++];
5981 TemplArgs.reserve(NumTemplateArgs);
5982 while (NumTemplateArgs--)
Sebastian Redlc3632732010-10-05 15:59:54 +00005983 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005984}
5985
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005986/// \brief Read a UnresolvedSet structure.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005987void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005988 const RecordData &Record, unsigned &Idx) {
5989 unsigned NumDecls = Record[Idx++];
5990 while (NumDecls--) {
Douglas Gregor409448c2011-07-21 22:35:25 +00005991 NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005992 AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
5993 Set.addDecl(D, AS);
5994 }
5995}
5996
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005997CXXBaseSpecifier
Douglas Gregor1a4761e2011-11-30 23:21:26 +00005998ASTReader::ReadCXXBaseSpecifier(ModuleFile &F,
Nick Lewycky56062202010-07-26 16:56:01 +00005999 const RecordData &Record, unsigned &Idx) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006000 bool isVirtual = static_cast<bool>(Record[Idx++]);
6001 bool isBaseOfClass = static_cast<bool>(Record[Idx++]);
6002 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006003 bool inheritConstructors = static_cast<bool>(Record[Idx++]);
Sebastian Redlc3632732010-10-05 15:59:54 +00006004 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx);
6005 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006006 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006007 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
Douglas Gregorf90b27a2011-01-03 22:36:02 +00006008 EllipsisLoc);
Sebastian Redlf677ea32011-02-05 19:23:19 +00006009 Result.setInheritConstructors(inheritConstructors);
6010 return Result;
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00006011}
6012
Sean Huntcbb67482011-01-08 20:30:50 +00006013std::pair<CXXCtorInitializer **, unsigned>
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006014ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record,
Sean Huntcbb67482011-01-08 20:30:50 +00006015 unsigned &Idx) {
6016 CXXCtorInitializer **CtorInitializers = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006017 unsigned NumInitializers = Record[Idx++];
6018 if (NumInitializers) {
Sean Huntcbb67482011-01-08 20:30:50 +00006019 CtorInitializers
Douglas Gregor35942772011-09-09 21:34:22 +00006020 = new (Context) CXXCtorInitializer*[NumInitializers];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006021 for (unsigned i=0; i != NumInitializers; ++i) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006022 TypeSourceInfo *TInfo = 0;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006023 bool IsBaseVirtual = false;
6024 FieldDecl *Member = 0;
Francois Pichet00eb3f92010-12-04 09:14:42 +00006025 IndirectFieldDecl *IndirectMember = 0;
Michael J. Spencer20249a12010-10-21 03:16:25 +00006026
Sean Hunt156b6402011-05-04 01:19:08 +00006027 CtorInitializerType Type = (CtorInitializerType)Record[Idx++];
6028 switch (Type) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006029 case CTOR_INITIALIZER_BASE:
6030 TInfo = GetTypeSourceInfo(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006031 IsBaseVirtual = Record[Idx++];
Sean Hunt156b6402011-05-04 01:19:08 +00006032 break;
Douglas Gregor76852c22011-11-01 01:16:03 +00006033
6034 case CTOR_INITIALIZER_DELEGATING:
6035 TInfo = GetTypeSourceInfo(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006036 break;
6037
6038 case CTOR_INITIALIZER_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006039 Member = ReadDeclAs<FieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006040 break;
6041
6042 case CTOR_INITIALIZER_INDIRECT_MEMBER:
Douglas Gregor409448c2011-07-21 22:35:25 +00006043 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx);
Sean Hunt156b6402011-05-04 01:19:08 +00006044 break;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006045 }
Sean Hunt156b6402011-05-04 01:19:08 +00006046
Douglas Gregor3fb9e4b2011-01-04 00:32:56 +00006047 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx);
Sebastian Redlc3632732010-10-05 15:59:54 +00006048 Expr *Init = ReadExpr(F);
Sebastian Redlc3632732010-10-05 15:59:54 +00006049 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx);
6050 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006051 bool IsWritten = Record[Idx++];
6052 unsigned SourceOrderOrNumArrayIndices;
Chris Lattner5f9e2722011-07-23 10:55:15 +00006053 SmallVector<VarDecl *, 8> Indices;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006054 if (IsWritten) {
6055 SourceOrderOrNumArrayIndices = Record[Idx++];
6056 } else {
6057 SourceOrderOrNumArrayIndices = Record[Idx++];
6058 Indices.reserve(SourceOrderOrNumArrayIndices);
6059 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
Douglas Gregor409448c2011-07-21 22:35:25 +00006060 Indices.push_back(ReadDeclAs<VarDecl>(F, Record, Idx));
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006061 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00006062
Sean Huntcbb67482011-01-08 20:30:50 +00006063 CXXCtorInitializer *BOMInit;
Sean Hunt156b6402011-05-04 01:19:08 +00006064 if (Type == CTOR_INITIALIZER_BASE) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006065 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, IsBaseVirtual,
Sean Huntcbb67482011-01-08 20:30:50 +00006066 LParenLoc, Init, RParenLoc,
6067 MemberOrEllipsisLoc);
Sean Hunt156b6402011-05-04 01:19:08 +00006068 } else if (Type == CTOR_INITIALIZER_DELEGATING) {
Douglas Gregor76852c22011-11-01 01:16:03 +00006069 BOMInit = new (Context) CXXCtorInitializer(Context, TInfo, LParenLoc,
6070 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006071 } else if (IsWritten) {
Francois Pichet00eb3f92010-12-04 09:14:42 +00006072 if (Member)
Douglas Gregor35942772011-09-09 21:34:22 +00006073 BOMInit = new (Context) CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006074 LParenLoc, Init, RParenLoc);
Francois Pichet00eb3f92010-12-04 09:14:42 +00006075 else
Douglas Gregor35942772011-09-09 21:34:22 +00006076 BOMInit = new (Context) CXXCtorInitializer(Context, IndirectMember,
Sean Huntcbb67482011-01-08 20:30:50 +00006077 MemberOrEllipsisLoc, LParenLoc,
6078 Init, RParenLoc);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006079 } else {
Douglas Gregor35942772011-09-09 21:34:22 +00006080 BOMInit = CXXCtorInitializer::Create(Context, Member, MemberOrEllipsisLoc,
Sean Huntcbb67482011-01-08 20:30:50 +00006081 LParenLoc, Init, RParenLoc,
6082 Indices.data(), Indices.size());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006083 }
6084
Argyrios Kyrtzidisf84cde12010-09-06 19:04:27 +00006085 if (IsWritten)
6086 BOMInit->setSourceOrder(SourceOrderOrNumArrayIndices);
Sean Huntcbb67482011-01-08 20:30:50 +00006087 CtorInitializers[i] = BOMInit;
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006088 }
6089 }
6090
Sean Huntcbb67482011-01-08 20:30:50 +00006091 return std::make_pair(CtorInitializers, NumInitializers);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00006092}
6093
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006094NestedNameSpecifier *
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006095ASTReader::ReadNestedNameSpecifier(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006096 const RecordData &Record, unsigned &Idx) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006097 unsigned N = Record[Idx++];
6098 NestedNameSpecifier *NNS = 0, *Prev = 0;
6099 for (unsigned I = 0; I != N; ++I) {
6100 NestedNameSpecifier::SpecifierKind Kind
6101 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6102 switch (Kind) {
6103 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006104 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006105 NNS = NestedNameSpecifier::Create(Context, Prev, II);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006106 break;
6107 }
6108
6109 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006110 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006111 NNS = NestedNameSpecifier::Create(Context, Prev, NS);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006112 break;
6113 }
6114
Douglas Gregor14aba762011-02-24 02:36:08 +00006115 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006116 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006117 NNS = NestedNameSpecifier::Create(Context, Prev, Alias);
Douglas Gregor14aba762011-02-24 02:36:08 +00006118 break;
6119 }
6120
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006121 case NestedNameSpecifier::TypeSpec:
6122 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregor393f2492011-07-22 00:38:23 +00006123 const Type *T = readType(F, Record, Idx).getTypePtrOrNull();
Douglas Gregor1ab55e92010-12-10 17:03:06 +00006124 if (!T)
6125 return 0;
6126
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006127 bool Template = Record[Idx++];
Douglas Gregor35942772011-09-09 21:34:22 +00006128 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006129 break;
6130 }
6131
6132 case NestedNameSpecifier::Global: {
Douglas Gregor35942772011-09-09 21:34:22 +00006133 NNS = NestedNameSpecifier::GlobalSpecifier(Context);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006134 // No associated value, and there can't be a prefix.
6135 break;
6136 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006137 }
Argyrios Kyrtzidisd2bb2c02010-07-07 15:46:30 +00006138 Prev = NNS;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006139 }
6140 return NNS;
6141}
6142
Douglas Gregordc355712011-02-25 00:36:19 +00006143NestedNameSpecifierLoc
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006144ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record,
Douglas Gregordc355712011-02-25 00:36:19 +00006145 unsigned &Idx) {
6146 unsigned N = Record[Idx++];
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006147 NestedNameSpecifierLocBuilder Builder;
Douglas Gregordc355712011-02-25 00:36:19 +00006148 for (unsigned I = 0; I != N; ++I) {
6149 NestedNameSpecifier::SpecifierKind Kind
6150 = (NestedNameSpecifier::SpecifierKind)Record[Idx++];
6151 switch (Kind) {
6152 case NestedNameSpecifier::Identifier: {
Douglas Gregor95eab172011-07-28 20:55:49 +00006153 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006154 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006155 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006156 break;
6157 }
6158
6159 case NestedNameSpecifier::Namespace: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006160 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006161 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006162 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006163 break;
6164 }
6165
6166 case NestedNameSpecifier::NamespaceAlias: {
Douglas Gregor409448c2011-07-21 22:35:25 +00006167 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx);
Douglas Gregordc355712011-02-25 00:36:19 +00006168 SourceRange Range = ReadSourceRange(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006169 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
Douglas Gregordc355712011-02-25 00:36:19 +00006170 break;
6171 }
6172
6173 case NestedNameSpecifier::TypeSpec:
6174 case NestedNameSpecifier::TypeSpecWithTemplate: {
Douglas Gregordc355712011-02-25 00:36:19 +00006175 bool Template = Record[Idx++];
6176 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx);
6177 if (!T)
6178 return NestedNameSpecifierLoc();
Douglas Gregordc355712011-02-25 00:36:19 +00006179 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006180
6181 // FIXME: 'template' keyword location not saved anywhere, so we fake it.
Douglas Gregor35942772011-09-09 21:34:22 +00006182 Builder.Extend(Context,
Douglas Gregor5f791bb2011-02-28 23:58:31 +00006183 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
6184 T->getTypeLoc(), ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006185 break;
6186 }
6187
6188 case NestedNameSpecifier::Global: {
Douglas Gregordc355712011-02-25 00:36:19 +00006189 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006190 Builder.MakeGlobal(Context, ColonColonLoc);
Douglas Gregordc355712011-02-25 00:36:19 +00006191 break;
6192 }
6193 }
Douglas Gregordc355712011-02-25 00:36:19 +00006194 }
6195
Douglas Gregor35942772011-09-09 21:34:22 +00006196 return Builder.getWithLocInContext(Context);
Douglas Gregordc355712011-02-25 00:36:19 +00006197}
6198
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006199SourceRange
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006200ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
Sebastian Redlc3632732010-10-05 15:59:54 +00006201 unsigned &Idx) {
6202 SourceLocation beg = ReadSourceLocation(F, Record, Idx);
6203 SourceLocation end = ReadSourceLocation(F, Record, Idx);
Daniel Dunbar8ee59392010-06-02 15:47:10 +00006204 return SourceRange(beg, end);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00006205}
6206
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006207/// \brief Read an integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006208llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006209 unsigned BitWidth = Record[Idx++];
6210 unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
6211 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]);
6212 Idx += NumWords;
6213 return Result;
6214}
6215
6216/// \brief Read a signed integral value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006217llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00006218 bool isUnsigned = Record[Idx++];
6219 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned);
6220}
6221
Douglas Gregor17fc2232009-04-14 21:55:33 +00006222/// \brief Read a floating-point value
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006223llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, unsigned &Idx) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00006224 return llvm::APFloat(ReadAPInt(Record, Idx));
6225}
6226
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006227// \brief Read a string
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006228std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006229 unsigned Len = Record[Idx++];
Jay Foadbeaaccd2009-05-21 09:52:38 +00006230 std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00006231 Idx += Len;
6232 return Result;
6233}
6234
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00006235VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
6236 unsigned &Idx) {
6237 unsigned Major = Record[Idx++];
6238 unsigned Minor = Record[Idx++];
6239 unsigned Subminor = Record[Idx++];
6240 if (Minor == 0)
6241 return VersionTuple(Major);
6242 if (Subminor == 0)
6243 return VersionTuple(Major, Minor - 1);
6244 return VersionTuple(Major, Minor - 1, Subminor - 1);
6245}
6246
Douglas Gregor1a4761e2011-11-30 23:21:26 +00006247CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
Douglas Gregor409448c2011-07-21 22:35:25 +00006248 const RecordData &Record,
Chris Lattnerd2598362010-05-10 00:25:06 +00006249 unsigned &Idx) {
Douglas Gregor409448c2011-07-21 22:35:25 +00006250 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
Douglas Gregor35942772011-09-09 21:34:22 +00006251 return CXXTemporary::Create(Context, Decl);
Chris Lattnerd2598362010-05-10 00:25:06 +00006252}
6253
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006254DiagnosticBuilder ASTReader::Diag(unsigned DiagID) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00006255 return Diag(SourceLocation(), DiagID);
6256}
6257
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006258DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00006259 return Diags.Report(Loc, DiagID);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00006260}
Douglas Gregor025452f2009-04-17 00:04:06 +00006261
Douglas Gregor668c1a42009-04-21 22:25:48 +00006262/// \brief Retrieve the identifier table associated with the
6263/// preprocessor.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006264IdentifierTable &ASTReader::getIdentifierTable() {
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006265 return PP.getIdentifierTable();
Douglas Gregor668c1a42009-04-21 22:25:48 +00006266}
6267
Douglas Gregor025452f2009-04-17 00:04:06 +00006268/// \brief Record that the given ID maps to the given switch-case
6269/// statement.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006270void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006271 assert((*CurrSwitchCaseStmts)[ID] == 0 &&
6272 "Already have a SwitchCase with this ID");
6273 (*CurrSwitchCaseStmts)[ID] = SC;
Douglas Gregor025452f2009-04-17 00:04:06 +00006274}
6275
6276/// \brief Retrieve the switch-case statement with the given ID.
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006277SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006278 assert((*CurrSwitchCaseStmts)[ID] != 0 && "No SwitchCase with this ID");
6279 return (*CurrSwitchCaseStmts)[ID];
Douglas Gregor025452f2009-04-17 00:04:06 +00006280}
Douglas Gregor1de05fe2009-04-17 18:18:49 +00006281
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006282void ASTReader::ClearSwitchCaseIDs() {
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006283 CurrSwitchCaseStmts->clear();
Argyrios Kyrtzidise09a2752010-10-28 09:29:32 +00006284}
6285
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006286void ASTReader::ReadComments() {
6287 std::vector<RawComment> Comments;
6288 for (SmallVectorImpl<std::pair<llvm::BitstreamCursor,
6289 serialization::ModuleFile *> >::iterator
6290 I = CommentsCursors.begin(),
6291 E = CommentsCursors.end();
6292 I != E; ++I) {
6293 llvm::BitstreamCursor &Cursor = I->first;
6294 serialization::ModuleFile &F = *I->second;
6295 SavedStreamPosition SavedPosition(Cursor);
6296
6297 RecordData Record;
6298 while (true) {
6299 unsigned Code = Cursor.ReadCode();
6300 if (Code == llvm::bitc::END_BLOCK)
6301 break;
6302
6303 if (Code == llvm::bitc::ENTER_SUBBLOCK) {
6304 // No known subblocks, always skip them.
6305 Cursor.ReadSubBlockID();
6306 if (Cursor.SkipBlock()) {
6307 Error("malformed block record in AST file");
6308 return;
6309 }
6310 continue;
6311 }
6312
6313 if (Code == llvm::bitc::DEFINE_ABBREV) {
6314 Cursor.ReadAbbrevRecord();
6315 continue;
6316 }
6317
6318 // Read a record.
6319 Record.clear();
6320 switch ((CommentRecordTypes) Cursor.ReadRecord(Code, Record)) {
Chandler Carruth13691bb2012-06-20 06:47:54 +00006321 case COMMENTS_RAW_COMMENT: {
6322 unsigned Idx = 0;
6323 SourceRange SR = ReadSourceRange(F, Record, Idx);
6324 RawComment::CommentKind Kind =
6325 (RawComment::CommentKind) Record[Idx++];
6326 bool IsTrailingComment = Record[Idx++];
6327 bool IsAlmostTrailingComment = Record[Idx++];
6328 Comments.push_back(RawComment(SR, Kind, IsTrailingComment,
6329 IsAlmostTrailingComment));
6330 break;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00006331 }
6332 }
6333 }
6334 }
6335 Context.Comments.addCommentsToFront(Comments);
6336}
6337
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006338void ASTReader::finishPendingActions() {
Douglas Gregorcff9f262012-01-27 01:47:08 +00006339 while (!PendingIdentifierInfos.empty() || !PendingDeclChains.empty()) {
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006340 // If any identifiers with corresponding top-level declarations have
6341 // been loaded, load those declarations now.
6342 while (!PendingIdentifierInfos.empty()) {
6343 SetGloballyVisibleDecls(PendingIdentifierInfos.front().II,
6344 PendingIdentifierInfos.front().DeclIDs, true);
6345 PendingIdentifierInfos.pop_front();
6346 }
6347
Douglas Gregora1be2782011-12-17 23:38:30 +00006348 // Load pending declaration chains.
6349 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) {
6350 loadPendingDeclChain(PendingDeclChains[I]);
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006351 PendingDeclChainsKnown.erase(PendingDeclChains[I]);
Douglas Gregora1be2782011-12-17 23:38:30 +00006352 }
6353 PendingDeclChains.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006354 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006355
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006356 // If we deserialized any C++ or Objective-C class definitions, any
6357 // Objective-C protocol definitions, or any redeclarable templates, make sure
6358 // that all redeclarations point to the definitions. Note that this can only
6359 // happen now, after the redeclaration chains have been fully wired.
Douglas Gregorfc529f72011-12-19 19:00:47 +00006360 for (llvm::SmallPtrSet<Decl *, 4>::iterator D = PendingDefinitions.begin(),
6361 DEnd = PendingDefinitions.end();
6362 D != DEnd; ++D) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006363 if (TagDecl *TD = dyn_cast<TagDecl>(*D)) {
6364 if (const TagType *TagT = dyn_cast<TagType>(TD->TypeForDecl)) {
6365 // Make sure that the TagType points at the definition.
6366 const_cast<TagType*>(TagT)->decl = TD;
6367 }
Douglas Gregorfc529f72011-12-19 19:00:47 +00006368
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006369 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(*D)) {
6370 for (CXXRecordDecl::redecl_iterator R = RD->redecls_begin(),
6371 REnd = RD->redecls_end();
6372 R != REnd; ++R)
6373 cast<CXXRecordDecl>(*R)->DefinitionData = RD->DefinitionData;
6374
6375 }
6376
Douglas Gregorfc529f72011-12-19 19:00:47 +00006377 continue;
6378 }
6379
Douglas Gregor1d784b22012-01-01 19:51:50 +00006380 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(*D)) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +00006381 // Make sure that the ObjCInterfaceType points at the definition.
6382 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
6383 ->Decl = ID;
6384
Douglas Gregor1d784b22012-01-01 19:51:50 +00006385 for (ObjCInterfaceDecl::redecl_iterator R = ID->redecls_begin(),
6386 REnd = ID->redecls_end();
6387 R != REnd; ++R)
6388 R->Data = ID->Data;
6389
6390 continue;
6391 }
6392
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006393 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(*D)) {
6394 for (ObjCProtocolDecl::redecl_iterator R = PD->redecls_begin(),
6395 REnd = PD->redecls_end();
6396 R != REnd; ++R)
6397 R->Data = PD->Data;
6398
6399 continue;
6400 }
6401
6402 RedeclarableTemplateDecl *RTD
6403 = cast<RedeclarableTemplateDecl>(*D)->getCanonicalDecl();
6404 for (RedeclarableTemplateDecl::redecl_iterator R = RTD->redecls_begin(),
6405 REnd = RTD->redecls_end();
Douglas Gregorfc529f72011-12-19 19:00:47 +00006406 R != REnd; ++R)
Douglas Gregor7c99bb5c2012-01-14 15:13:49 +00006407 R->Common = RTD->Common;
Douglas Gregorfc529f72011-12-19 19:00:47 +00006408 }
6409 PendingDefinitions.clear();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006410}
6411
Sebastian Redlc43b54c2010-08-18 23:56:43 +00006412void ASTReader::FinishedDeserializing() {
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00006413 assert(NumCurrentElementsDeserializing &&
6414 "FinishedDeserializing not paired with StartedDeserializing");
6415 if (NumCurrentElementsDeserializing == 1) {
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006416 // We decrease NumCurrentElementsDeserializing only after pending actions
6417 // are finished, to avoid recursively re-calling finishPendingActions().
6418 finishPendingActions();
6419 }
6420 --NumCurrentElementsDeserializing;
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006421
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006422 if (NumCurrentElementsDeserializing == 0 &&
6423 Consumer && !PassingDeclsToConsumer) {
6424 // Guard variable to avoid recursively redoing the process of passing
6425 // decls to consumer.
6426 SaveAndRestore<bool> GuardPassingDeclsToConsumer(PassingDeclsToConsumer,
6427 true);
Argyrios Kyrtzidis71168332011-12-17 04:13:28 +00006428
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006429 while (!InterestingDecls.empty()) {
Argyrios Kyrtzidis8d39c3d2011-11-30 23:18:26 +00006430 // We are not in recursive loading, so it's safe to pass the "interesting"
6431 // decls to the consumer.
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006432 Decl *D = InterestingDecls.front();
6433 InterestingDecls.pop_front();
Argyrios Kyrtzidis91707832011-12-17 08:11:25 +00006434 PassInterestingDeclToConsumer(D);
6435 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006436 }
Douglas Gregord89275b2009-07-06 18:54:52 +00006437}
Douglas Gregor501c1032010-08-19 00:28:17 +00006438
Douglas Gregorf8a1e512011-09-02 00:26:20 +00006439ASTReader::ASTReader(Preprocessor &PP, ASTContext &Context,
Douglas Gregor832d6202011-07-22 16:35:34 +00006440 StringRef isysroot, bool DisableValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006441 bool DisableStatCache, bool AllowASTWithCompilerErrors)
Sebastian Redle1dde812010-08-24 00:50:04 +00006442 : Listener(new PCHValidator(PP, *this)), DeserializationListener(0),
6443 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
Douglas Gregor712f2fc2011-09-09 22:02:16 +00006444 Diags(PP.getDiagnostics()), SemaObj(0), PP(PP), Context(Context),
Jonathan D. Turner1afb6612011-07-28 17:20:23 +00006445 Consumer(0), ModuleMgr(FileMgr.getFileSystemOptions()),
6446 RelocatablePCH(false), isysroot(isysroot),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006447 DisableValidation(DisableValidation),
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00006448 DisableStatCache(DisableStatCache),
6449 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
Argyrios Kyrtzidisb88acb02012-05-04 01:49:36 +00006450 CurrentGeneration(0), CurrSwitchCaseStmts(&SwitchCaseStmts),
6451 NumStatHits(0), NumStatMisses(0),
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006452 NumSLocEntriesRead(0), TotalNumSLocEntries(0),
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006453 NumStatementsRead(0), TotalNumStatements(0), NumMacrosRead(0),
6454 TotalNumMacros(0), NumSelectorsRead(0), NumMethodPoolEntriesRead(0),
6455 NumMethodPoolMisses(0), TotalNumMethodPoolEntries(0),
6456 NumLexicalDeclContextsRead(0), TotalLexicalDeclContexts(0),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006457 NumVisibleDeclContextsRead(0), TotalVisibleDeclContexts(0),
6458 TotalModulesSizeInBits(0), NumCurrentElementsDeserializing(0),
Argyrios Kyrtzidis44d2dbd2012-02-09 07:31:52 +00006459 PassingDeclsToConsumer(false),
Jonathan D. Turner1da90142011-07-21 21:15:19 +00006460 NumCXXBaseSpecifiersLoaded(0)
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +00006461{
Douglas Gregorf62d43d2011-07-19 16:10:42 +00006462 SourceMgr.setExternalSLocEntrySource(this);
Sebastian Redle1dde812010-08-24 00:50:04 +00006463}
6464
Sebastian Redle1dde812010-08-24 00:50:04 +00006465ASTReader::~ASTReader() {
Sebastian Redle1dde812010-08-24 00:50:04 +00006466 for (DeclContextVisibleUpdatesPending::iterator
6467 I = PendingVisibleUpdates.begin(),
6468 E = PendingVisibleUpdates.end();
6469 I != E; ++I) {
6470 for (DeclContextVisibleUpdates::iterator J = I->second.begin(),
6471 F = I->second.end();
6472 J != F; ++J)
Benjamin Kramerb1758c62012-04-15 12:36:49 +00006473 delete J->first;
Sebastian Redle1dde812010-08-24 00:50:04 +00006474 }
6475}